#!/bin/csh 

#### Change directories to ensure correct access to embedded scripts within this file ####
cd /lb13/aw732728/tigge/jetclimo/wpc_npj_files

## This script grabs the most recent GFS analysis grib and GEFS forecast data
## and converts it to netCDF. This data is then read into the eof_phasespace_RT.m script
## where it projects the 250-hPa U-wind onto the phase space.

######################################################
### Create variable names for 12 most recent dates ###
######################################################

set yrtwe = `date -d "12 days ago" +%y`
set yrele = `date -d "11 days ago" +%y`
set yrten = `date -d "10 days ago" +%y`
set yrnin = `date -d "9 days ago" +%y`
set yreig = `date -d "8 days ago" +%y`
set yrsev = `date -d "7 days ago" +%y`
set yrsix = `date -d "6 days ago" +%y`
set yrfiv = `date -d "5 days ago" +%y`
set yrfou = `date -d "4 days ago" +%y`
set yrthr = `date -d "3 days ago" +%y`
set yrtwo = `date -d "2 days ago" +%y`
set yrone = `date -d "1 days ago" +%y`
set yr = `date +%y`

set dytwe = `date -d "12 days ago" +%d`
set dyele = `date -d "11 days ago" +%d`
set dyten = `date -d "10 days ago" +%d`
set dynin = `date -d "9 days ago" +%d`
set dyeig = `date -d "8 days ago" +%d`
set dysev = `date -d "7 days ago" +%d`
set dysix = `date -d "6 days ago" +%d`
set dyfiv = `date -d "5 days ago" +%d`
set dyfou = `date -d "4 days ago" +%d`
set dythr = `date -d "3 days ago" +%d`
set dytwo = `date -d "2 days ago" +%d`
set dyone = `date -d "1 days ago" +%d`
set dy = `date +%d`

set motwe = `date -d "12 days ago" +%m`
set moele = `date -d "11 days ago" +%m`
set moten = `date -d "10 days ago" +%m`
set monin = `date -d "9 days ago" +%m`
set moeig = `date -d "8 days ago" +%m`
set mosev = `date -d "7 days ago" +%m`
set mosix = `date -d "6 days ago" +%m`
set mofiv = `date -d "5 days ago" +%m`
set mofou = `date -d "4 days ago" +%m`
set mothr = `date -d "3 days ago" +%m`
set motwo = `date -d "2 days ago" +%m`
set moone = `date -d "1 days ago" +%m`
set mo = `date +%m`

set modytwe = $motwe$dytwe
set modyele = $moele$dyele
set modyten = $moten$dyten
set modynin = $monin$dynin
set modyeig = $moeig$dyeig
set modysev = $mosev$dysev
set modysix = $mosix$dysix
set modyfiv = $mofiv$dyfiv
set modyfou = $mofou$dyfou
set modythr = $mothr$dythr
set modytwo = $motwo$dytwo
set modyone = $moone$dyone
set mody = $mo$dy

######################################################
### Move old files to new location and rename them ###
######################################################

	# Yesterday's files #
	
mv /winterslab_rit/realtime/images/phase_diagrams/probdiagram.pdf /winterslab_rit/realtime/images/old_phase_diagrams/probdiagram_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/deterministic.pdf /winterslab_rit/realtime/images/old_phase_diagrams/deterministic_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D0ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D0ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D1ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D1ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D2ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D2ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D3ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D3ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D4ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D4ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D5ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D5ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D6ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D6ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D7ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D7ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D8ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D8ens_$modyone.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/D9ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/D9ens_$modyone.pdf

	# Yesterday's Verification files #
	
mv /winterslab_rit/realtime/images/phase_diagrams/verif_probdiagram.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_probdiagram_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_deterministic.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_deterministic_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D1ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D1ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D2ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D2ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D3ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D3ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D4ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D4ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D5ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D5ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D6ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D6ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D7ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D7ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D8ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D8ens_$modyten.pdf
mv /winterslab_rit/realtime/images/phase_diagrams/verif_D9ens.pdf /winterslab_rit/realtime/images/old_phase_diagrams/verif_D9ens_$modyten.pdf

############################
##### Acquire GFS data #####
############################

	# Procure 00-h analyses from the previous 11 days at 6-h intervals #

set years = {$yrnin,$yreig,$yrsev,$yrsix,$yrfiv,$yrfou,$yrthr,$yrtwo,$yrone}
set dates = {$modynin,$modyeig,$modysev,$modysix,$modyfiv,$modyfou,$modythr,$modytwo,$modyone}
set hour = {'00','06','12','18'}
@ count = 1

