source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4317

Last change on this file since 4317 was 4317, checked in by omamce, 5 years ago

O.M: Mosaix. Correct NEMO mask to use maskutil

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