nccf_atm_monthly.sh 55.6 KB
Newer Older
  ncwa -O -h -a record tmp_cat2.nc tmp_cat2.nc
  mv tmp_cat2.nc tmp_cat.nc #new
 for var_check in $VAR_LST_3D_GG
  do
  if [ $VAR_OLD == $var_check ];then
   ncks -O -h -a -v lon,lat,$VAR_LOC tmp_cat.nc tmp_cat.nc
#   ncks -O -h -a -v lon,lat,$VAR_LOC tmp_cat2.nc tmp_cat3.nc
#   mv tmp_cat3.nc tmp_cat.nc
#  else
#   mv tmp_cat2.nc tmp_cat.nc
  fi
 done
 if [ ! -z $OLD_FILE ] ; then
  ncks -C -O -v longitude,latitude,$VAR_LOC $OLD_FILE tmp_0_${OLD_FILE}
  ncecat -O -h tmp_0_${OLD_FILE} tmp_${OLD_FILE}
  ncpdq -O -h -a ensemble,record tmp_${OLD_FILE} tmp_${OLD_FILE}
  ncwa -O -h -a record tmp_${OLD_FILE} tmp_${OLD_FILE}
  ncrcat tmp_cat.nc tmp_${OLD_FILE} $2
 else
  mv tmp_cat.nc $2
 fi
}

function save_final_output(){
# save final post-processed output (file in *.nc format)
 varnew=$1
 output=$2
   
 TARDIR=${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly
 [ ! -d $TARDIR ] && mkdir -p $TARDIR
# [[ ! -z $( find ${SAVEDIR}/${EXPID}/monthly_mean/. -type d ) ]] && echo ${SAVEDIR} && find ${SAVEDIR}/${EXPID}/monthly_mean/. -type d | xargs chmod 775 2>/dev/null
 echo llll
# [[ ! -z $( find ${SAVEDIR}/${EXPID}/monthly_mean/. -type f ) ]] && find ${SAVEDIR}/${EXPID}/monthly_mean/. -type f | xargs chmod 664 2>/dev/null

# I have commented the lines below which do not make any sense !!!! Virginie
# Why concatenating the new file  with the already existing file ?
# The only valid reason to concatenate would be if the old file spans a given 
# range of forecast times and the new file spans a subsequent range of forecast
# times but then they would not have the same name !!!

#  if [ -e ${TARDIR}/${output} ] ; then
#   mv ${output} new_${output}
#   ncpdq -O -h -a ensemble,time new_${output} new_${output} # shape the dimensions
#   mv ${TARDIR}/${output} old_${output}
#   ncpdq -O -h -a ensemble,time old_${output} old_${output} # shape the dimensions
#   ncrcat -O -h old_${output} new_${output} ${output}
#   ncpdq -O -h -a time,ensemble ${output} ${output}         # again reshape the dimensions as per requirement of final output
#   rm -f old_${output} new_${output}
#  fi
 chmod 770 ${output}
#for tos, change value on land from 0 to NaN
if [ $varnew == "tos" ]; then
  cp ${MASK_PATH} mask1.nc
  lstdims=`ncdump -h mask1.nc | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'` 
  if [[ ${lstdims/longitude} == ${lstdims} ]] && [[ ${lstdims/latitude} == ${lstdims} ]] ; then
    ncrename -h -v lon,longitude -v lat,latitude mask1.nc
    ncrename -h -d lon,longitude -d lat,latitude mask1.nc
  fi
for i in $( seq 1 4);
do
cp mask1.nc mask1.nc.$i
done
ncrcat mask1.nc.* mask.nc
cdo -gtc,0.1 mask.nc mask2.nc
mv mask2.nc mask.nc

lstdims=`ncdump -h $output | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'`
if [[ ${lstdims/leadtime} != ${lstdims} ]] ; then
  ncrename -v leadtime,time $output
fi
lstdims=`ncdump -h mask.nc | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'`
if [[ ${lstdims/surface} != ${lstdims} ]] ; then
  ncwa -a surface mask.nc
fi
cp mask.nc $output
ncap2 -O -s "tos=tos/(1-LSM)" $output ${output}.tmp
ncks -h -x -v LSM ${output}.tmp ${output}.tmp2
ncdump ${output}.tmp2 > tmp.txt
sed -i s/Infinityf/NaN/g tmp.txt
Virginie Guemas's avatar
Virginie Guemas committed
sed -i s/-NaN/NaN/g tmp.txt
ncgen -b tmp.txt -o  ${output}.tmp2
#  echo "where( tos = \"Infinityf\" ) tos[time,latitude,longitude]=\"NaN\"; " > mynco.nco
#  ncks -h -A mask.nc $output
#  echo "where( LSM != 0 ) tos[time,latitude,longitude]=\"NaN\"; " > mynco.nco
#  ncap2 -O -S mynco.nco ${output}.tmp2 ${output}.tmp3
#  ncks -h -x -v LSM ${output}.tmp ${output}.tmp2
  mv $output.tmp2 ${TARDIR}/${output}
  rm mask1.nc ${output}.tmp
fi
mv ${output} ${TARDIR}/${output}
#to make a smooth transition between the 2 versions of nccf_atm_monthly, as R functions look for atmospherical monthly means called $var_yyyymmdd.nc (without the members), a link between the 2 naming conventions is created so that R functions still work while they have not been updated.
 ln -sf ${TARDIR}/${output} ${TARDIR}/${varnew}_${SDATE}.nc
}
   
# Select variables and levels   
function combine_3d(){
for MEM in ${MEM_LST[@]}; do
#   FILES=$(ls MMA*SH*.nc.$MEM)
   FILES="$(find . -maxdepth 1 -name  "MMA*SH*.nc.$MEM" | sed -e 's/\.\///g' )"
   echo $FILES
   for f in ${FILES}; do   
    for var in ${VAR_LST_3D_SH[@]}; do
        new_name $var
        TMP_OUT=tmp_${varnew}_$f
        cdo selname,${var} -sellevel,${LEVEL_LST} ${f} ${TMP_OUT}
    done
#    FILES=$(ls MMA*GG*.nc.$MEM)
    FILES=$(find . -maxdepth 1 -name  "MMA*GG*.nc.$MEM" | sed -e 's/\.\///g' )
    for f in ${FILES}; do
   for var in ${VAR_LST_3D_GG[@]}; do
        new_name $var
        TMP_OUT=tmp_${varnew}_$f
     case $var in #pab
       Q)  
        cdo selname,${var} -sellevel,${LEVEL_LST} ${f} ${TMP_OUT} #
       ;;
       *)
        cdo selname,${var} ${f} ${TMP_OUT} #pab
        ;;
      esac
    done
    done

