#!/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
######################################################################################################
## CANNOT RUN 201912 until 20200302
set ST = 202104
set TS = `date -d "${ST}01" "+%Y%m"` 
while ( $TS <= 202104 )
  set YYYY = `echo $TS | cut -c1-4`
  set MM = `echo $TS | cut -c5-6`
  echo "================================================================"
  echo $TS $YYYY $MM
#
# 
  echo "Grabbing old OB..."
  set TS1 = `date -d "${TS}15 + 1 month" "+%Y%m"`
  set YYYY1 = `echo $TS1 | cut -c1-4`
  set MM1 = `echo $TS1 | cut -c5-6`
  cp $OLD_OBS/output/$YYYY1/$MM1/obs_Temp.01.gif $INPUT/obs_Temp.01_old.gif
#
#
  echo "Grabbing new OB..."
  set TS1 = `date -d "${TS}15 + 3 month" "+%Y%m"`
  set TS2 = `date -d "${TS}15 + 1 month" "+%Y%m"`  
  set YYYY2 = `echo $TS2 | cut -c1-4`
  set MM2 = `echo $TS2 | cut -c5-6`     
  ksh $NEW_OBS/scripts/automated_gridded_2x2deg_surface_obs_production.ksh $TS1 > $INPUT/log_newobs
  cp $NEW_OBS/output/$YYYY2/$MM2/obs_Temp.01.gif $INPUT/obs_Temp.01_new.gif
#
#
  echo "Differencing OB..."
  set GTS = `date -d "${TS}01" "+%b%Y"`
  set MMa = `date -d "${TS}01" "+%b"` 
  set MMb = `date -d "${TS}01 + 1 month" "+%b"` 
  set MMc = `date -d "${TS}01 + 2 month" "+%b %Y"`  
  sed "s/MONYYYY/$GTS/" base_obsdiff_seaNoNohads.gs > temp.gs
  sed "s/MMM/$MMa-$MMb-$MMc/" temp.gs > obsdiff_sea.gs  
  grads -lbc obsdiff_sea.gs > $INPUT/log_diffobs
  convert -crop 800x615+0+50 obs_Temp.01_diff.gif test.png
  cp test.png obs_Temp.01_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` 
  set TS2 = `date -d "${TS}15 + 1 month" "+%Y%m"`
  set YYYY2 = `echo $TS2 | cut -c1-4`
  set MM2 = `echo $TS2 | cut -c5-6` 
  # IMAGES FROM OFF OPS
  if ( -e $FC/$YYYY1/$MM1/fcst_Temp.01.gif ) then
    cp $FC/$YYYY1/$MM1/fcst_Temp.01.gif $INPUT/fcst_Temp.01.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/$YYYY2/fcst_Temp.01.$TS2.gif > $INPUT/log_fc
    mv $INPUT/fcst_Temp.01.$TS2.gif $INPUT/fcst_Temp.01.gif 
  endif
#
#
  echo "Grabbing old VF..."  
  # DATA FROM OFF OPS
  set OLD_H01 = `grep $TS $OLD_VF/output/temp01.heidke.txt`
#
#
  echo "Grabbing new VF..."
  # new VF taking OFF OPS FC data, not nn fc
  set TS1 = `date -d "${TS}15 + 3 month" "+%Y%m"`  
  ksh $NEW_VF/scripts/produce_long_range_verifications.ksh T 01 $TS1 > $INPUT/log_newvf
# 
  set NEW_H01 = `grep $TS $NEW_VF/output/temp01.heidke.txt`
#
  echo ""$TS" T01 Non-EC (new-old):" $NEW_H01[2] "-" $OLD_H01[2] "=" `calc ($NEW_H01[2]) - ($OLD_H01[2])` > $INPUT/comp_sea_scores.$TS.txt
  echo ""$TS" T01 All-FC (new-old):" $NEW_H01[3] "-" $OLD_H01[3] "=" `calc ($NEW_H01[3]) - ($OLD_H01[3])` >> $INPUT/comp_sea_scores.$TS.txt 
#
#
  echo "Appending..."
  convert +append block.gif block.gif fcst_Temp.01.gif fcs.gif
  convert -append fcs.gif block.gif fcs.gif
  convert +append obs_Temp.01_new.gif obs_Temp.01_old.gif obs_Temp.01_diff.gif obs.gif 
  convert -append fcs.gif obs.gif out.gif  
  set TEXT = "Non-EC (new-old): $NEW_H01[2] "-" $OLD_H01[2] "=" `calc ($NEW_H01[2]) - ($OLD_H01[2])`" 
  convert -font Courier-10-Pitch-Bold -fill black -pointsize 42 -draw "text 600,600 '$TEXT'" out.gif temp1.gif
  set TEXT = "All-FC (new-old): $NEW_H01[3] "-" $OLD_H01[3] "=" `calc ($NEW_H01[3]) - ($OLD_H01[3])`" 
  convert -font Courier-10-Pitch-Bold -fill black -pointsize 42 -draw "text 600,650 '$TEXT'" temp1.gif out.gif
  convert -font Courier-10-Pitch-Bold -fill black -pointsize 42 -draw "text 80, 1300 'NEW = GLBT 91-20 base'" out.gif temp1.gif
  convert -font Courier-10-Pitch-Bold -fill black -pointsize 42 -draw "text 900, 1300 'OLD = GLBT 81-10 base'" temp1.gif out.gif 
#
  echo "Moving Output..."  
  cp $INPUT/out.gif $OUTPUT/comp_sea.${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_sea_scores.$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





