#!/usr/bin/env perl

use strict;
use lib "$ENV{NCAST_PATH}/lib/perl";
require ('script-subs.pl');
use Time::Local;
use XML::Twig;
use XmlParam;

umask oct("002");
$|=1; #use unbuffered I/O

my ($verbose,$debug,$wrfmet_xml);
$debug=0;
$verbose=1;
if (@ARGV != 1) {
  print "ncast-wrfmet aborting because supplied ".scalar(@ARGV)." arguments\n".
    "It requires a single argument: the input xml file\n";
  &mydie ("ncast-wrfmet aborting because supplied ".scalar(@ARGV)." arguments");
} else {
  $wrfmet_xml=$ARGV[0];
}

my $twig= new XML::Twig(pretty_print => 'indented');
$twig->parsefile($wrfmet_xml);  # parse the new core wrf xml
&XmlParam::setRoot($twig->root); # call this prior to any other XmlParam calls

my $key;
my $section;
my @allkeys;

my %control_hash;
$section="control";
# Read in all input parameters from 'control' section of xml file:
@allkeys=XmlParam::findParamNames($section);
foreach $key (@allkeys) {
    # fill parameter values from xml file if present:
    my $local_val=&XmlParam::findParamValue($section,$key);
    $control_hash{$key}=$local_val unless (length($local_val) < 1);
}
$verbose = $control_hash{verbose} if defined($control_hash{verbose});
$debug = $control_hash{debug} if defined($control_hash{debug});
print "Reading input file: $wrfmet_xml \n" if $verbose;
my @requireds=("wrfmethome","gstats_cmd","astats_cmd");
my @missing;
foreach $key (@requireds) {
  push @missing,$key unless defined($control_hash{$key});
}
&mydie ("Missing required control parameter(s): ".join(", ",@missing))
  if @missing;
$control_hash{do_pbobs_get}=1 unless defined($control_hash{do_pbobs_get});
$control_hash{do_gstats}=1 unless defined($control_hash{do_gstats});
$control_hash{do_astats}=1 unless defined($control_hash{do_astats});

my $cmd;

$control_hash{do_pbobs_get}=1 unless defined($control_hash{do_pbobs_get});

# Date computation
my ($cmd,$outline,$first_epoch,$last_epoch,$cepoch);
my ($iyr,$imo,$ida,$ihr,$imin,$isec);
my ($csec,$cmin,$chour,$cmday,$cmon,$cyear,$cwday,$cyday);
if ($control_hash{init_date_time} =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)/ ) {
  $iyr=$1;
  $imo=$2;
  $ida=$3;
  $ihr=$4;
  $imin=0;
  $isec=0;
} else {
  &mydie("Invalid init_date_time specified in input file ".
	 $wrfmet_xml.", no remote access performed");
}
my $firstepoch=timegm($isec,$imin,$ihr,$ida,$imo-1,$iyr);
print "init_date_time=".$control_hash{init_date_time}.
  "(Converted to epoch and back:".scalar(gmtime($firstepoch)).")\n" if $verbose;
print "fhrs=".$control_hash{forecast_length_hours} if $verbose;
my $lastepoch=$firstepoch + 3600*$control_hash{forecast_length_hours};
print " (Ending date/time:".scalar(gmtime($lastepoch)).")\n" if $verbose;

# construct a list of days needed:
# reset firstepoch and lastepoch to be at 23 UTC
$cepoch=timegm(0,0,23,$ida,$imo-1,$iyr);
($csec,$cmin,$chour,$cmday,$cmon,$cyear,$cwday,$cyday) = gmtime($lastepoch);
$lastepoch=timegm(0,0,23,$cmday,$cmon,$cyear+1900);
my @days;
while ($cepoch <= $lastepoch) {
  ($csec,$cmin,$chour,$cmday,$cmon,$cyear,$cwday,$cyday) = gmtime($cepoch);
  push @days,sprintf("%4.4d",1900+$cyear).sprintf("%2.2d",1+$cmon).
    sprintf("%2.2d",$cmday);
  # increment:
  $cepoch=$cepoch + 86400;	#24*3600
}
print "Processing the following days: ".join(", ",@days)."\n" if $verbose;

my $utc_str=`date -u +%Y%m%d.%H.%M.%S.UTC`;
chomp $utc_str;

