#!/bin/csh
######################################################################################################
alias calc 'echo "\!:*" | bc -l'
######################################################################################################
### 1) SET LOCATIONS
######################################################################################################
set INPUT = "/cpc/home/sparker/lrf/reproc/reproc_glbt/input"
set SCRIPTS = "/cpc/home/sparker/lrf/reproc/reproc_glbt/scripts"
set INPUT = "/cpc/home/sparker/lrf/reproc/reproc_glbt/input"
set OUTPUT = "/cpc/home/sparker/lrf/reproc/reproc_glbt/output"
set LIB = "/cpc/home/sparker/lrf/reproc/reproc_glbt/lib"

cd $INPUT

set OLD_OBS = "/cpc/prod_tst/nick.novella/lrf/ob_glbt"
set NEW_OBS = "/cpc/home/sparker/lrf/reproc/ob_glbt"
set FC = "/cpc/data/forecasts/land_air/long_range/conus/gridded_manual"
set OLD_VF = "/cpc/prod_tst/nick.novella/lrf/vf_glbt"
set NEW_VF = "/cpc/home/sparker/lrf/reproc/vf_glbt"
######################################################################################################
### 2) FLUSH WORK & COPY SCRIPTS/.EXE's OVER
######################################################################################################
rm $INPUT/*
cp $SCRIPTS/* $INPUT
cp $LIB/*     $INPUT

######################################################################################################
### 3) GET DATES
######################################################################################################

set ST = 201101
set TS = `date -d "${ST}01" "+%Y%m"` 
while ( $TS <= 202012 )
  set YYYY = `echo $TS | cut -c1-4`
  set MM = `echo $TS | cut -c5-6`
  echo "================================================================"
  echo $TS $YYYY $MM
# 
  echo "Grabbing old OB..."
  cp $OLD_OBS/output/$YYYY/$MM/obs_Temp.14.gif $INPUT/obs_Temp.14_old.gif
  cp $OLD_OBS/output/$YYYY/$MM/obs_Prec.14.gif $INPUT/obs_Prec.14_old.gif  
#
  echo "Grabbing new OB..."
  set TS1 = `date -d "${TS}15 + 1 month" "+%Y%m"`
  ksh $NEW_OBS/scripts/automated_gridded_2x2deg_surface_obs_production.ksh $TS1 > $INPUT/log_newobs
  cp $NEW_OBS/output/$YYYY/$MM/obs_Temp.14.gif $INPUT/obs_Temp.14_new.gif
  cp $NEW_OBS/output/$YYYY/$MM/obs_Prec.14.gif $INPUT/obs_Prec.14_new.gif  
#
  echo "Differencing OB..."
  set GTS = `date -d "${TS}01" "+%b%Y"`
# T
  sed "s/MONYYYY/$GTS/" base_obsdiff_monNoNohads.gs > obsdiff.gs
  grads -lbc obsdiff.gs > $INPUT/log_diffobs
  convert -crop 800x615+0+50 obs_Temp.14_diff.gif test.png
  cp test.png obs_Temp.14_diff.gif 
# P 
  sed "s/MONYYYY/$GTS/" base_obsdiff_monNoNohads_prec.gs > obsdiff.gs
  grads -lbc obsdiff.gs > $INPUT/log_diffobs
  convert -crop 800x615+0+50 obs_Prec.14_diff.gif test.png
  cp test.png obs_Prec.14_diff.gif 

# 
  echo "Grabbing same FC..." 
  set TS1 = `date -d "${TS}15 - 1 month" "+%Y%m"`
  set YYYY1 = `echo $TS1 | cut -c1-4`
  set MM1 = `echo $TS1 | cut -c5-6` 
  # IMAGES FROM OFF OPS
  if ( -e $FC/$YYYY1/$MM1/fcst_Temp.14.gif ) then
    cp $FC/$YYYY1/$MM1/fcst_Temp.14.gif $INPUT/fcst_Temp.14.gif 
    cp $FC/$YYYY1/$MM1/fcst_Prec.14.gif $INPUT/fcst_Prec.14.gif     
  else
    echo "    FC image not in OFF loc, grabbing from web..."
    wget -q https://www.cpc.ncep.noaa.gov/products/predictions/long_range/tools/briefing/verification/$YYYY/fcst_Temp.14.$TS.gif > $INPUT/log_fc
    wget -q https://www.cpc.ncep.noaa.gov/products/predictions/long_range/tools/briefing/verification/$YYYY/fcst_Prec.14.$TS.gif >> $INPUT/log_fc   
    mv $INPUT/fcst_Temp.14.$TS.gif $INPUT/fcst_Temp.14.gif 
    mv $INPUT/fcst_Prec.14.$TS.gif $INPUT/fcst_Prec.14.gif     
  endif
  
  if ( -e $FC/$YYYY1/$MM1/fcst_Temp.15.gif ) then
    cp $FC/$YYYY1/$MM1/fcst_Temp.15.gif $INPUT/fcst_Temp.15.gif  
    cp $FC/$YYYY1/$MM1/fcst_Prec.15.gif $INPUT/fcst_Prec.15.gif      
  else
    echo "    FC image not in OFF loc, grabbing from web..."
    wget -q https://www.cpc.ncep.noaa.gov/products/predictions/long_range/tools/briefing/verification/$YYYY/fcst_Temp.15.$TS.gif >> $INPUT/log_fc
    wget -q https://www.cpc.ncep.noaa.gov/products/predictions/long_range/tools/briefing/verification/$YYYY/fcst_Prec.15.$TS.gif >> $INPUT/log_fc    
    mv $INPUT/fcst_Temp.15.$TS.gif $INPUT/fcst_Temp.15.gif 
    mv $INPUT/fcst_Prec.15.$TS.gif $INPUT/fcst_Prec.15.gif     
  endif
#
  echo "Grabbing old VF..."  
  # DATA FROM OFF OPS
  set OLD_H14 = `grep $TS $OLD_VF/output/temp14.heidke.txt`
  set OLD_H15 = `grep $TS $OLD_VF/output/temp15.heidke.txt`  
  set OLD_H14_P = `grep $TS $OLD_VF/output/prec14.heidke.txt`
  set OLD_H15_P = `grep $TS $OLD_VF/output/prec15.heidke.txt`    
#
  echo "Grabbing new VF..."
  # new VF taking OFF OPS FC data, not nn fc
  set TS1 = `date -d "${TS}15 + 1 month" "+%Y%m"`  
  ksh $NEW_VF/scripts/produce_long_range_verifications.ksh T 14 $TS1 > $INPUT/log_newvf
  ksh $NEW_VF/scripts/produce_long_range_verifications.ksh T 15 $TS1 >> $INPUT/log_newvf
  ksh $NEW_VF/scripts/produce_long_range_verifications.ksh P 14 $TS1 >> $INPUT/log_newvf
  ksh $NEW_VF/scripts/produce_long_range_verifications.ksh P 15 $TS1 >> $INPUT/log_newvf  
 
  set NEW_H14 = `grep $TS $NEW_VF/output/temp14.heidke.txt`
  set NEW_H15 = `grep $TS $NEW_VF/output/temp15.heidke.txt`  
  set NEW_H14_P = `grep $TS $NEW_VF/output/prec14.heidke.txt`
  set NEW_H15_P = `grep $TS $NEW_VF/output/prec15.heidke.txt`    

  echo ""$TS" T14 Non-EC (new-old):" $NEW_H14[2] "-" $OLD_H14[2] "=" `calc ($NEW_H14[2]) - ($OLD_H14[2])` > $INPUT/comp_mon_scores.$TS.txt
  echo ""$TS" T14 All-FC (new-old):" $NEW_H14[3] "-" $OLD_H14[3] "=" `calc ($NEW_H14[3]) - ($OLD_H14[3])` >> $INPUT/comp_mon_scores.$TS.txt 
  echo ""$TS" T15 Non-EC (new-old):" $NEW_H15[2] "-" $OLD_H15[2] "=" `calc ($NEW_H15[2]) - ($OLD_H15[2])` >> $INPUT/comp_mon_scores.$TS.txt
  echo ""$TS" T15 All-FC (new-old):" $NEW_H15[3] "-" $OLD_H15[3] "=" `calc ($NEW_H15[3]) - ($OLD_H15[3])` >> $INPUT/comp_mon_scores.$TS.txt  
  
  echo ""$TS" T14 Non-EC (new-old):" $NEW_H14_P[2] "-" $OLD_H14_P[2] "=" `calc ($NEW_H14_P[2]) - ($OLD_H14_P[2])` > $INPUT/comp_mon_scores_prec.$TS.txt
  echo ""$TS" T14 All-FC (new-old):" $NEW_H14_P[3] "-" $OLD_H14_P[3] "=" `calc ($NEW_H14_P[3]) - ($OLD_H14_P[3])` >> $INPUT/comp_mon_scores_prec.$TS.txt 
  echo ""$TS" T15 Non-EC (new-old):" $NEW_H15_P[2] "-" $OLD_H15_P[2] "=" `calc ($NEW_H15_P[2]) - ($OLD_H15_P[2])` >> $INPUT/comp_mon_scores_prec.$TS.txt
  echo ""$TS" T15 All-FC (new-old):" $NEW_H15_P[3] "-" $OLD_H15_P[3] "=" `calc ($NEW_H15_P[3]) - ($OLD_H15_P[3])` >> $INPUT/comp_mon_scores_prec.$TS.txt    
#
  echo "Appending..."
# T   
  convert +append block.gif fcst_Temp.14.gif fcst_Temp.15.gif fcs.gif
  convert -append fcs.gif block.gif fcs.gif
  convert +append obs_Temp.14_new.gif obs_Temp.14_old.gif obs_Temp.14_diff.gif obs.gif 
  convert -append fcs.gif obs.gif out.gif
  set TEXT = "Non-EC (new-old): $NEW_H14[2] "-" $OLD_H14[2] "=" `calc ($NEW_H14[2]) - ($OLD_H14[2])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 150,600 '$TEXT'" out.gif temp1.gif
  set TEXT = "All-FC (new-old): $NEW_H14[3] "-" $OLD_H14[3] "=" `calc ($NEW_H14[3]) - ($OLD_H14[3])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 150,650 '$TEXT'" temp1.gif temp2.gif
  set TEXT = "Non-EC (new-old): $NEW_H15[2] "-" $OLD_H15[2] "=" `calc ($NEW_H15[2]) - ($OLD_H15[2])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 1300,600 '$TEXT'" temp2.gif temp3.gif
  set TEXT = "All-FC (new-old): $NEW_H15[3] "-" $OLD_H15[3] "=" `calc ($NEW_H15[3]) - ($OLD_H15[3])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 1300,650 '$TEXT'" temp3.gif outT.gif
# P 
  convert +append block.gif fcst_Prec.14.gif fcst_Prec.15.gif fcs.gif
  convert -append fcs.gif block.gif fcs.gif
  convert +append obs_Prec.14_new.gif obs_Prec.14_old.gif obs_Prec.14_diff.gif obs.gif 
  convert -append fcs.gif obs.gif out.gif
  set TEXT = "Non-EC (new-old): $NEW_H14_P[2] "-" $OLD_H14_P[2] "=" `calc ($NEW_H14_P[2]) - ($OLD_H14_P[2])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 150,600 '$TEXT'" out.gif temp1.gif
  set TEXT = "All-FC (new-old): $NEW_H14_P[3] "-" $OLD_H14_P[3] "=" `calc ($NEW_H14_P[3]) - ($OLD_H14_P[3])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 150,650 '$TEXT'" temp1.gif temp2.gif
  set TEXT = "Non-EC (new-old): $NEW_H15_P[2] "-" $OLD_H15_P[2] "=" `calc ($NEW_H15_P[2]) - ($OLD_H15_P[2])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 1300,600 '$TEXT'" temp2.gif temp3.gif
  set TEXT = "All-FC (new-old): $NEW_H15_P[3] "-" $OLD_H15_P[3] "=" `calc ($NEW_H15_P[3]) - ($OLD_H15_P[3])`" 
  convert -font courier -fill black -pointsize 42 -draw "text 1300,650 '$TEXT'" temp3.gif outP.gif 
#
  echo "Moving Output..."  
  cp $INPUT/outT.gif $OUTPUT/comp_mon.${TS}.gif
  cp $INPUT/outP.gif $OUTPUT/comp_mon_prec.${TS}.gif  
  rm $INPUT/temp*.gif $INPUT/obs.gif $INPUT/fcs.gif $INPUT/test.png
  
  if ( ! -d $OUTPUT/${YYYY}/${MM} ) then
    mkdir -p $OUTPUT/${YYYY}/${MM} 
  endif  
  mv $INPUT/comp_mon_scores.$TS.txt $OUTPUT/${YYYY}/${MM}
  mv $INPUT/comp_mon_scores_prec.$TS.txt $OUTPUT/${YYYY}/${MM}  
  mv $INPUT/obs*.gif $OUTPUT/${YYYY}/${MM}
  mv $INPUT/fcst*.gif $OUTPUT/${YYYY}/${MM}
  
  set TS = `date -d "${TS}15 + 1 month" "+%Y%m"`
end





