#!/usr/bin/perl
use Time::Local;

#--- Directory Paths --------------------------------------------------#

$dirx="/home/bpierce/CEOS/Pre_process/Decode/";

$dirx_wgrib="/home/bpierce/CEOS/Pre_process/Grib2/wgrib2/wgrib2";

$outdir_bin="/data/Met/GFS/Pres/";

#----------------------------------------------------------------------#

my($sec,$min,$hr,$day,$month,$year,$wday,$yday,$isdst)=localtime(time);
$month=$month+1;
$year=$year+1900;
$yday=$yday+1;
$yday=sprintf("%03d",$yday);
$month=sprintf("%02d",$month);
$day=sprintf("%02d",$day);
$indir="/data/Met/GFS/Analysis/Original/".$year.$month.$day."/";
# 3D Variables to be extracted
$ugrd="UGRD";
$vgrd="VGRD";
$vvel="VVEL";
$hgt="HGT";
$temp="TMP";
$rh="RH";
# 2D Variables to be extracted
#ck08 $ubl="UGRD:10 m above gnd";
$ubl="UGRD:10 m above ground";
#ck08 $vbl="VGRD:10 m above gnd";
$vbl="VGRD:10 m above ground";
$qbl="RH:2 m";
$land="LAND";
#ck08 $mslp="PRMSL:MSL";
$mslp="PRMSL:mean sea level";
#ck08 $zsfc="HGT:sfc";
$zsfc="HGT:surface";
#ck08 $tsfc="TMP:sfc";
$tsfc="TMP:surface";
#ck08 $zpbl="HPBL:sfc";
$zpbl="HPBL:surface";
#ck08 $psfc="PRES:sfc";
$psfc="PRES:surface";
$apcp="APCP:surface";
#ck08 $clw="CWAT:atmos";
$clw="CWAT:entire atmosphere";
$ptrp="PRES:tropopause";
#$uwinds=":UGRD:850 mb:";
#$vwinds=":VGRD:850 mb:";
#$wwinds=":VVEL:850 mb:";

$fl_prefix="gfs.p5.".$year.$month.$day;
$fl_suffix="_000.ldm.grib2";

@level1=(1000.,975.,950.,925.,900.,850.,800.,750.,700.,650.,600.,550.,500.,450.,400.,350.,300.,250.,200.,150.,100.,70.,50.,30.,20.,10.);
@level2=(1000.,975.,950.,925.,900.,850.,800.,750.,700.,650.,600.,550.,500.,450.,400.,350.,300.,250.,200.,150.,100.);

# Make a list of files in Original directory
opendir(OD,$indir) || die("Cannot open the directory");
@fl_original=readdir(OD);
closedir(OD);

   # Loop over hours
   for($ihr=2;$ihr<=3;$ihr++)
    {
     $hour=$ihr*6;
     $hour=sprintf("%02d",$hour);
     $infl=$fl_prefix."_".$hour.$fl_suffix;
#ck08     $infl=$fl_prefix.$hour;
     $outfl_bin=$infl.".bin";

     # Delete existing bin file
     opendir(OD,$outdir_bin) || mkdir("$outdir_bin");
     @files_bin_all=readdir(OD);
     closedir(OD);
     foreach(@files_bin_all)
      {
       if($_ =~ m/$outfl_bin/)
        {
         $rm=("rm $outdir_bin$outfl_bin");
         system("$rm");
        }
      }
#ck08 Replace "o" option with "bin"
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$ubl\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$vbl\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$qbl\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$land\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$mslp\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$zsfc\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$tsfc\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$psfc\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
#     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$apcp\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
#     print WGRIB_bin;
#     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$zpbl\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$clw\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);
     $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$ptrp\" \|$dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
     print WGRIB_bin;
     close(WGRIB_bin);


     # Loop over levels
      for($ilev=0;$ilev<=$#level1;$ilev++)
      {
       $uwinds=":$ugrd:$level1[$ilev] mb:";
       $vwinds=":$vgrd:$level1[$ilev] mb:";
       $temps=":$temp:$level1[$ilev] mb:";
       $height=":$hgt:$level1[$ilev] mb:";
       $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$uwinds\" \|
                 $dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
       print WGRIB_bin;
       close(WGRIB_bin);
       $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$vwinds\" \|
                 $dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
       print WGRIB_bin;
       close(WGRIB_bin);
       $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$temps\" \|
                 $dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
       print WGRIB_bin;
       close(WGRIB_bin);
       $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$height\" \|
                 $dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
       print WGRIB_bin;
       close(WGRIB_bin);
      }  # End loop over levels
     # Loop over levels
     for($ilev=0;$ilev<=$#level2;$ilev++)
      {
       $relhum=":$rh:$level2[$ilev] mb:";
       $wwinds=":$vvel:$level1[$ilev] mb:";
       $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$relhum\" \|
                 $dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
       print WGRIB_bin;
       close(WGRIB_bin);
       $pid=open(WGRIB_bin,"|$dirx_wgrib -s $indir$infl \| grep \"$wwinds\" \|
                 $dirx_wgrib -i -append $indir$infl -bin $outdir_bin$outfl_bin");
       print WGRIB_bin;
       close(WGRIB_bin);

      }  # End loop over levels

     push(@fl_new,$infl);


    }  # End loop over hours


foreach $tmp (@fl_new)
 {
  $ENV{"INDIR"}=$outdir_bin;
  $ENV{"OUTDIR"}=$outdir_bin;
  $ENV{"INFL"}=$tmp.".bin";
  $ENV{"DIRX"}=$dirx;
  $ENV{"YYYYMMDD"}=$year.$month.$day;
  print "$yyyymmdd\n";
#  $ENV{"FL_LATLON"}=$fl_latlon;
  chdir("$dirx");
  $pid=open(IDL, "|/usr/local/bin/idl70 -quiet") or die "Couldn't fork: $!\n";
  print IDL ".com make_GFS_anal_dat2_pl.pro\n";
  print IDL "make_GFS_anal_dat2_pl\n";
  close(IDL);
 }

$rm="rm -f $outdir_bin"."*.bin";
system("$rm");

exit;
