source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 6105

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

MOSAIX, by O.M : adapt to new file format of NEMO4.2

(no halo or peridodicity band in files)

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