if (! -d $control_hash{tar_dir}) {system("mkdir -p ".$control_hash{tar_dir})};
if (! -d $control_hash{log_dir}) {system("mkdir -p ".$control_hash{log_dir})};
if (! -d $control_hash{nc_dir}) {system("mkdir -p ".$control_hash{nc_dir})};

foreach my $day (@days) {
  my $ncobs = $control_hash{nc_dir}."/obs-$day.nc";
  if (-e $ncobs) {
    print "ncobs file $ncobs already exists, skipping processing for day=$day\n"
      if $verbose;
    next;
  }
  my $pbobsfile="prepbufr.".$day.".wo40.tar.gz";
  $pbobsfile="prepbufr.".$day.".nr.tar.gz" if (substr($day,0,6) ge "200807");
  if ($control_hash{do_wget}) {
    # call remote access: run wget-pbobs.pl
    chdir $control_hash{tar_dir};
    print "Now in $control_hash{tar_dir}\n" if $verbose;
    if (-e $pbobsfile) {
      print "prepbufr file ".$pbobsfile." already exists, skipping wget-pbobs.pl for day=".
	$day."\n" if $verbose;
    } else {
      $cmd=$control_hash{wget_pbobs_path}."/wget-pbobs.pl -uname $control_hash{uname}".
	" -pword $control_hash{pword} -day ".$day.
	  " > wget-pbobs-".$utc_str.".out 2>&1";
      print "At ".scalar(localtime)." running $cmd\n" if $verbose;
      warn ("Error running $cmd") if system($cmd);
    }
  }
  if ($control_hash{do_pb2nc}) {
    my $log_dir=$control_hash{log_dir}."/$day";
    if (! -d ${log_dir}) {system("mkdir -p ".${log_dir})};
    my $work_dir=${log_dir}."/tmp";
    if (! -d ${work_dir}) {system("mkdir -p ".${work_dir})};
    chdir ${work_dir};
    print "Now in ${work_dir}\n" if $verbose;
    if (-e $control_hash{tar_dir}."/".$pbobsfile) {
      $cmd = "tar xzf ".$control_hash{tar_dir}."/".$pbobsfile;
      print "At ",scalar localtime()," Running $cmd\n" if $verbose;
      &mydie ("Error running $cmd\n") if system($cmd);
    } else {
      print "prepbufr file ".$pbobsfile." does not exist, cannot untar for day=".$day.
	"\n" if $verbose;
      warn("prepbufr file ".$pbobsfile." does not exist, cannot untar for day=".$day);
    }
    #first link to config file
    my $pbconfig="pb2nc-config.txt";
    symlink $control_hash{pb2nc_config},$pbconfig;

    my $pbobs00 = ${work_dir}."/$day.wo40/prepbufr.gdas.${day}00.wo40";
    my $pbobs06 = ${work_dir}."/$day.wo40/prepbufr.gdas.${day}06.wo40";
    my $pbobs12 = ${work_dir}."/$day.wo40/prepbufr.gdas.${day}12.wo40";
    my $pbobs18 = ${work_dir}."/$day.wo40/prepbufr.gdas.${day}18.wo40";

    if (substr($day,0,6) ge "200807") {
      $pbobs00 = ${work_dir}."/$day.nr/prepbufr.gdas.${day}.t00z.nr";
      $pbobs06 = ${work_dir}."/$day.nr/prepbufr.gdas.${day}.t06z.nr";
      $pbobs12 = ${work_dir}."/$day.nr/prepbufr.gdas.${day}.t12z.nr";
      $pbobs18 = ${work_dir}."/$day.nr/prepbufr.gdas.${day}.t18z.nr";
    }

    chdir ${log_dir};
    print "Now in ${log_dir}\n" if $verbose;
    $cmd=$control_hash{pbobs_qsub}." $control_hash{wrfmethome}/pb2nc $pbobs00 $ncobs ".
      ${work_dir}."/".$pbconfig." -v 10 ";
    $cmd=$cmd."-pbfile $pbobs06 -pbfile $pbobs12 -pbfile $pbobs18 ";
    print "\nAt ",scalar localtime()," Running $cmd\n" if $verbose;
    $outline=`$cmd`;
    &wait_for_qsub($outline,$control_hash{pbobs_sge});
    if ($control_hash{do_clean}) {
      $cmd="/bin/rm -fr ".${work_dir};
      print "\nAt ",scalar localtime()," Running $cmd\n" if $verbose;
      warn ("Error running $cmd") if system($cmd);
    }
  }
} #endloop days

1;
