source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 5158

Last change on this file since 5158 was 5158, checked in by omamce, 4 years ago

O.M. : MOSAIX smaal corrections

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