#!/bin/csh
#
# Get GFS files for ETI analysis

# contact  : chsegura@udec.cl
# last mod : 2020-07-28
############################################
echo "############################"
echo "   runSETI.csh is running   "
echo "############################"
echo " "

set diri=/home/matlab/hw_dynamic
cd ${diri}/ETI/old
rm -r 2021*

set d=`date +%d`
set y=`date +%Y`
set m=`date +%m`

cd ${diri}/ETI/figures
if ( -e "SETI forecast From GFS ${y}-${m}-${d} 00:00:00.png" ) then
	echo "SETI forecast From GFS ${y}-${m}-${d} 00:00:00.png exist"
	echo " "
	exit	
else 

cd ${diri}/ETI/scripts

echo "recoverSETI.sh is running"
echo " "
#./recoverSETI.sh -wl=0 -el=360 -nl=15 -sl=-70 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output
./recoverSETI.csh ${diri}

############################################
# GFS analysis 1 day before ################

cd ${diri}/ETI/scripts

echo "get_GFS_ana.csh is running"
echo " "
./get_GFS_ana.sh -wl=0 -el=360 -nl=15 -sl=-70 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output

############################################
# GFS forecast 16 days after ###############

cd ${diri}/ETI/scripts

echo "get_GFS_oper.sh is running"
echo " "
./get_GFS_oper.sh -d=${y}${m}${d} -r=00 -wl=0 -el=360 -nl=15 -sl=-70 -et=384 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output


if (-e ${diri}/ETI/output/forecast_16_days_after_oper.nc) then
	cdo -ntime ${diri}/ETI/output/forecast_16_days_after_oper.nc > ${diri}/ETI/output/records_oper_file.txt
	set records_oper=`cat ${diri}/ETI/output/records_oper_file.txt`
	@ nrecords=($records_oper)
	if ($nrecords < 65) then
		echo "The file 'forecast_16_days_after_oper.nc' has less than 65 records, try the next execution!"
		echo " "
		exit
	else
		echo "The file 'forecast_16_days_after_oper.nc' exists and has no errors, the execution continues :)"
		echo " "
	endif
else
	echo "Online files are missing ... try the next execution!"
	echo " "
	exit
endif

echo "get_GFS_eavg.sh is running"
echo " "
./get_GFS_eavg.sh -d=${y}${m}${d} -r=00 -wl=0 -el=360 -nl=15 -sl=-70 -et=384 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output

echo "get_GFS_ens.sh is running"
echo " "
./get_GFS_ens.sh -d=${y}${m}${d} -r=00 -wl=0 -el=360 -nl=15 -sl=-70 -et=384 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output


echo "get temperature is running"
echo " "
./get_GFS_oper_temp.sh -d=${y}${m}${d} -r=00 -wl=284 -el=294 -nl=-18 -sl=-55 -et=384 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output

./get_GFS_eavg_temp.sh -d=${y}${m}${d} -r=00 -wl=284 -el=294 -nl=-18 -sl=-55 -et=384 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output

./get_GFS_ens_temp.sh -d=${y}${m}${d} -r=00 -wl=284 -el=294 -nl=-18 -sl=-55 -et=384 -tmpdir=$diri/ETI/tmp -outdir=$diri/ETI/output

############################################
# GFS analysis cut ######################

cd ${diri}/ETI/data

echo "select dates"
echo " "
set d1=`date --date="5000 days ago" +%d`
set y1=`date --date="5000 days ago" +%Y`
set m1=`date --date="5000 days ago" +%m`

set d2=`date --date="2 days ago" +%d`
set y2=`date --date="2 days ago" +%Y`
set m2=`date --date="2 days ago" +%m`

echo "${y1}-${m1}-${d1} to ${y2}-${m2}-${d2}"
echo " "

cdo seldate,${y1}-${m1}-${d1},${y2}-${m2}-${d2} temp.nc backup.nc

mv backup.nc ${diri}/ETI/output

cd ${diri}/ETI/output

echo "change name 'Geopotential_height_isobaric' to 'gh'"
echo " "
cdo chname,Geopotential_height_isobaric,gh analysis_1_days_before.nc temp.nc
rm analysis_1_days_before.nc

/usr/local/bin/ncdump -v latitude temp.nc | sed -e '1,/data:/d' -e '$d' >& latitudes.log
tr ',' '\n' < latitudes.log > latitudes2.log
#rm latitudes.log

set lat2invert=`cat latitudes2.log`
set lat1=${lat2invert[4]}
set lat2=${lat2invert[6]}
set dif=`echo "($lat1 - $lat2) / 1" | bc`

if ( $dif > 0 ) then
	cdo invertlat temp.nc analysis_1_days_before.nc
else
	mv temp.nc analysis_1_days_before.nc
endif

