#!/bin/csh 

## This script creates real-time images of forecasted 24-h precipitation totals 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/precip_prob_D10_$archive[$a].gif
rm -f /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D10_$archive[$a].gif

mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D2_$archive[$a].gif

mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_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 24-h accumulated precipitation in today's forecast #####
set fcasthr = {'024','048','072','096','120','144','168','192','216'} 
set onetim = {'F06','F30','F54','F78','F102','F126','F150','F174','F198'}
set twotim = {'F12','F36','F60','F84','F108','F132','F156','F180','F204'}
set threetim = {'F18','F42','F66','F90','F114','F138','F162','F186','F210'}
set fourtim = {'F24','F48','F72','F96','F120','F144','F168','F192','F216'}
set filnum = {'2','3','4','5','6','7','8','9','10'}
set ensnum = {'D1','D2','D3','D4','D5','D6','D7','D8','D9'}

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

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

## Gray for landmask ##
gpcolor << EOF

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

exit
EOF

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

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

exit
EOF

## Black ##
gpcolor << EOF

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

exit
EOF

## Color scale for 24-h Precipitation Accum. ##
# 0–5 mm (.20 in) #
gpcolor << EOF

 COLORS   = 10 = 247:252:240
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 5-10 mm (.40 in.) #
gpcolor << EOF

 COLORS   = 11 = 224:243:219
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 10-15 mm (.60 in.) #
gpcolor << EOF

 COLORS   = 12 = 204:235:197
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 15-20 mm (.80 in.) #
gpcolor << EOF

 COLORS   = 13 = 168:221:181
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 20-25 mm (1.00 in.) #
gpcolor << EOF

 COLORS   = 14 = 123:204:196
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 25-35 mm (1.40 in.) #
gpcolor << EOF

 COLORS   = 15 = 78:179:211
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 35-50 mm (2.00 in.) #
gpcolor << EOF

 COLORS   = 16 = 43:140:190
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 50-75 mm (3.00 in.) #
gpcolor << EOF

 COLORS   = 17 = 8:104:172
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 75-125 mm (5.00 in.) #
gpcolor << EOF

 COLORS   = 18 = 8:64:129
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 125-175 mm (7.00 in.) #
gpcolor << EOF

 COLORS   = 19 = 136:65:157
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 175-250 mm (10 in.) #
gpcolor << EOF

 COLORS   = 20 = 140:150:198
 DEVICE   = ps|precip_gefs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 250+ mm (10+ in.) #
gpcolor << EOF

 COLORS   = 21 = 191:211:230
 DEVICE   = ps|precip_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|precip_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 GDFILEs BELOW ###
gdplot << EOF

 GDFILE   = /nfs/data7/nmc2/${yr}${mo}${dy}00_gefsavg_255.gem
 GDATTIM  = ${yr}${mo}${dy}/0000F$fcasthr[$h]
 GLEVEL   = 0
 GVCORD   = none
 PANEL    = 0
 SKIP     =  
 SCALE    = 0
 GFUNC    = sm5s(add(p06m^$onetim[$h],add(p06m^$twotim[$h],add(p06m^$threetim[$h],p06m^$fourtim[$h]))))
 CTYPE    = f!c
 CONTUR   = 3/1
 CINT     = 1;5;10;15;20;25;35;50;75;125;175;250
 LINE     = 30//2/0
 FINT     = 1;5;10;15;20;25;35;50;75;125;175;250!
 FLINE    = 0;10-21!
 HILO     =  
 HLSYM    =  
 CLRBAR   = 30/h/cc/0.5;0.125
 GVECT    =  
 WIND     =  
 REFVEC   =  
 TITLE    = 30/-2/${yr}${mo}${dy}/0000F$fcasthr[$h] 24-h Accum. Precip (mm)!
 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|precip_gefs_$fcasthr[$h].ps||c
 STNPLT   =  
 
run

 CTYPE    = c
 TITLE    =
 MAP      = 30//1
 LATLON   =
 
run
 
exit
EOF

gpend

sleep 5

#### Convert and crop synoptic map into a GIF ####
convert -density 400 precip_gefs_$fcasthr[$h].ps -transverse -flip precip_gefs_$fcasthr[$h].gif
convert precip_gefs_$fcasthr[$h].gif -crop 4275x2300+50+675 +repage precip_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 today's NPJ phase diagram forecasts #####

