#! /bin/bash
#
# François Massonnet
# April 2016
# Conversion of NetCDF to "UF" format (that the ensemble
# Kalman filter can read and treat).
#
# This uses a Fortran executable called prep_obs_${resolution}
# where resolution is either ORCA1 or ORCA25
#
# DON'T FORGET TO SOURCE THE RIGHT modules (source ../enkf_modules_to_load.txt)
#
# NOTE THAT TWO INSTANCES OF THIS SCRIPT CAN'T RUN AT THE SAME TIME
# (a temporary observation.uf file is produced)

set -o nounset
set -o errexit

echo ""
echo "?DID YOU SOURCE THE RIGHT MODULES FIRST?"
echo "source ../enkf_modules_to_load.txt"
echo ""
sleep 2


yearb=2000
yeare=2017

resolution=ORCA1   # ORCA1 or ORCA25
product=osisaf     # osisaf or esa
rootdir=/gpfs/projects/bsc32/repository/enkf/${product}_sic/netcdf/

for year in `seq $yearb $yeare`
do
  for file in `ls ${rootdir}/at_i_OSISAF-${resolution}_${year}????.nc`
  do
    tag=`basename $file | sed -e "s/at_i_OSISAF-${resolution}_//" -e "s/.nc//"`
    echo $tag
    ./prep_obs_${resolution} $file sic $tag
    mv observations.uf  ${rootdir}/../uf/observations-${resolution}_${tag}.uf
    mv observations.txt ${rootdir}/../txt/observations-${resolution}_${tag}.txt
  done
done

