#!/bin/csh

## This script creates real-time images of forecasted 500-hPa height, rel. vorticity, and
## standardized height anomalies at 24-h intervals for the GFS deterministic 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 old analysis plot from 10 days ago and adjust filenames for other analysis files ###

rm -f /winterslab_rit/realtime/images/vort/500hPa_gfs_D10.gif

mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D9.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D10.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D8.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D9.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D7.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D8.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D6.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D7.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D5.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D6.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D4.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D5.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D3.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D4.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D2.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D3.gif
mv /winterslab_rit/realtime/images/vort/500hPa_gfs_D1.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D2.gif
cp /winterslab_rit/realtime/images/vort/500hPa_gfs_000.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_D1.gif

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

set archive = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20'}

foreach a (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)

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

mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D9_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D10_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D8_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D9_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D7_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D8_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D6_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D7_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D5_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D6_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D4_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D5_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D3_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D4_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D2_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D3_$archive[$a].gif
mv /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D1_$archive[$a].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D2_$archive[$a].gif

end

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

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

##### Move yesterday's forecast into the archive and create first 10 frames of today's deterministic forecast #####
set filnumold = {'1','2','3','4','5','6','7','8','9','10'}
set filname = {'D10','D9','D8','D7','D6','D5','D4','D3','D2','D1'}

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

mv /lb13/aw732728/wpc/realtime/images/500hPa_deterministic_$filnumold[$t].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D1_$filnumold[$t].gif
#rm -f /lb13/aw732728/wpc/realtime/images/500hPa_deterministic_$filnumold[$t].gif

convert deterministic.gif /winterslab_rit/realtime/images/vort/500hPa_gfs_$filname[$t].gif +append 500hPa_deterministic_$filnumold[$t].gif

mv 500hPa_deterministic_$filnumold[$t].gif /lb13/aw732728/wpc/realtime/images

end

##### Create GIF images of 500-hPa height, vorticity, and standardized height anomalies in today's forecast #####
set fcasthr = {'000','024','048','072','096','120','144','168','192','216'} 
set filnum = {'11','12','13','14','15','16','17','18','19','20'}
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|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

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

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

exit
EOF

## Black ##
gpcolor << EOF

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

exit
EOF

## Color scale for Rel. Vorticity ##
# 6 1/s #
gpcolor << EOF

 COLORS   = 11 = 255:255:204
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 8 1/s #
gpcolor << EOF

 COLORS   = 12 = 254:217:118
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 10 1/s #
gpcolor << EOF

 COLORS   = 13 = 254:178:76
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 12 1/s #
gpcolor << EOF

 COLORS   = 14 = 253:141:60
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 16 1/s #
gpcolor << EOF

 COLORS   = 15 = 252:78:42
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 20 1/s #
gpcolor << EOF

 COLORS   = 16 = 227:26:28
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 24 1/s #
gpcolor << EOF

 COLORS   = 17 = 189:0:38
 DEVICE   = ps|500hPa_gfs_$fcasthr[$h].ps||c
 
run

exit
EOF

# 28 1/s #
gpcolor << EOF

 COLORS   = 18 = 128:0:38
 DEVICE   = ps|500hPa_gfs_$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|500hPa_gfs_$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_gfs_003.gem
 GDATTIM  = ${yr}${mo}${dy}/0000F$fcasthr[$h]
 GLEVEL   = 500
 GVCORD   = pres
 PANEL    = 0
 SKIP     =  
 SCALE    = 5!5!5!0
 GFUNC    = sm5s(vor(wnd))!sm5s(vor(wnd))!sm5s(vor(wnd))!hght
 CTYPE    = f!c!c!c
 CONTUR   = 3/1
 CINT     = !2/6/12!4/12!60
 LINE     = !30//2/0!30//2/0!30//5
 FINT     = 6;8;10;12;16;20;24;28!
 FLINE    = 0;11-19!
 HILO     =  
 HLSYM    =  
 CLRBAR   = 30/h/cc/0.5;0.125
 GVECT    =  
 WIND     =  
 REFVEC   =  
 TITLE    = 30/-2/~ 500 hPa Rel. Vort. (1/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|500hPa_gfs_$fcasthr[$h].ps||c
 STNPLT   =  
 
run

 GDFILE   = /nfs/data7/nmc2/${yr}${mo}${dy}00_gfs_003.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
 SCALE    = 0
 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

##### Take synoptic map and stitch it together with today's deterministic NPJ phase diagram forecast #####

convert -density 400 500hPa_gfs_$fcasthr[$h].ps -transverse -flip 500hPa_gfs_$fcasthr[$h].gif
convert 500hPa_gfs_$fcasthr[$h].gif -crop 4275x2300+50+675 +repage 500hPa_gfs_$fcasthr[$h].gif

rm -f /winterslab_rit/realtime/images/vort/500hPa_gfs_$fcasthr[$h].gif
mv /lb13/aw732728/wpc/realtime/images/500hPa_deterministic_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images/image_archive/500hPa_deterministic_D1_$filnum[$h].gif
#rm -f /lb13/aw732728/wpc/realtime/images/500hPa_deterministic_$filnum[$h].gif
rm -f 500hPa_gfs_$fcasthr[$h].ps

convert deterministic.gif 500hPa_gfs_$fcasthr[$h].gif +append 500hPa_deterministic_$filnum[$h].gif

mv 500hPa_gfs_$fcasthr[$h].gif /winterslab_rit/realtime/images/vort
mv 500hPa_deterministic_$filnum[$h].gif /lb13/aw732728/wpc/realtime/images

end

rm -f deterministic.gif



