source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 6093

Last change on this file since 6093 was 6093, checked in by omamce, 2 years ago

MOSAIX

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