source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 6203

Last change on this file since 6203 was 6190, checked in by omamce, 2 years ago

O.M. : Evolution on MOSAIX

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