#!/bin/csh 

## This script creates real-time images of forecast sea-level pressure, 1000–500 hPa thickness, and
## standardized 850 hPa temperature anomalies at 24-h intervals for the GEFS ensemble mean forecast.

##### !!!IT WILL BE NECESSARY TO CHANGE ALL DIRECTORIES!!! #####

## Set current date information ##

set yr = `date +%y`
set mo = `date +%m`
set dy = `date +%d`

### Remove 10-day old phase diagram forecast and adjust filenames for other existing archived forecasts ###

set archive = {'1','2','3','4','5','6','7','8','9','10'}

foreach a (1 2 3 4 5 6 7 8 9 10)

rm -f /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D10_$archive[$a].gif
rm -f /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D10_$archive[$a].gif

mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D2_$archive[$a].gif

mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D2_$archive[$a].gif

end

############################################################

##### Turn probabilistic NPJ phase diagram into a GIF #####
convert -density 400 /winterslab_rit/realtime/images/phase_diagrams/probdiagram.pdf probdiagram.gif
convert probdiagram.gif -crop 2800x2300+150+100 +repage probdiagram.gif

##### Create GIF images of 850-hPa temperature, MSLP, and 1000-500hPa Thickness in today's forecast #####
set fcasthr = {'000','024','048','072','096','120','144','168','192','216'} 
set filnum = {'1','2','3','4','5','6','7','8','9','10'}
set ensnum = {'D0','D1','D2','D3','D4','D5','D6','D7','D8','D9'}
set anomdate = {'10','11','12','13','14','15','16','17','18','19'}

foreach h (1 2 3 4 5 6 7 8 9 10)

##### Set the color scales #########

## Gray for landmask ##
gpcolor << EOF

 COLORS   = 1 = 180:180:180
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

## White for sea areas ##
gpcolor << EOF

 COLORS   = 31 = 255:255:255
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

## Black ##
gpcolor << EOF

 COLORS   = 30 = 0:0:0
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

## Color scale for Thickness and SLP ##
# Red #
gpcolor << EOF

 COLORS   = 2 = 165:15:21
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# Blue #
gpcolor << EOF

 COLORS   = 4 = 8:48:107
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

## Color scale for Temp anomalies ##
# <-5 std dev
gpcolor << EOF

 COLORS   = 10 = 5:48:97
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# -5 to -4 std dev
gpcolor << EOF

 COLORS   = 11 = 33:102:172
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# -4 to -3 std dev
gpcolor << EOF

 COLORS   = 12 = 67:147:195
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# -3 to -2 std dev
gpcolor << EOF

 COLORS   = 13 = 146:197:222
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# -2 to -1 std dev
gpcolor << EOF

 COLORS   = 14 = 209:229:240
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 1 to 2 std dev
gpcolor << EOF

 COLORS   = 15 = 253:219:199
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 2 to 3 std dev
gpcolor << EOF

 COLORS   = 16 = 244:165:130
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 3 to 4 std dev
gpcolor << EOF

 COLORS   = 17 = 214:96:77
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 4 to 5 std dev
gpcolor << EOF

 COLORS   = 18 = 178:24:43
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# >5 std dev
gpcolor << EOF

 COLORS   = 19 = 103:0:31
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF


### Create the background map ###
gpmap << EOF

 MAP      = 
 MSCALE   = 0
 GAREA    = 10;120;80;-60
 PROJ     = ced
 PANEL    = 0
 CLEAR    = y
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 TITLE    = 
 LATLON   = 
 BND      = bg/31 + world_bnds/1 + great_lakes/31
 
run

exit
EOF

### Plot the variables - NEED TO CHANGE THE GDFILEs BELOW ###
gdplot << EOF

 GDFILE   = /nfs/data7/nmc2/${yr}${mo}${dy}00_gefsavg_255.gem+rt_stdanoms.gem
 GDATTIM  = ${yr}${mo}${dy}/0000F$fcasthr[$h]
 GLEVEL   = 850
 GVCORD   = pres
 PANEL    = 0
 SKIP     =  
 SCALE    = 0
 GFUNC    = quo(sub(tmpk,tmean+2^1501$anomdate[$h]/0000F000),tstd+2^1501$anomdate[$h]/0000F000)
 CTYPE    = f
 CONTUR   = 3/1
 CINT     = 
 LINE     = 
 FINT     = -5;-4;-3;-2;-1;1;2;3;4;5!
 FLINE    = 10;11;12;13;14;0;15;16;17;18;19!
 HILO     =  
 HLSYM    =  
 CLRBAR   = 30/h/cc/0.5;0.125
 GVECT    =  
 WIND     =  
 REFVEC   =  
 TITLE    = 
 TEXT     = 1/22/1/1/s/l/hw
 CLEAR    = n
 GAREA    = 10;120;80;-60
 IJSKIP   =  
 PROJ     = ced
 MAP      = 30//1
 MSCALE   = 0
 LATLON   = 30/2/1//20;20
 DEVICE   = ps|sfcT_gefs_$fcasthr[$h].ps||c
 STNPLT   =  
 
run

 LATLON   =
 MAP      = 30//1
 CINT     = -5;-4;-3;-2;-1;1;2;3;4;5
 LINE     = 30//2/0
 CTYPE    = c
 TITLE    = 
 
run 

 GVCORD   = pres!pres!none
 GLEVEL   = 1000!1000!0
 GFUNC    = sm5s(sub(hght@500,hght))!sm5s(sub(hght@500,hght))!sm5s(pmsl)
 CTYPE    = c!c!c
 CINT     = 60/5460!60//5400!4
 LINE     = 2/2/6!4/2/6!30//8
 TITLE    = 30/-2/~ MSLP (hPa), 1000-500-hPa Thck (m), 850-hPa Temp Anom (K)!
 
run
 
exit
EOF

gpend

sleep 5


#### Convert and crop synoptic map into a GIF ####
convert -density 400 sfcT_gefs_$fcasthr[$h].ps -transverse -flip sfcT_gefs_$fcasthr[$h].gif
convert sfcT_gefs_$fcasthr[$h].gif -crop 4275x2300+50+675 +repage sfcT_gefs_$fcasthr[$h].gif
#### Convert ensemble scatterplot NPJ phase diagram into a GIF ####
convert -density 400 /winterslab_rit/realtime/images/phase_diagrams/$ensnum[$h]ens.pdf $ensnum[$h]ens.gif
convert $ensnum[$h]ens.gif -crop 2800x2300+150+100 +repage $ensnum[$h]ens.gif

##### Take synoptic map and stitch it together with NPJ phase diagram forecasts #####
rm -f /winterslab_rit/realtime/images/temp/sfcT_gefs_$fcasthr[$h].gif
mv /lb13/aw732728/wpc/realtime/images/sfcT_prob_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_prob_D1_$filnum[$h].gif
mv /lb13/aw732728/wpc/realtime/images/sfcT_scat_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/sfcT_scat_D1_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/sfcT_prob_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/sfcT_scat_$filnum[$h].gif
rm -f sfcT_gefs_$fcasthr[$h].ps

convert probdiagram.gif sfcT_gefs_$fcasthr[$h].gif +append sfcT_prob_$filnum[$h].gif
convert $ensnum[$h]ens.gif sfcT_gefs_$fcasthr[$h].gif +append sfcT_scat_$filnum[$h].gif

mv sfcT_gefs_$fcasthr[$h].gif /winterslab_rit/realtime/images/temp
mv sfcT_prob_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images
mv sfcT_scat_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images
 
rm -f $ensnum[$h]ens.gif

end

rm -f probdiagram.gif

 
 
 
 
