source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4195

Last change on this file since 4195 was 4195, checked in by omamce, 3 years ago

O.M. : mostly improvment of documentation

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