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
Line 
1#!/bin/bash
2#MSUB -r WeightsMask        # Job name
3#MSUB -o Out_WeightsMask    # Standard output
4#MSUB -e Out_WeightsMask    # Error output
5#MSUB -n 4                  # Number of processors
6#MSUB -T 1800              # Time limit (seconds)
7#MSUB -Q test
8#MSUB -q rome
9#MSUB -p devcmip6
10#MSUB -m work,store,scratch
11
12### ===========================================================================
13###
14### Creates interpolation weights between ORCA and atmosphere grids.
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
19###
20### Documentation : https://forge.ipsl.jussieu.fr/igcmg/wiki/IPSLCM6/MOSAIX
21### ===========================================================================
22##
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.
31##
32## SVN information
33#  $Author$
34#  $Date$
35#  $Revision$
36#  $Id$
37#  $HeadURL$
38#
39## Tested with :
40# CplModel=eORCA1.4.2xICO60 ; ccc_msub -r ${CplModel} -o Out_${CplModel} -e Out_${CplModel} CreateWeightsMask.bash
41# CplModel=ORCA2.3xLMD9695
42# CplModel=ORCA2.3xICO30
43# CplModel=ORCA2.3xICO40
44# CplModel=eORCA1.2xICO40
45# CplModel=eORCA1.2xICO450
46# CplModel=eORCA1.2xLMD256256
47# CplModel=eORCA025.1xLMD144142
48# CplModel=eORCA025.1xLMD256256
49# CplModel=eORCA1.4.2xICO40
50# CplModel=eORCA1.4.2xICO60
51# ccc_msub -r ${CplModel} -o Out_${CplModel} -e Out_${CplModel} CreateWeightsMask.bash
52
53echo 'Test'
54ls -alt /ccc/work/cont003/gencmip6/p86mart/TMP/MOSAIX
55
56set +vx
57# Prefixes for pretty print in color
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" )
63for i in $(seq 0 7) ; do eval "export ${couleurs[$i]}=$(tput setf ${i})" ; done
64export Norm=$(tput sgr0)
65export Titre=${Bold}${Blue}
66
67##
68## Configuration
69## ===========================================================================
70set -e
71echo BASH Version ${BASH_VERSION}
72echo SHELL ${SHELL}
73
74#
75echo ${Titre}"Defines model"${Norm}
76# =================================
77CplModel=ORCA2.3xLMD9695
78#CplModel=ORCA2.3xICO30
79#CplModel=ORCA2.3xICO40
80#CplModel=eORCA1.2xLMD144142
81#CplModel=eORCA1.4.0xLMD144142
82#CplModel=eORCA1.4.2xLMD144142
83#CplModel=eORCA1.2xLMD256256
84#CplModel=eORCA1.2xICO40
85#CplModel=eORCA1.2xICO60
86#CplModel=eORCA1.4.2xICO40
87#CplModel=eORCA1.2xICO450
88#CplModel=eORCA025.1xLMD256256
89
90#Version="v0" ; Comment="Fully tested in IPSLCM6 eORCA1.2 x LMD 144x142"
91#Version="v1" ; Comment="Fully tested in IPSLCM6 eORCA1.2 x LMD 144x142"
92#Version="NoSearchRadius" ; Comment="For testing new routing"
93Version="v3" ; Comment="Correction of ORCA masks to have a perfect conservation of run-off"
94
95# If available, get model name from job name
96if [[ X${SLURM_JOB_NAME} = X*ORC* ||  X${SLURM_JOB_NAME} = X*LMD*  ||  X${SLURM_JOB_NAME} = X*ICO* ]] ; then
97    CplModel=${SLURM_JOB_NAME} ;
98fi
99OCE=${CplModel//x*} ; ATM=${CplModel##*x}
100
101ModelName="IPSL CM6"
102[[ ${ATM} = ICO*      ]] && ModelName="IPSL CM7"
103
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
110# Runoff parameter. atmCoastWidth and oceCoastWidth in grid points, searchRadius in km
111atmCoastWidth=2 ; oceCoastWidth=2 ; searchRadius=550.0
112runOff_atmQuantity=Quantity ; runOff_oceQuantity=Surfacic
113
114# Specific cases
115[[ ${ATM} = ICO*      ]] && atmCoastWidth=0 # Parameter relevant for LMD rectilinear grid only
116[[ ${ATM} = LMD*      ]] && atmCoastWidth=2
117[[ ${OCE} = ORCA2.*   ]] && oceCoastWidth=1
118[[ ${OCE} = eORCA1.*  ]] && oceCoastWidth=2
119[[ ${OCE} = eORCA025* ]] && oceCoastWidth=1
120
121# More specific cases
122[[ ${CplModel} = eORCA1.*xLMD144142 ]] && { atmCoastWidth=2 ; oceCoastWidth=2 ; searchRadius=550.0 ; }
123
124if [[ ${CplModel} = eORCA1.*xLMD144142 && Version = "NoSearchRadius" ]] ; then
125    atmCoastWidth=2 ; oceCoastWidth=2 ; searchRadius=0.0
126fi
127
128# Default values, used to create ocean fraction on atmospheric grid
129DefaultValues=( Direction=o2a,oceGrid=t,atmGrid=t,Order=1st,Quantity=false,Renormalize=false,useArea=false,maskSrc=true,maskDst=false,Name=OceFrac )
130
131## List of weights to build
132## ====================================================================================================================================
133#
134# Each item in CommandList describes the properties of interpolation weights to generate.
135# White spaces separate analysis.
136#
137# \!/ No spaces in any analysis \!/
138#
139# Specific commands : 'Runoff', 'Calving', 'Grids'
140#
141# Keywords :
142#   Direction   : o2a for ocean to atmosphere, a2o for atmosphere to ocean.
143#   Order       : 1st or 2nd.
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, ...).
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.
152#
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
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
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
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
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
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
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
167
168## Creates the list
169
170# Standard list for IPSLCM6
171#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} ${AtmOceStressU} ${AtmOceStressV} ${OceAtmFluxes} Runoff Calving Grids )
172
173# More comprehensive list for IPSLCM6 with new features
174#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} ${AtmOceStressU} ${AtmOceStressV} ${OceAtmFluxes} ${AtmOceTemp} Runoff Calving Grids )
175
176# With 2nd order
177CommandList=( ${AtmOceFluxes} ${AtmOceFluxes2nd} ${OceAtmTemp} ${OceAtmTemp2nd} ${AtmOceStressU} ${AtmOceStressV} ${OceAtmFluxes} ${OceAtmFluxes2nd} ${AtmOceTemp} ${AtmOceTemp2nd} Runoff Calving Grids )
178
179# Debugs
180#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} Runoff Calving Grids )
181#CommandList=( ${AtmOceFluxes} Runoff Calving Grids )
182#CommandList=( ${AtmOceFluxes} ${OceAtmTemp} )
183#CommandList=( ${AtmOceFluxes} )
184#CommandList=( Runoff )
185#CommandList=( Runoff Calving Grids )
186#CommandList=( Calving )
187#CommandList=( Grids )
188
189if [[ ${Version} = test_runoff_* ]] ; then
190    CommandList=( Runoff )
191fi
192
193## ===========================================================================
194##
195## You should not change anything below this line ....
196##
197## ===========================================================================
198#Tag="MOSAIX"
199SUBMIT_DIR=$(pwd)
200FMT_XIOS=netcdf4
201
202# Functions to handle command parameters
203# ======================================
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
209function readCommand {
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}
219    for l_Element in $(echo ${l_Command} | tr "," "\n" ) ; do
220        [[ "X${l_Debug}" = "Xtrue" ]] && echo ${l_Element}
221        eval export ${l_Element}
222    done
223}
224
225function setValues {
226    ## Read a command line to define shell variables
227    # Set default values
228    readCommand "Direction=None,Order=None,Quantity=None,Renormalize=None,atmGrid=None,oceGrid=None,useArea=None,maskSrc=None,maskDst=None"
229    # Read command line
230    readCommand ${1}
231    #
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})
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
254    if [[ "${Name}" != "None" ]] ; then
255        FullName=${Name}_${Order}Order
256    else
257        FullName=${Order}Order_${NormName}_${QuantName}_${AreaName}
258    fi
259
260    case ${Direction} in
261        ( o2a )
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
264        ;;
265        ( a2o )
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}
268        ;;
269    esac
270    echo ${Green}"${SRC} ${SRCGRID} toward ${DST} ${DSTGRID} - ${Order} Order - Normalize: ${Renormalize} - Quantity: ${QuantName} - Area: ${AreaName}  "${Norm}
271    echo ${Green}"FullName : ${FullName}"${Norm}
272}
273
274#
275# Defines computer
276# ================
277if [[ $(hostname) = irene*    ]] ; then arch=irene ; center=tgcc ; fi
278if [[ $(hostname) = lsce*     ]] ; then arch=spip  ; center=spip ; fi
279
280case ${arch} in
281    ( irene )
282    set +vx
283    set +e
284    R_IN=$(ccc_home -u igcmg --cccwork)/IGCM
285    TMPDIR=${CCCWORKDIR}/TMP/MOSAIX
286    SUBMIT_DIR=${BRIDGE_MSUB_PWD:-${SUBMIT_DIR}}
287    MpiRun="time ccc_mprun"
288    PyRun="time ccc_mprun -n 1" # Needed to force python to run on one process only
289    module purge
290    source ${SUBMIT_DIR}/arch.env
291    module load nco
292    module load python3
293    module load datadir/igcmg
294    module list
295    set -e
296    ;;
297    ( spip )
298    R_IN=${HOME}/Scratch/IGCM
299    TMPDIR=${HOME}/Scratch/TMP
300    SUBMIT_DIR=$(pwd)
301    MpiRun="/opt/local/bin/mpirun -n 1"
302    PyRun="time"
303    ;;
304    ( * ) exit -1 ;;
305esac
306
307set -x ; set -e
308ls -alt ${TMPDIR}
309mkdir -p ${TMPDIR}/${CplModel} || exit 1
310cd       ${TMPDIR}/${CplModel} || exit 2
311rm -fr *
312
313#
314# Suffixes
315# ---------------------------------------------------------------------------
316
317case ${OCE} in
318    ( *ORC* )         oce=orc ; oceDomainType=curvilinear   ;;
319esac
320
321case ${ATM} in
322    ( *dynamico*    ) atm=ico ; atmDomainType=unstructured  ;;
323    ( *ICO*         ) atm=ico ; atmDomainType=unstructured  ;;
324    ( *lmd* | *LMD* ) atm=lmd ; atmDomainType=rectilinear   ;;
325esac
326
327case ${OCE} in # Periodicity type of ORCA grid
328    ( ORCA2.4              ) OcePerio=4.2 ;; # 4 ORCA 6 PALEORCA
329    ( ORCA2*               ) OcePerio=4   ;; # 4 ORCA 6 PALEORCA
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   ;;
336esac
337#
338
339cp ${SUBMIT_DIR}/bin/interpol.exe       .
340cp ${SUBMIT_DIR}/*.py                   .
341cp ${SUBMIT_DIR}/iodef_atm_to_oce.xml   .
342cp ${SUBMIT_DIR}/iodef_oce_to_atm.xml   .
343cp ${SUBMIT_DIR}/arch.env               .
344
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
351cp ${R_IN}/ATM/GRID/${ATM}_grid.nc .
352
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
366
367##
368echo ${Titre}"NEMO T point towards ATM - 1st order"${Norm}
369## ===========================================================================
370echo "Command parameters : ${Command}"
371setValues ${DefaultValues}
372
373OutFileName=${srcGrid}${src}_to_${dstGrid}${dst}_${FullName}
374
375cp iodef_oce_to_atm.xml iodef.xml
376
377cat << EOF > command_file_${OutFileName}.txt
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}"
404EOF
405
406python3 update_xml.py -i iodef.xml -c command_file_${OutFileName}.txt
407cp iodef.xml iodef_${OutFileName}.xml
408
409ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
410ln -fs ${ATM}_grid.nc              atm_grid.nc
411
412${MpiRun} ./interpol.exe --mask_src=${maskSrc} --mask_dst=${maskDst} --use_area=${useArea}
413
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
421##
422echo ${Titre}"Correct spurious values (extremes)"${Norm}
423## ===========================================================================
424cat <<EOF > correction_masque.nco
425where (OceFrac < 0.00001) OceFrac=OceFrac.get_miss() ;
426where (OceFrac > 0.99999) OceFrac=1.0 ;
427OceFrac.delete_miss() ;
428// Fill masked values to land values
429where (OceFrac >  1.0) OceFrac=0.0 ;
430where (OceFrac <  0.0) OceFrac=0.0 ;
431OceMask = OceFrac ;
432EOF
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
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
435 
436##
437echo ${Titre}"Creates mask on ATM grid"${Norm}
438## ===========================================================================
439cp dia_t${oce}_to_t${atm}_${FullName}.nc  dia_t${oce}_to_t${atm}_${FullName}_mask.nc
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
442
443cat <<EOF > creation_masque.nco
444Oce2AtmMask = OceMask ;
445where (OceMask >  0.0) Oce2AtmMask=1 ;
446where (OceMask <= 0.0) Oce2AtmMask=0 ;
447EOF
448
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
451
452if [[ $(ncdump -h ${ATM}_grid_maskFrom_${OCE}.nc | grep domain_dst | wc -l) -gt 0 ]] ; then
453    echo "Change dimension names, and some attributes accordingly"
454    ## ===========================================================
455    case ${atm} in
456        ( *ico* ) 
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
463        ;;
464        ( *lmd* )
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
468        ;;
469    esac
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
481fi
482
483ncks --history --no_abc --append --variable aire atm_grid.nc ${ATM}_grid_maskFrom_${OCE}.nc
484ncatted  --history --attribute units,aire,m,c,"m^2" ${ATM}_grid_maskFrom_${OCE}.nc
485
486[[ ${atm} = *ico* ]] && ncks --no_abc --history --append --variable bounds_lon,bounds_lat atm_grid.nc ${ATM}_grid_maskFrom_${OCE}.nc
487
488##
489#echo ${Titre}"Creates mask of coastal OCE points"${Norm}
490## ===========================================================================
491#${PyRun} python3 -u ComputeNemoCoast.py -n ${OcePerio} -i ${OCE}_coordinates_mask.nc # Creates OceCoastal
492
493##
494#echo ${Titre}"Creates mask of coastal ATM points"${Norm}
495## ===========================================================================
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
503
504##
505echo ${Titre}"Other weights files"${Norm}
506## ===========================================================================
507# Loop on commands
508for Command in ${CommandList[@]}
509do
510    echo "Command parameters : ${Command}"
511    setValues ${Command}
512    if [[ ${Command} = "Runoff"  ]] ; then okRunoff=yes  ; continue ; fi
513    if [[ ${Command} = "Calving" ]] ; then okCalving=yes ; continue ; fi
514    if [[ ${Command} = "Grids"   ]] ; then okGrids=yes   ; continue ; fi
515   
516    ln -fs ${OCE}_coordinates_mask.nc     oce_grid.nc
517    ln -fs ${ATM}_grid_maskFrom_${OCE}.nc atm_grid.nc
518
519    OutFileName=${srcGrid}${src}_to_${dstGrid}${dst}_${FullName}
520   
521    case ${Direction} in
522        ( o2a )
523        cp iodef_oce_to_atm.xml iodef.xml
524        cat << EOF > command_file_${OutFileName}.txt
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
531EOF
532        ;;
533        ( a2o )
534        cp iodef_atm_to_oce.xml iodef.xml
535        cat << EOF > command_file_${OutFileName}.txt
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}
542EOF
543        ;;
544    esac
545
546    cat << EOF >> command_file_${OutFileName}.txt
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}"
565EOF
566
567    python3 update_xml.py -i iodef.xml -c command_file_${OutFileName}.txt
568    cp iodef.xml iodef_${OutFileName}.xml
569   
570    ${MpiRun} ./interpol.exe --mask_src=${maskSrc} --mask_dst=${maskDst} --use_area=${useArea}
571
572    case ${numOrder} in
573        ( 1 ) Text="Conservative Remapping - 1st order" ;;
574        ( 2 ) Text="Conservative Remapping - 2nd order" ;;
575    esac
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
583done
584
585##
586echo ${Titre}"Add some metadata in file headers"${Norm}
587## ===========================================================================
588
589NCO="$(ncks --version |& tail -1 | sed 's/ncks //')"
590PYTHON_VER=$( python3 -c "import sys ; print (sys.version.split(' ')[0])" )
591for InFile in $(ls *${oce}_to_*${atm}_*.nc *${atm}_to_*${oce}_*.nc ${ATM}_grid_maskFrom_${OCE}.nc 2> /dev/null) ; do
592    ncatted --history \
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"               \
597            --attribute model,global,o,c,"${ModelName}"                           \
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"      \
603            --attribute associatedFiles,global,o,c,"grids_${CplModel}.nc areas_${CplModel}.nc masks_${CplModel}.nc" \
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"                           \
607            --attribute Program,global,o,c,"Generated by CreateWeightsMask"       \
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" \
612            --attribute Python,global,o,c,"Python3 version ${PYTHON_VER}"         \
613            --attribute machine,global,o,c,"$(uname -a)"                          \
614            --attribute directory,global,o,c,"$(pwd)"                             \
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" \
616            --attribute SVN_Author,global,o,c,'$Author$'                 \
617            --attribute SVN_Date,global,o,c,'$Date$' \
618            --attribute SVN_Revision,global,o,c,'$Revision$'               \
619            --attribute SVN_Id,global,o,c,'$Id$' \
620            ${InFile}
621done
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
625ncatted --history --attribute ,global,d,c, ${ATM}_grid_maskFrom_${OCE}_noglobal.nc
626
627##
628echo ${Titre}"Update and complete weight files to fit OASIS requested format"${Norm}
629## ===========================================================================
630cat <<EOF > add_dim.nco
631defdim("num_wgts",1) ;
632weight[n_weight, num_wgts] = weight ;
633EOF
634
635for rmpFile in rmp_*.nc ; do
636    mv ${rmpFile} tmp_${rmpFile}
637    ncap2 --history --script-file add_dim.nco tmp_${rmpFile} ${rmpFile} ; rm tmp_${rmpFile}
638   
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}
644 
645    ncatted --history --attribute conventions,global,o,c,"SCRIP" --attribute normalization,global,o,c,"none" ${rmpFile}
646   
647    case ${rmpFile} in
648        ( rmp_*${oce}_to_t${atm}_* )
649        ncatted --history \
650                --attribute title,global,o,c,"Weights ${OCE} to ${ATM}" \
651                --attribute source_grid,global,o,c,"${oceDomainType}" \
652                --attribute dest_grid,global,o,c,"${atmDomainType}"   \
653                ${rmpFile}     
654        ;;
655        ( rmp_*${atm}_to_*${oce}_* )
656        ncatted --history \
657                --attribute title,global,o,c,"Weights ${ATM} to ${OCE}" \
658                --attribute source_grid,global,o,c,"${atmDomainType}" \
659                --attribute dest_grid,global,o,c,"${oceDomainType}"   \
660                ${rmpFile}
661        ;;
662    esac
663done
664
665##
666echo ${Titre}"Add missing variables in rmp files"${Norm}
667## ===========================================================================
668for rmpFile in $(ls rmp_?${atm}_to_[tuv]${oce}_*.nc rmp_[tuv]${oce}_to_t${atm}_*.nc 2> /dev/null ) ; do
669    echo ${rmpFile}
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
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}
681    done
682    OGRID=${ogrid^}
683    AGRID=${agrid^}
684       
685    cat <<EOF >add_varoce.nco
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
710    cp add_varoce.nco add_varoce_$(basename ${rmpFile} .nc)_o_to_a.nco
711    [[ ${o_to_a} = true ]] && ncap2 --history --append --script-file add_varoce_$(basename ${rmpFile} .nc)_o_to_a.nco ${OCE}_coordinates_mask.nc ${rmpFile}
712    sed -i~ -e "s/src_/dst_/g" add_varoce.nco
713    cp add_varoce.nco add_varoce_$(basename ${rmpFile} .nc)_a_to_o.nco
714    [[ ${a_to_o} = true ]] && ncap2 --history --append --script-file add_varoce_$(basename ${rmpFile} .nc)_a_to_o.nco ${OCE}_coordinates_mask.nc ${rmpFile}
715               
716    if [[ ${atm} = ico ]] ; then
717        cat <<EOF >add_varatm.nco
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 ;
737dst_grid_imask (:) = 1 - Oce2AtmMask(:) ;
738dst_grid_imask.int() ;
739dst_grid_area  (:) = aire(:) ;
740dst_grid_frac  (:) = OceFrac(:) ;
741EOF
742        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco
743        if [[ ${o_to_a} = true ]] ; then
744            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
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
746            mv rmp_tmp.nc ${rmpFile}
747        fi
748                   
749        sed -i~ -e "s/dst_/src_/g" add_varatm.nco
750        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco
751        if [[ ${a_to_o} = true ]] ; then
752            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
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
754            mv rmp_tmp.nc ${rmpFile}
755        fi
756    fi
757   
758    if [[ ${atm} = lmd ]] ; then
759        cat <<EOF >add_varatm.nco
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 ;
781dst_grid_imask (:) = 1 - Oce2AtmMask(:,:) ;
782dst_grid_imask.int() ;
783dst_grid_area  (:) = aire(:,:) ;
784dst_grid_frac (:)  = OceFrac(:,:) ;
785EOF
786        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco
787        if [[ ${o_to_a} = true ]] ; then
788            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_o_to_a.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
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
790            mv rmp_tmp.nc ${rmpFile}
791        fi
792       
793        sed -i~ -e "s/dst/src/g" add_varatm.nco
794        cp add_varatm.nco add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco
795        if [[ ${a_to_o} = true ]] ; then
796            ncap2 --history --append --script-file add_varatm_$(basename ${rmpFile} .nc)_a_to_o.nco ${ATM}_grid_maskFrom_${OCE}_noglobal.nc ${rmpFile}
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
798            mv rmp_tmp.nc ${rmpFile}
799        fi
800    fi
801done
802ls -al ${OCE}_coordinates_mask.nc
803##
804## ============================================================================
805echo ${Titre}"Creates and save auxiliary files for OASIS : grids.nc, areas.nc and masks.nc"${Norm}
806cp ${SUBMIT_DIR}/CreateOasisGrids.bash .
807bash CreateOasisGrids.bash --oce ${OCE} --atm ${ATM}
808
809
810##
811echo ${Titre}"Runoff weights"${Norm}
812## ===========================================================================
813if [[ "X${okRunoff}" = "Xyes" ]] ; then
814    ${PyRun} python3 -u RunoffWeights.py --oce=${OCE} --atm=${ATM} \
815              --atmCoastWidth=${atmCoastWidth} --oceCoastWidth=${oceCoastWidth} --searchRadius=${searchRadius} \
816              --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
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} \
819              --atmQuantity=${runOff_atmQuantity} --oceQuantity=${runOff_oceQuantity} --ocePerio=${OcePerio} --modelName="${ModelName}"
820fi
821
822##
823echo ${Titre}"Calving weights"${Norm}
824## ===========================================================================
825if [[ "X${okCalving}" = "Xyes" ]] ; then
826    case ${OCE} in
827        ( eORCA025* )
828        cp /ccc/work/cont003/gencmip6/deshayej/eORCA_R025_runoff_v1.2.nc .
829        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_nosouth.nc  --fmt=${FMT_XIOS} \
830                 --oce=${OCE} --atm=${ATM} --type=nosouth   \
831                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
832                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
833        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceberg.nc  --fmt=${FMT_XIOS} \
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 \
836                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
837        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceshelf.nc --fmt=${FMT_XIOS} \
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 \
840                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
841        ;;
842       
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
851       
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 \
855                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
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 \
859                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
860        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_iceshelf.nc --fmt=${FMT_XIOS} \
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 \
863                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
864        ;;
865       
866        ( * )
867        ${PyRun} python3 -u CalvingWeights.py --output=rmp_t${atm}_to_t${oce}_calving_full.nc --fmt=${FMT_XIOS} \
868                 --oce=${OCE} --atm=${ATM} --type=full --ocePerio=${OcePerio} \
869                 --grids=grids_${CplModel}.nc --areas=areas_${CplModel}.nc --masks=masks_${CplModel}.nc \
870                 --o2a=${ATM}_grid_maskFrom_${OCE}.nc --modelName="${ModelName}"
871        ;;
872    esac
873fi
874
875##
876echo ${Titre}"Simplifies headers, add version and comment"${Norm}
877## ===========================================================================
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
879    ncatted --history --attribute history_of_appended_files,global,d,c,"" ${File}
880    ncatted --history --attribute history,global,d,c,"" ${File}
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}
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## ===========================================================================
889for File in $(ls *${oce}*${atm}* *${atm}*${oce}*  2> /dev/null ); do
890    NewFile=$(echo ${File} | sed -e "s/${atm}/${ATM}/" -e "s/${oce}/${OCE}/" )
891    mv ${File} ${NewFile}
892done
893
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
901fi
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## ===========================================================================
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
917
918
919##
920echo ${Titre}"Creates a README.txt file"${Norm}
921## ===========================================================================
922[[ -f README.txt ]] && rm README.txt
923UUID=$(uuid)
924
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
930grids_${CplModel}.nc areas_${CplModel}.nc masks_${CplModel}.nc are auxiliary file needed by OASIS-MCT
931All files have the same uuid in the global attributes
932
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
954SVN Information :
955$Author$
956$Date$
957$Revision$
958$Id$
959$HeadURL$
960
961EOF
962
963
964
965echo ${Titre}"Compute checksums and add them to README"${Norm}
966# ------------------------------------------------------------
967cat << EOF >> README.txt
968
969Files produced, with checksum produced by Unix command shasum (version $(shasum --version)) with default algorithm
970
971Checksum                                  File
972========================================================================================================
973EOF
974
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
976    ncatted --history --attribute uuid,global,o,c,"${UUID}" ${file}
977    echo "$(shasum ${file})" >> README.txt
978done
979
980cat <<EOF >> README.txt
981
982================= That's all folk's ! ========================
983EOF
984
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
990## ===========================================================================
991##
992echo ${Titre}"     That's all folk's !!!    "${Norm}
993##
994## ===========================================================================
Note: See TracBrowser for help on using the repository browser.