#!/bin/csh

## This script creates real-time images of forecasted 250-hPa height, wind speed, and
## standardized height anomalies at 24-h intervals using 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/250hPaJet_prob_D10_$archive[$a].gif
rm -f /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D10_$archive[$a].gif

mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D2_$archive[$a].gif

mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D2_$archive[$a].gif

end

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

###### Convert the probabilistic GEFS 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 250-hPa height, wind, and standardized height anomalies 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|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

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

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

exit
EOF

## Black ##
gpcolor << EOF

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

exit
EOF

## Color scale for Jets ##
# 30 m/s #
gpcolor << EOF

 COLORS   = 11 = 247:252:253
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 40 m/s #
gpcolor << EOF

 COLORS   = 12 = 224:236:244
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 50 m/s #
gpcolor << EOF

 COLORS   = 13 = 191:211:230
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 60 m/s #
gpcolor << EOF

 COLORS   = 14 = 158:188:218
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 70 m/s #
gpcolor << EOF

 COLORS   = 15 = 140:150:198
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 80 m/s #
gpcolor << EOF

 COLORS   = 16 = 140:107:177
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 90 m/s #
gpcolor << EOF

 COLORS   = 17 = 136:65:157
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 100 m/s #
gpcolor << EOF

 COLORS   = 18 = 129:15:124
 DEVICE   = ps|250hPaJet_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 110 m/s #
gpcolor << EOF

 COLORS   = 19 = 77:0:75
 DEVICE   = ps|250hPaJet_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|250hPaJet_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 GDFILE BELOW !!! ###
gdplot << EOF

 GDFILE   = /nfs/data7/nmc2/${yr}${mo}${dy}00_gefsavg_255.gem
 GDATTIM  = ${yr}${mo}${dy}/0000F$fcasthr[$h]
 GLEVEL   = 250
 GVCORD   = pres
 PANEL    = 0
 SKIP     =  
 SCALE    = 0
 GFUNC    = sm5s(mag(wnd))!sm5s(mag(wnd))!hght
 CTYPE    = f!c!c
 CONTUR   = 3/1
 CINT     = !10/30!120
 LINE     = !30//2/0!30//5
 FINT     = 30;40;50;60;70;80;90;100;110!
 FLINE    = 0;11-19!
 HILO     =  
 HLSYM    =  
 CLRBAR   = 30/h/cc/0.5;0.125
 GVECT    =  
 WIND     =  
 REFVEC   =  
 TITLE    = 30/-2/~ 250 hPa Wind Speed (m/s), Geo. Hght & Std. Hght Anom (m)!
 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|250hPaJet_gefs_$fcasthr[$h].ps||c
 STNPLT   =  
 
run

 GDFILE   = /nfs/data7/nmc2/${yr}${mo}${dy}00_gefsavg_255.gem+rt_stdanoms.gem
 GDATTIM  = ${yr}${mo}${dy}/0000F$fcasthr[$h]
 GFUNC    = quo(sub(hght,hmean+2^1501$anomdate[$h]/0000F000),hstd+2^1501$anomdate[$h]/0000F000)
 CINT     = 1/1
 LINE     = 2//8
 TITLE    =
 MAP      = 30//1
 LATLON   =
 CTYPE    = c
 
run

 MAP      = 30//1
 CINT     = 1//-1
 LINE     = 4//8
 
run

exit
EOF

gpend

sleep 5

### Convert and crop the GIF image created in GEMPAK ###
convert -density 400 250hPaJet_gefs_$fcasthr[$h].ps -transverse -flip 250hPaJet_gefs_$fcasthr[$h].gif
convert 250hPaJet_gefs_$fcasthr[$h].gif -crop 4275x2300+50+675 +repage 250hPaJet_gefs_$fcasthr[$h].gif
### Convert the 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

rm -f /winterslab_rit/realtime/images/jet/250hPaJet_gefs_$fcasthr[$h].gif
mv /lb13/aw732728/wpc/realtime/images/250hPaJet_prob_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_prob_D1_$filnum[$h].gif
mv /lb13/aw732728/wpc/realtime/images/250hPaJet_scat_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/250hPaJet_scat_D1_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/250hPaJet_prob_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/250hPaJet_scat_$filnum[$h].gif
rm -f 250hPaJet_gefs_$fcasthr[$h].ps

convert probdiagram.gif 250hPaJet_gefs_$fcasthr[$h].gif +append 250hPaJet_prob_$filnum[$h].gif
convert $ensnum[$h]ens.gif 250hPaJet_gefs_$fcasthr[$h].gif +append 250hPaJet_scat_$filnum[$h].gif

mv 250hPaJet_gefs_$fcasthr[$h].gif /winterslab_rit/realtime/images/jet
mv 250hPaJet_prob_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images
mv 250hPaJet_scat_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images

rm -f $ensnum[$h]ens.gif

end

rm -f probdiagram.gif
 
 
 
 
