#! /usr/bin/bash
#---------------------------------------------------------------------------------------------------
# This script concatenates the 4 seperate files containing CMIP7 N-deposition fields (dry and wet
# deposition of NHx and NOy) into one file for easier reading by NorESM. String type attributes
# are replaced to avoid problems with PIO and nccopy
#
# Currently this is done for climatological 1850 files and for historical. CMIP7 scenarios 
# will be added when available
#----------------------------------------------------------------------------------------------------


ndepvar=("drynhx" "drynoy" "wetnhx" "wetnoy")

#-----------------------------------------------
# pre-industrial
#-----------------------------------------------
echo 'Precessing piControl data...'
basename=input4MIPs_surfaceFluxes_CMIP_FZJ-CMIP-nitrogen-2-0_gn_185001-185012-clim.nc
outfile=ndep_${basename}
rm $outfile

cp ${ndepvar[0]}_${basename} $outfile

ncks  -A -C -v ${ndepvar[1]} ${ndepvar[1]}_$basename $outfile
ncks  -A -C -v ${ndepvar[2]} ${ndepvar[2]}_$basename $outfile
ncks  -A -C -v ${ndepvar[3]} ${ndepvar[3]}_$basename $outfile

# Replace all string-type attributes with character-type attributes
# (not a very nice way to do this, but I didn't find a better solution)
ncatted -a units,lon,m,c,"degrees_east" $outfile
ncatted -a bounds,lon,m,c,"lon_bnds" $outfile
ncatted -a long_name,lon,m,c,"longitude" $outfile
ncatted -a long_name,lon_bnds,m,c,"longitude cell boundaries" $outfile

ncatted -a units,lat,m,c,"degrees_north" $outfile
ncatted -a bounds,lat,m,c,"lat_bnds" $outfile
ncatted -a long_name,lat,m,c,"latitude" $outfile
ncatted -a long_name,lat_bnds,m,c,"latitude cell boundaries" $outfile

ncatted -a units,time,m,c,"days since 1850-01-01" $outfile
ncatted -a calendar,time,m,c,"no_leap" $outfile
ncatted -a climatology,time,m,c,"climatology_bnds" $outfile
ncatted -a long_name,time,m,c,"time" $outfile
ncatted -a long_name,climatology_bnds,m,c,"climatological time averaging interval" $outfile

ncatted -a units,'^dry|^wet',m,c,'kg m-2 s-1' $outfile
ncatted -a cell_methods,'^dry|^wet',m,c,'time: mean' $outfile

ncatted -a long_name,drynhx,m,c,'drynhx' $outfile
ncatted -a long_name,drynoy,m,c,'drynoy' $outfile
ncatted -a long_name,wetnhx,m,c,'wetnhx' $outfile
ncatted -a long_name,wetnoy,m,c,'wetnoy' $outfile

# Delete original global attributes and create new ones
ncatted -h -a '.*',global,d,string, $outfile
ncatted -h -a title,global,c,c,"Created from the CMIP7 nitrogen deposition forcing dataset v2.0 (FZJ-CMIP-nitrogen-2-0) downloaded from input4MIPs" $outfile
ncatted -h -a created_by,global,c,c,"J. Schwinger (jorg.schwinger0@gmail.com)" $outfile
ncatted -h -a doi,global,c,c,"Original data doi=10.25981/ESGF.input4MIPs.CMIP7/2584172" $outfile
ncatted -h -a further_info_url,global,c,c,"https://input4mips-cvs.readthedocs.io/en/latest/dataset-overviews/nitrogen-deposition/" $outfile

#-----------------------------------------------
# historical
#-----------------------------------------------
echo 'Precessing historical data...'
basename=input4MIPs_surfaceFluxes_CMIP_FZJ-CMIP-nitrogen-2-0_gn_185001-202212.nc
outfile=ndep_$basename
rm $outfile

cp ${ndepvar[0]}_${basename} $outfile

ncks  -A -C -v ${ndepvar[1]} ${ndepvar[1]}_$basename $outfile
ncks  -A -C -v ${ndepvar[2]} ${ndepvar[2]}_$basename $outfile
ncks  -A -C -v ${ndepvar[3]} ${ndepvar[3]}_$basename $outfile


# Replace all string-type attributes with character-type attributes
# (not a very nice way to do this, but I didn't find a better solution)
ncatted -a units,lon,m,c,"degrees_east" $outfile
ncatted -a bounds,lon,m,c,"lon_bnds" $outfile
ncatted -a long_name,lon,m,c,"longitude" $outfile
ncatted -a long_name,lon_bnds,m,c,"longitude cell boundaries" $outfile

ncatted -a units,lat,m,c,"degrees_north" $outfile
ncatted -a bounds,lat,m,c,"lat_bnds" $outfile
ncatted -a long_name,lat,m,c,"latitude" $outfile
ncatted -a long_name,lat_bnds,m,c,"latitude cell boundaries" $outfile

ncatted -a units,time,m,c,"days since 1850-01-01" $outfile
ncatted -a calendar,time,m,c,"no_leap" $outfile
ncatted -a bounds,time,m,c,"time_bnds" $outfile
ncatted -a long_name,time,m,c,"time" $outfile
ncatted -a long_name,time_bnds,m,c,"time averaging interval" $outfile

ncatted -a units,'^dry|^wet',m,c,'kg m-2 s-1' $outfile
ncatted -a cell_methods,'^dry|^wet',m,c,'time: mean' $outfile

ncatted -a long_name,drynhx,m,c,'drynhx' $outfile
ncatted -a long_name,drynoy,m,c,'drynoy' $outfile
ncatted -a long_name,wetnhx,m,c,'wetnhx' $outfile
ncatted -a long_name,wetnoy,m,c,'wetnoy' $outfile

# Delete original global attributes and create new ones
ncatted -h -a '.*',global,d,string, $outfile
ncatted -h -a title,global,c,c,"Created from the CMIP7 nitrogen deposition forcing dataset v2.0 (FZJ-CMIP-nitrogen-2-0) downloaded from input4MIPs" $outfile
ncatted -h -a created_by,global,c,c,"J. Schwinger (jorg.schwinger0@gmail.com)" $outfile
ncatted -h -a doi,global,c,c,"Original data doi=10.25981/ESGF.input4MIPs.CMIP7/2584172" $outfile
ncatted -h -a further_info_url,global,c,c,"https://input4mips-cvs.readthedocs.io/en/latest/dataset-overviews/nitrogen-deposition/" $outfile

