#!/bin/csh
# script to convert the RAW Netcdf into a Netcdf files formatted
# to use kp_nc and wi_nc from the HYCOM-tools
# this example is for the GEFS
# this script necessitate the nco package (http://nco.sourceforge.net)
# Alexandra Bozec (COAPS/FSU) Dec 2021
set echo
setenv TOOLS /nexsan/people/abozec/HYCOM-tools
# set the variables
set FNAME = ( t2m q2m precip u10 v10 dlwrf dswrf ) # from input filenames
set VNAME = ( tmp2m spfh2m prateb_avesfc ugrd10m vgrd10m dlwrf_avesfc dswrf_avesfc ) # Netcdf input variable names
set CNAME = ( t_2m q_2m precip u_10 v_10 lwdn swdn )  # Netcdf output variable names (less than 6 char.)
set units = ( "degC" "kg/kg" "kg/m2/s" "m/s" "m/s" "W/m2" "W/m2")

setenv Y 2000
setenv M 01
# Do every variables
foreach t ( 1 2 3 4 5 6 7)
#foreach t ( 1 )
# PATH
  setenv D /nexsan/people/abozec/GEFS/
  setenv F gefs.${Y}${M}_${FNAME[${t}]}.nc
  setenv NEW_F gefs_hycom.${Y}${M}_${FNAME[${t}]}.nc

# name of input variables
  setenv lon_var lon
  setenv lat_var lat
  setenv tim_var time
  setenv var     ${VNAME[${t}]}
  setenv offscale 0

# change variables name
  ncrename -d ${lon_var},Longitude -d ${lat_var},Latitude -d ${tim_var},MT -v ${lon_var},Longitude -v ${lat_var},Latitude -v ${tim_var},MT ${F} ${NEW_F}

# reverse the Latitude
  ncpdq -a "-Latitude" ${NEW_F} test.nc
  /bin/mv test.nc ${NEW_F}

# add  global attribute title
  ncatted -O -a title,global,a,c,"GEFS forcing" ${NEW_F}

# add offset to the time (time since 1900-12-31):
  setenv N `echo ${Y} ${M} 01 00 3 | ${TOOLS}/bin/hycom_ymdh_wind| awk '{printf($1)}'`
  echo ${N}
  set MT0=`ncks -s "%8.3f " -H -C -d MT,0,0 -v MT  ${NEW_F}`
  ncap2 -F -s "MT=MT/24.-${MT0}/24.+${N}"  ${NEW_F} test.nc
  /bin/mv test.nc ${NEW_F}

# add offset if temperature in Kelvin
  if ($t == 1 ) then
    ncap2 -F -s "${var}=${var}-273.15"  ${NEW_F} test.nc
    /bin/mv test.nc ${NEW_F}
  endif

# change calendar and unists attribute of time variable
  ncatted -O -a calendar,MT,o,c,gregorian ${NEW_F}
  ncatted -O -a units,MT,o,c,"days since 1900-12-31 00:00:00" ${NEW_F}

# add Next_MT
  set tt=`ncks -s "%8.3f " -H -C -v MT ${NEW_F}` ## get the time value
  setenv t_len $#tt                              ## get the size of the time array
  set inc = `echo "${tt[2]} - ${tt[1]}" | bc`    ## get the increment
  set nextT = `echo "${tt[${t_len}]} + ${inc}" | bc`   ## get the next_MT value
  ncatted -O -a next_MT,MT,a,f,${nextT} ${NEW_F}       ## add the attribute

# add units attribute to main variable
  ncatted -O -a units,${var},a,c,${units[${t}]} ${NEW_F}       ## add the attribute

# rename var to be less than 6 char in length (CNAME)
  ncrename -v ${var},${CNAME[${t}]} ${NEW_F} test.nc
  \mv test.nc ${NEW_F}

# add scale_factor and offset by packing the file if needed
  if (${offscale} == 0) then
    #ncatted -O -a add_offset,${var},a,f,0.0 ${NEW_F}
    #ncatted -O -a scale_factor,${var},a,f,1.0 ${NEW_F}
    ncpdq ${NEW_F} test.nc
    \mv test.nc ${NEW_F}
  endif


  echo ${t}
end

# get u10 and v10 in the same file
/bin/mv gefs_hycom.${Y}${M}_u10.nc gefs_hycom.${Y}${M}_uv-10m.nc
ncks -A -v v_10  gefs_hycom.${Y}${M}_v10.nc gefs_hycom.${Y}${M}_uv-10m.nc
/bin/rm gefs_hycom.${Y}${M}_v10.nc
