#!/bin/bash

export LC_ALL=C

set -x

for file in mon*CESM2*.nc ; do

ferret <<EOF
let crs=""
save/append/file=$file crs
quit
EOF

ncrename -h -O -d STRING1_1,char1 $file
ncrename -h -O -v CRS,crs $file

ncdump -h $file > ${file}.log

line=`grep -n "variables:" ${file}.log | head -n 1`
line=${line%%\:*}

xdim=`head -n $line ${file}.log | grep -i "x" | head -n 1`
xdim=${xdim%\ \=*}
xdim=${xdim//[[:blank:]]/}
ydim=`head -n $line ${file}.log | grep -i "y" | head -n 1`
ydim=${ydim%\ \=*}
ydim=${ydim//[[:blank:]]/}

#ncrename -h -O -d $ydim,y -v $ydim,y $file
#ncrename -h -O -d $xdim,x -v $xdim,x $file
#ncrename -h -O -d TIME,time -v TIME,time $file

#ncrename -h -O -d SECTOR,sector $file      &>/dev/null
#ncrename -h -O -v SECTOR,sector $file      &>/dev/null
#ncrename -h -O -d SECTOR1_1,sector1 $file  &>/dev/null
#ncrename -h -O -v SECTOR1_1,sector1 $file  &>/dev/null

#ncrename -h -O -d ATMLAY,atmlay $file      &>/dev/null
#ncrename -h -O -v ATMLAY,atmlay $file      &>/dev/null
#ncrename -h -O -d ATMLAY3_3,atmlay1 $file  &>/dev/null
#ncrename -h -O -v ATMLAY3_3,atmlay1 $file  &>/dev/null

#ncrename -h -O -d OUTLAY,outlay $file      &>/dev/null
#ncrename -h -O -v OUTLAY,outlay $file      &>/dev/null
#ncrename -h -O -d OUTLAY1_1,outlay1 $file  &>/dev/null
#ncrename -h -O -v OUTLAY1_1,outlay1 $file  &>/dev/null

#ncrename -h -O -d PLEV,plev $file          &>/dev/null
#ncrename -h -O -v PLEV,plev $file          &>/dev/null

#ncrename -h -O -d ZUVLEV,zuvlev $file      &>/dev/null
#ncrename -h -O -v ZUVLEV,zuvlev $file      &>/dev/null
#ncrename -h -O -d ZTQLEV,ztqlev $file      &>/dev/null
#ncrename -h -O -v ZTQLEV,ztqlev $file      &>/dev/null

ncdump -h $file > ${file}.log
xdim="x" ; ydim="y"
grep -i "y, x)" ${file}.log > ${file}.log2

while IFS= read -r var ; do 
 var2=${var%\(*}
 var2=${var2#*\ }
  dim=${var#*\(}
  dim=${dim%\)*}
echo $file $var2 $dim
# 
ncatted -h -a grid_mapping,$var2,o,c,"crs"     $file
ncatted -h -a coordinates,$var2,o,c,"$dim"     $file
ncatted -h -a history,$var2,d,"",""            $file
ncatted -h -a missing_value,$var2,o,f,-1.e+19  $file
ncatted -h -a _FillValue,$var2,o,f,-1.e+19     $file
if [ "${var2}" != "TT" ] || [ "${var2}" != "ST" ] ; then
ncatted -h -a units,$var2,o,c,"mm WE/month"     $file
fi
#
done < ${file}.log2
#
ncatted -h -a grid_mapping_name,crs,o,c,"polar_stereographic" $file
ncatted -h -a standard_name,crs,o,c,"polar_stereographic" $file
ncatted -h -a long_name,crs,o,c,"Polar Stereographic (variant B)" $file
ncatted -h -a authority,crs,o,c,"EPSG:3031" $file
ncatted -h -a standard_parallel,crs,o,f,-71.  $file
ncatted -h -a longitude_of_prime_meridian,crs,o,f,0.  $file
ncatted -h -a straight_vertical_longitude_from_pole,crs,o,f,0.  $file
ncatted -h -a semi_major_axis,crs,o,f,6378137.  $file
ncatted -h -a inverse_flattening,crs,o,f,298.257223563  $file
ncatted -h -a latitude_of_projection_origin,crs,o,f,-90.  $file
ncatted -h -a false_easting,crs,o,f,0.  $file
ncatted -h -a false_northing,crs,o,f,0.  $file

ncatted -h -a institute,global,o,c,"University of Liege (Belgium)" $file
ncatted -h -a institution,global,o,c,"University of Liege (Belgium)" $file
ncatted -h -a contact,global,o,c,"ckittel@uliege.be"        $file
#ncatted -h -a model,global,o,c,"regional climate model MAR (`INSTALL 0`)"  $file
ncatted -h -a date,global,o,c,"`date`" $file
ncatted -h -a NCO,global,d,"","" $file
ncatted -h -a history,global,d,"","" $file

rm -f $file.log $file.log2 &>/dev/null

done
