source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4172

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

O.M. :

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