source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4259

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

O.M. : MOSAIX

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