source: TOOLS/MOSAIX/CreateWeightsMask.bash @ 3912

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

O.M. : small change to bypass a bug in nco

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