# combine all time step in one file
     for var in ${VAR_LST_3D[@]}; do
	  new_name $var
#      FILES=$(ls tmp_${varnew}_*)
      FILES="$(find . -maxdepth 1 -name "tmp_${varnew}_*" | sed -e 's/\.\///g' )"
      output=${varnew}_$SDATE.$MEM.nc
      cdo -O mergetime ${FILES} ${output} # combine all the time steps in one file
      ncrename -h -v ${var},${varnew} ${output}
    done #loop for variables 
done # loop for members 
}
######end of combine3d ##########


# interpolate from SH to regular grid
function regrid2x2(){
 for var in ${VAR_LST_3D_SH[@]}; do
    new_name $var
   FILES=$( find . -maxdepth 1 -name "${varnew}_$SDATE.*.nc" | sed -e 's/\.\///g' )
   for f in ${FILES}; do   
      cdo -r sp2gpl -selname,${varnew} ${f} rg_${f}; rm -f ${f}
   done
 done
 for var in ${VAR_LST_3D_GG[@]}; do
   new_name $var
#   FILES="$(ls ${varnew}_$SDATE.*.nc)"
   FILES=$(find . -maxdepth 1 -name "${varnew}_$SDATE.*.nc" | sed -e 's/\.\///g' )
   for f in ${FILES}; do
 
      cdo selname,${varnew} ${f} rg_${f}; rm -f ${f}
   done
 done
}


