source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 6174

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

MOSAIX (O.M.) : update used module. Simplifies fortran after corrections in XIOS

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