#rm latitudes2.log
rm temp.nc

cdo mergetime backup.nc analysis_1_days_before.nc forecast_16_days_after_oper.nc gfs_data.nc 

############################################
# matlab figures ###########################

cd ${diri}/ETI/data/series

echo "MJO index is downloading ..."
echo " "
#curl http://www.bom.gov.au/climate/mjo/graphics/rmm.74toRealtime.txt > mjo_index.txt
#tail -n 500 mjo_index.txt > mjo_index
#rm mjo_index.txt

wget http://www.bom.gov.au/climate/mjo/graphics/rmm.74toRealtime.txt 
tail -n 500 rmm.74toRealtime.txt > mjo_index
rm rmm.74toRealtime.txt

wget https://iridl.ldeo.columbia.edu/SOURCES/.BoM/.MJO/.RMM/amplitude/data.nc -O amp.nc
wget https://iridl.ldeo.columbia.edu/SOURCES/.BoM/.MJO/.RMM/phase/data.nc -O pha.nc

# Define el año aquí
set YEAR = 2025

# Borrar archivos previos
echo "Eliminando archivos del año $YEAR..."
rm cr2_tasminDaily_${YEAR}.zip cr2_tasmaxDaily_${YEAR}.zip cr2_tasDaily_${YEAR}.zip

#
#rm cr2_tasminDaily_2020.zip cr2_tasmaxDaily_2020.zip cr2_tasDaily_2020.zip
#rm cr2_tasminDaily_2021.zip cr2_tasmaxDaily_2021.zip cr2_tasDaily_2021.zip
#rm cr2_tasminDaily_2022.zip cr2_tasmaxDaily_2022.zip cr2_tasDaily_2022.zip
#rm cr2_tasminDaily_2023.zip cr2_tasmaxDaily_2023.zip cr2_tasDaily_2023.zip
#rm cr2_tasminDaily_2024.zip cr2_tasmaxDaily_2024.zip cr2_tasDaily_2024.zip

#wget --user obsguest --password Xendan72 ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasDaily_2020.zip
#wget --user obsguest --password Xendan72 ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasmaxDaily_2020.zip
#wget --user obsguest --password Xendan72 ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasminDaily_2020.zip

echo "Temperature data is downloading ..."
echo " "
#curl -u obsguest:Xendan72 -O ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasDaily_2022.zip
#curl -u obsguest:Xendan72 -O ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasmaxDaily_2022.zip
#curl -u obsguest:Xendan72 -O ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasminDaily_2022.zip

#curl -u obsguest:Xendan72 -O ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasDaily_2023.zip
#curl -u obsguest:Xendan72 -O ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasmaxDaily_2023.zip
#curl -u obsguest:Xendan72 -O ftp://qhawayra2.dgf.uchile.cl/daily/OUTPUT/cr2_tasminDaily_2023.zip

#curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasDaily_2023.zip
#curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasmaxDaily_2023.zip
#curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasminDaily_2023.zip

#curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasDaily_2024.zip
#curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasmaxDaily_2024.zip
#curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasminDaily_2024.zip

# Descargar archivos
echo "Descargando datos de temperatura para el año $YEAR..."
curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasDaily_${YEAR}.zip
curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasmaxDaily_${YEAR}.zip
curl -u obsguest:Xendan72 -O ftp://cr2.cl/daily/OUTPUT/cr2_tasminDaily_${YEAR}.zip

# Descomprimir y mover archivos
echo "Descomprimiendo y organizando datos..."
foreach TYPE (tasmin tasmax tas)
    unzip cr2_${TYPE}Daily_${YEAR}.zip
    mv cr2_${TYPE}Daily_${YEAR}/cr2_${TYPE}Daily_${YEAR}.txt .
    rm -r cr2_${TYPE}Daily_${YEAR}
end

#unzip cr2_tasminDaily_2024.zip
#mv cr2_tasminDaily_2024/cr2_tasminDaily_2024.txt .
#rm -r cr2_tasminDaily_2024

#unzip cr2_tasmaxDaily_2024.zip
#mv cr2_tasmaxDaily_2024/cr2_tasmaxDaily_2024.txt .
#rm -r cr2_tasmaxDaily_2024

#unzip cr2_tasDaily_2024.zip
#mv cr2_tasDaily_2024/cr2_tasDaily_2024.txt .
#rm -r cr2_tasDaily_2024

# curico DGA: min 191, max 191, mean 176
# general freire: min 359, max 359, mean 300
#awk -F , -v OFS='\t' '{print $1, $359}' cr2_tasminDaily_2021.txt > T_min.txt
#awk -F , -v OFS='\t' '{print $1, $359}' cr2_tasmaxDaily_2021.txt > T_max.txt
#awk -F , -v OFS='\t' '{print $1, $300}' cr2_tasDaily_2021.txt > T_mean.txt

