source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 3966

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

O.M. : adaptation to Irene. Adaptation to recent version of NCO (4.6)

  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 36.1 KB
Line 
1#!/bin/bash
2#MSUB -r WeightsMask        # Job name
3#MSUB -o Out_WeightsMask    # Standard output
4#MSUB -e Out_WeightsMask    # Error output
5#MSUB -eo
6#MSUB -n 4                  # Number of processors
7#MSUB -T 1800               # Time limit (seconds)
8#MSUB -q skylake
9#MSUB -Q test
10#MSUB -p gen2212
11#MSUB -m work
12
13### ===========================================================================
14###
15### Creates interpolation weights between ORCA and atmosphere grids.
16### Interpolates ORCA mask to atmosphere grid.
17### Weight files are at OASIS-MCT format.
18###
19### Atmosphere grid may be lon/lat LMDZ or DYNAMICO icosahedron
20###
21### Documentation : https://forge.ipsl.jussieu.fr/igcmg/wiki/IPSLCM6/MOSAIX
22### ===========================================================================
23##
24##  Warning, to install, configure, run, use any of Olivier Marti's
25##  software or to read the associated documentation you'll need at least
26##  one (1) brain in a reasonably working order. Lack of this implement
27##  will void any warranties (either express or implied).
28##  O. Marti assumes no responsability for errors, omissions,
29##  data loss, or any other consequences caused directly or indirectly by
30##  the usage of his software by incorrectly or partially configured
31##  personal.
32##
33## SVN information
34#  $Author$
35#  $Date$
36#  $Revision$
37#  $Id$
38#  $HeadURL$
39export Bold=$(tput bold) 
40export Unde=$(tput smul) ; export OffUnde=$(tput rmul)
41export Stou=$(tput smso) ; export OffStou=$(tput rmso)
42export Reve=$(tput rev ) 
43couleurs=( "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" )
44for i in $(seq 0 7 ) ; do eval "export ${couleurs[$i]}=$(tput setf $i)" ; done
45export Norm=$(tput sgr0 )
46
47##
48## Configuration
49## ===========================================================================
50
51#
52# Defines models
53# ==============
54OCE=ORCA2.3
55#OCE=eORCA1.2
56#OCE=ORCA025
57
58#ATM=ICO30
59#ATM=ICO40
60#ATM=ICO450
61ATM=LMD9695
62#ATM=LMD144X142
63
64#
65# Defines OCE grids to handle
66# ===========================
67ListOCEGRID="T U V"
68ListOrder="1st" # 2nd"
69ListNormalize="false true"
70
71## ===========================================================================
72##
73## You should not change anything below this line ....
74##
75## ===========================================================================
76SUBMIT_DIR=$(pwd)
77
78#
79# Defines computer
80# ================
81if [[ $(hostname) = curie*    ]] ; then arch=curie ; center=tgcc ; fi
82if [[ $(hostname) = irene*    ]] ; then arch=irene ; center=tgcc ; fi
83if [[ $(hostname) = lsce3005* ]] ; then arch=spip  ; center=spip ; fi
84
85PROGRAM=$(basename ${0})
86
87case ${arch} in
88    ( curie )
89    set +vx
90    module purge
91    source /ccc/cont003/home/dsm/p86ipsl/.env_intel17.0.2_curie;
92    source /ccc/cont003/home/dsm/p86ipsl/.env_netcdf4.3.3.1_curie
93    module load datadir/igcmg
94    R_IN=$(ccc_home -u igcmg --cccwork)/IGCM
95    TMPDIR=${SCRATCHDIR}/TMP
96    SUBMIT_DIR=${BRIDGE_MSUB_PWD:-${SUBMIT_DIR}}
97    PROGRAM=${BRIDGE_MSUB_REQNAME}
98    MPIRUN=ccc_mprun
99    ;;
100    ( irene )
101    set +vx
102    module purge
103    source $(ccc_home -u igcmg)/MachineEnvironment/irene/env_irene
104    module load python/2.7.8
105    module load datadir/igcmg
106    R_IN=$(ccc_home -u igcmg --cccwork)/IGCM
107    TMPDIR=${CCCWORKDIR}/TMP
108    SUBMIT_DIR=${BRIDGE_MSUB_PWD:-${SUBMIT_DIR}}
109    PROGRAM=${BRIDGE_MSUB_REQNAME}
110    MPIRUN=ccc_mprun
111    module list
112    ;;
113    ( spip )
114    R_IN=${HOME}/Scratch/IGCM
115    TMPDIR=${HOME}/Scratch/TMP
116    SUBMIT_DIR=$(pwd)
117    MPIRUN=/opt/local/bin/mpirun-openmpi-gcc49 -n 2
118    ;;
119    ( * ) exit -1 ;;
120esac
121
122set -x ; set -e
123
124mkdir -p ${TMPDIR}/${OCE}x${ATM} || exit 1
125cd       ${TMPDIR}/${OCE}x${ATM} || exit 1
126rm -f *
127
128#
129# Suffixes
130# ---------------------------------------------------------------------------
131Listocegrid=${ListOCEGRID,,}
132
133case ${OCE} in
134    ( *ORC* )         oce=orc ; oce_domain_type=curvilinear   ;;
135esac
136case ${ATM} in
137    ( *dynamico*    ) atm=ico ; atm_domain_type=unstructured  ;;
138    ( *ICO*         ) atm=ico ; atm_domain_type=unstructured  ;;
139    ( *lmd* | *LMD* ) atm=lmd ; atm_domain_type=rectilinear   ;;
140esac
141
142case ${OCE} in
143    ( ORCA2.3*         ) OcePerio=4 ;;
144    ( ORCA1* | eORCA1* ) OcePerio=6 ;;
145esac
146#
147# Format for OASIS files : should be NetCDF3 classic or NetCDF3 64 bits
148# ---------------------------------------------------------------------------
149FMT_OASIS=64bit
150FMT_XIOS=netcdf4
151
152
153cp ${SUBMIT_DIR}/bin/interpol.exe    .
154cp ${SUBMIT_DIR}/*.py                .
155
156
157cp ${R_IN}/OCE/NEMO/${OCE}/${OCE}_coordinates_mask.nc  .
158cp ${R_IN}/ATM/GRID/${ATM}_grid.nc .
159
160ncks --overwrite --fl_fmt=${FMT_OASIS} --history ${OCE}_coordinates_mask.nc ${OCE}_coordinates_mask_${FMT_OASIS}.nc
161ncks --overwrite --fl_fmt=${FMT_OASIS} --history ${ATM}_grid.nc                    ${ATM}_grid_${FMT_OASIS}.nc
162#
163# Creates OCEAN C grid : redundant point removed to compute proper integrals # A passer dans CreateWeights
164# --------------------------------------------------------------------------------------------------------
165cat <<EOF >add_c_grid.nco
166nav_lon_grid_C    = nav_lon_grid_T    ;
167nav_lat_grid_C    = nav_lat_grid_T    ;
168bounds_lon_grid_C = bounds_lon_grid_T ;
169bounds_lat_grid_C = bounds_lat_grid_T ;
170mask_C            = maskutil_T        ;
171area_grid_C       = area_grid_T       ;
172EOF
173
174ncap2 --overwrite --history --script-file add_c_grid.nco ${OCE}_coordinates_mask.nc tmp_${OCE}_coordinates_mask.nc
175ncks --overwrite --variable nav_lon_grid_C,nav_lat_grid_C       tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
176ncks --append    --variable bounds_lon_grid_C,bounds_lat_grid_C tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
177#ncks --append    --variable mask_C                  tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
178ncks --append    --variable area_grid_C             tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
179
180ncrename --dimension x_grid_T,x_grid_C                C_${OCE}_coordinates_mask.nc
181ncrename --dimension y_grid_T,y_grid_C                C_${OCE}_coordinates_mask.nc
182ncrename --dimension nvertex_grid_T,nvertex_grid_C    C_${OCE}_coordinates_mask.nc
183ncks --append C_${OCE}_coordinates_mask.nc            ${OCE}_coordinates_mask.nc
184rm C_${OCE}_coordinates_mask.nc
185
186ncks --overwrite --fl_fmt=${FMT_OASIS} --history ${OCE}_coordinates_mask.nc ${OCE}_coordinates_mask_${FMT_OASIS}.nc
187
188ls -al
189
190##
191## NEMO T point towards ATM - 1st order
192## ===========================================================================
193echo ${Green}"${OCE} T toward ${ATM} - 1storder"${Norm}
194cp ${SUBMIT_DIR}/iodef_oce_to_atm.xml   iodef.xml
195
196python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]'   -k name  -v maskutil_T
197python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                   -k type  -v ${oce_domain_type}
198python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                   -k type  -v ${atm_domain_type}
199python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name  -v maskutil_T
200python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]'                               -k name  -v dia_t${oce}_to_t${atm}_1storder_false
201python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]'       -t "${OCE} mask interpolated to ${ATM}"
202python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${oce_domain_type}
203python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t ${atm_domain_type}
204python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'       -t 1
205python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                    -k type  -v ${oce_domain_type}
206python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                    -k type  -v ${atm_domain_type}
207python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_t${oce}_to_t${atm}_1storder_false.nc
208python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1
209python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t false
210python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   -k renormalize -v false
211
212cp iodef.xml iodef_t${oce}_to_t${atm}_1storder_false.xml
213ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
214ln -fs ${ATM}_grid.nc              atm_grid.nc
215
216time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=no
217
218##
219## Correct spurious values (extremes)
220## ===========================================================================
221cat <<EOF > correction_masque.nco
222where (OceFrac <   0.00001 )  OceFrac=OceFrac.get_miss() ;
223where (OceFrac >   0.99999 )  OceFrac=1.0 ;
224OceFrac.delete_miss() ;
225// Fill masked values to land values
226where (OceFrac >  1.0 )  OceFrac=0.0 ;
227where (OceFrac <  0.0 )  OceFrac=0.0 ;
228EOF
229ncap2 --history --overwrite --script-file correction_masque.nco dia_t${oce}_to_t${atm}_1storder_false.nc tmp_dia_t${oce}_to_t${atm}_1storder_false.nc ; mv tmp_dia_t${oce}_to_t${atm}_1storder_false.nc dia_t${oce}_to_t${atm}_1storder_false.nc
230ncatted --history -a missing_value,OceFrac,d,,"" -a _FillValue,OceFrac,d,,"" dia_t${oce}_to_t${atm}_1storder_false.nc
231
232##
233##  Creates mask on ATM grid
234## ===========================================================================
235cp dia_t${oce}_to_t${atm}_1storder_false.nc  dia_t${oce}_to_t${atm}_1storder_false_mask.nc
236ncks --alphabetize --history --overwrite --variable OceFrac dia_t${oce}_to_t${atm}_1storder_false_mask.nc  ${ATM}_grid_maskFrom_${OCE}.nc
237ncks --alphabetize --history --append    --variable aire    atm_grid.nc                                    ${ATM}_grid_maskFrom_${OCE}.nc
238[[ ${atm} = *ico* ]] && ncks --alphabetize --history --append --variable bounds_lon,bounds_lat atm_grid.nc ${ATM}_grid_maskFrom_${OCE}.nc
239
240cat <<EOF > creation_masque.nco
241where (OceFrac >  0.0 )  OceFrac=1 ;
242where (OceFrac <= 0.0 )  OceFrac=0 ;
243EOF
244ncap2 --history --overwrite --script-file creation_masque.nco dia_t${oce}_to_t${atm}_1storder_false_mask.nc tmp_dia_t${oce}_to_t${atm}_1storder_false_mask.nc ; mv tmp_dia_t${oce}_to_t${atm}_1storder_false_mask.nc dia_t${oce}_to_t${atm}_1storder_false_mask.nc
245ncrename --history --variable OceFrac,OceMask dia_t${oce}_to_t${atm}_1storder_false_mask.nc
246
247ncks --alphabetize --history --append --variable OceMask dia_t${oce}_to_t${atm}_1storder_false_mask.nc   ${ATM}_grid_maskFrom_${OCE}.nc
248rm dia_t${oce}_to_t${atm}_1storder_false_mask.nc
249
250##
251## NEMO, other case, towards ATM
252## ===========================================================================
253for order in ${ListOrder} ; do
254    case ${order} in
255        ( 1st ) num_order=1 ;;
256        ( 2nd ) num_order=2 ;;
257    esac
258    for normalize in ${ListNormalize} ; do
259        for OCEGRID in ${ListOCEGRID} ; do
260            ocegrid=${OCEGRID,,}
261           
262            if [[ ! -f rmp_${ocegrid}${oce}_to_t${atm}_${order}order_${normalize}.nc ]] ; then
263               
264                echo ${Green}"${OCE} ${OCEGRID} toward ${ATM} - ${order}order - normalize: ${normalize}"${Norm}
265               
266                python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]'     -k name  -v maskutil_${OCEGRID}
267                python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                     -k type  -v ${oce_domain_type}
268                python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                     -k type  -v ${atm_domain_type}
269                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]'   -k name  -v maskutil_${OCEGRID}
270                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]'                                 -k name  -v dia_${ocegrid}${oce}_to_t${atm}_${order}order_${normalize}
271                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]'         -t "${OCE} mask interpolated to ${ATM}"
272                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]'   -t ${oce_domain_type}
273                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'     -t ${atm_domain_type}
274                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'         -t ${num_order}
275                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${normalize}
276                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                      -k type  -v ${oce_domain_type}
277                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                      -k type  -v ${atm_domain_type}
278                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   -k order -v ${num_order}
279                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_${ocegrid}${oce}_to_t${atm}_${order}order_${normalize}.nc
280                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize     -v ${normalize}
281               
282                cp iodef.xml iodef_${ocegrid}${oce}_t${atm}_${order}order_${normalize}.xml
283               
284                ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
285                ln -fs ${ATM}_grid.nc              atm_grid.nc
286                time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=yes
287            fi
288           
289        done
290    done
291done
292
293##
294## ATM towards NEMO points
295## ===========================================================================
296cp ${SUBMIT_DIR}/iodef_atm_to_oce.xml           iodef.xml
297
298python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                   -k type  -v ${atm_domain_type}
299python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                   -k type  -v ${oce_domain_type}
300python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]'       -t "${ATM} mask interpolated to ${OCE}"
301python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${atm_domain_type}
302python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t ${oce_domain_type}
303python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                    -k type  -v ${atm_domain_type}
304python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                    -k type  -v ${oce_domain_type}
305           
306for order in ${ListOrder} ; do
307    case ${order} in
308        ( 1st ) num_order=1 ;;
309        ( 2nd ) num_order=2 ;;
310    esac
311    for normalize in ${ListNormalize} ; do
312        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   -k order -v ${num_order}
313        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'         -t ${num_order}   
314        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${normalize}
315        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   -k renormalize     -v ${normalize}
316       
317        for OCEGRID in ${ListOCEGRID} ; do
318            ocegrid=${OCEGRID,,}
319           
320            echo ${Green}"${ATM} toward ${OCE} ${OCEGRID} - ${order}order - normalize: ${normalize}"${Norm}
321            python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]'   -k name  -v mask_${OCEGRID}
322            python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]'   -k name  -v mask_${OCEGRID}
323            python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]'                               -k name  -v dia_t${atm}_to_${ocegrid}${oce}_${order}order_${normalize}
324            python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_t${atm}_to_${ocegrid}${oce}_${order}order_${normalize}.nc
325            cp iodef.xml iodef_t${atm}_to_${ocegrid}${oce}_${order}order_${normalize}.xml
326           
327            ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
328            ln -fs ${ATM}_grid.nc              atm_grid.nc
329            time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=yes
330           
331        done
332    done
333done
334
335##
336## Creates mask of coastal OCE points
337## ===========================================================================
338python ComputeNemoCoast.py -n ${OcePerio} -i ${OCE}_coordinates_mask.nc # Creates OceCoastal
339##
340## Creates mask of coastal ATM points
341## ===========================================================================
342cat <<EOF > coastal.nco
343AtmCoastal = OceFrac * 0.0 ;
344where (OceFrac > 0.0 && OceFrac < 1.0 )  AtmCoastal = 1.0 ;
345EOF
346ncap2 --history --overwrite --script-file coastal.nco ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_coastal_maskFrom_${OCE}.nc
347ncks --history --append --variable AtmCoastal ${ATM}_coastal_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
348rm ${ATM}_coastal_maskFrom_${OCE}.nc
349ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
350##
351## ATM towards NEMO points - runoff
352## ===========================================================================
353cp ${SUBMIT_DIR}/iodef_atm_to_oce.xml           iodef.xml
354for normalize in ${ListNormalize} ; do
355    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                   -k type  -v ${atm_domain_type}
356    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                   -k type  -v ${oce_domain_type}
357    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]'       -t "${ATM} coastal mask interpolated to ${OCE}"
358    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${atm_domain_type}
359    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t ${oce_domain_type}
360    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                    -k type  -v ${atm_domain_type}
361    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                    -k type  -v ${oce_domain_type}
362    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1
363    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'       -t 1
364    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${normalize}
365    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]'                         -k name  -v ${ATM}_grid_maskFrom_${OCE}
366    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]'   -k name  -v AtmCoastal
367    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]'   -k name  -v OceCoastal
368    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]'                               -k name  -v dia_o${atm}_to_c${oce}_1storder_${normalize}
369    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_o${atm}_to_c${oce}_1storder_${normalize}.nc
370    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize     -v ${normalize}
371   
372    cp iodef.xml iodef_o${atm}_to_c${oce}_1storder_${normalize}.xml
373   
374    ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
375    ln -fs ${ATM}_grid.nc              atm_grid.nc
376    time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=yes
377
378    # Now we should divide weight par source (atm) grid area : run-off is a quantity/s integrated on the grid mox, not a flux
379done
380##
381## Copy all NetCDF files to NetCDF 3 format (needed for OASIS)
382## ===========================================================================
383for InFile in *.nc ; do
384    OuFile=$(basename ${InFile} .nc)_${FMT_OASIS}.nc
385    if [[ ! -f ${OuFile} ]] ; then
386        ncks --alphabetize --history --fl_fmt=${FMT_OASIS} ${InFile} ${OuFile}
387    fi
388done
389
390##
391## Add time axis and coordinates information
392## (needed if files need to be read by XIOS)
393## ===========================================================================
394#ncatted --history -a coordinates,aire,m,c,"lat lon" ${ATM}_grid_maskFrom_${OCE}.nc
395ncap2 --overwrite --history --script 'defdim("time_counter",1) ;' ${ATM}_grid_maskFrom_${OCE}.nc tmp_${ATM}_grid_maskFrom_${OCE}.nc ; mv tmp_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
396ncdump -h ${ATM}_grid_maskFrom_${OCE}.nc
397
398ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
399ncdump -h ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
400
401if [[ ${atm} = ico ]] ; then
402    cat <<EOF > add_time.nco
403OceFrac    [time_counter,cell] = OceFrac    [cell] ;
404OceMask    [time_counter,cell] = OceMask    [cell] ;
405AtmCoastal [time_counter,cell] = AtmCoastal [cell] ;
406EOF
407    ncap2 --overwrite --history --script-file add_time.nco ${ATM}_grid_maskFrom_${OCE}.nc tmp_${ATM}_grid_maskFrom_${OCE}.nc ; mv tmp_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
408    ncatted --history \
409            -a coordinates,OceFrac,c,c,"time_counter cell"    \
410            -a coordinates,OceMask,c,c,"time_counter cell"    \
411            -a coordinates,AtmCoastal,c,c,"time_counter cell" \
412            -a coordinates,aire,c,c,"cell" \
413            ${ATM}_grid_maskFrom_${OCE}.nc
414fi
415ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
416
417if [[ ${atm} = lmd ]] ; then
418    cat <<EOF > add_time.nco
419OceFrac    [time_counter,lat,lon] = OceFrac    [lat,lon] ;
420OceMask    [time_counter,lat,lon] = OceMask    [lat,lon]  ;
421AtmCoastal [time_counter,lat,lon] = AtmCoastal [lat,lon]  ;
422EOF
423    ncap2 --overwrite --history --script-file add_time.nco ${ATM}_grid_maskFrom_${OCE}.nc tmp_${ATM}_grid_maskFrom_${OCE}.nc
424    ncdump -h tmp_${ATM}_grid_maskFrom_${OCE}.nc
425    mv tmp_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
426   
427    ncatted --history \
428            -a coordinates,OceFrac,m,c,"time_counter lat lon"    \
429            -a coordinates,OceMask,m,c,"time_counter lat lon"    \
430            -a coordinates,AtmCoastal,m,c,"time_counter lat lon" \
431            -a coordinates,aire,m,c,"lat lon" \
432            ${ATM}_grid_maskFrom_${OCE}.nc
433fi
434ncks --alphabetize --overwrite --mk_rec time_counter   ${ATM}_grid_maskFrom_${OCE}.nc tmp_${ATM}_grid_maskFrom_${OCE}.nc ; mv tmp_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
435ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
436
437##
438## Add some metadata in file headers
439## ===========================================================================
440
441UUID=$(uuid)
442NCO="$(ncks --version |& tail -1|sed 's/ncks //')"
443PYTHON_VER=$( python -i -c "import sys ; print (sys.version.split(' ')[0])" )
444for InFile in *${oce}_to_*${atm}_*.nc *${atm}_to_*${oce}_*.nc ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc; do
445    ncatted --history \
446            --attribute uuid,global,d,,                                           \
447            --attribute LongName,global,d,,                                       \
448            --attribute nco_openmp_thread_number,global,d,,                       \
449            --attribute Conventions,global,o,c,"CF-1.6"                           \
450            --attribute source,global,o,c,"IPSL Earth system model"               \
451            --attribute group,global,o,c,"ICMC IPSL Climate Modelling Center"     \
452            --attribute Institution,global,o,c,"IPSL https://www.ipsl.fr"         \
453            --attribute Ocean,global,o,c,"${OCE} https://www.nemo-ocean.eu"       \
454            --attribute Atmosphere,global,o,c,"${ATM} http://lmdz.lmd.jussieu.fr" \
455            --attribute production,global,o,c,"$(finger ${LOGNAME} | head -1 | awk '{print $4, $5}') " \
456            --attribute originalFiles,global,o,c,"${OCE}_coordinates_mask.nc ${ATM}_grid_mask.nc"      \
457            --attribute associatedFiles,global,o,c,"grids_${OCE}x${ATM}.nc areas_${OCE}x${ATM}.nc masks_${OCE}x${ATM}.nc" \
458            --attribute directory,global,o,c,"$(pwd)"                             \
459            --attribute description,global,o,c,"Fields needed by OASIS-MCT"       \
460            --attribute title,global,o,c,"${InFile}.nc"                           \
461            --attribute Program,global,o,c,"Generated by ${PROGRAM}"              \
462            --attribute timeStamp,global,o,c,"$(date)"                            \
463            --attribute uuid,global,o,c,"${UUID}"                                 \
464            --attribute HOSTNAME,global,o,c,"$(hostname)"                         \
465            --attribute LOGNAME,global,o,c,"$(whoami)"                            \
466            --attribute NCO,global,o,c,"NCO netCDF Operator ${NCO} http://nco.sourceforge.net" \
467            --attribute Python,global,o,c,"Python version ${PYTHON_VER}"          \
468            --attribute OS,global,o,c,"$(uname -o)"                               \
469            --attribute release,global,o,c,"$(uname -r)"                          \
470            --attribute hardware,global,o,c,"$(uname -i)"                         \
471            --attribute directory,global,o,c,"$(pwd)"                             \
472            --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" \
473            --attribute Comment,global,o,c,"Preliminary attempt - Do not trust !" \
474            --attribute SVN_Author,global,o,c,"$Author$"                 \
475            --attribute SVN_Date,global,o,c,"$Date$"                            \
476            --attribute SVN_Revision,global,o,c,"$Revision$"               \
477            --attribute SVN_Id,global,o,c,"$Id$"                                \
478            ${InFile}
479done
480
481
482##
483## Update and complete weights file to fit OASIS requested format
484## ===========================================================================
485cat <<EOF > add_dim.nco
486defdim("num_wgts",1) ;
487weight[n_weight, num_wgts] = weight ;
488EOF
489
490for rmpFile in rmp_*.nc ; do
491    mv ${rmpFile} xios_${rmpFile}
492    ncap2 --fl_fmt=${FMT_OASIS} --history --script-file add_dim.nco xios_${rmpFile} ${rmpFile}
493   
494    ncrename --history --dimension n_weight,num_links   ${rmpFile}
495    ncrename --history --variable  src_idx,src_address  ${rmpFile}
496    ncrename --history --variable  dst_idx,dst_address  ${rmpFile}
497    ncrename --history --variable  weight,remap_matrix  ${rmpFile}
498    case ${rmpFile} in
499        ( *_1storder_*.nc ) ncatted --history --attribute map_method,global,o,c,"Conservative Remapping - 1st order"  ${rmpFile} ;;
500        ( *_2ndorder_*.nc ) ncatted --history --attribute map_method,global,o,c,"Conservative Remapping - 2nd order"  ${rmpFile} ;;
501    esac
502    case ${rmpFile} in
503        ( *_true.nc  ) ncatted --history --attribute map_method,global,o,c,"Normalization: true"  ${rmpFile} ;;
504        ( *_false.nc ) ncatted --history --attribute map_method,global,o,c,"Normalization: false" ${rmpFile} ;;
505    esac
506    ncatted --history --attribute conventions,global,o,c,"SCRIP"   ${rmpFile}
507    ncatted --history --attribute normalization,global,o,c,"none"  ${rmpFile}
508   
509    case ${rmpFile} in
510        ( rmp_*${oce}_to_t${atm}_* )
511        ncatted --history \
512                --attribute title,global,o,c,"Weights ${OCE} to ${ATM}" \
513                --attribute source_grid,global,o,c,"${oce_domain_type}" \
514                --attribute dest_grid,global,o,c,"${atm_domain_type}"   \
515                ${rmpFile}     
516        ;;
517        ( rmp_*${atm}_to_*${oce}_* )
518        ncatted --history \
519                --attribute title,global,o,c,"Weights ${ATM} to ${OCE}" \
520                --attribute source_grid,global,o,c,"${atm_domain_type}" \
521                --attribute dest_grid,global,o,c,"${oce_domain_type}"   \
522                ${rmpFile}
523        ;;
524    esac
525done
526
527##
528## Add missing variables in rmp files
529## ===========================================================================
530for rmpFile in rmp_tlmd_to_[tuv]orc_*order_*.nc rmp_[tuv]orc_to_tlmd_*order_*.nc* ; do
531    echo ${rmpFile}
532    a_to_o=false ; o_to_a=false
533    case ${rmpFile} in
534        (  rmp_?${oce}_to_?${atm}_*.nc ) o_to_a=true ;;
535        (  rmp_?${atm}_to_?${oce}_*.nc ) a_to_o=true ;;
536    esac
537
538    for Grid in t u v o c ; do # Identify grids
539        [[ ${rmpFile} = rmp_${Grid}${oce}_to_?${atm}_*.nc ]] && ogrid=${Grid}
540        [[ ${rmpFile} = rmp_?${oce}_to_${Grid}${atm}_*.nc ]] && agrid=${Grid}
541        [[ ${rmpFile} = rmp_${Grid}${atm}_to_?${oce}_*.nc ]] && agrid=${Grid}
542        [[ ${rmpFile} = rmp_?${atm}_to_${Grid}${oce}_*.nc ]] && ogrid=${Grid}
543    done
544    OGRID=${ogrid^}
545    AGRID=${agrid^}
546       
547    cat <<EOF >add_varoce.nco
548defdim ("src_grid_size"   , \$x_grid_${OGRID}.size*\$y_grid_${OGRID}.size) ;
549defdim ("src_grid_corners", 4) ;
550defdim ("src_grid_rank"   , 2) ;
551//
552src_grid_dims[src_grid_rank] = { \$y_grid_${OGRID}.size, \$x_grid_${OGRID}.size } ;
553//
554src_grid_center_lat [src_grid_size] =  0.0d ;
555src_grid_center_lon [src_grid_size] =  0.0d ;
556src_grid_center_lat (:) = nav_lat_grid_${OGRID}(:,:)   ;
557src_grid_center_lon (:) = nav_lon_grid_${OGRID}(:,:)   ;
558//
559src_grid_corner_lat [src_grid_size, src_grid_corners] = 0.0d ;
560src_grid_corner_lon [src_grid_size, src_grid_corners] = 0.0d ;
561src_grid_corner_lat(:,:) = bounds_lat_grid_${OGRID}(:,:,:) ;
562src_grid_corner_lon(:,:) = bounds_lon_grid_${OGRID}(:,:,:) ;
563//
564src_grid_imask [src_grid_size] =    0 ;
565src_grid_area  [src_grid_size] = 0.0d ;
566src_grid_frac  [src_grid_size] = 1.0d ;
567src_grid_imask (:) = 1 - mask_${OGRID}(:,:) ;
568src_grid_imask.int() ;
569src_grid_area  (:) = area_grid_${OGRID}(:,:) ;
570EOF
571
572    [[ ${o_to_a} = true ]] && ncap2 --history --append --script-file add_varoce.nco ${OCE}_coordinates_mask_${FMT_OASIS}.nc ${rmpFile}
573    sed --in-place "s/src_/dst_/g" add_varoce.nco
574    [[ ${a_to_o} = true ]] && ncap2 --history --append --script-file add_varoce.nco ${OCE}_coordinates_mask_${FMT_OASIS}.nc ${rmpFile}
575               
576    if [[ ${atm} = ico ]] ; then
577        cat <<EOF >add_varatm.nco
578defdim ("dst_grid_size"   , \$cell.size) ;
579defdim ("dst_grid_corners", 6) ;
580defdim ("dst_grid_rank"   , 2) ;
581//
582dst_grid_dims[dst_grid_rank] = { \$cell.size, 1 } ;
583//
584dst_grid_center_lat [dst_grid_size] =  0.0d ;
585dst_grid_center_lon [dst_grid_size] =  0.0d ;
586dst_grid_center_lat (:) = lat(:)   ;
587dst_grid_center_lon (:) = lon(:)   ;
588//
589dst_grid_corner_lat [dst_grid_size, dst_grid_corners] = 0.0d ;
590dst_grid_corner_lon [dst_grid_size, dst_grid_corners] = 0.0d ;
591dst_grid_corner_lat(:,:) = bounds_lat(:,:) ;
592dst_grid_corner_lon(:,:) = bounds_lon(:,:) ;
593//
594dst_grid_imask [dst_grid_size] =    0 ;
595dst_grid_area  [dst_grid_size] = 0.0d ;
596dst_grid_frac  [dst_grid_size] = 1.0d ;
597dst_grid_imask (:) = 1 - OceMask(0,:) ;
598dst_grid_imask.int() ;
599dst_grid_area  (:) = aire(:) ;
600dst_grid_frac  (:) = OceFrac(0,:) ;
601EOF
602        if [[ ${o_to_a} = true ]] ; then
603            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
604            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
605            mv rmp_tmp.nc ${rmpFile}
606        fi
607                   
608        sed --in-place "s/dst_/src_/g" add_varatm.nco
609        if [[ ${a_to_o} = true ]] ; then
610            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
611            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
612            mv rmp_tmp.nc ${rmpFile}
613        fi
614    fi
615   
616    if [[ ${atm} = lmd ]] ; then
617        cat <<EOF >add_varatm.nco
618defdim ("dst_grid_size"   , \$lon.size*\$lat.size) ;
619defdim ("dst_grid_corners", 4) ;
620defdim ("dst_grid_rank"   , 2) ;
621//
622dst_grid_dims[dst_grid_rank] = { \$lat.size, \$lon.size } ;
623//
624dst_grid_center_lat [dst_grid_size] =  0.0d ;
625dst_grid_center_lon [dst_grid_size] =  0.0d ;
626lat0lon[lat,lon] = lat(:)+0*lon(:) ;
627lon0lat[lat,lon] = lon(:)+0*lat(:) ;
628dst_grid_center_lat (:) = lat0lon(:,:)   ;
629dst_grid_center_lon (:) = lon0lat(:,:)   ;
630//
631//dst_grid_corner_lat [dst_grid_size, dst_grid_corners] = 0.0d ; // Not available for LMDZ lon/lat grid
632//dst_grid_corner_lon [dst_grid_size, dst_grid_corners] = 0.0d ;
633//dst_grid_corner_lat(:,:) = bounds_lat(:,:) ;
634//dst_grid_corner_lon(:,:) = bounds_lon(:,:) ;
635//
636dst_grid_imask [dst_grid_size] =    0 ;
637dst_grid_area  [dst_grid_size] = 0.0d ;
638dst_grid_frac  [dst_grid_size] = 1.0d ;
639dst_grid_imask (:) = 1 - OceMask(0,:,:) ;
640dst_grid_imask.int() ;
641dst_grid_area  (:) = aire(:,:) ;
642dst_grid_frac (:)  = OceFrac(0,:,:) ;
643EOF
644        if [[ ${o_to_a} = true ]] ; then
645            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
646            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
647            mv rmp_tmp.nc ${rmpFile}
648        fi
649       
650        sed --in-place "s/dst/src/g" add_varatm.nco
651        if [[ ${a_to_o} = true ]] ; then
652            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
653            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
654            mv rmp_tmp.nc ${rmpFile}
655        fi
656    fi
657done
658ls -al ${OCE}_coordinates_mask.nc
659##
660## Save results
661## ===========================================================================
662cp ${ATM}_grid_maskFrom_${OCE}.nc ${SUBMIT_DIR}
663for File in dia_*.nc rmp_*.nc
664do
665    NewFile=$(echo ${File} | sed -e "s/${atm}/${ATM}/" -e "s/${oce}/${OCE}/" )
666    cp ${File} ${SUBMIT_DIR}/${NewFile}
667done
668
669##
670## Creates and save auxiliary files for OASIS
671## ===========================================================================
672bash ${SUBMIT_DIR}/CreateOasisGrids.bash --oce ${OCE} --atm ${ATM}
673
674cp areas_${OCE}x${ATM}.nc ${SUBMIT_DIR}
675cp grids_${OCE}x${ATM}.nc ${SUBMIT_DIR}
676cp masks_${OCE}x${ATM}.nc ${SUBMIT_DIR}
677
678
679## ===========================================================================
680##
681##                               That's all folk's !!!
682##
683## ===========================================================================
684
Note: See TracBrowser for help on using the repository browser.