#### Procure analysis from 11 and 10 days ago stored locally because NOMADS stopped carrying it ####
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t00z.pgrb2.1p00.$modyele.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t06z.pgrb2.1p00.$modyele.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t12z.pgrb2.1p00.$modyele.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t18z.pgrb2.1p00.$modyele.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t00z.pgrb2.1p00.$modyten.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t06z.pgrb2.1p00.$modyten.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t12z.pgrb2.1p00.$modyten.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 
cp /nfs/winterslab_rit/realtime/rawfiles/gfs.t18z.pgrb2.1p00.$modyten.anl /nfs/lb13/aw732728/tigge/jetclimo/wpc_npj_files/gfsanl$count.grb
@ count = $count + 1 

foreach d (1 2 3 4 5 6 7 8 9)
	foreach h (1 2 3 4)
		curl --retry 10 --retry-delay 10 "http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl?file=gfs.t$hour[$h]z.pgrb2.1p00.anl&lev_250_mb=on&var_UGRD=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgfs.20$years[$d]$dates[$d]$hour[$h]" -o gfs.t$hour[$h]z.pgrb2.1p00.anl
		sleep 5
	end
	
	if ($d == 1) then
		cp gfs.t00z.pgrb2.1p00.anl /nfs/winterslab_rit/realtime/rawfiles/gfs.t00z.pgrb2.1p00.$dates[$d].anl
		cp gfs.t06z.pgrb2.1p00.anl /nfs/winterslab_rit/realtime/rawfiles/gfs.t06z.pgrb2.1p00.$dates[$d].anl
		cp gfs.t12z.pgrb2.1p00.anl /nfs/winterslab_rit/realtime/rawfiles/gfs.t12z.pgrb2.1p00.$dates[$d].anl
		cp gfs.t18z.pgrb2.1p00.anl /nfs/winterslab_rit/realtime/rawfiles/gfs.t18z.pgrb2.1p00.$dates[$d].anl
	endif
	
	echo $count
	mv gfs.t00z.pgrb2.1p00.anl gfsanl$count.grb
	@ count = $count + 1
	mv gfs.t06z.pgrb2.1p00.anl gfsanl$count.grb
	@ count = $count + 1
	mv gfs.t12z.pgrb2.1p00.anl gfsanl$count.grb
	@ count = $count + 1
	mv gfs.t18z.pgrb2.1p00.anl gfsanl$count.grb
	@ count = $count + 1
	
end

	# Procure 0000 UTC GFS forecast data from F000-F240 at 6-h intervals #

set hr = {'00'}
set dates = {$yr$mody$hr}
set fhour = {'000','006','012','018','024','030','036','042','048','054','060','066','072','078','084','090','096','102','108','114','120','126','132','138','144','150','156','162','168','174','180','186','192','198','204','210','216','222','228','234','240'}
foreach f (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41)
	curl --retry 10 --retry-delay 10 "http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl?file=gfs.t00z.pgrb2.1p00.f$fhour[$f]&lev_250_mb=on&var_UGRD=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgfs.20$dates" -o gfs.t00z.pgrb2.1p00.f$fhour[$f]
	sleep 5
	echo $count
	mv gfs.t00z.pgrb2.1p00.f$fhour[$f] gfsanl$count.grb
	@ count = $count + 1
end

	# Assimilate the downloaded grib files for each forecast/analysis time into a single NetCDF file for the deterministic GFS #

ncl grib2nc_wpc.ncl monday=$mody

rm -f *.grb

###########################################
### Acquire GEFS ensemble forecast data ###
###########################################

	# Procure F006-F240 forecasts at 6-h intervals for each ensemble and control member in the operational GEFS #

set dates = {$yr$mody}
@ ensno = 0

