#!/bin/bash

# Load nco
#module load nco

YEAR=${1}
echo $YEAR

# change time units from days since 1/1/${YEAR} to minutes since 1/1/${YEAR}
ncap2 -s 'time=time*1440' lai_8-day_0.1_${YEAR}.nc tmp1.nc

# edittime attributes
ncatted -O -a units,time,m,c,"minutes since ${YEAR}-01-01 00:00:00 GMT" tmp1.nc
ncatted -O -a long_name,time,m,c,"time" tmp1.nc
ncatted -O -a begin_date,time,a,c,"${YEAR}0101" tmp1.nc
ncatted -O -a begin_time,time,a,c,"000000" tmp1.nc

# edit LAI attributes
ncatted -O -a units,lai,o,c,'cm2 cm-2' tmp1.nc
ncatted -O -a long_name,lai,o,c,'MODIS leaf area index' tmp1.nc
ncatted -O -a gamap_category,lai,o,c,'MODIS' tmp1.nc

# edit lat attributes
ncatted -O -a long_name,lat,o,c,'latitude' tmp1.nc
ncatted -O -a standard_name,lat,o,c,'latitude' tmp1.nc
ncatted -O -a axis,lat,o,c,'Y' tmp1.nc

# edit lon attributes
ncatted -O -a long_name,lon,o,c,'longitude' tmp1.nc
ncatted -O -a standard_name,lon,o,c,'longitude' tmp1.nc
ncatted -O -a axis,lon,o,c,'X' tmp1.nc

# change variable name
ncrename -v lai,MODIS tmp1.nc

# chunk and compress
nccopy -c lon/3600,lat/1800,time/1 -d1 tmp1.nc MODIS.LAI.BNUv6.generic.01x01.${YEAR}.nc

# remove temporary files
rm -f tmp1.nc
