#copy_crops.txt
#Author: Nancy.Y.Kiang@nasa.gov
#Copy crops variables (LAImax, height, monthly LAI)  into natural vegetation-only files generated by LUTaltclimate2modelEinput.sh.
#This should only be used for reproducing modern Earth vegetation with observed crops boundary conditions.
#Crops LAI should be scaled with the crop cover fraction in the land cover (lc) file.
#This script does not copy crop cover into the land cover (lc) file, but historical crop cover time series may be used from sources such as the Global Carbon Budget project.
#To use in NASA GISS ModelE simulations, an additonal post-processing step of applying the Ent_utils/extending_data fortran programs to the files to "extend" the LAImax, height, and monthly LAI boundary conditions across adjacent cells along the same latitude to ensure boundary condition values exist for changing historical crop cover.

#To run:
# If on NCCS discover:  module load nco/5.1.7
# or load nco tools locally.
# source copy_crops.txt <VERSION> <RES>

#ncks -A -v crops_herb <from file>  <to file>

#Example: 
#VERSION=ModernRepro_ba06166_hxh
#RES=720x360

VERSION=$1
RES=$2

if [ $# -lt 2 ] || [ $VERSION == '' ] || [ $RES == '' ]; then
  echo 'Copy crops variables into output files.'
  echo 'com_crops.txt <VERSION> <RES>';
  echo 'where';
  echo 'VERSION = short tag to identify your run, e.g. LUT hashtag or date';
  echo 'RES = 144x90 | 720x360';

  return;
fi

#Create new files:
cp V${RES}_laimax_${VERSION}.nc V${RES}_laimax_${VERSION}cr.nc
cp V${RES}_height_${VERSION}.nc V${RES}_height_${VERSION}cr.nc
cp V${RES}_lai_${VERSION}.nc V${RES}_lai_${VERSION}cr.nc

#Copy crops values

if [ $RES == '144x90' ]; then
#144x90

#LAI max
ncks -A -v crops_herb ../../kglut_modelE/data/V${RES}_EntGVSD_v1.0_MM16_lai_max_trimmed_scaled.nc V${RES}_laimax_${VERSION}cr.nc

#Height
ncks -A -v hgt_crops_herb ../../kglut_modelE/data/V${RES}_EntGVSD_v1.0_MM16_height_trimmed_scaled.nc V${RES}_height_${VERSION}cr.nc

#LAI monthly
ncks -A -v crops_herb ../../kglut_modelE/data/V${RES}_EntGVSD_v1.0_MM16_lai_trimmed_scaled.nc V${RES}_lai_${VERSION}cr.nc
fi

if [ $RES == '720x360' ]; then
#720x360 

#LAI max
ncks -A -v crops_herb ../../kglut_modelE/data/V${RES}_EntGVSD16_MM_lai_max_trimmed_scaled_v1.0b.nc V${RES}_laimax_${VERSION}cr.nc

#Height
#ncks -A -v crops_herb -s 'hgt_crops_herb=crops_herb' ../../kglut_modelE/data/V${RES}_EntGVSD16_MM_height_trimmed_scaled_v1.0b.nc V${RES}_height_${VERSION}cr.nc
ncrename -O -v hgt_crops_herb,crops_herb V${RES}_height_${VERSION}cr.nc
ncks -A -v crops_herb ../../kglut_modelE/data/V${RES}_EntGVSD16_MM_height_trimmed_scaled_v1.0b.nc V${RES}_height_${VERSION}cr.nc
ncrename -O -v crops_herb,hgt_crops_herb V${RES}_height_${VERSION}cr.nc

#LAI monthly
ncks -A -v crops_herb ../../kglut_modelE/data/V${RES}_EntGVSD16_MM_lai_trimmed_scaled_v1.0b.nc V${RES}_lai_${VERSION}cr.nc
fi

echo 'Copied crops laimax, height, and lai monthly to new boundary condition files.'

