source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 4049

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

O.M. : CreateWeightMask?
get around bugs in ncatted
start adaptation to ORCA025

  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 36.4 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 8                  # 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# ==============
54#OCE=ORCA2.3
55#OCE=eORCA1.2
56OCE=ORCA025
57
58ATM=ICO30
59#ATM=ICO40
60#ATM=ICO450
61#ATM=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 ;;
145    ( ORCA025*         ) OcePerio=6 ;;
146esac
147#
148# Format for OASIS files : should be NetCDF3 classic or NetCDF3 64 bits
149# ---------------------------------------------------------------------------
150FMT_OASIS=64bit
151FMT_XIOS=netcdf4
152
153
154cp ${SUBMIT_DIR}/bin/interpol.exe    .
155cp ${SUBMIT_DIR}/*.py                .
156
157
158cp ${R_IN}/OCE/NEMO/${OCE}/${OCE}_coordinates_mask.nc  .
159cp ${R_IN}/ATM/GRID/${ATM}_grid.nc .
160
161ncks --overwrite --fl_fmt=${FMT_OASIS} --history ${OCE}_coordinates_mask.nc ${OCE}_coordinates_mask_${FMT_OASIS}.nc
162ncks --overwrite --fl_fmt=${FMT_OASIS} --history ${ATM}_grid.nc                    ${ATM}_grid_${FMT_OASIS}.nc
163#
164# Creates OCEAN C grid : redundant point removed to compute proper integrals # A passer dans CreateWeights
165# --------------------------------------------------------------------------------------------------------
166cat <<EOF >add_c_grid.nco
167defdim("x_grid_C", \$x_grid_T.size) ;
168defdim("y_grid_C", \$y_grid_T.size) ;
169defdim("nvertex_grid_C",  4)         ;
170nav_lon_grid_C[y_grid_C,x_grid_C]    = nav_lon_grid_T(:,:)    ;
171nav_lat_grid_C[y_grid_C,x_grid_C]    = nav_lat_grid_T(:,:)    ;
172bounds_lon_grid_C[y_grid_C,x_grid_C,nvertex_grid_C] = bounds_lon_grid_T(:,:,:) ;
173bounds_lat_grid_C[y_grid_C,x_grid_C,nvertex_grid_C] = bounds_lat_grid_T(:,:,:) ;
174mask_C[y_grid_C,x_grid_C]            = maskutil_T(:,:)        ;
175area_grid_C[y_grid_C,x_grid_C]       = area_grid_T(:,:)       ;
176EOF
177
178ncap2 --overwrite --history --script-file add_c_grid.nco ${OCE}_coordinates_mask.nc tmp_${OCE}_coordinates_mask.nc
179ncatted --history --attribute bounds,nav_lon_grid_C,m,c,"bounds_lon_grid_C" tmp_${OCE}_coordinates_mask.nc
180ncatted --history --attribute bounds,nav_lat_grid_C,m,c,"bounds_lat_grid_C" tmp_${OCE}_coordinates_mask.nc
181ncks --history --overwrite --variable nav_lon_grid_C,nav_lat_grid_C       tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
182ncks --history --append    --variable bounds_lon_grid_C,bounds_lat_grid_C tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
183ncks --history --append    --variable area_grid_C             tmp_${OCE}_coordinates_mask.nc C_${OCE}_coordinates_mask.nc
184
185ncks --history --append C_${OCE}_coordinates_mask.nc            ${OCE}_coordinates_mask.nc
186rm C_${OCE}_coordinates_mask.nc
187
188ncks --history --overwrite --fl_fmt=${FMT_OASIS} ${OCE}_coordinates_mask.nc ${OCE}_coordinates_mask_${FMT_OASIS}.nc
189
190ls -al
191
192##
193## NEMO T point towards ATM - 1st order
194## ===========================================================================
195echo ${Green}"${OCE} T toward ${ATM} - 1storder"${Norm}
196cp ${SUBMIT_DIR}/iodef_oce_to_atm.xml   iodef.xml
197
198python 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
199python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                   -k type  -v ${oce_domain_type}
200python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                   -k type  -v ${atm_domain_type}
201python 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
202python 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
203python 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}"
204python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${oce_domain_type}
205python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t ${atm_domain_type}
206python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'       -t 1
207python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                    -k type  -v ${oce_domain_type}
208python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                    -k type  -v ${atm_domain_type}
209python 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
210python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1
211python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t false
212python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   -k renormalize -v false
213
214cp iodef.xml iodef_t${oce}_to_t${atm}_1storder_false.xml
215ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
216ln -fs ${ATM}_grid.nc              atm_grid.nc
217
218time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=no
219
220##
221## Correct spurious values (extremes)
222## ===========================================================================
223cat <<EOF > correction_masque.nco
224where (OceFrac <   0.00001 )  OceFrac=OceFrac.get_miss() ;
225where (OceFrac >   0.99999 )  OceFrac=1.0 ;
226OceFrac.delete_miss() ;
227// Fill masked values to land values
228where (OceFrac >  1.0 )  OceFrac=0.0 ;
229where (OceFrac <  0.0 )  OceFrac=0.0 ;
230EOF
231ncap2 --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
232ncatted --history -a missing_value,OceFrac,d,,"" -a _FillValue,OceFrac,d,,"" dia_t${oce}_to_t${atm}_1storder_false.nc
233
234##
235##  Creates mask on ATM grid
236## ===========================================================================
237cp dia_t${oce}_to_t${atm}_1storder_false.nc  dia_t${oce}_to_t${atm}_1storder_false_mask.nc
238ncks --alphabetize --history --overwrite --variable OceFrac dia_t${oce}_to_t${atm}_1storder_false_mask.nc  ${ATM}_grid_maskFrom_${OCE}.nc
239ncks --alphabetize --history --append    --variable aire    atm_grid.nc                                    ${ATM}_grid_maskFrom_${OCE}.nc
240[[ ${atm} = *ico* ]] && ncks --alphabetize --history --append --variable bounds_lon,bounds_lat atm_grid.nc ${ATM}_grid_maskFrom_${OCE}.nc
241
242cat <<EOF > creation_masque.nco
243where (OceFrac >  0.0 )  OceFrac=1 ;
244where (OceFrac <= 0.0 )  OceFrac=0 ;
245EOF
246ncap2 --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
247ncrename --history --variable OceFrac,OceMask dia_t${oce}_to_t${atm}_1storder_false_mask.nc
248
249ncks --alphabetize --history --append --variable OceMask dia_t${oce}_to_t${atm}_1storder_false_mask.nc   ${ATM}_grid_maskFrom_${OCE}.nc
250rm dia_t${oce}_to_t${atm}_1storder_false_mask.nc
251
252##
253## NEMO, other case, towards ATM
254## ===========================================================================
255for order in ${ListOrder} ; do
256    case ${order} in
257        ( 1st ) num_order=1 ;;
258        ( 2nd ) num_order=2 ;;
259    esac
260    for normalize in ${ListNormalize} ; do
261        for OCEGRID in ${ListOCEGRID} ; do
262            ocegrid=${OCEGRID,,}
263           
264            if [[ ! -f rmp_${ocegrid}${oce}_to_t${atm}_${order}order_${normalize}.nc ]] ; then
265               
266                echo ${Green}"${OCE} ${OCEGRID} toward ${ATM} - ${order}order - normalize: ${normalize}"${Norm}
267               
268                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}
269                python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                     -k type  -v ${oce_domain_type}
270                python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                     -k type  -v ${atm_domain_type}
271                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}
272                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}
273                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}"
274                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}
275                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}
276                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'         -t ${num_order}
277                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${normalize}
278                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                      -k type  -v ${oce_domain_type}
279                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                      -k type  -v ${atm_domain_type}
280                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}
281                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
282                python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize     -v ${normalize}
283               
284                cp iodef.xml iodef_${ocegrid}${oce}_t${atm}_${order}order_${normalize}.xml
285               
286                ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
287                ln -fs ${ATM}_grid.nc              atm_grid.nc
288                time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=yes
289            fi
290           
291        done
292    done
293done
294
295##
296## ATM towards NEMO points
297## ===========================================================================
298cp ${SUBMIT_DIR}/iodef_atm_to_oce.xml           iodef.xml
299
300python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                   -k type  -v ${atm_domain_type}
301python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                   -k type  -v ${oce_domain_type}
302python 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}"
303python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${atm_domain_type}
304python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t ${oce_domain_type}
305python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                    -k type  -v ${atm_domain_type}
306python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                    -k type  -v ${oce_domain_type}
307           
308for order in ${ListOrder} ; do
309    case ${order} in
310        ( 1st ) num_order=1 ;;
311        ( 2nd ) num_order=2 ;;
312    esac
313    for normalize in ${ListNormalize} ; do
314        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}
315        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'         -t ${num_order}   
316        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${normalize}
317        python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain'   -k renormalize     -v ${normalize}
318       
319        for OCEGRID in ${ListOCEGRID} ; do
320            ocegrid=${OCEGRID,,}
321           
322            echo ${Green}"${ATM} toward ${OCE} ${OCEGRID} - ${order}order - normalize: ${normalize}"${Norm}
323            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}
324            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}
325            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}
326            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
327            cp iodef.xml iodef_t${atm}_to_${ocegrid}${oce}_${order}order_${normalize}.xml
328           
329            ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
330            ln -fs ${ATM}_grid.nc              atm_grid.nc
331            time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=yes
332           
333        done
334    done
335done
336
337##
338## Creates mask of coastal OCE points
339## ===========================================================================
340python ComputeNemoCoast.py -n ${OcePerio} -i ${OCE}_coordinates_mask.nc # Creates OceCoastal
341##
342## Creates mask of coastal ATM points
343## ===========================================================================
344cat <<EOF > coastal.nco
345AtmCoastal = OceFrac * 0.0 ;
346where (OceFrac > 0.0 && OceFrac < 1.0 )  AtmCoastal = 1.0 ;
347EOF
348ncap2 --history --overwrite --script-file coastal.nco ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_coastal_maskFrom_${OCE}.nc
349ncks --history --append --variable AtmCoastal ${ATM}_coastal_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
350rm ${ATM}_coastal_maskFrom_${OCE}.nc
351ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
352##
353## ATM towards NEMO points - runoff
354## ===========================================================================
355cp ${SUBMIT_DIR}/iodef_atm_to_oce.xml           iodef.xml
356for normalize in ${ListNormalize} ; do
357    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]'                   -k type  -v ${atm_domain_type}
358    python update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]'                   -k type  -v ${oce_domain_type}
359    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}"
360    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}
361    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}
362    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]'                    -k type  -v ${atm_domain_type}
363    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]'                    -k type  -v ${oce_domain_type}
364    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1
365    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]'       -t 1
366    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${normalize}
367    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}
368    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
369    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
370    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}
371    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
372    python update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize     -v ${normalize}
373   
374    cp iodef.xml iodef_o${atm}_to_c${oce}_1storder_${normalize}.xml
375   
376    ln -fs ${OCE}_coordinates_mask.nc  oce_grid.nc
377    ln -fs ${ATM}_grid.nc              atm_grid.nc
378    time /usr/bin/time ${MPIRUN} ./interpol.exe --mask_src=yes --mask_dst=yes
379
380    # Now we should divide weight par source (atm) grid area : run-off is a quantity/s integrated on the grid mox, not a flux
381done
382##
383## Copy all NetCDF files to NetCDF 3 format (needed for OASIS)
384## ===========================================================================
385for InFile in *.nc ; do
386    OuFile=$(basename ${InFile} .nc)_${FMT_OASIS}.nc
387    if [[ ! -f ${OuFile} ]] ; then
388        ncks --alphabetize --history --fl_fmt=${FMT_OASIS} ${InFile} ${OuFile}
389    fi
390done
391
392##
393## Add time axis and coordinates information
394## (needed if files need to be read by XIOS)
395## ===========================================================================
396#ncatted --history -a coordinates,aire,m,c,"lat lon" ${ATM}_grid_maskFrom_${OCE}.nc
397ncap2 --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
398ncdump -h ${ATM}_grid_maskFrom_${OCE}.nc
399
400ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
401ncdump -h ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
402
403if [[ ${atm} = ico ]] ; then
404    cat <<EOF > add_time.nco
405OceFrac    [time_counter,cell] = OceFrac    [cell] ;
406OceMask    [time_counter,cell] = OceMask    [cell] ;
407AtmCoastal [time_counter,cell] = AtmCoastal [cell] ;
408EOF
409    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
410    ncatted --history \
411            -a coordinates,OceFrac,c,c,"time_counter cell"    \
412            -a coordinates,OceMask,c,c,"time_counter cell"    \
413            -a coordinates,AtmCoastal,c,c,"time_counter cell" \
414            -a coordinates,aire,c,c,"cell" \
415            ${ATM}_grid_maskFrom_${OCE}.nc
416fi
417ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
418
419if [[ ${atm} = lmd ]] ; then
420    cat <<EOF > add_time.nco
421OceFrac    [time_counter,lat,lon] = OceFrac    [lat,lon] ;
422OceMask    [time_counter,lat,lon] = OceMask    [lat,lon]  ;
423AtmCoastal [time_counter,lat,lon] = AtmCoastal [lat,lon]  ;
424EOF
425    ncap2 --overwrite --history --script-file add_time.nco ${ATM}_grid_maskFrom_${OCE}.nc tmp_${ATM}_grid_maskFrom_${OCE}.nc
426    ncdump -h tmp_${ATM}_grid_maskFrom_${OCE}.nc
427    mv tmp_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
428   
429    ncatted --history \
430            -a coordinates,OceFrac,m,c,"time_counter lat lon"    \
431            -a coordinates,OceMask,m,c,"time_counter lat lon"    \
432            -a coordinates,AtmCoastal,m,c,"time_counter lat lon" \
433            -a coordinates,aire,m,c,"lat lon" \
434            ${ATM}_grid_maskFrom_${OCE}.nc
435fi
436ncks --alphabetize --history --overwrite --mk_rec time_counter   ${ATM}_grid_maskFrom_${OCE}.nc tmp_${ATM}_grid_maskFrom_${OCE}.nc ; mv tmp_${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}.nc
437ncks --alphabetize --history --overwrite --fl_fmt=${FMT_OASIS} ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc
438
439##
440## Add some metadata in file headers
441## ===========================================================================
442
443UUID=$(uuid)
444NCO="$(ncks --version |& tail -1|sed 's/ncks //')"
445PYTHON_VER=$( python -i -c "import sys ; print (sys.version.split(' ')[0])" )
446for InFile in *${oce}_to_*${atm}_*.nc *${atm}_to_*${oce}_*.nc ${ATM}_grid_maskFrom_${OCE}.nc ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc; do
447    ncatted --history \
448            --attribute uuid,global,d,,                                           \
449            --attribute LongName,global,d,,                                       \
450            --attribute nco_openmp_thread_number,global,d,,                       \
451            --attribute Conventions,global,o,c,"CF-1.6"                           \
452            --attribute source,global,o,c,"IPSL Earth system model"               \
453            --attribute group,global,o,c,"ICMC IPSL Climate Modelling Center"     \
454            --attribute Institution,global,o,c,"IPSL https://www.ipsl.fr"         \
455            --attribute Ocean,global,o,c,"${OCE} https://www.nemo-ocean.eu"       \
456            --attribute Atmosphere,global,o,c,"${ATM} http://lmdz.lmd.jussieu.fr" \
457            --attribute production,global,o,c,"$(finger ${LOGNAME} | head -1 | awk '{print $4, $5}') " \
458            --attribute originalFiles,global,o,c,"${OCE}_coordinates_mask.nc ${ATM}_grid_mask.nc"      \
459            --attribute associatedFiles,global,o,c,"grids_${OCE}x${ATM}.nc areas_${OCE}x${ATM}.nc masks_${OCE}x${ATM}.nc" \
460            --attribute directory,global,o,c,"$(pwd)"                             \
461            --attribute description,global,o,c,"Fields needed by OASIS-MCT"       \
462            --attribute title,global,o,c,"${InFile}.nc"                           \
463            --attribute Program,global,o,c,"Generated by ${PROGRAM}"              \
464            --attribute timeStamp,global,o,c,"$(date)"                            \
465            --attribute uuid,global,o,c,"${UUID}"                                 \
466            --attribute HOSTNAME,global,o,c,"$(hostname)"                         \
467            --attribute LOGNAME,global,o,c,"$(whoami)"                            \
468            --attribute NCO,global,o,c,"NCO netCDF Operator ${NCO} http://nco.sourceforge.net" \
469            --attribute Python,global,o,c,"Python version ${PYTHON_VER}"          \
470            --attribute OS,global,o,c,"$(uname -o)"                               \
471            --attribute release,global,o,c,"$(uname -r)"                          \
472            --attribute hardware,global,o,c,"$(uname -i)"                         \
473            --attribute directory,global,o,c,"$(pwd)"                             \
474            --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" \
475            --attribute Comment,global,o,c,"Preliminary attempt - Do not trust !" \
476            --attribute SVN_Author,global,o,c,"$Author$"                 \
477            --attribute SVN_Date,global,o,c,"$Date$"                            \
478            --attribute SVN_Revision,global,o,c,"$Revision$"               \
479            --attribute SVN_Id,global,o,c,"$Id$"                                \
480            ${InFile}
481done
482
483
484##
485## Update and complete weights file to fit OASIS requested format
486## ===========================================================================
487cat <<EOF > add_dim.nco
488defdim("num_wgts",1) ;
489weight[n_weight, num_wgts] = weight ;
490EOF
491
492for rmpFile in rmp_*.nc ; do
493    mv ${rmpFile} xios_${rmpFile}
494    ncap2 --fl_fmt=${FMT_OASIS} --history --script-file add_dim.nco xios_${rmpFile} ${rmpFile}
495   
496    ncrename --history --dimension n_weight,num_links   ${rmpFile}
497    ncrename --history --variable  src_idx,src_address  ${rmpFile}
498    ncrename --history --variable  dst_idx,dst_address  ${rmpFile}
499    ncrename --history --variable  weight,remap_matrix  ${rmpFile}
500    case ${rmpFile} in
501        ( *_1storder_*.nc ) ncatted --history --attribute map_method,global,o,c,"Conservative Remapping - 1st order"  ${rmpFile} ;;
502        ( *_2ndorder_*.nc ) ncatted --history --attribute map_method,global,o,c,"Conservative Remapping - 2nd order"  ${rmpFile} ;;
503    esac
504    case ${rmpFile} in
505        ( *_true.nc  ) ncatted --history --attribute map_method,global,o,c,"Normalization: true"  ${rmpFile} ;;
506        ( *_false.nc ) ncatted --history --attribute map_method,global,o,c,"Normalization: false" ${rmpFile} ;;
507    esac
508    ncatted --history --attribute conventions,global,o,c,"SCRIP"   ${rmpFile}
509    ncatted --history --attribute normalization,global,o,c,"none"  ${rmpFile}
510   
511    case ${rmpFile} in
512        ( rmp_*${oce}_to_t${atm}_* )
513        ncatted --history \
514                --attribute title,global,o,c,"Weights ${OCE} to ${ATM}" \
515                --attribute source_grid,global,o,c,"${oce_domain_type}" \
516                --attribute dest_grid,global,o,c,"${atm_domain_type}"   \
517                ${rmpFile}     
518        ;;
519        ( rmp_*${atm}_to_*${oce}_* )
520        ncatted --history \
521                --attribute title,global,o,c,"Weights ${ATM} to ${OCE}" \
522                --attribute source_grid,global,o,c,"${atm_domain_type}" \
523                --attribute dest_grid,global,o,c,"${oce_domain_type}"   \
524                ${rmpFile}
525        ;;
526    esac
527done
528
529##
530## Add missing variables in rmp files
531## ===========================================================================
532for rmpFile in rmp_?${atm}_to_[tuv]${oce}_*order_*.nc rmp_[tuv]${oce}_to_t${atm}_*order_*.nc* ; do
533    echo ${rmpFile}
534    a_to_o=false ; o_to_a=false
535    case ${rmpFile} in
536        (  rmp_?${oce}_to_?${atm}_*.nc ) o_to_a=true ;;
537        (  rmp_?${atm}_to_?${oce}_*.nc ) a_to_o=true ;;
538    esac
539
540    for Grid in t u v o c ; do # Identify grids
541        [[ ${rmpFile} = rmp_${Grid}${oce}_to_?${atm}_*.nc ]] && ogrid=${Grid}
542        [[ ${rmpFile} = rmp_?${oce}_to_${Grid}${atm}_*.nc ]] && agrid=${Grid}
543        [[ ${rmpFile} = rmp_${Grid}${atm}_to_?${oce}_*.nc ]] && agrid=${Grid}
544        [[ ${rmpFile} = rmp_?${atm}_to_${Grid}${oce}_*.nc ]] && ogrid=${Grid}
545    done
546    OGRID=${ogrid^}
547    AGRID=${agrid^}
548       
549    cat <<EOF >add_varoce.nco
550defdim ("src_grid_size"   , \$x_grid_${OGRID}.size*\$y_grid_${OGRID}.size) ;
551defdim ("src_grid_corners", 4) ;
552defdim ("src_grid_rank"   , 2) ;
553//
554src_grid_dims[src_grid_rank] = { \$y_grid_${OGRID}.size, \$x_grid_${OGRID}.size } ;
555//
556src_grid_center_lat [src_grid_size] =  0.0d ;
557src_grid_center_lon [src_grid_size] =  0.0d ;
558src_grid_center_lat (:) = nav_lat_grid_${OGRID}(:,:)   ;
559src_grid_center_lon (:) = nav_lon_grid_${OGRID}(:,:)   ;
560//
561src_grid_corner_lat [src_grid_size, src_grid_corners] = 0.0d ;
562src_grid_corner_lon [src_grid_size, src_grid_corners] = 0.0d ;
563src_grid_corner_lat(:,:) = bounds_lat_grid_${OGRID}(:,:,:) ;
564src_grid_corner_lon(:,:) = bounds_lon_grid_${OGRID}(:,:,:) ;
565//
566src_grid_imask [src_grid_size] =    0 ;
567src_grid_area  [src_grid_size] = 0.0d ;
568src_grid_frac  [src_grid_size] = 1.0d ;
569src_grid_imask (:) = 1 - mask_${OGRID}(:,:) ;
570src_grid_imask.int() ;
571src_grid_area  (:) = area_grid_${OGRID}(:,:) ;
572EOF
573
574    [[ ${o_to_a} = true ]] && ncap2 --history --append --script-file add_varoce.nco ${OCE}_coordinates_mask_${FMT_OASIS}.nc ${rmpFile}
575    sed --in-place "s/src_/dst_/g" add_varoce.nco
576    [[ ${a_to_o} = true ]] && ncap2 --history --append --script-file add_varoce.nco ${OCE}_coordinates_mask_${FMT_OASIS}.nc ${rmpFile}
577               
578    if [[ ${atm} = ico ]] ; then
579        cat <<EOF >add_varatm.nco
580defdim ("dst_grid_size"   , \$cell.size) ;
581defdim ("dst_grid_corners", 6) ;
582defdim ("dst_grid_rank"   , 2) ;
583//
584dst_grid_dims[dst_grid_rank] = { \$cell.size, 1 } ;
585//
586dst_grid_center_lat [dst_grid_size] =  0.0d ;
587dst_grid_center_lon [dst_grid_size] =  0.0d ;
588dst_grid_center_lat (:) = lat(:)   ;
589dst_grid_center_lon (:) = lon(:)   ;
590//
591dst_grid_corner_lat [dst_grid_size, dst_grid_corners] = 0.0d ;
592dst_grid_corner_lon [dst_grid_size, dst_grid_corners] = 0.0d ;
593dst_grid_corner_lat(:,:) = bounds_lat(:,:) ;
594dst_grid_corner_lon(:,:) = bounds_lon(:,:) ;
595//
596dst_grid_imask [dst_grid_size] =    0 ;
597dst_grid_area  [dst_grid_size] = 0.0d ;
598dst_grid_frac  [dst_grid_size] = 1.0d ;
599dst_grid_imask (:) = 1 - OceMask(0,:) ;
600dst_grid_imask.int() ;
601dst_grid_area  (:) = aire(:) ;
602dst_grid_frac  (:) = OceFrac(0,:) ;
603EOF
604        if [[ ${o_to_a} = true ]] ; then
605            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
606            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
607            mv rmp_tmp.nc ${rmpFile}
608        fi
609                   
610        sed --in-place "s/dst_/src_/g" add_varatm.nco
611        if [[ ${a_to_o} = true ]] ; then
612            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
613            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
614            mv rmp_tmp.nc ${rmpFile}
615        fi
616    fi
617   
618    if [[ ${atm} = lmd ]] ; then
619        cat <<EOF >add_varatm.nco
620defdim ("dst_grid_size"   , \$lon.size*\$lat.size) ;
621defdim ("dst_grid_corners", 4) ;
622defdim ("dst_grid_rank"   , 2) ;
623//
624dst_grid_dims[dst_grid_rank] = { \$lat.size, \$lon.size } ;
625//
626dst_grid_center_lat [dst_grid_size] =  0.0d ;
627dst_grid_center_lon [dst_grid_size] =  0.0d ;
628lat0lon[lat,lon] = lat(:)+0*lon(:) ;
629lon0lat[lat,lon] = lon(:)+0*lat(:) ;
630dst_grid_center_lat (:) = lat0lon(:,:)   ;
631dst_grid_center_lon (:) = lon0lat(:,:)   ;
632//
633//dst_grid_corner_lat [dst_grid_size, dst_grid_corners] = 0.0d ; // Not available for LMDZ lon/lat grid
634//dst_grid_corner_lon [dst_grid_size, dst_grid_corners] = 0.0d ;
635//dst_grid_corner_lat(:,:) = bounds_lat(:,:) ;
636//dst_grid_corner_lon(:,:) = bounds_lon(:,:) ;
637//
638dst_grid_imask [dst_grid_size] =    0 ;
639dst_grid_area  [dst_grid_size] = 0.0d ;
640dst_grid_frac  [dst_grid_size] = 1.0d ;
641dst_grid_imask (:) = 1 - OceMask(0,:,:) ;
642dst_grid_imask.int() ;
643dst_grid_area  (:) = aire(:,:) ;
644dst_grid_frac (:)  = OceFrac(0,:,:) ;
645EOF
646        if [[ ${o_to_a} = true ]] ; then
647            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
648            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
649            mv rmp_tmp.nc ${rmpFile}
650        fi
651       
652        sed --in-place "s/dst/src/g" add_varatm.nco
653        if [[ ${a_to_o} = true ]] ; then
654            ncap2 --history --append --script-file add_varatm.nco ${ATM}_grid_maskFrom_${OCE}_${FMT_OASIS}.nc ${rmpFile}
655            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
656            mv rmp_tmp.nc ${rmpFile}
657        fi
658    fi
659done
660ls -al ${OCE}_coordinates_mask.nc
661##
662## Save results
663## ===========================================================================
664cp ${ATM}_grid_maskFrom_${OCE}.nc ${SUBMIT_DIR}
665for File in dia_*.nc rmp_*.nc
666do
667    ncatted --history --attribute history_of_appended_files,global,d,c,"" ${File}
668    NewFile=$(echo ${File} | sed -e "s/${atm}/${ATM}/" -e "s/${oce}/${OCE}/" )
669    cp ${File} ${SUBMIT_DIR}/${NewFile}
670done
671
672##
673## Creates and save auxiliary files for OASIS
674## ===========================================================================
675bash ${SUBMIT_DIR}/CreateOasisGrids.bash --oce ${OCE} --atm ${ATM}
676
677cp areas_${OCE}x${ATM}.nc ${SUBMIT_DIR}
678cp grids_${OCE}x${ATM}.nc ${SUBMIT_DIR}
679cp masks_${OCE}x${ATM}.nc ${SUBMIT_DIR}
680
681
682## ===========================================================================
683##
684##                               That's all folk's !!!
685##
686## ===========================================================================
687
Note: See TracBrowser for help on using the repository browser.