Newer
Older
#
# ./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
#module load NCO
#module load CDO
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
cd ${DATADIR}/${EXPID}/original_files/${SDATE}/${DIR}/outputs
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
NFILE=0
for file in $( find . -maxdepth 1 -name "MMA*" )
do
[[ $( echo $file | cut -f5 -d"_" | cut -f1 -d"-" ) -ge ${LEAD_LIST[0]}${LEAD_LIST[1]}01 ]] && [[ $( echo $file | cut -f5 -d"_" | cut -f1 -d"-" ) -le ${LEAD_LIST[2]}${LEAD_LIST[3]}01 ]] && NFILE=$(( NFILE + 1 ))
done
# NFILE="$( find . -maxdepth 1 -name "MMA*" | wc -l )"
pabretonniere
committed
for TMPFILE in $( find . -maxdepth 1 -name "MMA*" | sed -e 's/\.\///g' )
do
# if [[ $( echo $TMPFILE | cut -f5 -d"_" | cut -f1 -d"-" ) -ge ${LEAD_LIST[0]}${LEAD_LIST[1]}01 ]]; then
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
# fi
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
}
# function to get reftime time
function rtime(){
DATE1=$1
DATE2=$2
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)
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(){
for MEM in ${MEM_LST[@]}; do
# untar and unzip MMA SH files and GG files
if [ -z $( echo ${LEAD_LIST[@]} ) ];then
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
PATH_TO_SEARCH=${DATADIR}/${EXPID}/original_files/${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 ))
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
FILE_LIST=""
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))) )
[[ 10#$MON1 -eq 0 ]] && MON1=12
[[ 10#$MON2 -eq 0 ]] && MON2=12
[[ 10#$MON2 -eq 12 ]] && YEAR2=$(( $YEAR2 - 1 ))
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
FILE="$( find ${DATADIR}/${EXPID}/original_files/${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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
echo "Expected file ${DATADIR}/${EXPID}/original_files/${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
done
}
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))) )
#copy and format the mask
cp ${MASK_PATH} mask.nc
lstdims=`ncdump -h mask.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 mask.nc
ncrename -h -d lon,longitude -d lat,latitude mask.nc
fi
echo ${MEM_LST[@]}
for MEM in ${MEM_LST[@]}; do
pabretonniere
committed
# FILES=$(ls MMA*GG*.nc.$MEM)
Chloé Prodhomme
committed
FILES="$( find . -maxdepth 1 -name "MMA*GG*.nc.$MEM" | sed -e 's/\.\///g' | sort -u )"
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!"
#### 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
cdo chname,LSP,$varnew -divc,${FACTOR} -add -selname,LSP $f -selname,CP $f ${varnew}.${SUFFIX}
pabretonniere
committed
# prlr_files=$(ls ${varnew}*)
Chloé Prodhomme
committed
prlr_files="$(find . -maxdepth 1 -name "${varnew}*${MEM}*" | sed -e 's/\.\///g' | sort -u )"
cdo -O mergetime ${prlr_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
;;
"SSRU")
varnew=rsus
for f in ${FILES}; do
cdo chname,SSR,$varnew -divc,${FACTOR} -sub -selname,SSR $f -selname,SSRD $f ${varnew}.${SUFFIX}
pabretonniere
committed
# rsus_files=$(ls ${varnew}*)
Chloé Prodhomme
committed
rsus_files="$(find . -maxdepth 1 -name "${varnew}*${MEM}*" | sed -e 's/\.\///g' | sort -u )"
cdo -O mergetime ${rsus_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
;;
"STRU")
varnew=rlus
for f in ${FILES}; do
cdo chvar,STRD,rlus -divc,${FACTOR} -sub -selname,STRD $f -selname,STR $f ${varnew}.${SUFFIX}
pabretonniere
committed
# rsus_files=$(ls ${varnew}*)
Chloé Prodhomme
committed
rsus_files="$( find . -maxdepth 1 -name "${varnew}*${MEM}*" | sed -e 's/\.\///g' | sort -u )"
cdo -O mergetime ${rsus_files} tmp_${varnew}_$SDATE.$MEM.nc # combine all the time steps in one file
"SWVL") # for soil moisture, have to average SWVL1 SWVL3 SWVL2 SWVL4
varnew=mrso
for f in ${FILES}; do
fact1=$(echo "scale=8;7/255."|bc)
fact2=$(echo "scale=8;21/255."|bc)
fact3=$(echo "scale=8;72/255."|bc)
fact4=$(echo "scale=8;155/255."|bc)
cdo vertmean -chname,SWVL1,mrso \
-add -add -divc,$fact1 \
-selname,SWVL1 $f\
-divc,$fact2 \
-selname,SWVL2 $f \
-add -divc,$fact3 \
-selname,SWVL3 $f \
-divc,$fact4 \
-selname,SWVL4 $f \
${varnew}_aux.${SUFFIX}
ncwa -h -O -a depth ${varnew}_aux.${SUFFIX} -o ${varnew}.${SUFFIX}
rm -f ${varnew}_aux.${SUFFIX}
mrso_files="$(find . -maxdepth 1 -name "${varnew}*${MEM}*" | sed -e 's/\.\///g' | sort -u )"
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%.*}
Chloé Prodhomme
committed
TMP_FILES="$( find . -maxdepth 1 -name "${VAR}*.nc" | sed -e 's/\.\///g' | sort -u )"
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}
"prsn|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}
[[ ${varnew} != "prsn" ]] && ncatted -O -h -a units,${varnew},m,c,"W m-2" ${TMP_OUT}
#mask the continent for tos
if [[ $varnew == tos ]]
then
cdo div ${TMP_OUT} -ltc,0.5 mask.nc ${TMP_OUT}_aux
#replace the fill value
ncatted -O -a _FillValue,$varnew,d,, ${TMP_OUT}_aux
ncatted -O -a missing_value,$varnew,d,, ${TMP_OUT}_aux
cdo setrtoc,-9e34,-2000,1.e+12 ${TMP_OUT}_aux ${TMP_OUT}
rm -rf ${TMP_OUT}_aux
fi
rm -rf ${TMP_FILES}
;;
esac
done # loop for VAR
done # loop for members
Virginie Guemas
committed
# finish selecting the variables
# combine members and change the attributes
for VAR in ${VAR_LST_2D[@]}; do
new_name $VAR
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
pathout=${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_f${NFRP}h
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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" )"
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
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
Chloé Prodhomme
committed
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
ncatted -O -h -a ${att},${varnew},d,c, ${output}
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
# Adding and modifying the variable attributes
#
ncatted -O -h -a _FillValue,${varnew},a,f,1.e+12 ${output}
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
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
ncatted -O -h -a coordinates,${varnew},o,c,"longitude latitude reftime leadtime time_bnd experiment_id source realization institution " ${output} # variable coordinates
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
#
# 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}
#
# 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
# modify level information
# reshape the dimension and make time unlimited
[[ ! -z $( ncdump -h ${output} | grep ensemble ) ]] && ncpdq -O -h -a time,ensemble ${output} ${output}
cp ${output} hhh.nc
ncks -h -A header.nc ${output}
nt=$(cdo ntime ${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
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")
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
;;
"var78")
varnew=tclw
idx=38
;;
"AL")
varnew=al
idx=39
;;
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
Chloé Prodhomme
committed
OLD_FILE="$( find . -maxdepth 1 -name "${1}_*" | sed -e 's/\.\///g' | sort -u )"
NEW_FILES="$( find . -maxdepth 1 -name "${1}.*" | sed -e 's/\.\///g'| sort -u )"
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
ncwa -O -h -a record tmp_cat2.nc tmp_cat2.nc
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
mv tmp_cat2.nc tmp_cat.nc
pabretonniere
committed
else
mv ${NEW_FILES} tmp_cat.nc
fi
for var_check in $VAR_LST_3D_GG
do
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
if [ $VAR_OLD == $var_check ];then
ncks -O -h -a -v lon,lat,$VAR_LOC tmp_cat.nc tmp_cat.nc
fi
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
TARDIR=${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_f${NFRP}h
[ ! -d $TARDIR ] && mkdir -p $TARDIR
Virginie Guemas
committed
echo ${TARDIR} -- ${output}
chmod 770 ${output}
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.
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
ln -sf ${TARDIR}/${output} ${TARDIR}/${varnew}_${SDATE}.nc
}
# Select variables and levels
function combine_3d(){
for MEM in ${MEM_LST[@]}; do
pabretonniere
committed
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
pabretonniere
committed
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
case $var in
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
cdo selname,${var} -sellevel,${LEVEL_LST} ${f} ${TMP_OUT}
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
cdo selname,${var} ${f} ${TMP_OUT}
;;
esac
done
done
# combine all time step in one file
for var in ${VAR_LST_3D[@]}; do
new_name $var
pabretonniere
committed
# 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
pabretonniere
committed
ncrename -h -v ${var},${varnew} ${output}
rm -rf ${FILES}
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' )
pabretonniere
committed
cdo -r sp2gpl -selname,${varnew} ${f} rg_${f}; rm -f ${f}
done
done
for var in ${VAR_LST_3D_GG[@]}; do
new_name $var
pabretonniere
committed
FILES=$(find . -maxdepth 1 -name "${varnew}_$SDATE.*.nc" | sed -e 's/\.\///g' )
cdo selname,${varnew} ${f} rg_${f}; rm -f ${f}
done
done
}
function upper(){
for var in ${VAR_LST_3D[@]}; do
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
new_name $var
LSMBSH="${LISTMEMB[0]}-${LISTMEMB[${#LISTMEMB[@]}-1]}"
PREVIOUS_FILE="$( find ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_f${NFRP}h -maxdepth -name "${varnew}_${SDATE}_*.nc" | tail -1 | sed -e 's/\.\///g' )"
if [ ! -z $PREVIOUS_FILE ] ; then
cd ${SAVEDIR}/${EXPID}/monthly_mean/${varnew}_f${NFRP}h/;
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 )
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
echo "Some members are going to be treated twice! Revise the consistency between your member list and the previously processed files"
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
rm -f ${FILES}
for CHECK_VAR in $VAR_LST_3D_SH
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
if [ $CHECK_VAR == $var ];then
pabretonniere
committed
ncrename -h -d lon,longitude -d lat,latitude -d lev,level ${output}
ncrename -h -v lon,longitude -v lat,latitude -v lev,level ${output}
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
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
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
ncrename -h -d .depth,level -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
#
pabretonniere
committed
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
#moved when the mask is applied (by Chloe)
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}
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
#
# 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 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
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
##
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
pabretonniere
committed
SDATE1=$(echo ${SDATE}|cut -c1-6)
cdo timmean ${VAR}_${SDATE}_${SDATE1}.nc ${output} ; rm -f ${VAR}_${SDATE}_${SDATE1}.nc
pabretonniere
committed
files="$(find . -maxdepth 1 -name "${VAR}_${SDATE}_*.nc" | sed -e 's/\.\///g' )"
cdo timmean $f toto.nc;rm -f $f
ncrcat -O -h ${output} toto.nc ${output};rm -f toto.nc
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
#
pabretonniere
committed
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
#
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
# Adding and modifying the variable attributes
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
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 data_type,level_${varnew},o,c,"$cfulevel_type" ${output} # variable level type
ncatted -O -h -a units,level_${varnew},o,c,"$cfulevel_units" ${output} # variable 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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
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}
pabretonniere
committed
export WORKDIR
FACTOR=$((NFRP*3600))
ENSEMBLE=${#MEM_LST[@]}
pabretonniere
committed
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"
mkdir -p ${WORKDIR}
if [[ ! -d ${WORKDIR} ]]; then
exit 1
fi
cd ${WORKDIR}
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
if [[ -d /esarchive/exp/ecearth/${EXPID} ]]; then
DATADIR=/esarchive/exp/ecearth
else
DATADIR=/esnas/exp/ecearth
fi
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
base_path=${DATADIR}/${EXPID}/original_files/${SDATE}
mkdir -p ${SAVEDIR}/${EXPID}; chmod -R 775 $SAVEDIR/${EXPID}
get_leadtime
header $SDATE
extract
echo "after extract"
if [ -z ${VAR_LST_2D[@]} ]; then
pabretonniere
committed
GG="$(find . -maxdepth 1 -name "*GG*" | head -1 | sed -e 's/\.\///g' )"
pabretonniere
committed
SH="$(find . -maxdepth 1 -name "*SH*" | head -1 | sed -e 's/\.\///g' )"
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
pabretonniere
committed
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
pabretonniere
committed
[ $var == $var_sh ] && VAR_LST_3D_SH=$(echo $VAR_LST_3D_SH $var)
done
for var_gg in ${VAR_LST_3D_GG_DEFAULT[@]}
do
pabretonniere
committed
[ $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
pabretonniere
committed
[ $var == $var_gg ] && VAR_LST_2D_GG=$(echo $VAR_LST_2D_GG $var)
echo $YEARF - $YEAR0 - $MONF - 10#$MONF - $MON0
echo "before surface"
echo "after surface"
combine_3d
regrid2x2
upper
if [[ ${#VAR_LST_MXMN[@]} > 0 ]]; then
maxmin
fi
cd ~/; rm -rf $WORKDIR # clean up
pabretonniere
committed
trap - EXIT
![Pierre-Antoine Bretonnière Pierre-Antoine Bretonnière's avatar](/gitlab/uploads/-/system/user/avatar/4/avatar.png?width=36)
Pierre-Antoine Bretonnière
committed
#### End of the Main Part of Script ####