#!/bin/bash
#
#   Usage: amip-bc-cc2nc [options] cccma_file [cccma_file ...]
#
# Purpose: Convert AMIP boundary data from CCCma binary format to netcdf
#
# The arg cccma_file that is supplied on the command line is assumed
# to be a prefix to which "_gt", "_sicn" and "_sic" will be added to
# identify 3 separate files containing multi year time series for
# GT, SIC and SICN respectively. This behaviour may be changed by
# supplying the -c option (see below).
#
# Output files will overwrite any existing file of the same name.
#
# Options:
# All options begin with a dash (-)
#   -c ..the file supplied on the command line contains a climatology
#        for GT, SIC and SICN, typically an AGCM "AN" file, but can
#        be any CCCma format file with GT, SIC and SICN written as
#        they appear in an "AN" file (ie same name and ibuf2 values)
#   -s ..save the netcdf files to DATAPATH
#   -h ..show this usage message
#
########################################################################
#
# Larry Solheim ...Jan 2017

FULLPATH=`type $0|awk '{print $3}'` # pathname of this script
Runame=`basename $FULLPATH`
usage() {
  err_exit=0
  while getopts e opt; do
    case $opt in
      e) err_exit=1 ;;
    esac
  done
  shift `expr $OPTIND - 1`

  [ -n "$1" ] && echo >&2 "${Runame}:" "$@"
  echo >&2 " "
  sed >&2 -n '/^###/q; s/^#$/# /; s/^ *$/# /; 3,$s/^# //p;' "$FULLPATH"
  if [ $err_exit -eq 0 ]; then
    exit
  else
    exit 1
  fi
}

# Create time stamp to be used in file names etc
stamp=`date "+%j%H%M%S"$$`

clim=0
save=0