set ensnum = {'c00','p01','p02','p03','p04','p05','p06','p07','p08','p09','p10','p11','p12','p13','p14','p15','p16','p17','p18','p19','p20'}
foreach e (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
	@ count = 1
	set fhour = {'06','12','18','24','30','36','42','48','54','60','66','72','78','84','90','96','102','108','114','120','126','132','138','144','150','156','162','168','174','180','186','192','198','204','210','216','222','228','234','240'}
	foreach f (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40)
		curl --retry 10 --retry-delay 10 "http://nomads.ncep.noaa.gov/cgi-bin/filter_gens.pl?file=ge$ensnum[$e].t00z.pgrb2f$fhour[$f]&lev_250_mb=on&var_UGRD=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgefs.20$dates%2F00%2Fpgrb2" -o gefs$ensnum[$e].t00z.pgrb2f$fhour[$f]
		sleep 5
		echo $count
		mv gefs$ensnum[$e].t00z.pgrb2f$fhour[$f] gefs$count.grb
		
		if (-e "gefs$count.grb") then
			@ count = $count + 1
		else 
			curl --retry 10 --retry-delay 10 "http://nomads.ncep.noaa.gov/cgi-bin/filter_gens.pl?file=ge$ensnum[$e].t00z.pgrb2f$fhour[$f]&lev_250_mb=on&var_UGRD=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgefs.20$dates%2F00%2Fpgrb2" -o gefs$ensnum[$e].t00z.pgrb2f$fhour[$f]
			sleep 5
			echo $count
			mv gefs$ensnum[$e].t00z.pgrb2f$fhour[$f] gefs$count.grb
			@ count = $count + 1
		endif
	end
	
	# Assimilate all the downloaded grib files into a unique NetCDF file for each of the ensemble members #
	
	ncl grib2nc_ens_wpc.ncl ens=$ensno monday=$mody
	@ ensno = $ensno + 1
	rm -f *.grb
end

##############################################################
### Move the NetCDF files into a new directory for storage ###
##############################################################

mv gfsanl_* /winterslab_rit/realtime/rawfiles
mv gefsens0_* /winterslab_rit/realtime/rawfiles
mv gefsens1_* /winterslab_rit/realtime/rawfiles
mv gefsens2_* /winterslab_rit/realtime/rawfiles
mv gefsens3_* /winterslab_rit/realtime/rawfiles
mv gefsens4_* /winterslab_rit/realtime/rawfiles
mv gefsens5_* /winterslab_rit/realtime/rawfiles
mv gefsens6_* /winterslab_rit/realtime/rawfiles
mv gefsens7_* /winterslab_rit/realtime/rawfiles
mv gefsens8_* /winterslab_rit/realtime/rawfiles
mv gefsens9_* /winterslab_rit/realtime/rawfiles
mv gefsens10_* /winterslab_rit/realtime/rawfiles
mv gefsens11_* /winterslab_rit/realtime/rawfiles
mv gefsens12_* /winterslab_rit/realtime/rawfiles
mv gefsens13_* /winterslab_rit/realtime/rawfiles
mv gefsens14_* /winterslab_rit/realtime/rawfiles
mv gefsens15_* /winterslab_rit/realtime/rawfiles
mv gefsens16_* /winterslab_rit/realtime/rawfiles
mv gefsens17_* /winterslab_rit/realtime/rawfiles
mv gefsens18_* /winterslab_rit/realtime/rawfiles
mv gefsens19_* /winterslab_rit/realtime/rawfiles
mv gefsens20_* /winterslab_rit/realtime/rawfiles

sleep 20

#############################################################################################
### Use the downloaded data to compute the the NPJ phase diagram for the current forecast ###
#############################################################################################

python eof_phasespace_RT.py

#############################################################################################
### Use the downloaded data to verify the the NPJ phase diagram forecast from 9 days ago  ###
#############################################################################################

python phasespace_verify.py  

################################################################################
### Remove the downloaded NetCDF from 10 days ago and GRIB from 12 days ago  ###
################################################################################

cd /winterslab_rit/realtime/rawfiles
set motennozero = `date -d "10 days ago" +%-m`
set modytennozero = $motennozero$dyten
rm -f *_$modytennozero.nc

rm -f *.$modytwe.anl

#######################################################################################################
### Create a GEMPAK file that will hold standardized anomaly data for the relevant forecast period  ###
#######################################################################################################

cd /lb13/aw732728/tigge/jetclimo/wpc_npj_files

	# Remove yesterday's standardized anomaly GEMPAK file #

rm -f rt_stdanoms.gem

	# Create GEMPAK file with standardized anomaly data #

python std_anom_gempak.py   

./mkgdfl_BTH.exe < mkgdfl_BTH_inRT

rm -f RTstdanoms.out

sleep 5

############################################################################################
### Run scripts that create companion synoptic images for the NPJ phase diagram forecast ###
############################################################################################

./synoptic_wpc.csh


