source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4202

Last change on this file since 4202 was 4199, checked in by omamce, 6 years ago

O.M. : MOASAIX, suppress generation of NetCDF3 files

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