rm -f /winterslab_rit/realtime/images/precip/precip_gefs_$fcasthr[$h].gif
mv /lb13/aw732728/wpc/realtime/images/precip_prob_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D1_$filnum[$h].gif
mv /lb13/aw732728/wpc/realtime/images/precip_scat_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D1_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/precip_prob_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/precip_scat_$filnum[$h].gif
rm -f precip_gefs_$fcasthr[$h].ps

convert probdiagram.gif precip_gefs_$fcasthr[$h].gif +append precip_prob_$filnum[$h].gif
convert $ensnum[$h]ens.gif precip_gefs_$fcasthr[$h].gif +append precip_scat_$filnum[$h].gif

mv precip_gefs_$fcasthr[$h].gif /winterslab_rit/realtime/images/precip
mv precip_prob_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images
mv precip_scat_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images
 
rm -f $ensnum[$h]ens.gif 
 
end


####################### Create basemap for F000 and combine with NPJ phase diagram #######################

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

## Gray for landmask ##
gpcolor << EOF

 COLORS   = 1 = 180:180:180
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

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

 COLORS   = 31 = 255:255:255
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

## Black ##
gpcolor << EOF

 COLORS   = 30 = 0:0:0
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

## Color scale for 24-h Precipitation Accum. ##
# 0–5 mm (.20 in) #
gpcolor << EOF

 COLORS   = 10 = 247:252:240
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 5-10 mm (.40 in.) #
gpcolor << EOF

 COLORS   = 11 = 224:243:219
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 10-15 mm (.60 in.) #
gpcolor << EOF

 COLORS   = 12 = 204:235:197
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 15-20 mm (.80 in.) #
gpcolor << EOF

 COLORS   = 13 = 168:221:181
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 20-25 mm (1.00 in.) #
gpcolor << EOF

 COLORS   = 14 = 123:204:196
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 25-35 mm (1.40 in.) #
gpcolor << EOF

 COLORS   = 15 = 78:179:211
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 35-50 mm (2.00 in.) #
gpcolor << EOF

 COLORS   = 16 = 43:140:190
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 50-75 mm (3.00 in.) #
gpcolor << EOF

 COLORS   = 17 = 8:104:172
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 75-125 mm (5.00 in.) #
gpcolor << EOF

 COLORS   = 18 = 8:64:129
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 125-175 mm (7.00 in.) #
gpcolor << EOF

 COLORS   = 19 = 136:65:157
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 175-250 mm (10 in.) #
gpcolor << EOF

 COLORS   = 20 = 140:150:198
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF

# 250+ mm (10+ in.) #
gpcolor << EOF

 COLORS   = 21 = 191:211:230
 DEVICE   = ps|precip_gefs_000.ps||c
 
run

exit
EOF


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

 MAP      = 30//1
 MSCALE   = 0
 GAREA    = 10;120;80;-60
 PROJ     = ced
 PANEL    = 0
 CLEAR    = y
 DEVICE   = ps|precip_gefs_000.ps||c
 TITLE    = 30/-2/${yr}${mo}${dy}/0000F000 24-h Accum. Precip (mm)
 LATLON   = 30/2/1//20;20
 BND      = bg/31 + world_bnds/1 + great_lakes/31
 
run

exit
EOF

gpend

sleep 5

convert -density 400 precip_gefs_000.ps -transverse -flip precip_gefs_000.gif
convert precip_gefs_000.gif -crop 4275x2300+50+675 +repage precip_gefs_000.gif
convert -density 400 /winterslab_rit/realtime/images/phase_diagrams/D0ens.pdf D0ens.gif
convert D0ens.gif -crop 2800x2300+150+100 +repage D0ens.gif

rm -f /winterslab_rit/realtime/images/precip/precip_gefs_000.gif
mv /lb13/aw732728/wpc/realtime/images/precip_prob_1.gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_prob_D1_1.gif
mv /lb13/aw732728/wpc/realtime/images/precip_scat_1.gif /lb13/aw732728/wpc/realtime/images/image_archive/precip_scat_D1_1.gif
#rm -f /lb13/aw732728/wpc/realtime/images/precip_prob_1.gif
#rm -f /lb13/aw732728/wpc/realtime/images/precip_scat_1.gif
rm -f precip_gefs_000.ps

convert probdiagram.gif precip_gefs_000.gif +append precip_prob_1.gif
convert D0ens.gif precip_gefs_000.gif +append precip_scat_1.gif

mv precip_gefs_000.gif /winterslab_rit/realtime/images/precip
mv precip_prob_1.gif /lb13/aw732728/wpc/realtime/images
mv precip_scat_1.gif /lb13/aw732728/wpc/realtime/images

rm -f probdiagram.gif
rm -f D0ens.gif
 














 
 