Newer
Older
#!/bin/bash
#
# ./nccf_atm_monthly.sh path_to_config_file >& EXPID-startdate.log &
#
#
# This script will extract variabls from EC-Earth monthly atmospheric output
# which is available in MMA files, save each variable in one file and also
# combine members together. It will modify the header informaton of the
# generated files according to CMIP5 standard and the variable names will be
# modified according PCMDI standard variable names.
#
# Written by Hui Du
#
# Institut Català de Ciències del Clima / Climate Forecasting Unit (IC3/CFU)
# Created: February 22, 2010
# Adapted: Pierre-Antoine Bretonnière - IC3 , January 2013
pabretonniere
committed
set -ex #v
pabretonniere
committed
msg='Your experiment crashed! Your workdir \($WORKDIR\) will be kept for one week from now and then deleted'
trap " echo $msg ; rm -rf $WORKDIR | at now + 7 days ; exit" SIGINT SIGHUP SIGTERM SIGSEGV SIGKILL EXIT
pabretonniere
committed
#################################
#### User Defined Funtions ####
#################################
# check if args are ok
function check_args(){
NB_ARGS=$#
if [ $# -ne 1 ] ; then
echo
echo "USAGE: config_file "
echo "For example: ./nccf_atm_monthly.new.sh /home/Earth/$user/es_git/atmospheric_diagnostics/config_file "
echo
exit 1
fi
}
function get_leadtime(){
#gets lead_time and checks if all members have the same number of months
NMONTH=9999999
for DIR in $MEM_LST
do
cd ${DATADIR}/${EXPID}/${SDATE}/${DIR}/outputs # hard coded
pabretonniere
committed
# NFILE=$(ls MMA*|wc -l)
NFILE="$( find . -maxdepth 1 -name "MMA*" |wc -l)"
# for TMPFILE in $(ls MMA*)
for TMPFILE in $( find . -maxdepth 1 -name "MMA*" | sed -e 's/\.\///g' )
pabretonniere
committed
NMONTHS_LOC=$(tar tvf $TMPFILE | grep GG | wc -l)
[ $NMONTH -ne $NMONTHS_LOC ] && [ $NMONTH -ne 9999999 ] && echo "all members or start dates don't have the same number of months, be careful"
[ $NMONTH -ge $NMONTHS_LOC ] && NMONTH=$NMONTHS_LOC
done
cd ${WORKDIR}
done
}
function header(){
SD=$1
rtime 19500101 ${SD}
ncks -h -d ensemble,0,$((ENSEMBLE-1)),1 ${HEAD_DIR}/template.nc toto.nc # select sub member
cp toto.nc toto1.nc
TIME_BND1=0
pabretonniere
committed
TIME_BND2=$(get_hours $SD)
LEADTIME=$(((TIME_BND1+TIME_BND2)/2))
ncap2 -O -h -s "leadtime(0)=${LEADTIME};time_bnd(,0)=${TIME_BND1};time_bnd(,1)=${TIME_BND2}" toto.nc toto.nc
for ((i=1;i<=$((NLT-1));i++)); do
pabretonniere
committed
FORDATE=$(leadtime2date $SD $i)
INTERVAL=$(get_hours $FORDATE)
TIME_BND1=$TIME_BND2
TIME_BND2=$((TIME_BND1+INTERVAL))
LEADTIME=$(((TIME_BND1+TIME_BND2)/2))
echo $fordate $interval $time_bnd1 $time_bnd2
ncap2 -O -h -s "reftime(0)=${REFTIME_VALUE};leadtime(0)=${LEADTIME};time_bnd(,0)=${TIME_BND1};time_bnd(,1)=${TIME_BND2}" toto1.nc toto1.nc
ncrcat -O -h toto.nc toto1.nc toto.nc
done
mv toto.nc header.nc; rm toto1.nc
ncap2 -O -h -s "reftime(0)=reftime(1)" header.nc header.nc
}
# function to get reftime time
function rtime(){
DATE1=$1
DATE2=$2
# FACTOR=day # h for hour, d for days
# echo "NFRP:",$NFRP
# FACTOR=day # h for hour, d for days
pabretonniere
committed
YEAR1=$(echo $DATE1|cut -c1-4)
MONTH1=$(echo $DATE1|cut -c5-6)
DAY1=$(echo $DATE1|cut -c7-8)
YEAR2=$(echo $DATE2|cut -c1-4)
MONTH2=$(echo $DATE2|cut -c5-6)
DAY2=$(echo $DATE2|cut -c7-8)
SEC1=$(date --utc --date "${YEAR1}-${MONTH1}-${DAY1}" +%s)
SEC2=$(date --utc --date "${YEAR2}-${MONTH2}-${DAY2}" +%s)
# case $FACTOR in
# hour)
# FACTOR=3600 # 60*60
# ;;
# day)
# FACTOR=86400 # 60*60*24
# ;;
# esac
REFTIME_VALUE=$(((SEC2-SEC1)/FACTOR))
}
function leadtime2date(){
# function leadtime2date, based on starting date and lead time to calculate date(year & month) of the corresponding leadtime
INIDATE=$1
OFFSET=$2
pabretonniere
committed
YY=$(echo $INIDATE|cut -c1-4)
MM=$(echo $INIDATE|cut -c5-6)
MM=$(echo $MM | sed 's/^0*//;s/^$/0/' )
YY1=$((YY+offset/12))
NMONTH=$((offset%12))
MM1=$((MM+NMONTH))
if [ $MM1 -gt 12 ]; then
YY1=$((YY1+1))
MM1=$((MM1-12))
fi
if [ $MM1 -lt 10 ]; then
MM1="0$MM1"
fi
}
# get the total number of HOURS for a specific MONTH
function get_hours(){
YYMM=$1
pabretonniere
committed
YEAR=$(echo $YYMM|cut -c1-4)
MONTH=$(echo $YYMM|cut -c5-6)
NDAYS=$(cal $MONTH $YEAR |egrep "^[ 0-9][0-9]| [ 0-9][0-9]$" |wc -w)
HOURS=$((NDAYS*24))
echo $HOURS
}
function extract(){
#typeset var jt
#typeset var YEAR0
#typeset var YEARF
#typeset var MON0
#typeset var MONF
for MEM in ${MEM_LST[@]}; do
# untar and unzip MMA SH files and GG files
if [ -z $( echo ${LEAD_LIST[@]} ) ];then
PATH_TO_SEARCH=${DATADIR}/${EXPID}/${SDATE}/$MEM
pabretonniere
committed
FILE_LIST=$(find $PATH_TO_SEARCH -type f -iname "MMA*" 2> /dev/null)
export YEAR0=${LEAD_LIST[0]}
export MON0=${LEAD_LIST[1]}
export YEARF=${LEAD_LIST[2]}
export MONF=${LEAD_LIST[3]}
export CHUNK_SIZE=${LEAD_LIST[4]}
pabretonniere
committed
export YYYY0=$(echo $SDATE | cut -b -4)
export MM0=$(echo $SDATE | cut -b 5-6)
export LTIMEF=$(( ( ($YEARF - $YEAR0) * 12 + 10#$MONF - 10#$MON0+1 )/$CHUNK_SIZE ))
jt=$LTIME0
while [ $jt -le $((LTIMEF)) ]
do
YEAR1=$(( $YEAR0 +(10#$MON0+($jt-1)*$CHUNK_SIZE-1)/ 12 ))
MON1=$(( ( 10#$MON0 + ( $jt - 1 ) * ($CHUNK_SIZE) ) % 12))
MON1=$(echo $(printf "%02d" $(( 10#$MON1))) )
YEAR2=$(( $YEAR1 + ( 10#$MON1 + $CHUNK_SIZE-1 ) / 12 ))
MON2=$(( ( 10#$MON1 + $CHUNK_SIZE-1 ) % 12 ))
MON2=$(echo $(printf "%02d" $(( 10#$MON2))) )
if [ $MON1 -eq 0 ];then
pabretonniere
committed
YEAR1=$(expr $YEAR1 - 1 )
fi
if [ $MON2 -eq 0 ];then
MON2=12
pabretonniere
committed
YEAR2=$(expr $YEAR2 - 1 )
FILE="$( find ${DATADIR}/${EXPID}/${SDATE}/*/outputs/ -maxdepth 1 -name "MMA_${EXPID}_${SDATE}_${MEM}_${YEAR1}"$(printf "%02d" $((10#$MON1)))"01-${YEAR2}"$(printf "%02d" $((10#$MON2)))"*.tar" )"
pabretonniere
committed
if [[ -z ${FILE} ]]; then
echo "Expected file ${DATADIR}/${EXPID}/${SDATE}/${MEM}/outputs/MMA_${EXPID}_${SDATE}_${MEM}_${YEAR1}${MON1}01-${YEAR2}${MON2}*.tar not found, check your Namelist parameters"
pabretonniere
committed
exit
fi
FILE_LIST="$FILE_LIST ${FILE}"
done
fi
echo ${FILE_LIST}
for f in ${FILE_LIST};do
pabretonniere
committed
SH_FILES=$(tar tf ${f}|grep SH)
for FILE in ${SH_FILES};do
tar xvf ${f} ${FILE} ;gunzip -q ${FILE}; mv ${FILE%???} ${FILE%???}.$MEM
done
pabretonniere
committed
GG_FILES=$(tar tf ${f}|grep GG)
for FILE in ${GG_FILES};do
tar xvf ${f} ${FILE} ;gunzip -q ${FILE}; mv ${FILE%???} ${FILE%???}.$MEM
done
done
echo "jt ltimef " $jt $((LTIMEF))
if [ $jt -le $((LTIMEF)) ]; then
echo le
else
echo gt
fi
echo "sortie de extract" $MEM -- ${MEM_LST[@]}
echo "sortie de extract"
}
function read_vars(){
#if no list of variables is provided in the namelist, looks for all variables in the files
OUTPUT_FILE=$1
GRID_TYPE=$2
pabretonniere
committed
VAR_LST_DIM=$(ncdump -h $OUTPUT_FILE | grep float | sed -e s/float//g | sed -e s/\,\ /@/g | sed -e s/\;//g ) #get the names of the variables in output files
for VAR_DIM in $VAR_LST_DIM
do
pabretonniere
committed
NB_DIMS=$(echo $VAR_DIM | sed -e s/[^@]/\ /g | wc -w) #count the number of dimensions of each variable
VAR=$(echo $VAR_DIM | sed -e 's/(.*)//g' )
pabretonniere
committed
[ $NB_DIMS -eq 2 ] && VAR_LST_2D_GG=$(echo ${VAR_LST_2D_GG} $VAR) || VAR_LST_3D_GG=$(echo ${VAR_LST_3D_GG} $VAR) #create a separate list of variables for 2d and 3d
pabretonniere
committed
[ $NB_DIMS -eq 2 ] && VAR_LST_2D_SH=$(echo ${VAR_LST_2D_SH} $VAR) || VAR_LST_3D_SH=$(echo ${VAR_LST_3D_SH} $VAR) #create a separate list of variables for 2d and 3d
;;
esac
done
}
# for surface variables (manipulate GG files)
function surface(){
export YEAR0=${LEAD_LIST[0]}
export MON0=${LEAD_LIST[1]}
export YEARF=${LEAD_LIST[2]}
export MONF=${LEAD_LIST[3]}
MONF=$(echo $(printf "%02d" $(( 10#$MONF))) )
echo ${MEM_LST[@]}
for MEM in ${MEM_LST[@]}; do
pabretonniere
committed
# FILES=$(ls MMA*GG*.nc.$MEM)
FILES="$( find . -maxdepth 1 -name "MMA*GG*.nc.$MEM" | sed -e 's/\.\///g' )"
nb_months_asked=$(( ( ($YEARF - $YEAR0) * 12 + 10#$MONF - 10#$MON0+1 ) ))
if [[ $NLT -ne $nb_months_asked ]];then
echo "Warning, the number of months in MMA (",$nb_files,") is different from the number of months you required according to the LEADLIST",$(( ( ($YEARF - $YEAR0) * 12 + $MONF - $MON0+1 )))," check your configfile!"
exit
fi
#### process each variable ####
for VAR in ${VAR_LST_2D[@]}; do # untar once and extract all the variables
new_name $VAR
case $VAR in
"PRECIP") # for precip, have to add CP and LSP to get total precip
varnew=prlr
for f in ${FILES}; do
SUFFIX=$MEM.${f%????}
cdo selname,CP $f CP.$SUFFIX # select CP
ncrename -h -v CP,prlr CP.$SUFFIX
cdo selname,LSP $f LSP.$SUFFIX # select LSP
ncrename -h -v LSP,prlr LSP.$SUFFIX
cdo add CP.$SUFFIX LSP.$SUFFIX ${varnew}.${SUFFIX} # add CP and LSP to get total precipitation
cdo divc,${FACTOR} ${varnew}.${SUFFIX} toto.nc;rm -f ${varnew}.${SUFFIX}; mv toto.nc ${varnew}.${SUFFIX}
pabretonniere
committed
# prlr_files=$(ls ${varnew}*)
prlr_files="$(find . -maxdepth 1 -name "${varnew}*" | sed -e 's/\.\///g' )"
cdo -O mergetime ${prlr_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
rm -rf prlr*.nc CP* LSP*
;;
"SSRU")
varnew=rsus
for f in ${FILES}; do
SUFFIX=$MEM.${f%????}
cdo selname,SSRD $f SSRD.$SUFFIX
ncrename -h -v SSRD,rsus SSRD.$SUFFIX
cdo selname,SSR $f SSR.$SUFFIX
ncrename -h -v SSR,rsus SSR.$SUFFIX
cdo sub SSR.$SUFFIX SSRD.$SUFFIX ${varnew}.${SUFFIX}
cdo divc,${FACTOR} ${varnew}.${SUFFIX} toto.nc;rm -f ${varnew}.${SUFFIX}; mv toto.nc ${varnew}.${SUFFIX}
pabretonniere
committed
# rsus_files=$(ls ${varnew}*)
rsus_files="$(find . -maxdepth 1 -name "${varnew}*" | sed -e 's/\.\///g' )"
cdo -O mergetime ${rsus_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
rm -rf rsus*.nc SSR* SSRD*
;;
"STRU")
varnew=rlus
for f in ${FILES}; do
SUFFIX=$MEM.${f%????}
cdo selname,STRD $f STRD.$SUFFIX
ncrename -h -v STRD,rlus STRD.$SUFFIX
cdo selname,STR $f STR.$SUFFIX
ncrename -h -v STR,rlus STR.$SUFFIX
cdo sub STR.$SUFFIX STRD.$SUFFIX ${varnew}.${SUFFIX}
cdo divc,${FACTOR} ${varnew}.${SUFFIX} toto.nc;rm -f ${varnew}.${SUFFIX}; mv toto.nc ${varnew}.${SUFFIX}
pabretonniere
committed
# rsus_files=$(ls ${varnew}*)
rsus_files="$( find . -maxdepth 1 -name "${varnew}*" | sed -e 's/\.\///g' )"
cdo -O mergetime ${rsus_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
rm -rf rlus*.nc STR* STRD*
"SWVL") # for soil moisture, have to average SWVL1 SWVL3 SWVL2 SWVL4
varnew=mrso
for f in ${FILES}; do
SUFFIX=$MEM.${f%????}
for var in SWVL1 SWVL3 SWVL2 SWVL4
do
cdo selname,$var $f ${var}.$SUFFIX # select CP
done
#define the layer thickness
fact1="scale=8;7/255."|bc
fact2="scale=8;21/255."|bc
fact3="scale=8;72/255."|bc
fact4="scale=8;155/255."|bc
# average soil moisture
cdo -chname,SWVL1,mrso \
-add -add -divc,$fact1 SWVL1.$SUFFIX -divc,$fact2 SWVL2.$SUFFIX \
-add -divc,$fact3 SWVL3.$SUFFIX -divc,$fact4 SWVL4.$SUFFIX
done
mrso_files="$(find . -maxdepth 1 -name "${varnew}*" | sed -e 's/\.\///g' )"
cdo -O mergetime ${mrso_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
;;
*)
new_name $VAR
for f in ${FILES};do
pabretonniere
committed
echo $f --
cdo selname,${VAR} ${f} ${VAR}.$MEM.${f%????}
done
pabretonniere
committed
# TMP_FILES=$(ls ${VAR}*.nc)
TMP_FILES="$( find . -maxdepth 1 -name "${VAR}*.nc" | sed -e 's/\.\///g' )"
TMP_OUT=tmp_${varnew}_$SDATE.$MEM.nc
# combine all the time steps in one file
cdo -O mergetime ${TMP_FILES} ${TMP_OUT}
[[ ${VAR} != ${varnew} ]] && ncrename -h -v ${VAR},${varnew} ${TMP_OUT}
case ${varnew} in
"rss"|"rls"|"rsscs"|"rsds"|"rlds"|"hflsd"|"hfssd"|"rlt"|"rst"|"rltcs"|"rstcs")
cdo divc,${FACTOR} ${TMP_OUT} toto.nc; rm -rf ${TMP_OUT}; mv toto.nc ${TMP_OUT}
pabretonniere
committed
ncatted -O -h -a units,${varnew},m,c,"W m-2" ${TMP_OUT}
rm -rf ${TMP_FILES}
# cdo divc,${FACTOR} ${TMP_OUT} toto.nc; rm ${TMP_OUT};mv toto.nc ${TMP_OUT}
;;
esac
done # loop for VAR
done # loop for members
echo "after loop on memebers in surface"
Virginie Guemas
committed
# finish selecting the variables
# combine members and change the attributes
for VAR in ${VAR_LST_2D[@]}; do
new_name $VAR
Virginie Guemas
committed
pathout=${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly
mkdir -m ug+w -m o-w -p $pathout
pabretonniere
committed
LSMBSH="${LISTMEMB[0]}-${LISTMEMB[${#LISTMEMB[@]}-1]}"
# PREVIOUS_FILE="$(ls -tr ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly/${varnew}_${SDATE}_*.nc | tail -1)" (if left like this, we have the "no such file" pb. but if we chose the "find" option, we can sort the files and get hte last one edited => have to make a find to be sure there are some previous_files and then, if so, get the actual previous_file with the ls command.
#savedir
Virginie Guemas
committed
PREVIOUS_FILE="$( find ${pathout} -maxdepth 1 -name "${varnew}_${SDATE}_*.nc" )"
echo okkkk
if [ ! -z $( ls $PREVIOUS_FILE ) ] ; then
Virginie Guemas
committed
PREVIOUS_FILE="$(ls -tr ${pathout}/${varnew}_${SDATE}_*.nc | tail -1)"
cd ${pathout}
pabretonniere
committed
FILE_NAME_PREVIOUS="$( find . -maxdepth 1 -name "${varnew}_${SDATE}_*.nc" | tail -1 | sed -e 's/\.\///g' )"
cd -
Virginie Guemas
committed
IDX_1ST=$(echo ${varnew}_${SDATE}_fc | wc -m )
pabretonniere
committed
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 )
pabretonniere
committed
if [ $LAST_MEMBER_PREVIOUS -le $(expr ${LISTMEMB[0]} - 1 ) ] ; then
cp $PREVIOUS_FILE tmp_$FILE_NAME_PREVIOUS
pabretonniere
committed
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"
pabretonniere
committed
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! If you are not adding new lead_times, revise the consistency between your member list and the previously processed files"
fi
YRSO=${YEAR0}$(printf "%02d" $(( 10#$MON0)))_${YEARF}$(printf "%02d" $(( 10#$MONF)))
gather_memb tmp_${varnew}_$SDATE ${varnew}_${SDATE}_fc${LSMBSH}_${YRSO}.nc $varnew $VAR
#test chloe gather_memb tmp_${varnew}_$SDATE ${varnew}_${SDATE}_${LSMBSH}_${YRSO}.nc $varnew $VAR
output=${varnew}_${SDATE}_fc${LSMBSH}_${YRSO}.nc
##### Change the header informations #####
#
# Get the CFU standard attributes to be written in the variable
#
pabretonniere
committed
variables="$(cat ${HEAD_DIR}/table_of_variable | cut -f$idx -d'|' | sed -e 's/ /@/g')" #to be changed into more interactive with xml table
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
#
pabretonniere
committed
ncap2 -h -O -s level_${varnew}="$cfulevel_number" ${output} ${output}
#
# Removing unnecessary attributes
#
#if [[ $idx -ne 40 ]]; then
for att in units valid_range actual_range code table GRID_TYPE ; do
ncatted -O -h -a ${att},${varnew},d,c, ${output}
done
#
# Adding and modifying the {varnew}iable attributes
#
ncatted -O -h -a _FillValue,${varnew},a,f,1.e+12 ${output}
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 unit_long,${varnew},o,c,"$cfuunit_long" ${output} # {varnew}iable long unit name
ncatted -O -h -a units,${varnew},o,c,"$cfuunit" ${output} # {varnew}iable units
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} # variable coordinates
#
# If the NetCDF file had a horizontal axis name different from longitude
#
ncrename -h -d lon,longitude -v lon,longitude ${output}
pabretonniere
committed
[[ ! -z $( ncdump -h ${output} | grep record ) ]] && ncrename -h -d record,ensemble ${output}
# [[ ! -z $( ncdump -h ${output} | grep ensemble ) ]] && ncrename -h -d record,ensemble ${output}
#
# If the NetCDF file had a vertical axis name different from latitude
#
ncrename -h -d lat,latitude -v lat,latitude ${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
#[[ $idx -ne 40 ]] &&
ncatted -O -h -a axis,level_${varnew},o,c,"Z" ${output} # variable level axis
# modify level information
# reshape the dimension and make time unlimited
# [[ ! -z $( ncdump -h ${output} | grep record ) ]] && ncpdq -O -h -a time,ensemble ${output} ${output}
[[ ! -z $( ncdump -h ${output} | grep ensemble ) ]] && ncpdq -O -h -a time,ensemble ${output} ${output}
#if [[ $idx -ne 40 ]]; then
nt=$(cdo ntime ${output})
ncatted -O -h -a standard_name,level_${varnew},c,c,"height" ${output} # standard name
ncatted -O -h -a long_name,level_${varnew},c,c,"reference height" ${output} # long name
ncatted -O -h -a data_type,level_${varnew},c,c,"float" ${output} # data type
ncatted -O -h -a units,level_${varnew},c,c,"m" ${output} # units
ncatted -O -h -a axis,level_${varnew},c,c,"Z" ${output} # axis
ncatted -O -h -a positive,level_${varnew},c,c,"up" ${output}
ncap2 -O -h -s "level_${varnew}=float(${cfulevel_number})" $output $output
# 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
echo "before modify ncvalue" $output ; ncdump -h $output
for MEM in ${LISTMEMB[@]}; do
cat>modify_ncvalue<<EOF
pabretonniere
committed
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
EOF
cat modify_ncvalue
bash modify_ncvalue $output; rm modify_ncvalue
i=$((i+1))
done
##
ncrename -O -h -v time,kaka $output # delete time variable
ncks -O -h -x -v kaka $output $output # delete time variable
save_final_output $varnew $output
done # loop for variables
}
function new_name(){ #to be rethought: make a match between var_name and xml table
case $1 in # rename variable names in model output to the standard names which should be used in post-processed files
"T2M")
varnew=tas
idx=1
;;
"D2M")
varnew=d2m
idx=25
;;
"U10M")
varnew=uas
idx=15
;;
"V10M")
varnew=vas
idx=16
;;
"PRECIP")
varnew=prlr
idx=7
;;
"CP")
varnew=prc
idx=26
;;
"SF")
varnew=prsn
idx=27
;;
"E")
varnew=evspsbl
idx=28
;;
"SSTK")
# "SST")
#weird... VAR=SSTK
varnew=tos
idx=10
;;
"MSL")
varnew=psl
idx=2
;;
"SSR")
varnew=rss
idx=5
;;
"SSRU")
varnew=rsus
idx=30
;;
"SSRC")
varnew=rsscs
idx=29
;;
"TSR")
varnew=rst
idx=21
;;
"TSRC")
varnew=rstcs
idx=22
;;
"TTR")
varnew=rlt
idx=23
;;
"TTRC")
varnew=rltcs
idx=24
;;
"STR")
varnew=rls
idx=6
;;
"STRD")
varnew=rlds
idx=31
;;
"STRU")
varnew=rlus
idx=32
;;
"SLHF")
varnew=hflsd
idx=9
;;
"SSHF")
varnew=hfssd
idx=8
;;
"SSRD")
varnew=rsds
idx=20
;;
"T")
varnew=ta
idx=11
;;
"U")
varnew=ua
idx=12
;;
"V")
varnew=va
idx=13
;;
"W")
varnew=wap
idx=34
;;
"CLWC")
varnew=clw
idx=36
;;
"CIWC")
varnew=cli
idx=37
;;
"Z")
varnew=g
idx=14
;;
"Q")
varnew=hus
idx=19
;;
"tasmax")
varnew=tasmax
idx=17
;;
"tasmin")
varnew=tasmin
idx=18
;;
"TCC")
varnew=clt
idx=33
;;
"CC")
varnew=cl
idx=35
#pab!!!! new variables to be added when variable list read directly in outputs, some are missing
;;
"var78")
varnew=tclw
idx=38
;;
"AL")
varnew=al
idx=39
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
;;
esac
done # loop for VAR
done # loop for members
echo "after loop on memebers in surface"
# finish selecting the variables
# combine members and change the attributes
for VAR in ${VAR_LST_2D[@]}; do
new_name $VAR
pathout=${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly
mkdir -m ug+w -m o-w -p $pathout
LSMBSH="${LISTMEMB[0]}-${LISTMEMB[${#LISTMEMB[@]}-1]}"
# PREVIOUS_FILE="$(ls -tr ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_${NFRP}hourly/${varnew}_${SDATE}_*.nc | tail -1)" (if left like this, we have the "no such file" pb. but if we chose the "find" option, we can sort the files and get hte last one edited => have to make a find to be sure there are some previous_files and then, if so, get the actual previous_file with the ls command.
#savedir
PREVIOUS_FILE="$( find ${pathout} -maxdepth 1 -name "${varnew}_${SDATE}_*.nc" )"
echo okkkk
if [ ! -z $( ls $PREVIOUS_FILE ) ] ; then
PREVIOUS_FILE="$(ls -tr ${pathout}/${varnew}_${SDATE}_*.nc | tail -1)"
cd ${pathout}
FILE_NAME_PREVIOUS="$( find . -maxdepth 1 -name "${varnew}_${SDATE}_*.nc" | tail -1 | sed -e 's/\.\///g' )"
cd -
IDX_1ST=$(echo ${varnew}_${SDATE}_fc | 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 tmp_$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! If you are not adding new lead_times, revise the consistency between your member list and the previously processed files"
fi
echo ok2
fi
echo ok3
pwd
ls
YRSO=${YEAR0}$(printf "%02d" $(( 10#$MON0)))_${YEARF}$(printf "%02d" $(( 10#$MONF)))
gather_memb tmp_${varnew}_$SDATE ${varnew}_${SDATE}_fc${LSMBSH}_${YRSO}.nc $varnew $VAR
#test chloe gather_memb tmp_${varnew}_$SDATE ${varnew}_${SDATE}_${LSMBSH}_${YRSO}.nc $varnew $VAR
output=${varnew}_${SDATE}_fc${LSMBSH}_${YRSO}.nc
##### Change the header informations #####
#
# 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')" #to be changed into more interactive with xml table
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 -h -O -s level_${varnew}="$cfulevel_number" ${output} ${output}
#
# Removing unnecessary attributes
#
#if [[ $idx -ne 40 ]]; then
for att in units valid_range actual_range code table GRID_TYPE ; do
ncatted -O -h -a ${att},${varnew},d,c, ${output}
done
#
# Adding and modifying the {varnew}iable attributes
#
ncatted -O -h -a _FillValue,${varnew},a,f,1.e+12 ${output}
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 unit_long,${varnew},o,c,"$cfuunit_long" ${output} # {varnew}iable long unit name
ncatted -O -h -a units,${varnew},o,c,"$cfuunit" ${output} # {varnew}iable units
#fi
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} # variable coordinates
#
# If the NetCDF file had a horizontal axis name different from longitude
#
ncrename -h -d lon,longitude -v lon,longitude ${output}
[[ ! -z $( ncdump -h ${output} | grep record ) ]] && ncrename -h -d record,ensemble ${output}
# [[ ! -z $( ncdump -h ${output} | grep ensemble ) ]] && ncrename -h -d record,ensemble ${output}
#
# If the NetCDF file had a vertical axis name different from latitude
#
ncrename -h -d lat,latitude -v lat,latitude ${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
#[[ $idx -ne 40 ]] &&
ncatted -O -h -a axis,level_${varnew},o,c,"Z" ${output} # variable level axis
# modify level information
# reshape the dimension and make time unlimited
# [[ ! -z $( ncdump -h ${output} | grep record ) ]] && ncpdq -O -h -a time,ensemble ${output} ${output}
[[ ! -z $( ncdump -h ${output} | grep ensemble ) ]] && ncpdq -O -h -a time,ensemble ${output} ${output}
cp ${output} hhh.nc
ncks -h -A header.nc ${output}
#if [[ $idx -ne 40 ]]; then
nt=$(cdo ntime ${output})
ncatted -O -h -a standard_name,level_${varnew},c,c,"height" ${output} # standard name
ncatted -O -h -a long_name,level_${varnew},c,c,"reference height" ${output} # long name
ncatted -O -h -a data_type,level_${varnew},c,c,"float" ${output} # data type
ncatted -O -h -a units,level_${varnew},c,c,"m" ${output} # units
ncatted -O -h -a axis,level_${varnew},c,c,"Z" ${output} # axis
ncatted -O -h -a positive,level_${varnew},c,c,"up" ${output}
ncap2 -O -h -s "level_${varnew}=float(${cfulevel_number})" $output $output
#fi
# 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
echo "before modify ncvalue" $output ; ncdump -h $output
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
##
ncrename -O -h -v time,kaka $output # delete time variable
ncks -O -h -x -v kaka $output $output # delete time variable
save_final_output $varnew $output
done # loop for variables
}
function new_name(){ #to be rethought: make a match between var_name and xml table
case $1 in # rename variable names in model output to the standard names which should be used in post-processed files
"T2M")
varnew=tas
idx=1
;;
"D2M")
varnew=d2m
idx=25
;;
"U10M")
varnew=uas
idx=15
;;
"V10M")
varnew=vas
idx=16
;;
"PRECIP")
varnew=prlr
idx=7
;;
"CP")
varnew=prc
idx=26
;;
"SF")
varnew=prsn
idx=27
;;
"E")
varnew=evspsbl
idx=28
;;
"SSTK")
# "SST")
#weird... VAR=SSTK
varnew=tos
idx=10
;;
"MSL")
varnew=psl
idx=2
;;
"SSR")
varnew=rss
idx=5
;;
"SSRU")
varnew=rsus
idx=30
;;
"SSRC")
varnew=rsscs
idx=29
;;
"TSR")
varnew=rst
idx=21
;;
"TSRC")
varnew=rstcs
idx=22
;;
"TTR")
varnew=rlt
idx=23
;;
"TTRC")
varnew=rltcs
idx=24
;;
"STR")
varnew=rls
idx=6
;;
"STRD")
varnew=rlds
idx=31
;;
"STRU")
varnew=rlus
idx=32
;;
"SLHF")
varnew=hflsd
idx=9
;;
"SSHF")
varnew=hfssd
idx=8
;;
"SSRD")
varnew=rsds
idx=20
;;
"T")
varnew=ta
idx=11
;;
"U")
varnew=ua
idx=12
;;
"V")
varnew=va
idx=13
;;
"W")
varnew=wap
idx=34
;;
"CLWC")
varnew=clw
idx=36
;;
"CIWC")
varnew=cli
idx=37
;;
"Z")
varnew=g
idx=14
;;
"Q")
varnew=hus
idx=19
;;
"tasmax")
varnew=tasmax
idx=17
;;
"tasmin")
varnew=tasmin
idx=18
;;
"TCC")
varnew=clt
idx=33
;;
"CC")
varnew=cl
idx=35
;;
#pab!!!! new variables to be added when variable list read directly in outputs, some are missing
"var78")
varnew=tclw
idx=38
;;
"AL")
varnew=al
idx=39
;;
"SWVL")
varnew=mrso
idx=40
"SWVL1")
varnew=mrso1
idx=41
"SWVL2")
varnew=mrso2
idx=42
"SWVL3")
varnew=mrso3
idx=43
"SWVL4")
varnew=mrso4
idx=44
pabretonniere
committed
varnew=$( echo $1 | tr [A-Z] [a-z] )
pabretonniere
committed
function gather_memb() {
# Gather the members in a single netcdf file
# $1 : prefix netcdf file name for all the members
# $2 : output file name
# Created in May 2012 Author : vguemas@ic3.cat
# Adapted for atmospherical outputs January 2013 pierre-antoine.bretonniere@ic3.cat
pabretonniere
committed
# OLD_FILE=$(ls ${1}_* || echo "")
# NEW_FILES=$(ls "${1}".*)
OLD_FILE="$( find . -maxdepth 1 -name "${1}_*" | sed -e 's/\.\///g' )"
NEW_FILES="$( find . -maxdepth 1 -name "${1}.*" | sed -e 's/\.\///g' )"
VAR_LOC=$3
VAR_OLD=$4
rm -f tmp_cat.nc tmp_0_${OLD_FILE} tmp_${OLD_FILE}
pabretonniere
committed
if [[ $( echo $NEW_FILES | wc -w ) -gt 1 ]]; then
ncecat ${NEW_FILES} tmp_cat.nc || exit
pabretonniere
committed
ncrename -h -d record,ensemble tmp_cat.nc
ncecat -O -h tmp_cat.nc tmp_cat2.nc
ncpdq -O -h -a ensemble,record tmp_cat2.nc tmp_cat2.nc