#tail -n 500 T_min.txt > T_min
#tail -n 500 T_max.txt > T_max
#tail -n 500 T_mean.txt > T_mean

#( head -n15 cr2_tasminDaily_2024.txt && tail -n500 cr2_tasminDaily_2024.txt; ) > T_min
#( head -n15 cr2_tasmaxDaily_2024.txt && tail -n500 cr2_tasmaxDaily_2024.txt; ) > T_max
#( head -n15 cr2_tasDaily_2024.txt && tail -n500 cr2_tasDaily_2024.txt; ) > T_mean

# Procesar datos
echo "Procesando datos para T_min, T_max y T_mean..."
( head -n15 cr2_tasminDaily_${YEAR}.txt && tail -n500 cr2_tasminDaily_${YEAR}.txt ) > T_min
( head -n15 cr2_tasmaxDaily_${YEAR}.txt && tail -n500 cr2_tasmaxDaily_${YEAR}.txt ) > T_max
( head -n15 cr2_tasDaily_${YEAR}.txt && tail -n500 cr2_tasDaily_${YEAR}.txt ) > T_mean

echo "Proceso completado para el año $YEAR."


#rm cr2_tasminDaily_2020.txt cr2_tasmaxDaily_2020.txt cr2_tasDaily_2020.txt T_min.txt T_max.txt T_mean.txt 
#
cd ${diri}/ETI/scripts
#/usr/local/bin/matlab -nodisplay -nosplash -nodesktop -r "run('sETI.m');exit;" | tail -n +11
echo "Matlab script is running ..."
echo " "
/usr/local/bin/matlab -nodisplay -nosplash -nodesktop < sETI.m

############################################
# upload figures ###########################

cd ${diri}/ETI/figures
if ( -e "SETI forecast From GFS ${y}-${m}-${d} 00:00:00.png" ) then
	echo "SETI forecast From GFS ${y}-${m}-${d} 00:00:00.png was produced successfully and it will be uploaded to the directory 'upload'"
	echo " "
	cp 'SETI forecast From GFS '${y}'-'${m}'-'${d}' 00:00:00.png' ${diri}/ETI/upload/fig_01.png
	cd ${diri}/ETI/upload
	#set URL='https://www.cpc.ncep.noaa.gov/products/precip/CWlink/MJO/diagram_40days_forecast_GEFS_member.gif'
	set URL='https://www.cpc.ncep.noaa.gov/products/precip/CWlink/MJO/ensplume_full.gif'
	wget ${URL} -O fig_02.gif
	set URL='http://web.directemar.cl/met/jturno/cartas/imagen.jpg'
	wget ${URL} -O fig_03.jpg
	mogrify -format png fig_03.jpg 

	set d2=`date --date="2 days ago" +%d`
	set y2=`date --date="2 days ago" +%Y`
	set m2=`date --date="2 days ago" +%m` 
	set URL="https://ds.data.jma.go.jp/tcc/tcc/products/clisys/anim/GIF/tp/anom/${y2}/${m2}/${d2}/5day/OlrPsiWaf_tp200hPa_${y2}${m2}${d2}.gif"
	wget ${URL} -O fig_04.gif
	mogrify -format png fig_04.gif
else
	echo "SETI forecast From GFS ${y}-${m}-${d} 00:00:00.png was not produced successfully!"
	echo " "
	exit
endif

############################################
# GFS backup ###############################

cd ${diri}/ETI/figures
if ( -e "SETI forecast From GFS ${y}-${m}-${d} 00:00:00.png" ) then
	cd ${diri}/ETI/data/backup/
	rm analysis_backup.nc
	cd ${diri}/ETI/data
	mv analysis_backup.nc ${diri}/ETI/data/backup/
	mv temp.nc analysis_backup.nc

	cd ${diri}/ETI/output
	cdo mergetime backup.nc analysis_1_days_before.nc temp.nc

	mv temp.nc ${diri}/ETI/data

	cp SETI_all.csv ../files/${y}-${m}-${d}.csv

	#mv ${diri}/ETI/output ${diri}/ETI/old/${y}${m}${d}
	#mkdir ${diri}/ETI/output
	echo "SETI script finished successfully :)"
	echo " "
else 
	exit
endif
endif

# useful commands 
#
# cdo chname,Geopotential_height_isobaric,gh otro otro
# cdo invertlat infile outfile
# cdo seldate,2019-10-25,2019-11-10 analysis_21_days_before.nc out
# cdo -sellonlatbox,-180,180,-70,15 infile outfile
# cdo sellevel,50000 gh.nc gh_5.nc

#awk -F , -v OFS='\t' 'NR == 1 || $6 > 4 {print $1, $191}' temporal.txt

