source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4320

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

O.M.: MOSAIX. Correct netcdf header

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