# Process command line arguments
declare -a cl_args file_prefix
for arg in "$@"; do
  case $arg in
       -*) # An option
           opt=${arg#-}
           case $opt in
             h) usage ;;
             c) clim=1 ;;
             s) save=1 ;;
             x) set -x ;;
             *) echo "Unknown option -$opt"; exit 1 ;;
           esac
           ;;
      *=*) # add this variable definition to the current environment
           var=$(echo $arg|awk -F\= '{printf "%s",$1}' -)
           val=$(echo "$arg"|awk '{i=index($0,"=")+1;printf "%s",substr($0,i)}' -)
           [ -n "$var" ] && eval ${var}=\"\$val\"  # preserve quoted assignments
           val=$(echo $val|sed 's/^ *//; s/ *$//')  # remove leading and trailing space
           [ -z "$val" ] && { echo "Invalid command line arg --> $arg <-- Empty value."; exit 1; }
           ;;
        *) # Push anything else onto cl_args
           cl_args[${#cl_args[*]}]=$arg
           ;;
  esac
done

# All remaining command line args should be file names
[[ ${#cl_args[*]} > 0 ]] && file_prefix=("${cl_args[@]}")

[[ ${#file_prefix[*]} == 0 ]] && usage -e "No file name was provided on the command line"

# Create a meta data file for use with nccrip
meta_data=tmp_meta_data_$$

if [ $clim -eq 1 ]; then
  cat > $meta_data << end_meta_data
  variable: time
    char units: days since 1-1-1
  variable: SICN = SICN_atm
    char long_name: Sea-ice area fraction
    char units: 1
    char description: Area fraction of grid cell covered by sea ice
  variable:  SIC = SIC_atm
    char long_name: Sea-ice mass per area
    char units: kg m-2
    char description: Total mass of sea ice divided by grid-cell area
  variable:   GT = GT_atm
    char long_name: Surface Temperature
    char units: K
    char description: Temperature of the lower boundary of the atmosphere
end_meta_data
else
  cat > $meta_data << end_meta_data
  variable: SICN = SICN_atm
    char long_name: Sea-ice area fraction
    char units: 1
    char description: Area fraction of grid cell covered by sea ice
  variable:  SIC = SIC_atm
    char long_name: Sea-ice mass per area
    char units: kg m-2
    char description: Total mass of sea ice divided by grid-cell area
  variable:   GT = GT_atm
    char long_name: Surface Temperature
    char units: K
    char description: Temperature of the lower boundary of the atmosphere
end_meta_data
fi

# Define the time origin
time_units="days since 1850-1-1"

# Create a temporary file name for use below
tmp_file=tmp_file_$$

NCCRIP="nccrip -var_as_double -list_output"

# Process each entry in file_prefix in the order provided on the command line
for pfx in "${file_prefix[@]}"; do
  # SST
  if [ $clim -eq 1 ]; then
    file_name=${pfx}
    out_name_gt=${file_name}_gt.nc
    ib2_fmt=RAW
    time_min="start=2"
  else
    file_name=${pfx}_gt
    out_name_gt=${file_name}.nc
    ib2_fmt=YYYYMMDDHH
    time_min=""
  fi
  if [ ! -s $file_name -a ! -L $file_name ]; then
    access $file_name $file_name || { echo "$file_name is missing"; exit 1; }
    accessed=1
  else
    accessed=0
  fi
  # Calculate a SHA1 hash of the source data and add it as global meta data
  echo "char source_file: $(sha1sum $file_name)"|cat - $meta_data > $tmp_file
  nccrip_out=$($NCCRIP $time_min time_units="$time_units" ib2_fmt=$ib2_fmt meta_data=$tmp_file var=GT $file_name 2>&1) ||
    { echo "$nccrip_out"; exit 1; }
  [ $accessed -eq 1 ] && release $file_name
  gt_name=$(head -1 nccrip_file_list_netcdf)
  mv $gt_name $out_name_gt
  if [ $clim -eq 0 ]; then
    echo "Created $out_name_gt"
    [ $save -eq 1 ] && save $out_name_gt $out_name_gt
  fi

  # Sea ice fraction
  if [ $clim -eq 1 ]; then
    file_name=${pfx}
    out_name_sicn=${file_name}_sicn.nc
    ib2_fmt=RAW
    time_min="start=2"
  else
    file_name=${pfx}_sicn
    out_name_sicn=${file_name}.nc
    ib2_fmt=YYYYMMDDHH
    time_min=""
  fi
  if [ ! -s $file_name -a ! -L $file_name ]; then
    access $file_name $file_name || { echo "$file_name is missing"; exit 1; }
    accessed=1
  else
    accessed=0
  fi
  # Calculate a SHA1 hash of the source data and add it as global meta data
  echo "char source_file: $(sha1sum $file_name)"|cat - $meta_data > $tmp_file
  nccrip_out=$($NCCRIP $time_min time_units="$time_units" ib2_fmt=$ib2_fmt meta_data=$tmp_file var=SICN $file_name 2>&1) ||
    { echo "$nccrip_out"; exit 1; }
  [ $accessed -eq 1 ] && release $file_name
  sicn_name=$(head -1 nccrip_file_list_netcdf)
  mv $sicn_name $out_name_sicn
  if [ $clim -eq 0 ]; then
    echo "Created $out_name_sicn"
    [ $save -eq 1 ] && save $out_name_sicn $out_name_sicn
  fi

  # Sea ice thickness (IWE)
  if [ $clim -eq 1 ]; then
    file_name=${pfx}
    out_name_sic=${file_name}_sic.nc
    ib2_fmt=RAW
    time_min="start=2"
  else
    file_name=${pfx}_sic
    out_name_sic=${file_name}.nc
    ib2_fmt=YYYYMMDDHH
    time_min=""
  fi
  if [ ! -s $file_name -a ! -L $file_name ]; then
    access $file_name $file_name || { echo "$file_name is missing"; exit 1; }
    accessed=1
  else
    accessed=0
  fi
  # Calculate a SHA1 hash of the source data and add it as global meta data
  echo "char source_file: $(sha1sum $file_name)"|cat - $meta_data > $tmp_file
  nccrip_out=$($NCCRIP $time_min time_units="$time_units" ib2_fmt=$ib2_fmt meta_data=$tmp_file var=SIC $file_name 2>&1) ||
    { echo "$nccrip_out"; exit 1; }
  [ $accessed -eq 1 ] && release $file_name
  sic_name=$(head -1 nccrip_file_list_netcdf)
  mv $sic_name $out_name_sic
  if [ $clim -eq 0 ]; then
    echo "Created $out_name_sic"
    [ $save -eq 1 ] && save $out_name_sic $out_name_sic
  fi

  if [ $clim -eq 1 ]; then
    # For climatology data concatenate the 3 files just created
    out_name_clim=${pfx}.nc
    mv $out_name_gt $out_name_clim
    ncks -A -h $out_name_sicn $out_name_clim
    ncks -A -h $out_name_sic  $out_name_clim
    rm -f $out_name_sicn $out_name_sic
    echo "Created $out_name_clim"
    [ $save -eq 1 ] && save $out_name_clim $out_name_clim
  fi

done

rm -f nccrip_file_list_netcdf $meta_data $tmp_file
