source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4298

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

O.M. : corection on MOSAIX

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