function upper(){
   for var in ${VAR_LST_3D[@]}; do
    new_name $var

    echo ${LISTMEMB[0]} -- ${#LISTMEMB[@]} -- ${LISTMEMB[${#LISTMEMB[@]}-1]} 
    LSMBSH="${LISTMEMB[0]}-${LISTMEMB[${#LISTMEMB[@]}-1]}"
#    PREVIOUS_FILE="$(ls ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly/${varnew}_${SDATE}_*.nc | tail -1)"
#    PREVIOUS_FILE="$( find . -maxdepth 1 -name  "${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly/${varnew}_${SDATE}_*.nc" | tail -1 | sed -e 's/\.\///g' )"
#savedir 
   PREVIOUS_FILE="$( find  ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly -maxdepth -name  "${varnew}_${SDATE}_*.nc" | tail -1 | sed -e 's/\.\///g' )"
     cd ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly/; 
     FILE_NAME_PREVIOUS="$( find . -maxdepth 1 -name "${varnew}_${SDATE}_*.nc" | tail -1 | sed -e 's/\.\///g' )"; cd -
     IDX_1ST=$(echo ${varnew}_${SDATE}_ | wc -m )
     FIRST_MEMBER_PREVIOUS=$(echo $FILE_NAME_PREVIOUS | cut -b$IDX_1ST )
     IDX_LST=$( expr $IDX_1ST + 2 )
     LAST_MEMBER_PREVIOUS=$(echo $FILE_NAME_PREVIOUS  | cut -b$IDX_LST )
#security check:
     if [ $LAST_MEMBER_PREVIOUS -le $(expr ${LISTMEMB[0]} - 1 ) ] ; then
        cp $PREVIOUS_FILE rg_$FILE_NAME_PREVIOUS
       if [ $LAST_MEMBER_PREVIOUS -eq $(expr ${LISTMEMB[0]} - 1 ) ] ; then
        LSMBSH=${FIRST_MEMBER_PREVIOUS}-${LISTMEMB[${#LISTMEMB[@]}-1]}
       else
        echo "Actual list of members does not follow directly the ones already post-processed! Check you did not forget any members at the beginning of your list"
        MISSING_FIRST=$(expr ${LAST_MEMBER_PREVIOUS} + 1 )
        MISSING_LAST=$(expr ${LISTMEMB[0]} - 1 )
        LSMBSH=${FIRST_MEMBER_PREVIOUS}_${LISTMEMB[${#LISTMEMB[@]}-1]}-${MISSING_FIRST}_${MISSING_LAST}
       fi
     else
      echo "Some members are going to be treated twice! Revise the consistency between your member list and the previously processed files"
     fi
    fi
    YRSO=${YEAR0}$(printf "%02d" $(( 10#$MON0)))_${YEARF}$(printf "%02d" $((10#$MONF))).nc
    [[ 10#${MONF} -le 9 ]] && [[ $( echo $MONF | wc -c) -eq 2 ]] && YRSO=${YEAR0}${MON0}_${YEARF}$(printf "%02d" ${MONF}).nc
    gather_memb rg_${varnew}_$SDATE ${varnew}_${SDATE}_fc${LSMBSH}_${YRSO}.nc $varnew $VAR
    output=${varnew}_${SDATE}_fc${LSMBSH}_${YRSO}.nc
#    gather_memb rg_${varnew}_$SDATE ${varnew}_${SDATE}_${LSMBSH}.nc $varnew $VAR
#    output=${varnew}_${SDATE}_${LSMBSH}.nc
   for CHECK_VAR in $VAR_LST_3D_SH
    do
    if [ $CHECK_VAR == $var ];then
    	ncrename -h -d lon,longitude -d lat,latitude -d lev,level ${output}
    	ncrename -h -v lon,longitude -v lat,latitude -v lev,level ${output}
    fi
   done
   for CHECK_VAR in $VAR_LST_3D_GG
    do
    if [ $CHECK_VAR == $var ];then
	  ncrename -h -d lon,longitude -d lat,latitude ${output}
      [[ ! -z $( ncdump -h ${output} | grep "mlev =" ) ]] && ncrename -h -d mlev,level -v mlev,level ${output}
      [[ ! -z $( ncdump -h ${output} | grep "lev =" ) ]] && ncrename -h -d lev,level -v lev,level ${output}
      if [[ ! -z $( ncdump -h ${output} | grep "depth =" ) ]]; then
        ncrename -h -d depth,level ${output}
        ncrename -h -v depth,level ${output}
      fi
      ncrename -h -v lon,longitude -v lat,latitude ${output}
    fi
   done
#
# Get the CFU standard attributes to be written in the variable
#
        variables="$(cat ${HEAD_DIR}/table_of_variable | cut -f$idx -d'|' | sed -e 's/ /@/g')"
        cfustandard_name="$(echo $variables | cut -f2 -d' ' | sed -e 's/@/ /g')"  # variable standard name
        cfulong_name="$(echo $variables     | cut -f3 -d' ' | sed -e 's/@/ /g')"  # variable long name
        cfucell_methods="$(echo $variables  | cut -f4 -d' ' | sed -e 's/@/ /g')"  # variable cell methods
        cfuunit="$(echo $variables          | cut -f5 -d' ' | sed -e 's/@/ /g')"  # variable unit
        cfuunit_long="$(echo $variables     | cut -f6 -d' ' | sed -e 's/@/ /g')"  # variable unit long name
        cfulevel_number="$(echo $variables  | cut -f7 -d' ' | sed -e 's/@/ /g')"  # variable level
        cfulevel_type="$(echo $variables    | cut -f8 -d' ' | sed -e 's/@/ /g')"  # variable level type
        cfulevel_units="$(echo $variables   | cut -f9 -d' ' | sed -e 's/@/ /g')"  # variable level unit
#
# modify variable attributes
#
        for att in units valid_range actual_range code table GRID_TYPE truncation; do
          ncatted -O -h -a ${att},${varnew},d,, ${output}
        done

        ncatted -O -h -a _FillValue,${varnew},a,f,1.e+12 ${output}
        ncatted -O -h -a standard_name,${varnew},o,c,"$cfustandard_name" ${output} # variable standard name
        ncatted -O -h -a long_name,${varnew},o,c,"$cfulong_name" ${output}         # variable long name
        ncatted -O -h -a cell_methods,${varnew},o,c,"$cfucell_methods" ${output}   # variable cell methods
        ncatted -O -h -a unit_long,${varnew},o,c,"$cfuunit_long" ${output}         # variable long unit name
        ncatted -O -h -a units,${varnew},o,c,"$cfuunit" ${output}                  # variable units
        lstdims=`ncdump -h ${output} | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'` 
        if [[ ${lstdims/lev_2} != ${lstdims} ]] ; then
          ncrename -d lev_2,level -v lev_2,level $output
        fi
        if [[ ${lstdims/depth_2} != ${lstdims} ]] ; then
          ncrename -d depth_2,level -v depth_2,level $output
        fi
        if [[ ${lstdims/depth_3} != ${lstdims} ]] ; then
          ncrename -d depth_3,level -v depth_3,level $output
        fi
        if [[ ${lstdims/depth_4} != ${lstdims} ]] ; then
          ncrename -d depth_4,level -v depth_4,level $output
        fi
        lstdims=`ncdump -h ${output} | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'` 
        if [[ ${lstdims/depth} != ${lstdims} ]] ; then
          ncrename -d depth,level -v depth,level $output
        fi
        lstdims=`ncdump -h ${output} | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'` 
        if [[ ${lstdims/level} == ${lstdims} ]] ; then
          ncrename -d lev,level -v lev,level $output
        fi
        ncatted -O -h -a data_type,level,o,c,"$cfulevel_type" ${output}       # variable level type
        ncatted -O -h -a units,level,o,c,"$cfulevel_units" ${output}          # variable level units
        ncatted -O -h -a coordinates,${varnew},o,c,"longitude latitude leadtime reftime time_bnd experiment_id source realization institution level" ${output}  
#
# If the NetCDF file had a horizontal axis name different from longitude
#
        [[ ! -z $( ncdump -h $output | grep record ) ]] && ncrename  -h -d record,ensemble ${output}
#
# modify logitude attributes  
#
        lon_min=0
        lon_max=359.25
        lat_min=-89.4270841760375
        lat_max=89.4270841760375  # These valuse shoud be obtaind from the file instead of hardcoded

        ncatted -O -h -a axis,longitude,o,c,"X" ${output}     # variable longitude axis
        ncatted -O -h -a topology,longitude,c,c,"circular" ${output}     # variable longitude axis
        ncatted -O -h -a modulo,longitude,c,f,"360" ${output}     # variable longitude axis
        ncatted -O -h -a valid_min,longitude,c,f,"$lon_min" ${output}     # variable longitude valid_min
        ncatted -O -h -a valid_max,longitude,c,f,"$lon_max" ${output}     # variable longitude valid_max
# modify latitude attributes 
        ncatted -O -h -a axis,latitude,o,c,"Y" ${output}      # variable latitude axis
        ncatted -O -h -a valid_min,latitude,c,f,"$lat_min" ${output}     # variable latitude valid_min
        ncatted -O -h -a valid_max,latitude,c,f,"$lat_max" ${output}     # variable latitude valin_max
# modify level attributes 
        ncatted -O -h -a standard_name,level,o,c,"air_pressure" ${output} # standard name
        ncatted -O -h -a long_name,level,o,c,"air pressure" ${output}         # long name
        ncatted -O -h -a data_type,level,o,c,"float" ${output}       # data type
        ncatted -O -h -a units,level,o,c,"hPa" ${output}                  #  units
        ncatted -O -h -a axis,level,o,c,"Z" ${output}          # axis
        ncatted -O -h -a positive,level,c,c,"up" ${output}
###
# modify the level values, should be hPa instead of Pa
	    ncap2 -O -h -s "level()=level()/100" ${output} ${output} 

        lstdims=`ncdump -h ${output} | awk /dimensions:/,/variables:/ | grep -v dimensions: | grep -v variables: | awk '{print $1}'` 
        if [[ ${lstdims/ensemble} == ${lstdims} ]] ; then
          ncecat -h ${output} tmp_${output}
          rm -f $output
          mv tmp_${output} ${output} 
          ncrename -h -O -d record,ensemble $output $output
        fi
        ncpdq -O -h -a time,ensemble ${output} ${output} # reshape the dimension and make time unlimited
      #  ncks  -h -A $HEAD_DIR/${SDATE}.nc ${output}
        ncks  -h -A header.nc ${output}
        ncrename -O -h -v time,kaka $output # delete time variable 
        ncks -O -h -x -v kaka $output $output # delete time variable
        ncatted -h -a history,global,d,, $output  #delete history
        ncatted -h -a institution,global,m,c,"IC3" $output ## change institution name in global attributes

# create a script to change the EXPID, insitutution, ensember, source and realiazation 
	i=0 # index
	for mem in ${LISTMEMB[@]}; do
cat>modify_ncvalue<<EOF
ncap2 -O -h -s 'experiment_id($i,0:3)="$EXPID";realization($i)=$mem;institution($i,0:$(( ${#INSTITUTION} -1  )))="$INSTITUTION";source($i,0:$(( ${#SOURCE} -1  )))="$SOURCE"' \$1 \$1
                cat modify_ncvalue
	        bash modify_ncvalue $output; rm modify_ncvalue
		i=$((i+1))
	done
##
#        rm -f ${files}*
        save_final_output $varnew $output
 done # loop over variables 
}



# for maximum and minimum temperature (manipulate daily data files)
function maxmin(){
for VAR in ${VAR_LST_MXMN[@]}; do
 new_name $VAR
 output=${VAR}_${SDATE}.nc
 cp $DATADIR/${EXPID}/daily/$VAR/${VAR}_${SDATE}_*.nc.gz .
 gunzip ${VAR}_${SDATE}_*.nc.gz 
 cdo timmean ${VAR}_${SDATE}_${SDATE1}.nc ${output} ; rm -f ${VAR}_${SDATE}_${SDATE1}.nc
# files=$(ls ${VAR}_${SDATE}_*.nc) 
 files="$(find . -maxdepth 1 -name "${VAR}_${SDATE}_*.nc" | sed -e 's/\.\///g' )"  
 for f in $files;do
  cdo timmean $f toto.nc;rm -f $f
  ncrcat -O -h ${output} toto.nc ${output};rm -f toto.nc
 done # loop for files
 ncrename -h -d lev,ensemble ${output}
 ncrename -O -h -v lev,kaka ${output} #delete lev var
 ncks -O -h -x -v kaka ${output} ${output} #delete lev var
 ncks -h -A header.nc ${output}
#
# Get the CFU standard attributes to be written in the variable
#
variables="$(cat ${HEAD_DIR}/table_of_variable | cut -f$idx -d'|' | sed -e 's/ /@/g')"
cfustandard_name="$(echo $variables | cut -f2 -d' ' | sed -e 's/@/ /g')"  # variable standard name
cfulong_name="$(echo $variables     | cut -f3 -d' ' | sed -e 's/@/ /g')"  # variable long name
cfucell_methods="$(echo $variables  | cut -f4 -d' ' | sed -e 's/@/ /g')"  # variable cell methods
cfuunit="$(echo $variables          | cut -f5 -d' ' | sed -e 's/@/ /g')"  # variable unit
cfuunit_long="$(echo $variables     | cut -f6 -d' ' | sed -e 's/@/ /g')"  # variable unit long name
cfulevel_number="$(echo $variables  | cut -f7 -d' ' | sed -e 's/@/ /g')"  # variable level
cfulevel_type="$(echo $variables    | cut -f8 -d' ' | sed -e 's/@/ /g')"  # variable level type
cfulevel_units="$(echo $variables   | cut -f9 -d' ' | sed -e 's/@/ /g')"  # variable level unit
#
# Adding the variable level
#
ncap2 -s level_${varnew}="$cfulevel_number" ${output} -h -O ${output}
#
# Removing unnecessary attributes
#
for att in GRID_TYPE ; do 
 ncatted -O -h -a ${att},${varnew},d,c, ${output}      
done
#
# Adding and modifying the {varnew}iable attributes
#
ncatted -O -h -a standard_name,${varnew},o,c,"$cfustandard_name" ${output} # {varnew}iable standard name
ncatted -O -h -a long_name,${varnew},o,c,"$cfulong_name" ${output}         # {varnew}iable long name
ncatted -O -h -a cell_methods,${varnew},o,c,"$cfucell_methods" ${output}   # {varnew}iable cell methods
ncatted -O -h -a data_type,level_${varnew},o,c,"$cfulevel_type" ${output}  # {varnew}iable level type
ncatted -O -h -a units,level_${varnew},o,c,"$cfulevel_units" ${output}     # {varnew}iable level units
ncatted -O -h -a coordinates,${varnew},o,c,"longitude latitude reftime leadtime time_bnd experiment_id source realization institution level_${varnew}" ${output}
#
# Adding variable axis
#
ncatted -O -h -a axis,longitude,o,c,"X" ${output}       # variable longitude axis
ncatted -O -h -a axis,latitude,o,c,"Y" ${output}        # variable latitude axis
ncatted -O -h -a axis,level_${varnew},o,c,"Z" ${output} # variable level axis
# delete history
ncatted -h -a history,global,d,, $output
# change institution name
ncatted -h -a institution,global,m,c,"IC3" $output 

# create a script to change the EXPID, insitutution, ensember, source and realiazation 
	i=0 # index
	for mem in ${LISTMEMB[@]}; do
cat>modify_ncvalue<<EOF
ncap2 -O -h -s 'experiment_id($i,0:3)="$EXPID";realization($i)=$MEM;institution($i,0:$(( ${#INSTITUTION} -1  )))="$INSTITUTION";source($i,0:$(( ${#SOURCE} -1  )))="$SOURCE"' \$1 \$1
EOF

		cat modify_ncvalue
		bash modify_ncvalue $output; rm modify_ncvalue
		i=$((i+1))
	done
##

    save_final_output $varnew $output
done # loop for variables 
}
####  End of the User Defined Functions  #### 




###################################
####  Main Part of the Script  ####
###################################

# NCO and CDO must be available in $PATH

date

config_file=$1
check_args $@ 
#read config_file and initialize variables
#check config file syntaxis problems
sed -i -e 's/= /=/g' ${config_file}
. ${config_file}

FACTOR=$((NFRP*3600))
ENSEMBLE=${#MEM_LST[@]}
if [[ $ENSEMBLE -eq 0 ]]; then
  MEM_LST=( $( ls ${DATADIR}/${EXPID}/${SDATE} ) ) 
  ENSEMBLE=${#MEM_LST[@]}
  echo "Warning: you passed an empty member list. By default, it will treat all the available members: " ${MEM_LST[@]}
fi
LISTMEMB=( $(echo ${MEM_LST[@]} | sed -e 's/fc//g') ) #member list without "fc"
echo ${MEM_LST[0]}
#LISTMEMB=( "$(echo ${MEM_LST[@]} | sed -e 's/fc//g')" ) #member list without "fc"
echo $LISTMEMB
echo  ${LISTMEMB[0]}
echo ${LISTMEMB[${#LISTMEMB[@]}-1]}
echo ${#LISTMEMB[@]}

#rm -rf ${WORKDIR}
mkdir -p ${WORKDIR}
if [[ ! -d ${WORKDIR} ]]; then
 exit 1
fi

cd ${WORKDIR}

base_path=${DATADIR}/${EXPID}/${SDATE}
mkdir -p ${SAVEDIR}/${EXPID}; # chmod -R 775 $SAVEDIR/${EXPID} 

get_leadtime
header $SDATE
extract
if [ -z ${VAR_LST_2D[@]}  ]; then
# GG=$(ls *GG* | head -1)
 GG="$(find . -maxdepth 1 -name "*GG*" | head -1 | sed -e 's/\.\///g' )"
 read_vars $GG GG
# SH=$(ls *SH* | head -1)
 SH="$(find . -maxdepth 1 -name "*SH*" | head -1 | sed -e 's/\.\///g' )"
 read_vars $SH SH
rm -f /scratch/Earth/${USER}/pp/VAR_LST_2D.txt 
VAR_LST_2D=$(echo ${VAR_LST_2D_GG} ${VAR_LST_2D_SH} 'PRECIP')
echo $VAR_LST_2D >> /scratch/Earth/${USER}/pp/VAR_LST_2D.txt
VAR_LST_3D=$(echo ${VAR_LST_3D_GG} ${VAR_LST_3D_SH})
rm -f /scratch/Earth/${USER}/pp/VAR_LST_3D.txt 
else
 VAR_LST_3D_SH_DEFAULT='T U V W Z'
 VAR_LST_3D_GG_DEFAULT='Q CC CIWC CLWC'
 VAR_LST_2D_GG_DEFAULT='T2M D2M SSTK MSL PRECIP SSR STR TTR TSR TSRC TTRC SLHF SSHF U10M V10M SSRD CP SF E SSRU SSRC STRU STRD TCC'
#if list of variables read from config_file, separation between GG and SH variables
 for var in ${VAR_LST_3D[@]}
 do
  for var_sh in ${VAR_LST_3D_SH_DEFAULT[@]} 
  do
   [ $var == $var_sh ] && VAR_LST_3D_SH=$(echo $VAR_LST_3D_SH $var)
  done
  for var_gg in ${VAR_LST_3D_GG_DEFAULT[@]}
  do
   [ $var == $var_gg ] && VAR_LST_3D_GG=$(echo $VAR_LST_3D_GG $var)
  done
 done  
 for var in ${VAR_LST_2D[@]}
 do
  for var_gg in ${VAR_LST_2D_GG_DEFAULT[@]}
  do
   [ $var == $var_gg ] && VAR_LST_2D_GG=$(echo $VAR_LST_2D_GG $var)
echo $YEARF - $YEAR0 - $MONF - 10#$MONF - $MON0 

echo "before surface"
combine_3d
regrid2x2
upper


if [[ ${#VAR_LST_MXMN[@]} > 0 ]]; then
 maxmin
#else 
# echo "" #empty line to avoid trap issue
fi

cd; rm -rf $WORKDIR # clean up

date
####  End of the Main Part of Script  ####