Changeset 3847


Ignore:
Timestamp:
05/25/18 15:20:39 (2 years ago)
Author:
cugnet
Message:

Simplifications and extensions for clims_CMIP6.bash:

  • easier management of existing raw forcing files on ciclad (several paths added).
  • general functions extended and put in a separated file: tools.bash
  • other minor improvements (for example: fields resolutions and ranks detection)
Location:
TOOLS/CMIP6_FORCING/OZONE
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • TOOLS/CMIP6_FORCING/OZONE/clims_CMIP6.bash

    r3744 r3847  
    22module load nco/4.6.9 
    33 
    4 ia="1"    #=== Indices of chosen Amip  datasets (see below AMIP_VERSION_NAMES/AMIP_VERSION_FOLDERS) 
    5 io="1"    #=== Indices of chosen Ozone datasets (see below OZON_VERSION_NAMES/OZON_VERSION_FOLDERS) 
     4#=== Indices of chosen datasets among available collection (see DATANAMES after) 
     5DOPROCESS=('y' 'y')          #--- PROCESSED DATA TYPES (SSTandSeaIce, ozone) 
     6IXPROCESS=("0" "3")        #--- PROCESSED DATASETS INDICES 
     7recomp='n'                   #--- RECOMPUTE FILES THAT ARE ALREADY PRESENT (y/n) 
    68 
    79#=============================================================================== 
     
    1719#=== PARAMETERS NOT SUPPOSED TO BE CHANGED OFTEN =============================== 
    1820#=============================================================================== 
    19 local=$PWD 
    2021case $machine in 
    2122  ada)    work=$WORKDIR 
     
    2627          DATAOU='/ccc/work/cont003/igcmg/igcmg/IGCM/ATM' ;; 
    2728  ciclad) work=/data/$USER 
    28           DATAIN='/prodigfs/project/input4MIPs/CMIP6/CMIP' 
     29          DATAIN='/prodigfs/project' 
    2930          DATAOU='/prodigfs/ipslfs/igcmg/IGCM/ATM'        ;; 
    3031esac 
    3132if [ "$DATAIN" = "" ]; then echo "Raw data folder for $machine has to be specified."; exit; fi 
    3233DATAOU=/data/dcugnet$DATAOU 
    33 #--- INPUT DATA: AMIP 
    34 AMIP_VERSION_NAMES=('PCMDI-AMIP-1-1-3') 
    35 AMIP_VERSION_FOLDERS=('gn/v20161031') 
    36 AMIP_FOLDER_IN=$DATAIN'/PCMDI/${amip_version_name}/${W}/mon/${V}/${amip_version_folder}' 
    37 amip_tname_in=('ocean' 'seaIce') 
    38 amip_vname_in=('tos' 'siconc') 
    39 amip_fname_in='${V}_input4MIPs_SSTsAndSeaIce_CMIP_${amip_version_name}_${amip_version_folder%/*}_187001-20????.nc' 
    4034 
    41 #--- INPUT DATA: OZONE 
    42 OZON_VERSION_NAMES=('UReading-CCMI-1-0') 
    43 OZON_VERSION_FOLDERS=('gn/v20160711') 
    44 OZON_VERSION_FOLDERS_CLIM=('gn/v20160830') 
    45 OZON_FOLDER_IN=$DATAIN/'UReading/${ozon_version_name}/atmos/mon${clim}/${V}/${ozon_version_folder}' 
    46 ozon_vname_in='vmro3' 
    47 ozon_fname_in='${V}_input4MIPs_ozone_CMIP_${ozon_version_name}_${ozon_version_folder%/*}_${Yi}01-${Yf}12.nc' 
    48 res3D='144x96x66' 
    49 res2D='96x66_zonal' 
     35#--- DATA TO TREAT (INTERANNUAL AND CLIMATOLOGIES): "SSTsAndSeaIce" "ozone": 
     36DATATYPES=('SSTsAndSeaIce' 'ozone') 
     37f='${var}_${datasets}_${datatype}_${project}_${dataset}_${grid}_${Yb}01-${Ye}??.nc' 
     38FILES_IN=("input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-0/ocean/mon/\${var}/gn/v20160609/$f  \ 
     39           input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-1/ocean/mon/\${var}/gn/v20161020/$f  \ 
     40           input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-2/ocean/mon/\${var}/gn/v20170419/$f  \ 
     41           input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-3/ocean/mon/\${var}/gn/v20171031/$f  \ 
     42           input4MIPs/CMIP6/CMIP/PCMDI/PCMDI-AMIP-1-1-4/ocean/mon/\${var}/gn/v20180427/$f" \ 
     43          "input4MIPs/CMIP6/CMIP/UReading/UReading-CCMI-1-0/atmos/mon/\${var}/gn/v20160711/$f   \ 
     44           input4MIPs/CMIP6/DAMIP/CCCma/CCMI-hist-nat-1-0/atmos/mon/\${var}/gn/v20180503/$f     \ 
     45           input4MIPs/CMIP6/DAMIP/CCCma/CCMI-hist-sol-1-0/atmos/mon/\${var}/gn/v20180503/$f     \ 
     46           input4MIPs/CMIP6/DAMIP/CCCma/CCMI-hist-stratO3-1-0/atmos/mon/\${var}/gn/v20170814/$f \ 
     47           input4MIPs/CMIP6/DAMIP/CCCma/CCMI-hist-volc-1-0/atmos/mon/\${var}/gn/v20180503/$f") 
     48CLIMS_IN=("/ / / / /" \ 
     49          "input4MIPs/CMIP6/CMIP/UReading/UReading-CCMI-1-0/atmos/monC/\${var}/gn/v20160830/$f  /  /  /  /") 
     50VARNAM_IN=('tosbcs siconcbcs' 'vmro3') 
     51DIRSTRUCT='datasets/exercise/project/origin/dataset/domain/frequency/var/grid/version/' 
     52DATADATES=()                                #--- DETERMINED AUTOMATICALLY LATER 
     53 
     54#--- RESULTS STORAGE 
     55if [ ! -d $DATAOU ]; then mkdir -p $DATAOU; fi 
     56FILES_OU=('LIMIT/AMIP.${version}/original/amipbcs_${var}_360x180_${Y}.nc' \ 
     57          'OZONE/${origin}/${dataset}.${version}/original/${var}_${Y}.nc') 
     58#           SSTsAndSeaIce:        ozone: 
     59VARNAM_OU=('tosbcs sicbcs'       'tro3')    #--- VARIABLES NAMES IN FILES 
     60VARNAMFOU=('sst sic'             'tro3')    #--- VARIABLES NAMES FOR FILES NAMES 
     61CLIMDATES=('1870-1899 1979-2008' '1850-1879 1979-2008') 
     62coords="X:longitude Y:latitude Z:plev"      #--- COORDINATES FOR ce0l 
     63 
     64#================================================================================== 
     65#=== FEW FUNCTIONS 
     66#   NOTE: These functions are not specific to this script. 
     67#   EXCEPTION: in get_date (error in ciclad storage tree for PCMDI-AMIP-1-1-0): 
     68#          if [ $dataset = "PCMDI-AMIP-1-1-0" ]; then grid='gs1x1'; fi 
     69#================================================================================== 
     70source tools.bash 
    5071 
    5172 
    52 #--- VERSIONS OF INPUT FILES EFFECTIVELY CHOSEN 
    53 ((ia--)); ((io--)) 
    54  
    55 #--- OUTPUT DATA 
    56 if [ ! -d $DATAOU ]; then mkdir -p $DATAOU; fi 
    57 AMIP_FOLDER_OU=$DATAOU/'LIMIT/AMIP.${amip_version_folder##*/}/${suf}' 
    58 OZON_FOLDER_OU=$DATAOU/'OZONE/UReading/historical.${ozon_version_folder##*/}/original' 
    59 amip_vname_ou=('tosbcs' 'sicbcs'); amip_fname_ou='amip${bc}_${V}_360x180_${Y}.nc' 
    60 ozon_vname_ou='tro3'             ; ozon_fname_ou='${V}_${Y}.nc' 
    61 Y_amip_beg=1870 ; Y_amip_end=2017    #--- FOR AMIP  FILES (INTERANNUAL) 
    62 Y_ozon_beg=1850 ; Y_ozon_end=2014    #--- FOR OZONE FILES (INTERANNUAL) 
    63 Y_clim_beg=1979 ; Y_clim_end=2008    #--- FOR CLIMATOLOGIES (COMMON PERIOD FOR O3 AND SIC/SST) 
    64 Y_pind_beg=1870 ; Y_pind_end=1899    #--- FOR SST/SIC PRE-INDUSTRIAL CLIMATOLOGY 
    65 # NB: CLIMATOLOGIES ARE PRODUCED FOR EACH FILE CONTAINING "-clim" SUFFIX FOUND IN THE FOLDER TREE, IN PARTICULAR FOR OZONE. 
    66  
    67 #--- RECOMPUTE FILES THAT ARE ALREADY PRESENT (y/n) 
    68 recomp='n' 
    69 do_amip='n' 
    70 do_ozon='y' 
    71  
    72 if [ "$do_amip" = "y" ]; then 
    73   amip_version_name=${AMIP_VERSION_NAMES[$ia]}; amip_version_folder=${AMIP_VERSION_FOLDERS[$ia]} 
    74   echo ">> Chosen SST files:   $(V=${amip_vname_in[0]}; amip_version_name=$amip_version_name; amip_version_folder=$amip_version_folder; eval echo $AMIP_FOLDER_IN)" 
    75   echo ">> Chosen SIC files:   $(V=${amip_vname_in[1]}; amip_version_name=$amip_version_name; amip_version_folder=$amip_version_folder; eval echo $AMIP_FOLDER_IN)" 
    76 fi 
    77 if [ "$do_ozon" = "y" ]; then 
    78   ozon_version_name=${OZON_VERSION_NAMES[$io]}; ozon_version_folder=${OZON_VERSION_FOLDERS[$io]} 
    79   echo ">> Chosen OZONE files: $(V=${ozon_vname_in};    ozon_version_name=$ozon_version_name; ozon_version_folder=$ozon_version_folder; eval echo $OZON_FOLDER_IN)" 
    80 fi 
     73#=== DETERMINE TIME INTERVALS AND DISPLAY WHAT DATASET ARE ABOUT TO BE TREATED 
     74for i in $(eval echo {0..$((${#DATATYPES[@]}-1))}); do 
     75  fin=${FILES_IN[$i]}; dt=${DATATYPES[$i]}; var=${VARNAM_IN[$i]%% *} 
     76  DATADATES[$i]=$(get_date $var $dt $DATAIN $fin); date=($DATADATES[$i]); fin=($fin) 
     77  if [ ${DOPROCESS[$i]} != 'y' ]; then continue; fi 
     78  echo ">> Chosen $dt:" 
     79  for ka in ${IXPROCESS[$i]}; do echo "  * ${fin[$ka]%/*} (${date[$ka]%-*})"; done 
     80done 
    8181 
    8282#================================================================================== 
    83 function addc { 
    84   local s 
    85   s="$1"; while [ ${#s} -lt $2 ]; do s="_$s"; done ; echo "$s"; unset s 
    86 } 
     83for i in $(eval echo {0..$((${#DATATYPES[@]}-1))}); do     #=== LOOP ON DATASETS 
    8784#================================================================================== 
    88 function progress_bar { 
    89 # $1: iteration  $2: iterations number  $3: length of the bar 
    90   local n bar 
    91   bar=`echo "scale=1 ; (100*$1)/$2" | bc` 
    92   bar="`addc "$bar" 5`% [" 
    93   n=`echo "scale=0 ; ($3*$1)/$2" | bc` 
    94   if [ $n -ne 0 ]; then bar="$bar"`eval "printf '='%.0s {1..$n}"`; fi 
    95   n=$(($3-$n)) 
    96   if [ $n -ne 0 ]; then bar="$bar"`eval "printf '.'%.0s {1..$n}"`']\r'; fi 
    97   echo -ne $bar; if [ $2 -eq $1 ]; then echo; fi 
    98   unset n bar 
    99 } 
    100 #================================================================================== 
    101 function extract { 
    102 # Input arguments: 
    103 #    $1: vin,vou       (input/output variable names) 
    104 #    $2: fin,fou       (input/output files names) 
    105 #    $3: Ymin,Ymax,ny  (min/max available years, years/file number) 
    106 #    $4: nmrg          (number of additional months before and after) 
    107 # 
    108 # Purpose: 
    109 #  * Extract year $Y for variable $v_in from files $f_in containing data from 
    110 #    $Ymin to $Ymax, where each input file contains $ny years (=0 for single file) 
    111 #  * Outputs are stored in file $fou, and variable renamed $vou. Special cases: 
    112 #  * Optionally, add before/after the 12 months the $nmrg previous/next months. 
    113 #    - Y=Ymin (missing Ymin-1) => $nmrg last  months of year $Y used instead. 
    114 #    - Y=Ymax (missing Ymax+1) => $nmrg first months of year $Y used instead. 
    115 # 
    116 # Remark:  First record of first file has to be january $Yb. 
    117 # 
    118   local v_in v_ou d_in f_ou Ymin Ymax Yi Yf Y ny nbm nmrg it fin fou fm ff fp ib ie 
    119   v_in="${1%%,*}"; v_ou="${1#*,}"; shift 
    120   f_in="${1%%,*}"; f_ou="${1#*,}"; shift 
    121   Ymin="${1%%,*}"; Ymax="${1#*,}"; ny="${Ymax#*,}"; Ymax="${Ymax%%,*}"; shift 
    122   Y=$1; shift; if [ $# -eq 1 ]; then nmrg=$1; else nmrg=0; fi 
    123   ff=actu_${v_in}_${Y}.nc; fm=""; fp="" 
    124   #--- INITIAL AND FINAL YEAR OF FILE CONTAINING CURRENT YEAR 
    125   if [ $ny -eq 0 ]; then 
    126     Yi=$Ymin; Yf=$Ymax 
    127   else 
    128     Yi=$((ny*(Y/ny))); Yf=$((Yi+ny-1)); Yf=$(($Yf>$Ymax?$Ymax:$Yf)) 
    129   fi 
    130   nbm=$((12*(Yf-Yi+1)-1))                           #--- MONTHS NUMBER OF CURRENT FILE 
    131   it=$((12*(Y-Yi)))                                 #--- JANUARY INDEX IN CURRENT FILE 
    132   fin=`V=$v_in; Yi=$Yi; Yf=$Yf; eval echo $f_in`    #--- CURRENT FILE NAME 
    133   fou=`V=$v_ou; Y=$Y; eval echo $f_ou`              #--- OUTPUT FILE NAME 
    134   ib=$(($it<=nmrg?0:$((it-nmrg))))                  #--- INDEX OF FIRST USED MONTH 
    135   ie=$(($((it+11+mrg))>=$nbm?$nbm:$((it+11+nmrg)))) #--- INDEX OF LAST USED MONTH 
    136   rm -f $ff ; rm -f $ff ; ncks -d time,$ib,$ie $fin $ff 
    137   #--- MISSING PREVIOUS YEAR LAST  MONTHS => CURRENT YEAR INSTEAD 
    138   if [ $it           -lt $nmrg ]; then fm="prev_${v_in}_${Y}.nc"; rm -f $fm 
    139     if [[ $Y -eq $Ymin || $ny -eq 0  ]]; then ncks -d time,$((it-nmrg+12)),11 $fin $fm 
    140     else Yi=$((ny*((Y-1)/ny))); Yf=$((Yi+ny-1)); Yf=$(($Yf>$Ymax?$Ymax:$Yf)) 
    141       ncks -d time,$((12*(Yf-Yi+1)-nmrg+it)),$((12*(Yf-Yi+1)-1)) `V=$v_in; Yi=$Yi; Yf=$Yf; eval echo $f_in` $fm 
    142     fi 
    143   fi 
    144   #--- MISSING FOLLOWING YEAR FIRST MONTHS => CURRENT YEAR INSTEAD 
    145   if [ $((it+nmrg+11)) -gt $nbm ]; then fp="next_${v_in}_${Y}.nc"; rm -f $fp 
    146     if [[ $Y -eq $Ymax || $ny -eq 0  ]]; then ncks -d time,$((nbm-11)),$((it+nmrg-1)) $fin $fp 
    147     else Yi=$((ny*((Y+1)/ny))); Yf=$((Yi+ny-1)); Yf=$(($Yf>$Ymax?$Ymax:$Yf)) 
    148       ncks -d time,0,$((nmrg-1)) `V=$v_in; Yi=$Yi; Yf=$Yf; eval echo $f_in` $fp 
    149     fi 
    150   fi 
    151   if [ ! -d ${fou%/*} ]; then mkdir -p ${fou%/*}; fi 
    152   if [[ "$fm" != "" || "$fp" != "" ]]; then 
    153     rm -f $fou ; ncrcat $fm $ff $fp $fou ; rm -f $fm $ff $fp 
    154   else 
    155     mv $ff $fou 
    156   fi 
    157   if [ "$v_in" != "$v_ou" ]; then 
    158     ncrename -v $v_in,$v_ou $fou > /dev/null 2>&1 
    159   fi 
    160 } 
    161 #================================================================================== 
    162 function make_clim { 
    163 # Input arguments: 
    164 #    $1: variable name   $2: input files name 
    165 #    $3: beg,end years   $4: index for january (optional, starting from 0) 
    166 # 
    167 # Purpose: 
    168 #    Create a 12 months climatology for a variable using 1-year monthly files. 
    169 # 
    170 # Remark: 
    171 #    Files with additional months are usable if the index for january is given (default: 0) 
    172 # 
    173   local f_ou f0 Yb Ye ib ie M MM 
    174   Yb="${3%%,*}"; Ye="${3#*,}" 
    175   if [ $# -eq 4 ]; then ib=$4; else ib=0; fi; ie=$((ib+11))  #=== INDEX $ib FOR JANUARY 
    176   for M in $(eval echo {$ib..$ie}); do 
    177     MM=$((M+1-ib)); if [ $MM -le 9 ]; then MM=0$MM; fi 
    178     for Y in `eval echo {${Yb}..${Ye}}`; do 
    179       f_ou=$(V=$1; Y=$Y; eval echo $2) 
    180       rm -f ${1}_$Y$MM.nc ; ncks -d time,$M $(V=$1; Y=$Y; eval echo $f_ou) ${1}_$Y$MM.nc 
     85  if [ ${DOPROCESS[$i]} != 'y' ]; then continue; fi 
     86     dt=${DATATYPES[$i]}                                   #=== SSTAndSeaIce, ozone... 
     87    fin=(${FILES_IN[$i]}); vin=(${VARNAM_IN[$i]})          #=== INPUT FILE/VAR NAMES (each version) 
     88     fou=${FILES_OU[$i]} ; vou=(${VARNAM_OU[$i]})          #=== OUTPUT FILE/VAR NAMES (single pair) 
     89  date=(${DATADATES[$i]})                                  #=== ${Yb}-${Ye}-${dY} 
     90   vfo=(${VARNAMFOU[$i]})                                  #=== VARNAME, FOR FILE NAMING 
     91    cli=(${CLIMS_IN[$i]})                                  #=== SINGLE FILE CLIMATO 
     92  #================================================================================ 
     93  for is in ${IXPROCESS[$i]}; do                           #=== LOOP ON VERSIONS 
     94  #================================================================================ 
     95    parse_dir ${fin[$is]} $DIRSTRUCT                       #--- Parse input directory 
     96    gridin=$grid; if [ $dataset = "PCMDI-AMIP-1-1-0" ]; then gridin='gs1x1'; fi 
     97    dso=$dataset                                           #--- For naming purpose 
     98    case $dt in                                            #--- NAMING EXCEPTIONS 
     99      ozone) if [ "$dataset" =    "PCMDI-AMIP-1-0" ]; then dso='AMIP'; fi ;; 
     100      SSTAndSeaIce) dso=${dso#*hist-}; dso=hist-${dso%%-*} 
     101             if [ "$dataset" = "UReading-CCMI-1-0" ]; then dso="historical"; fi ;; 
     102    esac 
     103    #============================================================================== 
     104    for iv in $(eval echo {0..$((${#vin[@]}-1))}); do      #--- Loop on variables 
     105    #============================================================================== 
     106      da=${date[$is]}; Yb=${da%%-*}; Ye=${da#*-}; dY=${Ye##*-}; Ye=${Ye%-*} 
     107      v_in=${vin[$iv]}; v_ou=${vou[$iv]}                   #--- Variables names 
     108      f_in=$(var=$v_in; Yb=\${Yb}; Ye=\${Ye}; datatype=$dt; grid=$gridin; eval echo $DATAIN/${fin[$is]}) 
     109      axis=$(get_grid $(Yb=$Yb; Ye=????; eval echo $f_in) $v_in) 
     110      res=${axis#*=}; axis=${axis%=*} 
     111      doZon='n'; if [ $axis = 'XYZT' ]; then doZon='y'; fi #--- Zonal mean ?  For 3D files 
     112      if [ $doZon = 'y' ]; then                            #--- 2 directories for 3D files 
     113        res3D=${res%x*}; res2D=${res3D#*x}_zonal; f_ou=${f_ou%/*}/\\\${res}/${f_ou##*/} 
     114      fi 
     115      f_ou=$(var=${vfo[$iv]}; Y=\${Y}; dataset=$dso; eval echo $DATAOU/${fou}) 
     116      echo ">> BUILDING 12 MONTHS ${v_in} FILES FOR THE $Yb-$Ye PERIOD USING $dataset DATASET ($version)..." 
     117      #============================================================================ 
     118      for Y in $(eval echo {$Yb..$Ye}); do                 #--- Loop on years 
     119      #============================================================================ 
     120        fO=$(Y=$Y; res=${res3D}; eval echo ${f_ou})        #--- Original resolution 
     121        if [[ ! -f $fO || $recomp = 'y' ]]; then 
     122          extract $v_in,$v_ou $f_in,$fO $Yb,$Ye,$dY $Y 0   #--- Field extraction 
     123          rename_dims $fO $coords                          #--- Rename dims/atts 
     124        fi 
     125        if [ $doZon = 'y' ]; then                          #--- Zonal mean 
     126          fZ=$(Y=$Y; res=${res2D}; eval echo ${f_ou})      #--- Zonal File name 
     127          if [[ ! -f $fZ || $recomp = 'y' ]]; then zonal_mean $fO $fZ; fi 
     128        fi 
     129        progress_bar $((Y-Yb+1)) $((Ye-Yb+1)) 50           #--- Check progress 
     130      #============================================================================ 
     131      done 
     132      #============================================================================ 
     133      for per in ${CLIMDATES[$i]}; do                      #--- Loop on periods 
     134      #============================================================================ 
     135        Yb=${per%%-*}; Ye=${per##*-}; Yc=${Yb}_${Ye}_clim; c='n'; cli0=${cli[$is]} 
     136 
     137        #=== Is there a single file climatology available ? 
     138        fC0=$(var=$v_in; Yb=$Yb; Ye=$Yb; datatype=$dt; eval echo $DATAIN/${cli0}) 
     139        if [[ ${cli0} != "/" && -f $fC0 ]]; then c='y'; Yc=${Yb}_${Yb}_clim; fi 
     140 
     141        fO=$(Y=$Yc; res=${res3D}; eval echo ${f_ou})       #--- Original resolution 
     142        if [[ ! -f $fO || $recomp = 'y' ]]; then 
     143          if [ "$c" = "y" ]; then 
     144            echo ">> BUILDING $v_ou CLIMATOLOGY USING SPECIAL FILE ${fC0##*/} ($version)..." 
     145            cp $fC0 $fO ; rename_dims $fO $coords          #--- Rename dims/atts 
     146          else 
     147            echo ">> BUILDING $v_ou CLIMATOLOGY USING YEARLY FILES FOR $per ($version)..." 
     148            make_clim ${v_ou} $f_ou $per 0                 #--- Compute climatology 
     149          fi 
     150        fi 
     151        if [ $doZon = 'y' ]; then 
     152          fZ=$(Y=$Y; res=${res2D}; eval echo ${f_ou})      #--- Zonal File name 
     153          if [[ ! -f $fZ || $recomp = 'y' ]]; then zonal_mean $fO $fZ; fi 
     154        fi 
     155      done 
     156      #============================================================================ 
    181157    done 
    182     rm -f ${1}_$MM.nc ; ncra ${1}_????$MM.nc ${1}_$MM.nc ; rm -f ${1}_????$MM.nc 
    183     progress_bar $((M+1-ib)) $((ie-ib+1)) 50 
     158    #============================================================================== 
    184159  done 
    185   f_ou=$(V=$1; Y=${Yb}_${Ye}_clim; eval echo $2); f_ou=$(V=$1; Y=${Yb}_${Ye}_clim; eval echo $f_ou) 
    186   rm -f $f_ou ; ncrcat ${1}_??.nc $f_ou ; rm -f ${1}_??.nc 
    187   f0=`V=$1; Y='YYYY'; eval echo $2`; f0=$(V=$1; Y='YYYY'; eval echo $f0); f0=${f0##*/} 
    188   ncatted -a comment,global,a,c,"\nClimatology from $Yb to $Ye using file(s) $f0" $f_ou 
    189   echo ">> Done for variable $1." 
    190 } 
    191 #================================================================================== 
    192 function str2ascii() { 
    193   LC_CTYPE=C printf '%d' "'$1" 
    194 } 
    195  
    196 #================================================================================== 
    197 function deblank { 
    198 #--- REMOVE UNEXPECTED CHARACTERS (IN PARTICULAR THOSE WEIRD ASCII(0) CHARS) 
    199   local a 
    200   a=$1; while [ `str2ascii ${a:0:1}` -lt 32 ]; do a=${a:1:${#a}}; done; echo $a 
    201 } 
    202  
    203 #================================================================================== 
    204 function dim_nam { 
    205   #--- GET DIMENSION $2 (X/Y/Z/T) NAME FOR FILE $1 
    206   #--- SPECIAL CASE: RETURN 'Seasons' IF "$2" = "S". 
    207   local a v Units 
    208   case $2 in 
    209     X) Units='degrees_east  degree_east  degree_e degrees_e "degrees east"  "degree east"  degreee degreese' ;; 
    210     Y) Units='degrees_north degree_north degree_n degrees_n "degrees north" "degree north" degreen degreesn' ;; 
    211     Z) Units='pa pascal pascals hpa millibar millibars mbar mbars mb bar bars atm atms atmosphere atmospheres' ;; 
    212     T) Units=' since' ;; 
    213     S) echo Seasons; return 0 ;; 
    214     *) echo "error in dim_nam: unrecognized axis $2"; return 1 ;; 
    215   esac 
    216   for u in $Units; do 
    217     case $2 in 
    218       X|Y|Z) a=`ncdump -h $1 | grep -i ':units = "'"$u"'"'` ;; 
    219       T)     a=`ncdump -h $1 | grep -i $u | grep ':units = "'` ;; 
    220     esac 
    221     if [ "$a" != "" ]; then 
    222       if [ "$2" = "Z" ]; then v=${a%%:*}; v=`deblank $v` 
    223         if [ "`ncdump -h $1 | grep -i "$v($v)"`" != "" ]; then break; fi 
    224       else break 
    225       fi 
    226     fi 
    227   done 
    228   if [[ "$a" = "" && "$2" = "Z" ]]; then a=`ncdump -h $1 | grep -i ':positive = "'`; fi 
    229   if [ "$a" = "" ]; then echo "error in dim_nam: unable to recognize axis $2"; return 1; fi 
    230   echo ${a%%:*}; return 0 
    231 } 
    232  
    233 #================================================================================== 
    234 function test_var { 
    235 # Input arguments: 
    236 #    $1: file name  $2: variable name 
    237   ncdump -h $1 | grep "double $2" > /dev/null; if [ $? -eq 0 ]; then return 0; fi 
    238   ncdump -h $1 | grep "float  $2" > /dev/null; return $? 
    239 } 
    240 #================================================================================== 
    241 function get_boundsvar { 
    242 # Input arguments: 
    243 #    $1: file name  $2: variable name 
    244   local a=$(ncdump -h $1 | grep -P "\t$2:bounds") 
    245   if [ "$a" != "${a#*\"}" ]; then a=${a#*\"}; echo ${a%\"*}; else echo ""; fi 
    246 } 
    247 #================================================================================== 
    248 function rename_bounds { 
    249 # Input arguments: 
    250 #    $1: file name  $2: variable name  $3: new variable name 
    251   local vin=$(get_boundsvar $1 $2) beg end vou 
    252   if [ "$vin" = "" ]; then return 1; fi 
    253   beg=${vin##*$2}; if [ "$beg" = "$vin" ]; then beg=$vin; fi 
    254   beg=${vin%%$2*}; if [ "$end" = "$vin" ]; then end=$vin; fi 
    255   if [[  "$beg" = ""  &&  "$end" = ""  ]]; then return 1; fi 
    256   vou=$beg$3$end 
    257   echo "$2,$vin,$vou" 
    258 } 
    259 #================================================================================== 
    260 function renam_dims { 
    261 # Input arguments: 
    262 #    $1: file name  $2...: <axis>:<target_name> (axis=X,Y,Z or T) 
    263 # 
    264 # Purpose: 
    265 #    Rename dimensions, dimensional variables. 
    266 #    In case of boundary grid variables with bounds_<variable> names are available, 
    267 #    they are renamed together with the corresponding "bounds" attributes. 
    268   local vio f=$1 out="|" nam_in nam_ou 
    269   shift 
    270   while [ $# -ge 1 ]; do 
    271     nam_ou=${1##*:}; nam_in=$(dim_nam $f ${1%:*}); shift 
    272     if [ "$nam_in" != "$nam_ou" ]; then 
    273       out="$out -d $nam_in,$nam_ou -v $nam_in,$nam_ou" 
    274       #--- CHECK WETHER "bounds" ATTRIBUTE IS THERE 
    275       vio="$(rename_bounds $f $nam_in $nam_ou)" 
    276       if [ "$vio" != "" ]; then out="-a bounds,${vio%%,*},m,c,${vio##*,} ${out} -v ${vio#*,}"; fi 
    277     fi 
    278     ((ia++)) 
    279   done 
    280   echo $out 
    281 } 
     160  #================================================================================ 
     161done 
    282162#================================================================================== 
    283163 
    284 tmp=tmp.$$.nc 
    285164 
    286 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    287 if [ "$do_amip" = "y" ]; then 
    288 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    289  
    290 #=== BUILD YEARLY INTERANNUAL SIC/SST FILES WITH 12 RECORDS 
    291 for is in $ia; do                                                            #--- Loop on Amip versions 
    292   amip_version_name=${AMIP_VERSION_NAMES[$is]} 
    293   amip_version_folder=${AMIP_VERSION_FOLDERS[$is]} 
    294   for suf in AVE BCS; do                                                     #--- Loop on var type 
    295     for ivar in $(eval echo {1..${#amip_vname_in[@]}}); do iv=$((ivar-1))    #--- Loop on variables 
    296  
    297       #--- VARIABLES NAMES (INPUT, OUTPUT AND FOR FILE NAMING PURPOSE) 
    298       v_in=${amip_vname_in[$iv]}; if [ "$suf" = "BCS" ]; then v_in=${v_in}bcs; fi 
    299       v_ou=${amip_vname_ou[$iv]}; if [ "$suf" = "BCS" ]; then v_ou=${v_ou}bcs; fi 
    300       var='sst';                  if [ $ivar  -eq  2  ]; then var='sic';       fi 
    301  
    302       #--- INPUT/OUTPUT FOLDER NAME 
    303       d_in=$(amip_version_name=$amip_version_name; amip_version_folder=$amip_version_folder; suf=$suf; V=$v_in; eval echo $AMIP_FOLDER_IN) 
    304       d_ou=$(amip_version_name=$amip_version_name; amip_version_folder=$amip_version_folder; suf=$suf;          eval echo $AMIP_FOLDER_OU) 
    305       if [ ! -d $d_ou ]; then mkdir -p $d_ou; fi 
    306  
    307       #--- FILES NAMES (INPUT, OUTPUT) 
    308       bc=""; if [ "$suf" = "BCS" ]; then bc=bc; fi 
    309       f_in=$d_in/$(V=$v_in;               eval echo ${amip_fname_in}) 
    310       f_ou=$d_ou/$(V=$var; Y=\$Y; bc=$bc; eval echo ${amip_fname_ou}) 
    311  
    312       echo ">> BUILDING 12 MONTHS $v_in FILES FOR THE ${Y_amip_beg}-${Y_amip_end} PERIOD USING $amip_version_name DATASET (${amip_version_folder##*/})..." 
    313       for Y in $(eval echo {${Y_amip_beg}..${Y_amip_end}}); do               #--- Loop on years 
    314  
    315         #--- RECOMPUTE OR NOT 
    316         if [[ -f $(Y=$Y; eval echo $f_ou) && $recomp = 'n' ]]; then continue; fi 
    317  
    318         #--- 2D FIELDS EXTRACTION 
    319         extract $v_in,$v_ou $AMIP_FOLDER_IN/$amip_fname_in,$f_ou $Y_amip_beg,$Y_amip_end,0 $Y 0 
    320  
    321         progress_bar $((Y-Y_amip_beg+1)) $((Y_amip_end-Y_amip_beg+1)) 50 
    322       done 
    323     done 
    324   done 
    325 done 
    326  
    327 #=== BUILD SIC/SST 12 RECORDS CLIMATOLOGIES 
    328 for is in $ia; do                                                            #--- Loop on Amip versions 
    329   amip_version_name=${AMIP_VERSION_NAMES[$is]} 
    330   amip_version_folder=${AMIP_VERSION_FOLDERS[$is]} 
    331   for suf in AVE BCS; do                                                     #--- Loop on var type 
    332     for ivar in $(eval echo {1..${#amip_vname_in[@]}}); do iv=$((ivar-1))    #--- Loop on variables 
    333  
    334       #--- VARIABLES (OUTPUT, FOR FILE NAMING PURPOSE) AND OUTPUT FILE NAMES 
    335       bc=""; if [ "$suf" = "BCS" ]; then bc=bc; fi 
    336       v_ou=${amip_vname_ou[$iv]}; if [ "$suf" = "BCS" ]; then v_ou=${v_ou}bcs; fi 
    337       var='sst';                  if [ $ivar  -eq  2  ]; then var='sic';       fi 
    338       d_ou=$(amip_version_name=$amip_version_name; amip_version_folder=$amip_version_folder; suf=$suf; eval echo $AMIP_FOLDER_OU) 
    339       f_ou=$(V=$var; Y=\$Y; bc=$bc; eval echo ${amip_fname_ou}) 
    340  
    341       for per in ${Y_clim_beg},${Y_clim_end} ${Y_pind_beg},${Y_pind_end}; do #--- Loop on periods 
    342         echo ">> BUILDING 12 MONTHS $v_ou CLIMATOLOGIES ON PERIOD ${per%%,*}-${per##*,} for $suf DATA..." 
    343  
    344         #--- RECOMPUTE OR NOT 
    345         Yint=${per%%,*}_${per##*,}_clim 
    346         if [[ -f $d_ou/$(Y=$Yint; eval echo $f_ou) && $recomp = 'n' ]]; then continue; fi 
    347  
    348         #--- COMPUTE CLIMATOLOGY 
    349         make_clim $v_ou $d_ou/$f_ou ${per} 0 
    350  
    351       done 
    352       cd $d_ou; ln -sf $(Y=2002; eval echo $f_ou) $(Y=2002_2002_clim; eval echo $f_ou); cd - 2>&1 > /dev/null 
    353     done 
    354   done 
    355 done 
    356 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    357 fi 
    358 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    359  
    360  
    361 coords="X:longitude Y:latitude Z:plev" 
    362  
    363 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    364 if [ "$do_ozon" = "y" ]; then 
    365 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    366  
    367 #=== BUILD YEARLY INTERANNUAL OZONE FILES WITH 12 RECORDS 
    368 for is in $io; do                                                 #--- Loop on Ozone versions 
    369   ozon_version_name=${OZON_VERSION_NAMES[$is]} 
    370   ozon_version_folder=${OZON_VERSION_FOLDERS[$is]} 
    371   d_ou=$(ozon_version_name=$ozon_version_name; ozon_version_folder=$ozon_version_folder; eval echo $OZON_FOLDER_OU)               #--- Output folder 
    372   if [ ! -d $d_ou ]; then mkdir -p $d_ou; fi 
    373   v_ou=${ozon_vname_ou}                                           #--- Output variable 
    374   v_in=${ozon_vname_in}                                           #--- Input  variable 
    375   f_in=$(V=$v_in; Yi=\${Yi}; Yf=\${Yf}; ozon_version_name=$ozon_version_name; ozon_version_folder=$ozon_version_folder; eval echo $OZON_FOLDER_IN/${ozon_fname_in[$is]}) 
    376   f_ou=$(V=$v_ou; Y=\$Y; eval echo $ozon_fname_ou) 
    377  
    378   nm=12 
    379   d_o2=$d_ou/${res2D}                                             #--- Output folder (2D) 
    380   d_o3=$d_ou/${res3D}                                             #--- Output folder (3D) 
    381   if [ ! -d $d_o2 ]; then mkdir -p $d_o2; fi; f_o2=$d_o2/$f_ou 
    382   if [ ! -d $d_o3 ]; then mkdir -p $d_o3; fi; f_o3=$d_o3/$f_ou 
    383  
    384   echo ">> BUILDING $nm MONTHS OZONE FILES FOR PERIOD ${Y_ozon_beg}-${Y_ozon_end} USING $OZON_VERSION_NAMES DATASET (${ozon_version_folder##*/})..." 
    385   for Y in $(eval echo {${Y_ozon_beg}..${Y_ozon_end}}); do       #--- Loop on years 
    386   
    387     #--- RECOMPUTE OR NOT 
    388     if [[ -f $(Y=$Y; eval echo $f_o3) && $recomp = 'n' ]]; then continue; fi 
    389  
    390     #--- 3D FIELDS EXTRACTION 
    391     extract $v_in,$v_ou $f_in,$f_o3 $Y_ozon_beg,$Y_ozon_end,50 $Y 0 
    392  
    393     #--- RENAME DIMS/VARS ACCORDING TO $coords VARIABLE + "bounds" ATTRIBUTE IF REQUIRED 
    394     fo3=$(Y=$Y; eval echo $f_o3); fo2=$(Y=$Y; eval echo $f_o2) 
    395     if [ $Y -eq $Y_ozon_beg ]; then 
    396       args_all=$(renam_dims $fo3 $coords) 
    397       args_att=${args_all%|*}; args_ren=${args_all#*|} 
    398     fi 
    399     if [ "$args_att" != "" ]; then ncatted  $args_att $fo3 2>&1 > /dev/null ; fi 
    400     if [ "$args_ren" != "" ]; then ncrename $args_ren $fo3 2>&1 > /dev/null ; fi 
    401  
    402     #--- ZONAL MEAN 
    403     ncwa    -a longitude $fo3 $tmp ; mv $tmp $fo2 
    404     ncks -x -v longitude $fo2 $tmp ; mv $tmp $fo2 
    405  
    406     #--- REMOVE USELESS VARIABLE 
    407     v="bounds_longitude"; test_var $fo2 $v 
    408     if [ $? -eq 0 ]; then ncks -x -v $v $fo2 $tmp ; mv $tmp $fo2; fi 
    409  
    410     progress_bar $((Y-Y_ozon_beg+1)) $((Y_ozon_end-Y_ozon_beg+1)) 50 
    411  
    412   done 
    413 done 
    414  
    415 #=== BUILD OZONE 12 RECORDS CLIMATOLOGIES 
    416 for is in $io; do                                                 #--- Loop on Ozone versions 
    417   ozon_version_name=${OZON_VERSION_NAMES[$is]} 
    418   ozon_version_folder=${OZON_VERSION_FOLDERS[$is]} 
    419   v_ou=${ozon_vname_ou}                                           #--- Output variable 
    420  
    421   for res in ${res3D} ${res2D}; do                                #--- zonal/3D loop 
    422     dr=$d_ou/$res 
    423  
    424     #--- CLIMATOLOGY ON GIVEN YEARS INTERVAL (Y_clim_beg-Y_clim_end) 
    425     f_ou=$(V=$v_ou; Y=${Y_clim_beg}_${Y_clim_end}_clim; eval echo $ozon_fname_ou) #--- Regular climatology name 
    426     if [[ ! -f $dr/$f_ou || $recomp = 'y' ]]; then 
    427       echo ">> BUILDING 12 MONTHS $res $v_ou CLIMATOLOGY ON PERIOD ${Y_clim_beg}-${Y_clim_end}..." 
    428       make_clim $v_ou $dr/\$ozon_fname_ou ${Y_clim_beg},${Y_clim_end} 0 
    429     fi 
    430  
    431     #--- PRE-INDUSTRIAL CLIMATOLOGY USING SPECIAL FILE 
    432     ozon_version_folder=$(eval echo ${OZON_VERSION_FOLDERS_CLIM[$is]}) 
    433  
    434     f_in=$(V=$v_in; Yi=1850; Yf=1850; ozon_version_name=$ozon_version_name; ozon_version_folder=$ozon_version_folder; clim='C'; eval echo $OZON_FOLDER_IN/$ozon_fname_in); #f_in=${f_in%.nc*}-clim.nc 
    435     f_ou=$(V=$v_ou; Y=1850_1850_clim; eval echo $ozon_fname_ou) 
    436     if [[ ! -f $dr/$f_ou || $recomp = 'y' ]]; then 
    437       echo ">> BUILDING 12 MONTHS $res $v_ou PRE-INDUSTRIAL CLIMATOLOGY..." 
    438       if [ "$res" = "${res3D}" ]; then f_ou0=$dr/$f_ou 
    439  
    440         #--- DUPLICATE FILE AND RENAME DIMENSIONS 
    441         cp $f_in $dr/$f_ou 
    442         args_all=$(renam_dims $dr/$f_ou $coords) 
    443         args_att=${args_all%|*}; args_ren=" -v $ozon_vname_in,$ozon_vname_ou ${args_all#*|}" 
    444         if [ "$args_att" != "" ]; then ncatted  $args_att $dr/$f_ou 2>&1 > /dev/null ; fi 
    445         if [ "$args_ren" != "" ]; then ncrename $args_ren $dr/$f_ou 2>&1 > /dev/null ; fi 
    446       else 
    447         #--- ZONAL MEAN 
    448         ncwa    -a longitude     $f_ou0 $tmp ; mv $tmp $dr/$f_ou 
    449         ncks -x -v longitude $dr/$f_ou  $tmp ; mv $tmp $dr/$f_ou 
    450  
    451         #--- REMOVE USELESS VARIABLE 
    452         v="bounds_longitude" 
    453         test_var $dr/$f_ou $v 
    454         if [ $? -eq 0 ]; then ncks -x -v $v $dr/$f_ou $tmp ; mv $tmp $dr/$f_ou; fi 
    455       fi 
    456     fi 
    457   done 
    458 done 
    459  
    460 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    461 fi 
    462 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    463  
    464  
Note: See TracChangeset for help on using the changeset viewer.