#!/bin/bash set -eu # experiemnt name expname=$1 # read configuration file . $2 tmpdir=$tmpdir_root/nemo mkdir -p $tmpdir start_yyyymmdd=${start_yyyy}0101 end_yyymmdd=${end_yyyy}1231 d=$resdir nprocs=$nprocs_nemo # land-sea mask for nemo lsm_file=/nobackup/rossby17/sm_wyser/ece2esmvaltool/lsm_orca1.nc # area for each ocean gridcell (undef over land) area_file=/nobackup/rossby17/sm_wyser/ece2esmvaltool/area_orca1.nc # variable names to be processed var_2d_list="tos sic" var_1d_list="tos sos zos heatc saltc sienh siesh sivolnh sivolsh" var_list= for var in $var_2d_list $var_1d_list do awk -v var=$var -f split_ptab.awk $nemo_par > $tmpdir/nemo-par-$var eval $(sed -n "/name[[:blank:]]*=/ p" $tmpdir/nemo-par-$var | tr -d [:blank:]) var_list=$var_list" "$name done # remove duplicates var_list=$(echo $var_list | tr '[:blank:]' '\n' | sort | uniq | tr '\n' ' ') # create list with filenames to be included # NB: the approach with "find" may not be the best possible choice in case of # very long simulations rm -f $tmpdir/fname_list for fname in $(find $d/nemo -regextype posix-extended -regex '.*_1m_.*(grid_T|icemod).nc$' | sort ) do f=$(basename $fname) fstart=$(echo $f | cut -d _ -f 3) fend=$(echo $f | cut -d _ -f 4) [ $fend -ge $start_yyyymmdd ] && [ $fstart -le $end_yyymmdd ] && echo $fname >> $tmpdir/fname_list done # loop over all filenames; execute in parallel icount=0 while read fname do (( icount+=1 )) echo "...now processing $(basename $fname)" iiii=$(printf %04d $icount) # cdo on bi has problems with pipes and compressed netcdf files # therefore save temporary files with -f nc (maybe only on bi?) cdo -s -f nc splitvar -ifthen $lsm_file \ -selvar,$(echo $var_list | tr -s '[:blank:]' ',') \ $fname $tmpdir/x_${iiii}_ & if (( icount%nprocs == 0 )); then wait; fi done < $tmpdir/fname_list wait # concatenate files icount=0 for var in $var_list do (( icount+=1 )) echo "...concatenating $var" ( rm -f $tmpdir/y_${var}.nc cdo -s cat $tmpdir/x_*_$var.nc $tmpdir/y_${var}.nc # change time variable ncrename -O \ -d time_counter,time -v time_counter,time \ $tmpdir/y_${var}.nc $tmpdir/z_${var}.nc ) & if (( icount%nprocs == 0 )); then wait; fi done wait # save files icount=0 for out_name in $var_2d_list do (( icount+=1 )) echo "...now processing $out_name" ( # get variable name from $nemo_par var=$(grep ' name ' $tmpdir/nemo-par-$out_name | cut -d = -f 2 | tr -d '[:blank:]') # convert units (this may become superfluos once UDUNITS2 support # has been activated when compiling cdo case $out_name in ( tos ) unit_conv='-addc,273.15' ;; ( sic ) unit_conv='-mulc,100' ;; ( * ) unit_conv= ;; esac outfile=$outdir/CMIP5_Omon_historical_${expname}_r1i1p1_T2Ms_${out_name}_${start_yyyy}-${end_yyyy}.nc rm -f $outfile cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \ -setreftime,$refdate,00:00:00,days \ -invertlat \ $unit_conv -remapdis,n128 \ $tmpdir/z_${var}.nc $outfile ) & if (( icount%nprocs == 0 )); then wait; fi done wait icount=0 for out_name in $var_1d_list do (( icount+=1 )) echo "...now processing $out_name" ( # get variable name from $nemo_par var=$(grep ' name ' $tmpdir/nemo-par-$out_name | cut -d = -f 2 | tr -d '[:blank:]') # convert units (this may become superfluos once UDUNITS2 support # has been activated when compiling cdo case $out_name in ( tos ) unit_conv='-addc,273.15' ;; ( sie* ) unit_conv='-divc,1e12' ;; ( sivol* ) unit_conv='-divc,1e12' ;; ( * ) unit_conv= ;; esac # threshold for sea-ice extent case $out_name in ( sie* ) ext_thresh='-gtc,.15' ;; ( * ) ext_thresh= ;; esac outfile=$outdir/CMIP5_Omon_historical_${expname}_r1i1p1_T1Ms_${out_name}_${start_yyyy}-${end_yyyy}.nc rm -f $outfile case $out_name in ( sienh | sivolnh ) cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \ -setreftime,$refdate,00:00:00,days $unit_conv \ -fldsum -maskindexbox,1,362,147,292 \ -mul $ext_thresh $tmpdir/z_${var}.nc $area_file \ $outfile ;; ( siesh | sivolsh ) cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \ -setreftime,$refdate,00:00:00,days $unit_conv \ -fldsum -maskindexbox,1,362,1,146 \ -mul $ext_thresh $tmpdir/z_${var}.nc $area_file \ $outfile ;; ( * ) cdo -s -f nc4c -z zip setpartabn,$tmpdir/nemo-par-$out_name \ -setreftime,$refdate,00:00:00,days $unit_conv \ -div -fldsum -mul $area_file $tmpdir/z_${var}.nc \ -fldsum $area_file $outfile ;; esac ) & if (( icount%nprocs == 0 )); then wait; fi done wait # clean up rm -r $tmpdir echo "!!! ece2evt-nemo done !!!" exit 0