#! /bin/csh -f

set start_time = `date +%s`
source ~/.cshrc

set mm = `date -u +%m`
set dd = `date -u +%d`
set hh = `date -u +%k`
set yy = `date -u +%y`
echo The Current Date Is $yy$mm$dd
set date = $yy$mm$dd

cd /network/rit/lab/mesonet/mesonet/climate/daily

set missing = -996

########### only for backfill
set currentdate = 20${yy}${mm}${dd}
set startday = -20
set endday   = -10
while ( $startday <= $endday)
set dd = `date +%d -d "$currentdate UTC + $startday day"`
set mm = `date +%m -d "$currentdate UTC + $startday day"`
set yy = `date +%y -d "$currentdate UTC + $startday day"`

set ddtwo = `date +%d -d "$currentdate UTC + 1 day + $startday day"`
set mmtwo = `date +%m -d "$currentdate UTC + 1 day + $startday day"`
set yytwo = `date +%y -d "$currentdate UTC + 1 day + $startday day"`
set jj = `date +%j -d "$currentdate UTC  + $startday day"`

scp nbassill@operations.nysmesonet.org:/mnt/remote/data/archive/nysm/netcdf/proc/20${yy}/${mm}/20${yy}${mm}${dd}.nc .
mv 20${yy}${mm}${dd}.nc dayone.nc

scp nbassill@operations.nysmesonet.org:/mnt/remote/data/archive/nysm/netcdf/proc/20${yytwo}/${mmtwo}/20${yytwo}${mmtwo}${ddtwo}.nc .
mv 20${yytwo}${mmtwo}${ddtwo}.nc daytwo.nc

foreach station (LKPL)

rm station.txt
echo $jj                              >> station.txt
echo $yy                              >> station.txt
echo $station                         >> station.txt

rm $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt

ncl net.ncl
mv $station$yy$jj.txt $station
end
@ startday ++
end

########### regular part
set currentdate = 20${yy}${mm}${dd}
set startday = -10
set endday   = 12
while ( $startday <= $endday)
set dd = `date +%d -d "$currentdate UTC + $startday day"`
set mm = `date +%m -d "$currentdate UTC + $startday day"`
set yy = `date +%y -d "$currentdate UTC + $startday day"`

set ddtwo = `date +%d -d "$currentdate UTC + 1 day + $startday day"`
set mmtwo = `date +%m -d "$currentdate UTC + 1 day + $startday day"`
set yytwo = `date +%y -d "$currentdate UTC + 1 day + $startday day"`
set jj = `date +%j -d "$currentdate UTC  + $startday day"`

scp nbassill@operations.nysmesonet.org:/mnt/remote/data/archive/nysm/netcdf/proc/20${yy}/${mm}/20${yy}${mm}${dd}.nc .
mv 20${yy}${mm}${dd}.nc dayone.nc

scp nbassill@operations.nysmesonet.org:/mnt/remote/data/archive/nysm/netcdf/proc/20${yytwo}/${mmtwo}/20${yytwo}${mmtwo}${ddtwo}.nc .
mv 20${yytwo}${mmtwo}${ddtwo}.nc daytwo.nc

foreach station (SCHU WHIT WATE CSQR SPRA COLD JORD WEST BERK BELD ESSX CHES STEP OTIS LAUR GABR COBL TICO WFMB COPE BATA SBRI SHER DELE WOLC JOHN LOUI TULL WARW WALT CINC FRED BRAN WELL CAPE BELL WARS BELM COHO BURD TYRO PENN CLYM RAND OLEA BURT BROC SCHA REDF HARR OSCE WALL MORR CAMD ELMI PHIL POTS OSWE CHAZ SARA RUSH TUPP CLIF FAYE EDIN MEDI GROV OWEG NBRA DUAN ILAK OPPE REDH COPA ANDE HARP BROO EAUR YORK HART HERK ADDI SPRI MALO VOOR HAMM EDWA SUFF BSPA BEAC DEPO MEDU ROXB ELDR GFLD WGAT ELLE KIND WBOU OLDF NHUD SCIP BING NEWC CLAR ONTA CROG RAQU WANT SOUT HFAL DOVE GROT SCHO TANN SOME BUFF GFAL PISE BREW QUEE STAT BKLN MANH BRON STON LKPL)

rm station.txt
echo $jj                              >> station.txt
echo $yy                              >> station.txt
echo $station                         >> station.txt

rm $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt
echo $missing                         >> $station$yy$jj.txt

ncl net.ncl
mv $station$yy$jj.txt $station
end
@ startday ++
end

getthirty.csh

mkdir /network/rit/lab/mesonet/mesonet/$date/climo
cp *png *dat /network/rit/lab/mesonet/mesonet/$date/climo
cp *dat /network/rit/lab/mesonet/mesonet/dhses/daily
set mm = `date -u +%m`
set dd = `date -u +%d`
set yy = `date -u +%y`
scp *png nbassill@operations.nysmesonet.org:/home/nbassill/public_html/archive/20$yy/$mm/$dd/climate
scp *../*ncl ../*csh *ncl *csh nbassill@operations.nysmesonet.org:/home/nbassill/public_html/archive/20$yy/$mm/$dd/climate/scripts
cd /network/rit/lab/mesonet/mesonet/dhses/daily/
./regions.csh >! regions.out

set end_time   = `date  +%s`
@ length_time  = $end_time - $start_time
echo "It took this long to run getdaily.csh: $length_time seconds"
