#!/bin/bash

var=psl   #sfcWind               # name of the variable to check
path=/esnas/exp/ecmwf/system4_m1/daily_mean/${var}_f6h/   # its path
suffix=01                        # this suffix is usually present in all S4 netCDF file, right begore the file extension
ext=.nc                          # file extension

yearStart=1981                   # time period to check inside the files
yearEnd=2015

nameLatitude=latitude            # name of the latitude dimension to check in the file
nLatitude=181 #256 #181          # number of latitude values to check

nameLongitude=longitude          # name of the longitude dimension to check in the file
nLongitude=360 #512 #360         # number of longitude values to check

nameMembers=ensemble #lev #number  # name of the dimension with the model members to check in the file
nMembers=15 #51 #15              # number of members to check in the file 

nameLeadtimes=time               # name of the dimension with the forecast time to check in the file
nLeadtimes=216 #214 #216         # number of forecast times to check in the file

# function that detect the size of a dimension variable in a netCDF file:
#
# usage: get_size myNetCDF.nc myDimensionName
#
function get_size {
   ncks -m ${1} | grep -E -i ": ${2}, size =" | cut -d ' ' -f 7 | uniq
}

# check file existence:
for ((i=$yearStart;i<=$yearEnd;i++))
do
for j in 01 02 03 04 05 06 07 08 09 10 11 12
do
  ok1=0; ok2=0; ok3=0; ok4=0

  #echo $path${var}_$i$j$suffix$ext
  if [ ! -f $path${var}_$i$j$suffix$ext ]
    then
    echo ${var}_$i$j$suffix$ext ": >>>>> Missing file <<<<<"
  else
      nMemb=`get_size $path${var}_$i$j$suffix$ext $nameMembers`
      nLead=`get_size $path${var}_$i$j$suffix$ext $nameLeadtimes`
      nLat=`get_size $path${var}_$i$j$suffix$ext $nameLatitude`
      nLon=`get_size $path${var}_$i$j$suffix$ext $nameLongitude`

      if [ -z $nLat ]; then # check if nLat is empty
        echo ${var}_$i$j$suffix$ext ": there is no dimension called " $nameLatitude " inside this file "
      elif [ ${nLat//[[:blank:]]/} -ne $nLatitude ]; then
        echo ${var}_$i$j$suffix$ext ": " $nameLatitude " dimension has " $nLat " values instead of " $nLatitude
      else
        ok1=1
      fi

      if [ -z $nLon ]; then # check if nLon is empty
        echo ${var}_$i$j$suffix$ext ": there is no dimension called " $nameLongitude " inside this file "
      elif [ ${nLon//[[:blank:]]/} -ne $nLongitude ]; then
        echo ${var}_$i$j$suffix$ext ": " $nameLongitude "dimension has " $nLon " values instead of " $nLongitude
      else
        ok2=1
      fi

      if [ -z $nMemb ]; then # check if nMemb is empty
        echo ${var}_$i$j$suffix$ext ": there is no dimension called " $nameMembers " inside this file "
      elif [ ${nMemb//[[:blank:]]/} -ne $nMembers ]; then
        echo ${var}_$i$j$suffix$ext ": " $nameMembers " dimension has " $nMemb " values instead of " $nMembers
      else
        ok3=1
      fi

      if [ -z $nLead ]; then
        echo ${var}_$i$j$suffix$ext ": there is no dimension called " $nameLeadtimes " inside this file "
      elif [ ${nLead//[[:blank:]]/} -ne $nLeadtimes ]; then
        echo ${var}_$i$j$suffix$ext ": " $nameLeadtimes " dimension has " $nLead " values instead of " $nLeadtimes
      else
        ok4=1
      fi
      
      if [ $ok1 -eq 1 ] && [ $ok2 -eq 1 ] && [ $ok3 -eq 1 ] && [ $ok4 -eq 1 ]; then
         echo ${var}_$i$j$suffix$ext ": ok"
      fi
  fi
done
done


# # other common changes to netCDF:

# for file in *; ncrename -d .
# # convert a 6-horly file to a daily one:
# cdo daymean <file_6-hourly.nc> <file_daily.nc>

# cdo showdate <file.nc>

# convert a .nc file of ECMWF S4 after downloading:
#file=_grib2netcdf-atls01-95e2cf....nc ; ncrename -v .msl,psl -d .number,ensemble -v .number,realization $file ; date=$(cdo showdate $file | cut -f3 -d" " | sed -e s/-//g) ; mv $file /esnas/exp/ecmwf/system4_m1/6hourly/psl/psl_${date}.nc ; cdo daymean /esnas/exp/ecmwf/system4_m1/6hourly/psl/psl_${date}.nc /esnas/exp/ecmwf/system4_m1/daily_mean/psl_f6h/psl_${date}.nc ; cdo monmean /esnas/exp/ecmwf/system4_m1/daily_mean/psl_f6h/psl_${date}.nc /esnas/exp/ecmwf/system4_m1/monthly_mean/psl_f6h/psl_${date}.nc

#file=_grib2netcdf-atls01-95e2cf....nc
#ncrename -v .msl,psl -d .number,ensemble -v .number,realization $file
#date=$(cdo showdate $file | cut -f3 -d" " | sed -e s/-//g)
#mv $file /esnas/exp/ecmwf/system4_m1/6hourly/psl/psl_${date}.nc
#cdo daymean /esnas/exp/ecmwf/system4_m1/6hourly/psl/psl_${date}.nc /esnas/exp/ecmwf/system4_m1/daily_mean/psl_f6h/psl_${date}.nc
#cdo monmean /esnas/exp/ecmwf/system4_m1/daily_mean/psl_f6h/psl_${date}.nc /esnas/exp/ecmwf/system4_m1/monthly_mean/psl_f6h/psl_${date}.nc

# psl_19810101.nc
# psl_19810501.nc
# psl_19830501.nc
# psl_19850501.nc
# psl_19880501.nc
# psl_19900501.nc
# psl_19910601.nc
# psl_19910701.nc
# psl_19910801.nc
# psl_19910901.nc
# psl_19911001.nc
# psl_19911101.nc
# psl_19911201.nc
# psl_19920201.nc
# psl_19920301.nc
# psl_19920401.nc
# psl_19921101.nc
# psl_19930501.nc
# psl_19931101.nc
# psl_19940901.nc
# psl_19941101.nc
# psl_19961001.nc
# psl_19970501.nc
# psl_19971101.nc
# psl_19971201.nc
# psl_19980501.nc
# psl_19990501.nc
# psl_20000501.nc
# psl_20000601.nc
# psl_20011101.nc
# psl_20020501.nc
# psl_20021101.nc
# psl_20050601.nc
# psl_20051001.nc
# psl_20051101.nc


































