#!/bin/csh
# Download MODIS netCDF data
# Written by: Taylor Shropshire 
# date: 9/1/2021

# Parameters
setenv url https://oceandata.sci.gsfc.nasa.gov/ob/getfile/
setenv path1 /nexsan/people/taylor/postdoc/observations/MODIS/MODIS_sst/data_4km/

# Year loop
#foreach year (2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021)
#foreach year (2013 2014 2015 2016 2017 2018 2019 2020)
setenv year 2002

/bin/rm -rf ${path1}${year}
/bin/mkdir ${path1}${year}

setenv odir ${path1}${year}/
setenv mon 1

# Month loop
while($mon<13)
setenv mon_char `echo $mon | awk '{printf("%02d", $1)}'`
setenv day 1

# Day loop
while ($day<32)
setenv day_char `echo $day | awk '{printf("%02d", $1)}'`

# example: AQUA_MODIS.20030101.L3m.DAY.SST.sst.9km.nc	(different the chl file name)
setenv fname AQUA_MODIS.${year}${mon_char}${day_char}.L3m.DAY.SST.sst.4km.nc
setenv in_file ${url}${fname}

setenv out_file1 ${odir}${fname}.txt
setenv out_file2 ${odir}MODIS_sst_4km_daily_${year}${mon_char}${day_char}.nc

echo $in_file 

# !!!!!!
# Note: You need to get your own username and pass from the oceandata website
# !!!!!

#wget --user=USERNAME --password=PASS --auth-no-challenge=on ${in_file} -O ${out_file1}
wget --user=USERNAME --password=PASS --auth-no-challenge=on --no-check-certificate ${in_file} -O ${out_file1}

chmod 755 $out_file1
echo 'Processing file: '$out_file1
ncks -d lon,-102.,-70. -d lat,10.,50. $out_file1 $out_file2
/bin/rm $out_file1

setenv day `expr $day + 1`
end

setenv mon `expr $mon + 1`
end




