source: TOOLS/MOSAIX/CreateWeightsMask.bash

Last change on this file was 6666, checked in by omamce, 6 months ago

O.M. : MOSAIX

Improved code with pylint analysis

  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 47.5 KB
RevLine 
[5158]1#!/bin/bash
[3719]2#MSUB -r WeightsMask        # Job name
3#MSUB -o Out_WeightsMask    # Standard output
4#MSUB -e Out_WeightsMask    # Error output
[6666]5#MSUB -n 4                  # Number of processors
6#MSUB -T 1800              # Time limit (seconds)
7#MSUB -Q test
[6360]8#MSUB -q rome
[6045]9#MSUB -p devcmip6
10#MSUB -m work,store,scratch
[3620]11
12### ===========================================================================
13###
[3719]14### Creates interpolation weights between ORCA and atmosphere grids.
[3620]15### Interpolates ORCA mask to atmosphere grid.
16### Weight files are at OASIS-MCT format.
17###
18### Atmosphere grid may be lon/lat LMDZ or DYNAMICO icosahedron
[3627]19###
20### Documentation : https://forge.ipsl.jussieu.fr/igcmg/wiki/IPSLCM6/MOSAIX
[3620]21### ===========================================================================
22##
[6666]23##  Warning, to install, configure, run, use any of Olivier Marti's
24##  software or to read the associated documentation you'll need at least
25##  one (1) brain in a reasonably working order. Lack of this implement
26##  will void any warranties (either express or implied).
27##  O. Marti assumes no responsability for errors, omissions,
28##  data loss, or any other consequences caused directly or indirectly by
29##  the usage of his software by incorrectly or partially configured
30##  personal.
[3620]31##
[3623]32## SVN information
[3665]33#  $Author$
34#  $Date$
35#  $Revision$
36#  $Id$
[3632]37#  $HeadURL$
[4090]38#
[6093]39## Tested with :
[6666]40# CplModel=eORCA1.4.2xICO60 ; ccc_msub -r ${CplModel} -o Out_${CplModel} -e Out_${CplModel} CreateWeightsMask.bash
[4317]41# CplModel=ORCA2.3xLMD9695
42# CplModel=ORCA2.3xICO30
43# CplModel=ORCA2.3xICO40
44# CplModel=eORCA1.2xICO40
[4320]45# CplModel=eORCA1.2xICO450
[4317]46# CplModel=eORCA1.2xLMD256256
[4320]47# CplModel=eORCA025.1xLMD144142
[4317]48# CplModel=eORCA025.1xLMD256256
[6666]49# CplModel=eORCA1.4.2xICO40
50# CplModel=eORCA1.4.2xICO60
51# ccc_msub -r ${CplModel} -o Out_${CplModel} -e Out_${CplModel} CreateWeightsMask.bash
[4195]52
[6666]53echo 'Test'
54ls -alt /ccc/work/cont003/gencmip6/p86mart/TMP/MOSAIX
55
[4172]56set +vx
[6093]57# Prefixes for pretty print in color
[3901]58export Bold=$(tput bold) 
59export Unde=$(tput smul) ; export OffUnde=$(tput rmul)
60export Stou=$(tput smso) ; export OffStou=$(tput rmso)
61export Reve=$(tput rev ) 
62couleurs=( "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" )
[4259]63for i in $(seq 0 7) ; do eval "export ${couleurs[$i]}=$(tput setf ${i})" ; done
[4186]64export Norm=$(tput sgr0)
[4172]65export Titre=${Bold}${Blue}
[3620]66
67##
68## Configuration
69## ===========================================================================
[4159]70set -e
[5158]71echo BASH Version ${BASH_VERSION}
72echo SHELL ${SHELL}
[3620]73
74#
[4317]75echo ${Titre}"Defines model"${Norm}
76# =================================
[6666]77CplModel=ORCA2.3xLMD9695
[6066]78#CplModel=ORCA2.3xICO30
[6105]79#CplModel=ORCA2.3xICO40
[6107]80#CplModel=eORCA1.2xLMD144142
[6105]81#CplModel=eORCA1.4.0xLMD144142
82#CplModel=eORCA1.4.2xLMD144142
[4317]83#CplModel=eORCA1.2xLMD256256
84#CplModel=eORCA1.2xICO40
[6666]85#CplModel=eORCA1.2xICO60
[6190]86#CplModel=eORCA1.4.2xICO40
[4317]87#CplModel=eORCA1.2xICO450
88#CplModel=eORCA025.1xLMD256256
[3620]89
[4402]90#Version="v0" ; Comment="Fully tested in IPSLCM6 eORCA1.2 x LMD 144x142"
[6045]91#Version="v1" ; Comment="Fully tested in IPSLCM6 eORCA1.2 x LMD 144x142"
[6064]92#Version="NoSearchRadius" ; Comment="For testing new routing"
[6360]93Version="v3" ; Comment="Correction of ORCA masks to have a perfect conservation of run-off"
[3620]94
[4317]95# If available, get model name from job name
[4195]96if [[ X${SLURM_JOB_NAME} = X*ORC* ||  X${SLURM_JOB_NAME} = X*LMD*  ||  X${SLURM_JOB_NAME} = X*ICO* ]] ; then
[4317]97    CplModel=${SLURM_JOB_NAME} ;
[4195]98fi
[4317]99OCE=${CplModel//x*} ; ATM=${CplModel##*x}
[4195]100
[6093]101ModelName="IPSL CM6"
102[[ ${ATM} = ICO*      ]] && ModelName="IPSL CM7"
[4172]103
[6093]104echo ${Titre}"ATM model  : ${ATM}"      ${Norm}
105echo ${Titre}"OCE model  : ${OCE}"      ${Norm}
106echo ${Titre}"Cpl model  : ${CplModel}" ${Norm}
107echo ${Titre}"Version    : ${Version}"  ${Norm}
108echo ${Titre}"Model Name : ${ModelName}"${Norm}
109
[4186]110# Runoff parameter. atmCoastWidth and oceCoastWidth in grid points, searchRadius in km
[4402]111atmCoastWidth=2 ; oceCoastWidth=2 ; searchRadius=550.0
[4298]112runOff_atmQuantity=Quantity ; runOff_oceQuantity=Surfacic
[4172]113
[4402]114# Specific cases
[4298]115[[ ${ATM} = ICO*      ]] && atmCoastWidth=0 # Parameter relevant for LMD rectilinear grid only
116[[ ${ATM} = LMD*      ]] && atmCoastWidth=2
[5548]117[[ ${OCE} = ORCA2.*   ]] && oceCoastWidth=1
118[[ ${OCE} = eORCA1.*  ]] && oceCoastWidth=2
[4298]119[[ ${OCE} = eORCA025* ]] && oceCoastWidth=1
[4186]120
[4402]121# More specific cases
[6105]122[[ ${CplModel} = eORCA1.*xLMD144142 ]] && { atmCoastWidth=2 ; oceCoastWidth=2 ; searchRadius=550.0 ; }
[4402]123
[6105]124if [[ ${CplModel} = eORCA1.*xLMD144142 && Version = "NoSearchRadius" ]] ; then
[6045]125    atmCoastWidth=2 ; oceCoastWidth=2 ; searchRadius=0.0
126fi
127
[4153]128# Default values, used to create ocean fraction on atmospheric grid
[4186]129DefaultValues=( Direction=o2a,oceGrid=t,atmGrid=t,Order=1st,Quantity=false,Renormalize=false,useArea=false,maskSrc=true,maskDst=false,Name=OceFrac )
[3620]130
[4172]131## List of weights to build
132## ====================================================================================================================================
133#
134# Each item in CommandList describes the properties of interpolation weights to generate.
[6093]135# White spaces separate analysis.
[6107]136#
[6093]137# \!/ No spaces in any analysis \!/
[4159]138#
[6666]139# Specific commands : 'Runoff', 'Calving', 'Grids'
[4172]140#
[4159]141# Keywords :
[4298]142#   Direction   : o2a for ocean to atmosphere, a2o for atmosphere to ocean.
143#   Order       : 1st or 2nd.
[6093]144#   Quantity    : true if integrated quantity over a grid box (i.e runoff on atmosphere side),
145#                 false for flux (quantity / m^2) or intensive value (temperature, salinity, sea-ice fraction, ...).
[4298]146#   Renormalize : used when source grid is masked, to use values on non masked points only.
147#   oceGrid     : t, u or v point for NEMO C grid.
148#   atmGrid     : up to know, only t grid used in the atmosphere.
149#   useArea     : if true area from the model metrics is used. If false, areas are computed by XIOS from grid corners.
150#   maskSrc     : true to use the source grid mask, false to used all grid points.
151#   maskDst     : true to use the destination grid mask, false to use all grid points.
[4159]152#
[4259]153## Classic cases for IPSLCM6
154AtmOceFluxes="   Direction=a2o,Order=1st,Quantity=false,Renormalize=false,atmGrid=t,oceGrid=t,useArea=true,maskSrc=true,maskDst=true,Name=HeatWaterFluxes" # Heat and water fluxes
[4320]155OceAtmTemp="     Direction=o2a,Order=1st,Quantity=false,Renormalize=true,oceGrid=t,atmGrid=t,useArea=false,maskSrc=true,maskDst=true,Name=TempIceAlb"      # Temperature, sea-ice fraction, albedo
156OceAtmTemp2nd="  Direction=o2a,Order=2nd,Quantity=false,Renormalize=true,oceGrid=t,atmGrid=t,useArea=false,maskSrc=true,maskDst=true,Name=TempIceAlb"      # Temperature, sea-ice fraction, albedo
157AtmOceStressU="  Direction=a2o,Order=2nd,Quantity=false,Renormalize=false,atmGrid=t,oceGrid=u,useArea=true,maskSrc=true,maskDst=true,Name=WindStress"      # Wind stress to NEMO U point
158AtmOceStressV="  Direction=a2o,Order=2nd,Quantity=false,Renormalize=false,atmGrid=t,oceGrid=v,useArea=true,maskSrc=true,maskDst=true,Name=WindStress"      # Wind stress to NEMO V point
159AtmOceQuantity=" Direction=a2o,Order=1st,Quantity=true,Renormalize=false,atmGrid=t,oceGrid=t,useArea=false,maskSrc=true,maskDst=true,Name=Quantity"        # e.g. runoff
[4298]160# For new parameterization
161AtmOceTemp="     Direction=o2a,Order=1st,Quantity=false,Renormalize=true,oceGrid=t,atmGrid=t,useArea=false,maskSrc=true,maskDst=true,Name=OceTemp" # e.g. T and S correction from DWL parametrization
[4259]162OceAtmFluxes="   Direction=o2a,Order=1st,Quantity=false,Renormalize=false,atmGrid=t,oceGrid=t,useArea=true,maskSrc=true,maskDst=true,Name=OceAtmFluxes" # e.g. CO2 fluxes
[4298]163# Test to do with 2nd order
164AtmOceFluxes2nd="Direction=a2o,Order=2nd,Quantity=false,Renormalize=false,atmGrid=t,oceGrid=t,useArea=true,maskSrc=true,maskDst=true,Name=HeatWaterFluxes" # Heat and water fluxes
[4259]165OceAtmFluxes2nd="Direction=o2a,Order=2nd,Quantity=false,Renormalize=false,atmGrid=t,oceGrid=t,useArea=true,maskSrc=true,maskDst=true,Name=OceAtmFluxes" # e.g. CO2 fluxes
[4317]166AtmOceTemp2nd="  Direction=o2a,Order=2nd,Quantity=false,Renormalize=true,oceGrid=t,atmGrid=t,useArea=false,maskSrc=true,maskDst=true,Name=OceTemp" # e.g. T and S correction from DWL parametrization
[4159]167
[4259]168## Creates the list
169
[4186]170# Standard list for IPSLCM6
171#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} ${AtmOceStressU} ${AtmOceStressV} ${OceAtmFluxes} Runoff Calving Grids )
[4172]172
[4259]173# More comprehensive list for IPSLCM6 with new features
[4317]174#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} ${AtmOceStressU} ${AtmOceStressV} ${OceAtmFluxes} ${AtmOceTemp} Runoff Calving Grids )
[4172]175
[4259]176# With 2nd order
[4317]177CommandList=( ${AtmOceFluxes} ${AtmOceFluxes2nd} ${OceAtmTemp} ${OceAtmTemp2nd} ${AtmOceStressU} ${AtmOceStressV} ${OceAtmFluxes} ${OceAtmFluxes2nd} ${AtmOceTemp} ${AtmOceTemp2nd} Runoff Calving Grids )
[4259]178
[4186]179# Debugs
180#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} Runoff Calving Grids )
[4199]181#CommandList=( ${AtmOceFluxes} Runoff Calving Grids )
[4317]182#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} )
[4320]183#CommandList=( ${AtmOceFluxes} )
[4259]184#CommandList=( Runoff )
185#CommandList=( Runoff Calving Grids )
[4186]186#CommandList=( Calving )
187#CommandList=( Grids )
[4172]188
[6666]189if [[ ${Version} = test_runoff_* ]] ; then
190    CommandList=( Runoff )
191fi
192
[3620]193## ===========================================================================
194##
195## You should not change anything below this line ....
196##
197## ===========================================================================
[6064]198#Tag="MOSAIX"
[3620]199SUBMIT_DIR=$(pwd)
[4259]200FMT_XIOS=netcdf4
[4159]201
202# Functions to handle command parameters
203# ======================================
[6105]204function tolow { echo ${*} | tr '[:upper:]' '[:lower:]' ; }
205function toup  { echo ${*} | tr '[:lower:]' '[:upper:]' ; }
206function capit { echo $(toup ${1:0:1})$(tolow ${1:1})   ; }
207function Capit { echo $(toup ${1:0:1})${1:1}            ; }
208
[6093]209function readCommand {
[4153]210    # Decipher the command line to set bash variables
211    local l_Debug="no" l_Element
212    while [[ ${1} = -* ]] ; do
213        case ${1} in
214            ( -- ) shift ; break ;;
215            ( -d | --debug ) l_Debug="true" ; shift ;;
216        esac
217    done
218    local l_Command=${1}
[4159]219    for l_Element in $(echo ${l_Command} | tr "," "\n" ) ; do
[4153]220        [[ "X${l_Debug}" = "Xtrue" ]] && echo ${l_Element}
221        eval export ${l_Element}
222    done
223}
[3620]224
[4153]225function setValues {
[6093]226    ## Read a command line to define shell variables
[4317]227    # Set default values
[6093]228    readCommand "Direction=None,Order=None,Quantity=None,Renormalize=None,atmGrid=None,oceGrid=None,useArea=None,maskSrc=None,maskDst=None"
[4317]229    # Read command line
[6093]230    readCommand ${1}
[4186]231    #
[6105]232    #oceGrid=${oceGrid,,} ; atmGrid=${atmGrid,,} # Model names to lower case
233    #OCEGRID=${oceGrid^^} ; ATMGRID=${atmGrid^^} # Model names to upper case
234    oceGrid=$(tolow ${oceGrid}) ; atmGrid=$(tolow ${atmGrid})
235    OCEGRID=$(toup  ${oceGrid}) ; ATMGRID=$(toup  ${atmGrid})
[4153]236
237    case ${Order} in
238        ( 1st ) numOrder=1 ;;
239        ( 2nd ) numOrder=2 ;;
240    esac
241    case ${Renormalize} in
242        ( true )  NormName=Normalized   ;;
243        ( false ) NormName=UnNormalized ;;
244    esac
245    case ${Quantity} in
246        ( true )  QuantName=Integrated ;;
247        ( false ) QuantName=Surfacic   ;;
248    esac
249    case ${useArea} in
250        ( true )  AreaName=Area   ;;
251        ( false ) AreaName=NoArea ;;
252    esac
253
[4186]254    if [[ "${Name}" != "None" ]] ; then
[4259]255        FullName=${Name}_${Order}Order
[4186]256    else
[4199]257        FullName=${Order}Order_${NormName}_${QuantName}_${AreaName}
[4186]258    fi
259
[4153]260    case ${Direction} in
261        ( o2a )
[4159]262        src=${oce} ; SRC=${OCE} ; srcGrid=${oceGrid} ; srcDomainType=${oceDomainType} ; SRCGRID=${OCEGRID} ; srcArea=area_grid_${OCEGRID}
263        dst=${atm} ; DST=${ATM} ; dstGrid=${atmGrid} ; dstDomainType=${atmDomainType} ; DSTGRID=${ATMGRID} ; dstArea=aire
[4153]264        ;;
265        ( a2o )
[4159]266        src=${atm} ; SRC=${ATM} ; srcGrid=${atmGrid} ; srcDomainType=${atmDomainType} ; SRCGRID=${ATMGRID} ; srcArea=aire
267        dst=${oce} ; DST=${OCE} ; dstGrid=${oceGrid} ; dstDomainType=${oceDomainType} ; DSTGRID=${OCEGRID} ; dstArea=area_grid_${OCEGRID}
[4153]268        ;;
269    esac
[4186]270    echo ${Green}"${SRC} ${SRCGRID} toward ${DST} ${DSTGRID} - ${Order} Order - Normalize: ${Renormalize} - Quantity: ${QuantName} - Area: ${AreaName}  "${Norm}
[4199]271    echo ${Green}"FullName : ${FullName}"${Norm}
[4153]272}
[4186]273
[3620]274#
[6045]275# Defines computer
[3620]276# ================
[3901]277if [[ $(hostname) = irene*    ]] ; then arch=irene ; center=tgcc ; fi
[4153]278if [[ $(hostname) = lsce*     ]] ; then arch=spip  ; center=spip ; fi
[3734]279
[3620]280case ${arch} in
[4153]281    ( irene )
[3620]282    set +vx
[4259]283    set +e
[3966]284    R_IN=$(ccc_home -u igcmg --cccwork)/IGCM
[6666]285    TMPDIR=${CCCWORKDIR}/TMP/MOSAIX
[3966]286    SUBMIT_DIR=${BRIDGE_MSUB_PWD:-${SUBMIT_DIR}}
[4172]287    MpiRun="time ccc_mprun"
[4195]288    PyRun="time ccc_mprun -n 1" # Needed to force python to run on one process only
[6045]289    module purge
[4153]290    source ${SUBMIT_DIR}/arch.env
[6666]291    module load nco
[4172]292    module load python3
[4153]293    module load datadir/igcmg
[3966]294    module list
[4259]295    set -e
[3966]296    ;;
[3734]297    ( spip )
298    R_IN=${HOME}/Scratch/IGCM
299    TMPDIR=${HOME}/Scratch/TMP
300    SUBMIT_DIR=$(pwd)
[6107]301    MpiRun="/opt/local/bin/mpirun -n 1"
[4172]302    PyRun="time"
[3734]303    ;;
[3901]304    ( * ) exit -1 ;;
[3620]305esac
306
307set -x ; set -e
[6666]308ls -alt ${TMPDIR}
[4195]309mkdir -p ${TMPDIR}/${CplModel} || exit 1
[6045]310cd       ${TMPDIR}/${CplModel} || exit 2
[4146]311rm -fr *
[3620]312
313#
314# Suffixes
315# ---------------------------------------------------------------------------
316
317case ${OCE} in
[4153]318    ( *ORC* )         oce=orc ; oceDomainType=curvilinear   ;;
[3620]319esac
[4159]320
[3620]321case ${ATM} in
[4153]322    ( *dynamico*    ) atm=ico ; atmDomainType=unstructured  ;;
323    ( *ICO*         ) atm=ico ; atmDomainType=unstructured  ;;
324    ( *lmd* | *LMD* ) atm=lmd ; atmDomainType=rectilinear   ;;
[3620]325esac
326
[4159]327case ${OCE} in # Periodicity type of ORCA grid
[6105]328    ( ORCA2.4              ) OcePerio=4.2 ;; # 4 ORCA 6 PALEORCA
[6107]329    ( ORCA2*               ) OcePerio=4   ;; # 4 ORCA 6 PALEORCA
[6105]330    ( eORCA1.2             ) OcePerio=6   ;;
331    ( eORCA1.4.0           ) OcePerio=6   ;;
332    ( eORCA1.4.2           ) OcePerio=6.2 ;;
333    ( ORCA1*               ) OcePerio=6   ;;
334    ( eORCA025.4           ) OcePerio=6.2 ;;
335    ( ORCA025* | eORCA025* ) OcePerio=6   ;;
[3723]336esac
[3620]337#
338
[4195]339cp ${SUBMIT_DIR}/bin/interpol.exe       .
340cp ${SUBMIT_DIR}/*.py                   .
[4153]341cp ${SUBMIT_DIR}/iodef_atm_to_oce.xml   .
342cp ${SUBMIT_DIR}/iodef_oce_to_atm.xml   .
[6666]343cp ${SUBMIT_DIR}/arch.env               .
[3620]344
[6107]345[[ -f ${R_IN}/OCE/NEMO/${OCE}/${OCE}_coordinates_mask.nc       ]] && cp ${R_IN}/OCE/NEMO/${OCE}/${OCE}_coordinates_mask.nc        .
346[[ -f ${R_IN}/OCE/NEMO/${OCE}/GRIDS/${OCE}_coordinates_mask.nc ]] && cp ${R_IN}/OCE/NEMO/${OCE}/GRIDS/${OCE}_coordinates_mask.nc  .
347if [[ ! -f ${OCE}_coordinates_mask.nc ]] ; then
348    exit 1
349fi
350
[3639]351cp ${R_IN}/ATM/GRID/${ATM}_grid.nc .
[3620]352
[4317]353# Soucis a corriger plus tard
354if [[ ${ATM} = LMD9695 ]] ; then
355    cat <<EOF > toDouble.nco
356lat                = double (lat) ;
357lon                = double (lon) ;
358aire               = double (aire) ;
359fract_oce          = double (fract_oce) ;
360fract_sic          = double (fract_sic) ;
361fract_oce_plus_sic = double (fract_oce_plus_sic) ;
362EOF
363    ncap2 --history --overwrite --script-file toDouble.nco ${ATM}_grid.nc tmp_${ATM}_grid.nc
364    mv tmp_${ATM}_grid.nc ${ATM}_grid.nc
365fi
[3620]366
[3901]367##
[4172]368echo ${Titre}"NEMO T point towards ATM - 1st order"${Norm}
[3901]369## ===========================================================================
[4153]370echo "Command parameters : ${Command}"
[4186]371setValues ${DefaultValues}
[3620]372
[4320]373OutFileName=${srcGrid}${src}_to_${dstGrid}${dst}_${FullName}
374
[6105]375cp iodef_oce_to_atm.xml iodef.xml
[4153]376
[6091]377cat << EOF > command_file_${OutFileName}.txt
[6093]378-n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]'     --key name=maskutil_T
379-n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="area_src"]'     --key name=area_grid_T
380-n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                     --key type=${srcDomainType}
381-n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                     --key type=${dstDomainType}
382-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key order=1
383-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key quantity=false
384-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key renormalize=false
385-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key use_area=false
386-n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]'   --key name=maskutil_T
387-n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]'   --key name=area_grid_T
388-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]'                                 --key name=dia_${OutFileName}
389-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]'         --text "${SRC} mask interpolated to ${DST}"
390-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]'   --text ${srcDomainType}
391-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'     --text ${dstDomainType}
392-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'         --text 1
393-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                      --key type=${srcDomainType}
394-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                      --key type=${dstDomainType}
395-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key weight_filename=rmp_${OutFileName}.nc
396-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key order=1
397-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' --text false
398-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="quantity"]'      --text false
399-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="use_area"]'      --text false
400-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key renormalize=false
401-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key quantity=false
402-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key use_area=false
403-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="Model"]'         --text "${ModelName}"
[6091]404EOF
[3620]405
[6091]406python3 update_xml.py -i iodef.xml -c command_file_${OutFileName}.txt
[4320]407cp iodef.xml iodef_${OutFileName}.xml
[6091]408
[3901]409ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
410ln -fs ${ATM}_grid.nc              atm_grid.nc
[3620]411
[4172]412${MpiRun} ./interpol.exe --mask_src=${maskSrc} --mask_dst=${maskDst} --use_area=${useArea}
413
[6093]414for File in rmp_${OutFileName}.nc dia_${OutFileName}.nc ; do
415    ncatted --history \
416            --attribute map_method,global,o,c,"Conservative Remapping - 1st order"  \
417            --attribute normalization,global,o,c,"false"  \
418            --attribute Quantity,global,o,c,"false"  \
419            --attribute UseArea,global,o,c,"false"  rmp_${OutFileName}.nc
420done
[3620]421##
[4172]422echo ${Titre}"Correct spurious values (extremes)"${Norm}
[3620]423## ===========================================================================
424cat <<EOF > correction_masque.nco
[6093]425where (OceFrac < 0.00001) OceFrac=OceFrac.get_miss() ;
426where (OceFrac > 0.99999) OceFrac=1.0 ;
[3620]427OceFrac.delete_miss() ;
428// Fill masked values to land values
[6093]429where (OceFrac >  1.0) OceFrac=0.0 ;
430where (OceFrac <  0.0) OceFrac=0.0 ;
[4298]431OceMask = OceFrac ;
[3620]432EOF
[4199]433ncap2 --history --overwrite --script-file correction_masque.nco dia_t${oce}_to_t${atm}_${FullName}.nc tmp_dia_t${oce}_to_t${atm}_${FullName}.nc ; mv tmp_dia_t${oce}_to_t${atm}_${FullName}.nc dia_t${oce}_to_t${atm}_${FullName}.nc
[6093]434ncatted --history -a missing_value,OceFrac,d,,"" -a _FillValue,OceFrac,d,,"" -a missing_value,OceMask,d,,"" -a _FillValue,OceMask,d,,"" dia_t${oce}_to_t${atm}_${FullName}.nc
[4298]435 
[3620]436##
[4298]437echo ${Titre}"Creates mask on ATM grid"${Norm}
[3620]438## ===========================================================================
[4199]439cp dia_t${oce}_to_t${atm}_${FullName}.nc  dia_t${oce}_to_t${atm}_${FullName}_mask.nc
[6360]440ncks --no_abc --history --overwrite --variable OceFrac dia_t${oce}_to_t${atm}_${FullName}_mask.nc ${ATM}_grid_maskFrom_${OCE}.nc
441ncatted --history --attribute ,global,d,c, ${ATM}_grid_maskFrom_${OCE}.nc
[3620]442
443cat <<EOF > creation_masque.nco
[4298]444Oce2AtmMask = OceMask ;
[6093]445where (OceMask >  0.0) Oce2AtmMask=1 ;
446where (OceMask <= 0.0) Oce2AtmMask=0 ;
[3620]447EOF
[4081]448
[4298]449ncap2 --history --overwrite --script-file creation_masque.nco dia_t${oce}_to_t${atm}_${FullName}.nc tmp_dia_${ATM}_grid_maskFrom_${OCE}.nc
450ncks --overwrite --history --variable OceMask,OceFrac,Oce2AtmMask tmp_dia_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
[3620]451
[4081]452if [[ $(ncdump -h ${ATM}_grid_maskFrom_${OCE}.nc | grep domain_dst | wc -l) -gt 0 ]] ; then
[4317]453    echo "Change dimension names, and some attributes accordingly"
454    ## ===========================================================
[4081]455    case ${atm} in
456        ( *ico* ) 
[6093]457        ncrename --history \
458                 --dimension cell_domain_dst,cell \
459                 --dimension nvertex_domain_dst,nvertex \
460                 --variable lat_domain_dst,lat               --variable lon_domain_dst,lon  \
461                 --variable bounds_lat_domain_dst,bounds_lat --variable bounds_lon_domain_dst,bounds_lon ${ATM}_grid_maskFrom_${OCE}.nc
462        ncatted  --history --attribute bounds,lat,m,c,"bounds_lat" --attribute bounds,lon,m,c,"bounds_lon" ${ATM}_grid_maskFrom_${OCE}.nc
[4081]463        ;;
[4317]464        ( *lmd* )
[6093]465        ncrename --history \
466                 --variable  lon_domain_dst,lon --variable  lat_domain_dst,lat \
467                 --dimension lon_domain_dst,lon --dimension lat_domain_dst,lat ${ATM}_grid_maskFrom_${OCE}.nc
[4081]468        ;;
469    esac
[6093]470   
471    ncatted  --history \
472             --attribute coordinates,OceFrac,m,c,"lat lon"\
473             --attribute coordinates,OceMask,m,c,"lat lon" \
474             --attribute coordinates,Oce2AtmMask,m,c,"lat lon" \
475             --attribute long_name,OceFrac,c,c,"fraction of ocean in a grid cell" \
476             --attribute units,OceFrac,c,c,"[0,1]" \
477             --attribute long_name,OceMask,c,c,"fraction of ocean in a grid cell" \
478             --attribute units,OceMask,c,c,"[0,1]" \
479             --attribute long_name,Oce2AtmMask,c,c,"land mask" \
480             --attribute units,Oce2AtmMask,c,c,"Land:0, Ocean:1" ${ATM}_grid_maskFrom_${OCE}.nc
[4081]481fi
482
[6360]483ncks --history --no_abc --append --variable aire atm_grid.nc ${ATM}_grid_maskFrom_${OCE}.nc
[4317]484ncatted  --history --attribute units,aire,m,c,"m^2" ${ATM}_grid_maskFrom_${OCE}.nc
485
[6360]486[[ ${atm} = *ico* ]] && ncks --no_abc --history --append --variable bounds_lon,bounds_lat atm_grid.nc ${ATM}_grid_maskFrom_${OCE}.nc
[4081]487
[4298]488##
[4199]489#echo ${Titre}"Creates mask of coastal OCE points"${Norm}
[3901]490## ===========================================================================
[4199]491#${PyRun} python3 -u ComputeNemoCoast.py -n ${OcePerio} -i ${OCE}_coordinates_mask.nc # Creates OceCoastal
[4172]492
[3901]493##
[4199]494#echo ${Titre}"Creates mask of coastal ATM points"${Norm}
[3901]495## ===========================================================================
[4199]496#cat <<EOF > coastal.nco
497#AtmCoastal = OceFrac * 0.0 ;
498#where (OceFrac > 0.0 && OceFrac < 1.0 )  AtmCoastal = 1.0 ;
499#EOF
500#ncap2 --history --overwrite --script-file coastal.nco ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_coastal_maskFrom_${OCE}.nc
501#ncks --history --append --variable AtmCoastal ${ATM}_coastal_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
502#rm ${ATM}_coastal_maskFrom_${OCE}.nc
[4081]503
[3901]504##
[4172]505echo ${Titre}"Other weights files"${Norm}
[3718]506## ===========================================================================
[4153]507# Loop on commands
508for Command in ${CommandList[@]}
509do
510    echo "Command parameters : ${Command}"
[4186]511    setValues ${Command}
[4172]512    if [[ ${Command} = "Runoff"  ]] ; then okRunoff=yes  ; continue ; fi
513    if [[ ${Command} = "Calving" ]] ; then okCalving=yes ; continue ; fi
[4186]514    if [[ ${Command} = "Grids"   ]] ; then okGrids=yes   ; continue ; fi
[4172]515   
[6093]516    ln -fs ${OCE}_coordinates_mask.nc     oce_grid.nc
[4298]517    ln -fs ${ATM}_grid_maskFrom_${OCE}.nc atm_grid.nc
[6091]518
519    OutFileName=${srcGrid}${src}_to_${dstGrid}${dst}_${FullName}
[4153]520   
521    case ${Direction} in
522        ( o2a )
523        cp iodef_oce_to_atm.xml iodef.xml
[6091]524        cat << EOF > command_file_${OutFileName}.txt
[6093]525-n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]'   --key name=maskutil_${DSTGRID}
526-n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]'   --key name=Oce2AtmMask
527-n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' --key name=maskutil_${SRCGRID}
528-n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' --key name=area_grid_${SRCGRID}
529-n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]'   --key name=Oce2AtmMask
530-n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="area_dest"]'   --key name=aire
[6091]531EOF
[4153]532        ;;
533        ( a2o )
534        cp iodef_atm_to_oce.xml iodef.xml
[6091]535        cat << EOF > command_file_${OutFileName}.txt
[6093]536-n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]'   --key name=Oce2AtmMask
537-n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]'   --key name=mask_${DSTGRID}
538-n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' --key name=Oce2AtmMask
539-n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' --key name=aire
540-n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]'   --key name=mask_${DSTGRID}
541-n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="area_dest"]'   --key name=area_grid_${DSTGRID}
[6091]542EOF
[4153]543        ;;
544    esac
[4320]545
[6091]546    cat << EOF >> command_file_${OutFileName}.txt
[6093]547-n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                     --key type=${dstDomainType}
548-n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                     --key type=${srcDomainType}
549-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                      --key type=${srcDomainType}
550-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                      --key type=${dstDomainType}
551-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key weight_filename=rmp_${OutFileName}.nc
552-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key order=${numOrder}
553-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key quantity=${Quantity}
554-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key renormalize=${Renormalize}
555-n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   --key use_area=${useArea}
556-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]'                                 --key name=dia_${OutFileName}
557-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]'         --text "${SRC} mask interpolated to ${DST}"
558-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'     --text ${dstDomainType}
559-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]'   --text ${srcDomainType}
560-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' --text ${Renormalize}
561-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'         --text ${numOrder}   
562-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="quantity"]'      --text ${Quantity}
563-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="use_area"]'      --text ${useArea}
564-n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="Model"]'         --text "${ModelName}"
[6091]565EOF
566
567    python3 update_xml.py -i iodef.xml -c command_file_${OutFileName}.txt
568    cp iodef.xml iodef_${OutFileName}.xml
[4153]569   
[4172]570    ${MpiRun} ./interpol.exe --mask_src=${maskSrc} --mask_dst=${maskDst} --use_area=${useArea}
[4320]571
572    case ${numOrder} in
[6093]573        ( 1 ) Text="Conservative Remapping - 1st order" ;;
574        ( 2 ) Text="Conservative Remapping - 2nd order" ;;
[4320]575    esac
[6093]576    for File in rmp_${OutFileName}.nc   dia_${OutFileName}.nc ; do
577        ncatted --history \
578                --attribute map_method,global,o,c,"${Text}" \
579                --attribute normalization,global,o,c,"${Renormalize}" \
580                --attribute Quantity,global,o,c,"${Quantity}" \
581                --attribute UseArea,global,o,c,"${useArea}" ${File}
582    done
[3719]583done
[4153]584
[3620]585##
[4172]586echo ${Titre}"Add some metadata in file headers"${Norm}
[3620]587## ===========================================================================
588
[6093]589NCO="$(ncks --version |& tail -1 | sed 's/ncks //')"
[4172]590PYTHON_VER=$( python3 -c "import sys ; print (sys.version.split(' ')[0])" )
[4199]591for InFile in $(ls *${oce}_to_*${atm}_*.nc *${atm}_to_*${oce}_*.nc ${ATM}_grid_maskFrom_${OCE}.nc 2> /dev/null) ; do
[3639]592    ncatted --history \
[3620]593            --attribute LongName,global,d,,                                       \
594            --attribute nco_openmp_thread_number,global,d,,                       \
595            --attribute Conventions,global,o,c,"CF-1.6"                           \
596            --attribute source,global,o,c,"IPSL Earth system model"               \
[6093]597            --attribute model,global,o,c,"${ModelName}"                           \
[3620]598            --attribute group,global,o,c,"ICMC IPSL Climate Modelling Center"     \
599            --attribute Institution,global,o,c,"IPSL https://www.ipsl.fr"         \
600            --attribute Ocean,global,o,c,"${OCE} https://www.nemo-ocean.eu"       \
601            --attribute Atmosphere,global,o,c,"${ATM} http://lmdz.lmd.jussieu.fr" \
602            --attribute originalFiles,global,o,c,"${OCE}_coordinates_mask.nc ${ATM}_grid_mask.nc"      \
[4195]603            --attribute associatedFiles,global,o,c,"grids_${CplModel}.nc areas_${CplModel}.nc masks_${CplModel}.nc" \
[3620]604            --attribute directory,global,o,c,"$(pwd)"                             \
605            --attribute description,global,o,c,"Fields needed by OASIS-MCT"       \
606            --attribute title,global,o,c,"${InFile}.nc"                           \
[4320]607            --attribute Program,global,o,c,"Generated by CreateWeightsMask"       \
[3620]608            --attribute timeStamp,global,o,c,"$(date)"                            \
609            --attribute HOSTNAME,global,o,c,"$(hostname)"                         \
610            --attribute LOGNAME,global,o,c,"$(whoami)"                            \
611            --attribute NCO,global,o,c,"NCO netCDF Operator ${NCO} http://nco.sourceforge.net" \
[6093]612            --attribute Python,global,o,c,"Python3 version ${PYTHON_VER}"         \
613            --attribute machine,global,o,c,"$(uname -a)"                          \
[3620]614            --attribute directory,global,o,c,"$(pwd)"                             \
[3624]615            --attribute description,global,o,c,"Generated with XIOS http://forge.ipsl.jussieu.fr/ioserver and MOSAIX https://forge.ipsl.jussieu.fr/igcmg/browser/TOOLS/MOSAIX" \
[4090]616            --attribute SVN_Author,global,o,c,'$Author$'                 \
[6093]617            --attribute SVN_Date,global,o,c,'$Date$' \
[4090]618            --attribute SVN_Revision,global,o,c,'$Revision$'               \
[6093]619            --attribute SVN_Id,global,o,c,'$Id$' \
[3620]620            ${InFile}
621done
[4320]622
623# Duplicate with no global attribute to avoid erasing attributes in other files
624cp ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_noglobal.nc
[6360]625ncatted --history --attribute ,global,d,c, ${ATM}_grid_maskFrom_${OCE}_noglobal.nc
[4320]626
[3620]627##
[4172]628echo ${Titre}"Update and complete weight files to fit OASIS requested format"${Norm}
[3620]629## ===========================================================================
630cat <<EOF > add_dim.nco
631defdim("num_wgts",1) ;
632weight[n_weight, num_wgts] = weight ;
633EOF
634
[3719]635for rmpFile in rmp_*.nc ; do
[4199]636    mv ${rmpFile} tmp_${rmpFile}
637    ncap2 --history --script-file add_dim.nco tmp_${rmpFile} ${rmpFile} ; rm tmp_${rmpFile}
[3620]638   
[6093]639    ncrename --history \
640             --dimension n_weight,num_links   \
641             --variable  src_idx,src_address  \
642             --variable  dst_idx,dst_address  \
643             --variable  weight,remap_matrix  ${rmpFile}
[4320]644 
[6093]645    ncatted --history --attribute conventions,global,o,c,"SCRIP" --attribute normalization,global,o,c,"none" ${rmpFile}
[3620]646   
[3719]647    case ${rmpFile} in
[3620]648        ( rmp_*${oce}_to_t${atm}_* )
[3639]649        ncatted --history \
[3620]650                --attribute title,global,o,c,"Weights ${OCE} to ${ATM}" \
[4153]651                --attribute source_grid,global,o,c,"${oceDomainType}" \
652                --attribute dest_grid,global,o,c,"${atmDomainType}"   \
[3719]653                ${rmpFile}     
[3620]654        ;;
655        ( rmp_*${atm}_to_*${oce}_* )
[3639]656        ncatted --history \
[3620]657                --attribute title,global,o,c,"Weights ${ATM} to ${OCE}" \
[4153]658                --attribute source_grid,global,o,c,"${atmDomainType}" \
659                --attribute dest_grid,global,o,c,"${oceDomainType}"   \
[3719]660                ${rmpFile}
[3620]661        ;;
662    esac
663done
664
665##
[4172]666echo ${Titre}"Add missing variables in rmp files"${Norm}
[3639]667## ===========================================================================
[4259]668for rmpFile in $(ls rmp_?${atm}_to_[tuv]${oce}_*.nc rmp_[tuv]${oce}_to_t${atm}_*.nc 2> /dev/null ) ; do
[3901]669    echo ${rmpFile}
[3719]670    a_to_o=false ; o_to_a=false
671    case ${rmpFile} in
672        (  rmp_?${oce}_to_?${atm}_*.nc ) o_to_a=true ;;
673        (  rmp_?${atm}_to_?${oce}_*.nc ) a_to_o=true ;;
674    esac
675
[3901]676    for Grid in t u v o c ; do # Identify grids
677        [[ ${rmpFile} = rmp_${Grid}${oce}_to_?${atm}_*.nc ]] && ogrid=${Grid}
678        [[ ${rmpFile} = rmp_?${oce}_to_${Grid}${atm}_*.nc ]] && agrid=${Grid}
679        [[ ${rmpFile} = rmp_${Grid}${atm}_to_?${oce}_*.nc ]] && agrid=${Grid}
680        [[ ${rmpFile} = rmp_?${atm}_to_${Grid}${oce}_*.nc ]] && ogrid=${Grid}
[3719]681    done
682    OGRID=${ogrid^}
683    AGRID=${agrid^}
[3639]684       
[3719]685    cat <<EOF >add_varoce.nco
[3639]686defdim ("src_grid_size"   , \$x_grid_${OGRID}.size*\$y_grid_${OGRID}.size) ;
687defdim ("src_grid_corners", 4) ;
688defdim ("src_grid_rank"   , 2) ;
689//
690src_grid_dims[src_grid_rank] = { \$y_grid_${OGRID}.size, \$x_grid_${OGRID}.size } ;
691//
692src_grid_center_lat [src_grid_size] =  0.0d ;
693src_grid_center_lon [src_grid_size] =  0.0d ;
694src_grid_center_lat (:) = nav_lat_grid_${OGRID}(:,:)   ;
695src_grid_center_lon (:) = nav_lon_grid_${OGRID}(:,:)   ;
696//
697src_grid_corner_lat [src_grid_size, src_grid_corners] = 0.0d ;
698src_grid_corner_lon [src_grid_size, src_grid_corners] = 0.0d ;
699src_grid_corner_lat(:,:) = bounds_lat_grid_${OGRID}(:,:,:) ;
700src_grid_corner_lon(:,:) = bounds_lon_grid_${OGRID}(:,:,:) ;
701//
702src_grid_imask [src_grid_size] =    0 ;
703src_grid_area  [src_grid_size] = 0.0d ;
704src_grid_frac  [src_grid_size] = 1.0d ;
705src_grid_imask (:) = 1 - mask_${OGRID}(:,:) ;
706src_grid_imask.int() ;
707src_grid_area  (:) = area_grid_${OGRID}(:,:) ;
708EOF
709
[4146]710    cp add_varoce.nco add_varoce_$(basename ${rmpFile} .nc)_o_to_a.nco
[4199]711    [[ ${o_to_a} = true ]] && ncap2 --history --append --script-file add_varoce_$(basename ${rmpFile} .nc)_o_to_a.nco ${OCE}_coordinates_mask.nc ${rmpFile}
[6045]712    sed -i~ -e "s/src_/dst_/g" add_varoce.nco
[4146]713    cp add_varoce.nco add_varoce_$(basename ${rmpFile} .nc)_a_to_o.nco
[4199]714    [[ ${a_to_o} = true ]] && ncap2 --history --append --script-file add_varoce_$(basename ${rmpFile} .nc)_a_to_o.nco ${OCE}_coordinates_mask.nc ${rmpFile}
[3719]715               
716    if [[ ${atm} = ico ]] ; then
717        cat <<EOF >add_varatm.nco
[3639]718defdim ("dst_grid_size"   , \$cell.size) ;
719defdim ("dst_grid_corners", 6) ;
720defdim ("dst_grid_rank"   , 2) ;
721//
722dst_grid_dims[dst_grid_rank] = { \$cell.size, 1 } ;
723//
724dst_grid_center_lat [dst_grid_size] =  0.0d ;
725dst_grid_center_lon [dst_grid_size] =  0.0d ;
726dst_grid_center_lat (:) = lat(:)   ;
727dst_grid_center_lon (:) = lon(:)   ;
728//
729dst_grid_corner_lat [dst_grid_size, dst_grid_corners] = 0.0d ;
730dst_grid_corner_lon [dst_grid_size, dst_grid_corners] = 0.0d ;
731dst_grid_corner_lat(:,:) = bounds_lat(:,:) ;
732dst_grid_corner_lon(:,:) = bounds_lon(:,:) ;
733//
734dst_grid_imask [dst_grid_size] =    0 ;
735dst_grid_area  [dst_grid_size] = 0.0d ;
736dst_grid_frac  [dst_grid_size] = 1.0d ;
[4298]737dst_grid_imask (:) = 1 - Oce2AtmMask(:) ;
[3639]738dst_grid_imask.int() ;
739dst_grid_area  (:) = aire(:) ;
[4259]740dst_grid_frac  (:) = OceFrac(:) ;
[3639]741EOF
[4146]742        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco
[3719]743        if [[ ${o_to_a} = true ]] ; then
[4320]744            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
[6360]745            ncks --no_abc --history --overwrite --variable src_address,dst_address,remap_matrix,src_grid_dims,src_grid_center_lat,src_grid_center_lon,src_grid_corner_lon,src_grid_corner_lat,src_grid_area,src_grid_imask,dst_grid_dims,dst_grid_center_lat,dst_grid_center_lon,dst_grid_corner_lon,dst_grid_corner_lat,dst_grid_area,dst_grid_imask ${rmpFile} rmp_tmp.nc
[3719]746            mv rmp_tmp.nc ${rmpFile}
747        fi
[3718]748                   
[6045]749        sed -i~ -e "s/dst_/src_/g" add_varatm.nco
[4146]750        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco
[3719]751        if [[ ${a_to_o} = true ]] ; then
[4320]752            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
[6360]753            ncks --no_abc --history --overwrite --variable src_address,dst_address,remap_matrix,src_grid_dims,src_grid_center_lat,src_grid_center_lon,src_grid_corner_lon,src_grid_corner_lat,src_grid_area,src_grid_imask,dst_grid_dims,dst_grid_center_lat,dst_grid_center_lon,dst_grid_corner_lon,dst_grid_corner_lat,dst_grid_area,dst_grid_imask ${rmpFile} rmp_tmp.nc
[3719]754            mv rmp_tmp.nc ${rmpFile}
755        fi
756    fi
[3966]757   
[3719]758    if [[ ${atm} = lmd ]] ; then
759        cat <<EOF >add_varatm.nco
[3639]760defdim ("dst_grid_size"   , \$lon.size*\$lat.size) ;
761defdim ("dst_grid_corners", 4) ;
762defdim ("dst_grid_rank"   , 2) ;
763//
764dst_grid_dims[dst_grid_rank] = { \$lat.size, \$lon.size } ;
765//
766dst_grid_center_lat [dst_grid_size] =  0.0d ;
767dst_grid_center_lon [dst_grid_size] =  0.0d ;
768lat0lon[lat,lon] = lat(:)+0*lon(:) ;
769lon0lat[lat,lon] = lon(:)+0*lat(:) ;
770dst_grid_center_lat (:) = lat0lon(:,:)   ;
771dst_grid_center_lon (:) = lon0lat(:,:)   ;
772//
773//dst_grid_corner_lat [dst_grid_size, dst_grid_corners] = 0.0d ; // Not available for LMDZ lon/lat grid
774//dst_grid_corner_lon [dst_grid_size, dst_grid_corners] = 0.0d ;
775//dst_grid_corner_lat(:,:) = bounds_lat(:,:) ;
776//dst_grid_corner_lon(:,:) = bounds_lon(:,:) ;
777//
778dst_grid_imask [dst_grid_size] =    0 ;
779dst_grid_area  [dst_grid_size] = 0.0d ;
780dst_grid_frac  [dst_grid_size] = 1.0d ;
[4298]781dst_grid_imask (:) = 1 - Oce2AtmMask(:,:) ;
[3639]782dst_grid_imask.int() ;
783dst_grid_area  (:) = aire(:,:) ;
[4259]784dst_grid_frac (:)  = OceFrac(:,:) ;
[3639]785EOF
[4146]786        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco
[3719]787        if [[ ${o_to_a} = true ]] ; then
[4320]788            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
[6360]789            ncks --no_abc --history --overwrite --variable src_address,dst_address,remap_matrix,src_grid_dims,src_grid_center_lat,src_grid_center_lon,src_grid_corner_lon,src_grid_corner_lat,src_grid_area,src_grid_imask,dst_grid_dims,dst_grid_center_lat,dst_grid_center_lon,dst_grid_area,dst_grid_imask ${rmpFile} rmp_tmp.nc
[3719]790            mv rmp_tmp.nc ${rmpFile}
791        fi
792       
[6045]793        sed -i~ -e "s/dst/src/g" add_varatm.nco
[4146]794        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco
[3719]795        if [[ ${a_to_o} = true ]] ; then
[4320]796            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
[6360]797            ncks --no_abc --history --overwrite --variable src_address,dst_address,remap_matrix,src_grid_dims,src_grid_center_lat,src_grid_center_lon,src_grid_area,src_grid_imask,dst_grid_dims,dst_grid_center_lat,dst_grid_center_lon,dst_grid_corner_lon,dst_grid_corner_lat,dst_grid_area,dst_grid_imask ${rmpFile} rmp_tmp.nc
[3719]798            mv rmp_tmp.nc ${rmpFile}
799        fi
800    fi
[3639]801done
[3901]802ls -al ${OCE}_coordinates_mask.nc
[3639]803##
[4186]804## ============================================================================
[4172]805echo ${Titre}"Creates and save auxiliary files for OASIS : grids.nc, areas.nc and masks.nc"${Norm}
[4186]806cp ${SUBMIT_DIR}/CreateOasisGrids.bash .
807bash CreateOasisGrids.bash --oce ${OCE} --atm ${ATM}
[4097]808
809
810##
[4172]811echo ${Titre}"Runoff weights"${Norm}
[4097]812## ===========================================================================
[4186]813if [[ "X${okRunoff}" = "Xyes" ]] ; then
[4195]814    ${PyRun} python3 -u RunoffWeights.py --oce=${OCE} --atm=${ATM} \
[4172]815              --atmCoastWidth=${atmCoastWidth} --oceCoastWidth=${oceCoastWidth} --searchRadius=${searchRadius} \
[4195]816              --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[4199]817              --o2a=${ATM}_grid_maskFrom_${OCE}.nc --output=rmp_t${atm}_to_t${oce}_runoff_${runOff_atmQuantity}_to_${runOff_oceQuantity}.nc \
818              --fmt=${FMT_XIOS} \
[6666]819              --atmQuantity=${runOff_atmQuantity} --oceQuantity=${runOff_oceQuantity} --ocePerio=${OcePerio} --modelName="${ModelName}"
[4172]820fi
[6066]821
[4097]822##
[4172]823echo ${Titre}"Calving weights"${Norm}
[3620]824## ===========================================================================
[4172]825if [[ "X${okCalving}" = "Xyes" ]] ; then
826    case ${OCE} in
[4298]827        ( eORCA025* )
[4186]828        cp /ccc/work/cont003/gencmip6/deshayej/eORCA_R025_runoff_v1.2.nc .
[4199]829        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_nosouth.nc  --fmt=${FMT_XIOS} \
[6066]830                 --oce=${OCE} --atm=${ATM} --type=nosouth   \
831                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]832                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[4199]833        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceberg.nc  --fmt=${FMT_XIOS} \
[6066]834                 --oce=${OCE} --atm=${ATM} --type=iceberg  --repartition_file=eORCA_R025_runoff_v1.2.nc --repartition_var=Icb_flux \
835                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]836                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[4199]837        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceshelf.nc --fmt=${FMT_XIOS} \
[6066]838                 --oce=${OCE} --atm=${ATM} --type=iceshelf --repartition_file=eORCA_R025_runoff_v1.2.nc --repartition_var=sornfisf \
839                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]840                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[4172]841        ;;
842       
[6360]843        ( eORCA1.2 | eORCA1.4 | eORCA1.4.2 )
844
845        case ${OCE} in
846            ( eORCA1.2 | eORCA1.4 ) 
847            cp ${R_IN}/OCE/NEMO/${OCE}/OPA/runoff-icb_DaiTrenberth_Depoorter_eORCA1_JD.nc . ;;
848            ( eORCA1.4.2 ) 
849            cp ${R_IN}/OCE/NEMO/${OCE}/OPA/eORCA1.4.2_runoff-icb_DaiTrenberth_Depoorter.nc runoff-icb_DaiTrenberth_Depoorter_eORCA1_JD.nc ;;
850        esac
[6105]851       
[6066]852        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_nosouth.nc  --fmt=${FMT_XIOS} \
853                 --oce=${OCE} --atm=${ATM} --type=nosouth   \
854                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]855                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[6066]856        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceberg.nc  --fmt=${FMT_XIOS} \
857                 --oce=${OCE} --atm=${ATM} --type=iceberg --repartition_file=runoff-icb_DaiTrenberth_Depoorter_eORCA1_JD.nc --repartition_var=Icb_flux \
858                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]859                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[6090]860        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceshelf.nc --fmt=${FMT_XIOS} \
[6066]861                 --oce=${OCE} --atm=${ATM} --type=iceshelf --repartition_file=runoff-icb_DaiTrenberth_Depoorter_eORCA1_JD.nc --repartition_var=sornfisf \
862                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]863                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[4172]864        ;;
865       
866        ( * )
[4199]867        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_full.nc --fmt=${FMT_XIOS} \
[6066]868                 --oce=${OCE} --atm=${ATM} --type=full --ocePerio=${OcePerio} \
869                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
[6666]870                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
[4172]871        ;;
872    esac
873fi
874
875##
[4199]876echo ${Titre}"Simplifies headers, add version and comment"${Norm}
[4172]877## ===========================================================================
[4199]878for File in $(ls dia_*.nc rmp_*.nc ${ATM}_grid_maskFrom_${OCE}*.nc areas_${OCE}x${ATM}*.nc grids_${OCE}x${ATM}*.nc masks_${OCE}x${ATM}*.nc 2> /dev/null ) ; do
[4037]879    ncatted --history --attribute history_of_appended_files,global,d,c,"" ${File}
[4199]880    ncatted --history --attribute history,global,d,c,"" ${File}
[4172]881    [[ "X${Comment}" != "X" ]] && ncatted --history --attribute Comment,global,o,c,"${Comment}"  ${File}
882    [[ "X${Version}" != "X" ]] && ncatted --history --attribute Version,global,o,c,"${Version}"  ${File}
[4199]883    [[ "X${Tag}" != "X"     ]] && ncatted --history --attribute Version,global,o,c,"${Tag}"      ${File}
884done
885
886##
887echo ${Titre}"Rename rmp and dia files"${Norm}
888## ===========================================================================
[4259]889for File in $(ls *${oce}*${atm}* *${atm}*${oce}*  2> /dev/null ); do
[3719]890    NewFile=$(echo ${File} | sed -e "s/${atm}/${ATM}/" -e "s/${oce}/${OCE}/" )
[4259]891    mv ${File} ${NewFile}
[3620]892done
893
[4199]894##
895echo ${Titre}"Add Tag"${Norm}
896## ===========================================================================
897if [[ "X${Tag}" != "X" ]] ; then
898    for File in $(ls dia_*.nc rmp_*.nc ${ATM}_grid_maskFrom_${OCE}*.nc areas_${OCE}x${ATM}*.nc grids_${OCE}x${ATM}*.nc masks_${OCE}x${ATM}*.nc 2> /dev/null ) ; do
899        mv ${File} $(basename ${File} .nc)_${Tag}.nc
900    done
[4186]901fi
[4199]902##
903echo ${Titre}"Add Version"${Norm}
904## ===========================================================================
905if [[ "X${Version}" != "X" ]] ; then
906    for File in $(ls dia_*.nc rmp_*.nc ${ATM}_grid_maskFrom_${OCE}*.nc areas_${OCE}x${ATM}*.nc grids_${OCE}x${ATM}*.nc masks_${OCE}x${ATM}*.nc 2> /dev/null ) ; do
907        mv ${File} $(basename ${File} .nc)_${Version}.nc
908    done
909fi
910   
911## ===========================================================================
912echo ${Titre}"Save results"${Norm}
913## ===========================================================================
[6190]914#for File in $(ls dia_*.nc rmp_*.nc ${ATM}_grid_maskFrom_${OCE}*.nc areas_${OCE}x${ATM}*.nc grids_${OCE}x${ATM}*.nc masks_${OCE}x${ATM}*.nc 2> /dev/null ) ; do
915#    cp ${File} ${SUBMIT_DIR}
916#done
[4172]917
[4199]918
[3620]919##
[4172]920echo ${Titre}"Creates a README.txt file"${Norm}
[4090]921## ===========================================================================
922[[ -f README.txt ]] && rm README.txt
[4298]923UUID=$(uuid)
[3620]924
[4090]925cat <<EOF > README.txt
926Files produced by CreateWeightsMask.bash and CreateOasisGrids.bash
927
928rmp_* are weights files
929dia_* are diagnostic files not needed for the coupler
[4195]930grids_${CplModel}.nc areas_${CplModel}.nc masks_${CplModel}.nc are auxiliary file needed by OASIS-MCT
[4090]931All files have the same uuid in the global attributes
932
[6093]933Description      : Weigths and auxiliary files for coupling ${OCE} and ${ATM} needed by OASIS-MCT
934Conventions      : CF-1.6
935Source           : IPSL Earth system model
936Model            : ${ModelName}
937Group            : ICMC IPSL Climate Modelling Center
938Institution      : IPSL https://www.ipsl.fr
939Ocean            : ${OCE} https://www.nemo-ocean.eu
940Atmosphere       : ${ATM} http://lmdz.lmd.jussieu.fr
941Production       : $(finger ${LOGNAME} | head -1 | awk '{print $4}')
942Original files   : ${OCE}_coordinates_mask.nc ${ATM}_grid_mask.nc
943Associated files : grids_${CplModel}.nc areas_${CplModel}.nc masks_${CplModel}.nc
944Description      : Generated with XIOS http://forge.ipsl.jussieu.fr/ioserver and MOSAIX https://forge.ipsl.jussieu.fr/igcmg/browser/TOOLS/MOSAIX"
945directory        : $(pwd)
946timeStamp        : $(date)
947Machine info     : $(uname -a)
948uuid             : ${UUID}
949HOSTNAME         : $(hostname)
950LOGNAME          : $(whoami)
951NCO              : NCO netCDF Operator ${NCO} http://nco.sourceforge.net
952Python version   : ${PYTHON_VER}
953
[6666]954SVN Information :
955$Author$
956$Date$
957$Revision$
958$Id$
959$HeadURL$
960
[4091]961EOF
[4090]962
963
[6666]964
[4172]965echo ${Titre}"Compute checksums and add them to README"${Norm}
[4199]966# ------------------------------------------------------------
[4091]967cat << EOF >> README.txt
[4090]968
[4159]969Files produced, with checksum produced by Unix command shasum (version $(shasum --version)) with default algorithm
[4090]970
[6093]971Checksum                                  File
972========================================================================================================
[4090]973EOF
974
[4199]975for file in $(ls dia_*.nc rmp_*.nc ${ATM}_grid_maskFrom_${OCE}*.nc areas_${OCE}x${ATM}*.nc grids_${OCE}x${ATM}*.nc masks_${OCE}x${ATM}*.nc 2> /dev/null ) ; do
[4320]976    ncatted --history --attribute uuid,global,o,c,"${UUID}" ${file}
[4090]977    echo "$(shasum ${file})" >> README.txt
978done
979
980cat <<EOF >> README.txt
981
982================= That's all folk's ! ========================
983EOF
984
[4259]985if [[ "X${Version}" != "X" ]] ; then
986    cp README.txt ${SUBMIT_DIR}/README_${CplModel}_MOSAIX_${Version}.txt
987else
988    cp README.txt ${SUBMIT_DIR}/README_${CplModel}_MOSAIX.txt
989fi
[3620]990## ===========================================================================
991##
[4172]992echo ${Titre}"     That's all folk's !!!    "${Norm}
[3620]993##
994## ===========================================================================
Note: See TracBrowser for help on using the repository browser.