source: TOOLS/MOSAIX/CreateOasisGrids.bash @ 3740

Last change on this file since 3740 was 3718, checked in by omamce, 3 years ago

O.M. : add computation of runoff weights

  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 17.9 KB
Line 
1#!/bin/bash
2### ===========================================================================
3###
4### Creates grids.nc, masks.nc and areas.nc for OASIS-MCT
5###
6### ===========================================================================
7##
8##  Warning, to install, configure, run, use any of Olivier Marti's
9##  software or to read the associated documentation you'll need at least
10##  one (1) brain in a reasonably working order. Lack of this implement
11##  will void any warranties (either express or implied).
12##  O. Marti assumes no responsability for errors, omissions,
13##  data loss, or any other consequences caused directly or indirectly by
14##  the usage of his software by incorrectly or partially configured
15##  personal.
16##
17###
18### Documentation : https://forge.ipsl.jussieu.fr/igcmg/wiki/IPSLCM6/MOSAIX
19###
20## SVN information
21#  $Author$
22#  $Date$
23#  $Revision$
24#  $Id$
25#  $HeadURL$
26
27##
28## Default configuration (may be overrided by command line parameters)
29## ===========================================================================
30OCE=ORCA2.3
31# OCE=eORCA1.2
32ATM=ICO30
33#ATM=ICO40
34# ATM=ICO450
35# ATM=LMD144142
36# ATM=LMD9695
37
38## ===========================================================================
39##
40## You should not change anything below this line ....
41##
42## ===========================================================================
43
44# Defines computer
45# ================
46if [[ $(hostname) = curie* ]] ; then arch=curie ; center=tgcc ; fi
47if [[ $(hostname) = irene* ]] ; then arch=irene ; center=tgcc ; fi
48PROGRAM=$(basename ${0})
49
50case ${arch} in
51    ( curie | irene )
52    set +vx
53    module unload cdo nco ferret
54    module unload netcdf hdf5
55    module load python # /2.7.12
56    module load netcdf/4.3.3.1_hdf5_parallel # Version for XIOS
57    module load nco
58    R_IN=$(ccc_home -u igcmg --cccwork)/IGCM
59    TMPDIR=${SCRATCHDIR}/TMP
60    SUBMIT_DIR=${BRIDGE_MSUB_PWD:-${SUBMIT_DIR}}
61    ;;
62    ( * )
63    exit -1
64    ;;
65esac
66##
67## Command line parameters
68## ===========================================================================
69set -e
70set -o verbose
71set -o xtrace
72
73while [[ ${1} = -* ]] ; do
74    case ${1} in
75        ( -- ) shift ; break ;;
76        ( -o=* | --oce=*    ) OCE=${1##*=}     ; echo OCE:${OCE}    ;;
77        ( -o   | --oce      ) shift ; OCE=${1} ; echo OCE:${OCE}    ;;
78        ( -a=* | --atm=*    ) ATM=${1##*=}     ; echo ATM:${ATM}    ;;
79        ( -a   | --atm      ) shift ; ATM=${1} ; echo ATM:${ATM}    ;;
80        ( -v | --verbose    ) set -o verbose   ;;
81        ( -x | --xtrace     ) set -o xtrace    ;;
82        ( -e                ) set -e           ;;
83        ( -V | --noverbose  ) set +o verbose   ;;
84        ( -X | --noxtrace   ) set +o xtrace    ;;
85        ( -E                ) set +e           ;;
86        ( -* ) echo ${Bold}"Unknown option : ${1}"${Norm} ; return 1 ;;
87    esac
88    shift
89done
90
91##
92## Short names (grid names in Oasis)
93## ===========================================================================
94case ${OCE} in
95    ( *ORC*) oce=orc ;;
96esac
97case ${ATM} in
98    ( *ICO*         ) atm=ico ;;
99    ( *dynamico*    ) atm=ico ;;
100    ( *lmd* | *LMD* ) atm=lmd ;;
101esac
102
103CplModel=${OCE}x${ATM}
104
105##
106## Get input files
107## ===========================================================================
108[[ -f ${OCE}_coordinates_mask.nc ]] || cp ${R_IN}/OCE/NEMO/${OCE}/${OCE}_coordinates_mask.nc  .
109[[ -f ${ATM}_grid.nc             ]] || cp ${R_IN}/ATM/GRID/${ATM}_grid.nc                   .
110
111#
112# Format for OASIS-MCT files : should be NetCDF3 classic or NetCDF3 64 bits
113# ---------------------------------------------------------------------------
114FL_FMT=64bit
115
116#
117# Cleaning
118# ----------------------------------------------------------------------------
119rm -f grids_${CplModel}.nc masks_${CplModel}.nc areas_${CplModel}.nc
120rm  -f *_tmp.nc
121
122#
123# Creates NetCDF3 version of input files
124# ----------------------------------------------------------------------------
125ncks --overwrite --fl_fmt=${FL_FMT} --history ${OCE}_coordinates_mask.nc      ${OCE}_coordinates_mask_${FL_FMT}.nc
126ncks --overwrite --fl_fmt=${FL_FMT} --history ${ATM}_grid_maskFrom_${OCE}.nc  ${ATM}_grid_maskFrom_${OCE}_${FL_FMT}.nc
127#
128# Creates ocean fields
129# ----------------------------------------------------------------------------
130
131for OCEGRID in T U V ; do
132    ncks --fl_fmt=${FL_FMT} -C --history --append --variable nav_lon_grid_${OCEGRID},nav_lat_grid_${OCEGRID},bounds_lon_grid_${OCEGRID},bounds_lat_grid_${OCEGRID} ${OCE}_coordinates_mask_${FL_FMT}.nc grids_${CplModel}.nc
133    ncks --fl_fmt=${FL_FMT} -C --history --append --variable area_grid_${OCEGRID} ${OCE}_coordinates_mask_${FL_FMT}.nc areas_${CplModel}.nc
134    # Inverts mask values and switch to integer
135    ncks --fl_fmt=${FL_FMT} --history -C --variable mask_${OCEGRID} ${OCE}_coordinates_mask_${FL_FMT}.nc mask_${OCEGRID}_tmp.nc
136    ncatted --history \
137            --attribute coordinates,mask_${OCEGRID},d,,         \
138            --attribute online_operation,mask_${OCEGRID},d,,    \
139            --attribute cell_measures,mask_${OCEGRID},d,,       \
140    mask_${OCEGRID}_tmp.nc
141   
142    ncap2 --fl_fmt=${FL_FMT} --history --append --script "mask_${OCEGRID}=int(1-mask_${OCEGRID});"    mask_${OCEGRID}_tmp.nc masks_${CplModel}.nc
143    ncatted --history \
144            --attribute long_name,mask_${OCEGRID},o,c,"Land-sea mask" \
145            --attribute units,mask_${OCEGRID},o,c,"Land:1, Ocean:0"    masks_${CplModel}.nc
146    # Change order of dimensions
147    mv grids_${CplModel}.nc grids_${CplModel}_tmp.nc
148    ncpdq --permute nvertex_grid_${OCEGRID},y_grid_${OCEGRID},x_grid_${OCEGRID} grids_${CplModel}_tmp.nc grids_${CplModel}.nc
149done
150
151ncks --history --append --variable lon,lat ${ATM}_grid_maskFrom_${OCE}_${FL_FMT}.nc grids_${CplModel}.nc
152
153#
154# We need to have different names for dimensions and variables
155# ----------------------------------------------------------------------------
156ncrename --history --variable lon,alon   grids_${CplModel}.nc
157ncrename --history --variable lat,alat   grids_${CplModel}.nc
158
159if [[ ${ATM} = dynamico* || ${ATM} = ICO* ]] ; then
160    ncks --fl_fmt=${FL_FMT} --history --append --variable bounds_lon,bounds_lat ${ATM}_grid_maskFrom_${OCE}_${FL_FMT}.nc grids_${CplModel}.nc
161fi
162
163ncks --fl_fmt=${FL_FMT} --history -C --append --variable aire   ${ATM}_grid_maskFrom_${OCE}_${FL_FMT}.nc areas_${CplModel}.nc
164ncatted --history --attribute coordinates,aire,d,,     \
165        --attribute _FillValue,aire,d,,                \
166        --attribute missing_value,aire,d,,             \
167        --attribute online_operation,aire,d,,          \
168        --attribute units,aire,o,c,"m2"                \
169        --attribute standard_name,aire,o,c,"cell_area" \
170        areas_${CplModel}.nc
171
172ncks --fl_fmt=${FL_FMT} --history -C --variable OceMask         ${ATM}_grid_maskFrom_${OCE}_${FL_FMT}.nc OceMask_tmp.nc
173ncatted --history \
174        --attribute coordinates,OceMask,d,,         \
175        --attribute online_operation,OceMask,d,,    \
176        --attribute _FillValue,OceMask,d,,          \
177        --attribute missing_value,OceMask,d,,       \
178        OceMask_tmp.nc
179ncap2 --fl_fmt=${FL_FMT} --history --append --script "OceMask=int(1-OceMask)"            OceMask_tmp.nc masks_${CplModel}.nc # For OASIS, ocean=0, land=1
180ncatted --history \
181        --attribute long_name,OceMask,o,c,"Land-sea mask"   \
182        --attribute units,OceMask,o,c,"Land:1, Ocean:0"  masks_${CplModel}.nc
183
184ncatted --history --attribute history,global,d,, grids_${CplModel}.nc
185ncatted --history --attribute history,global,d,, masks_${CplModel}.nc
186ncatted --history --attribute history,global,d,, areas_${CplModel}.nc
187
188if [[ ${atm} = *dynamico* || ${atm} = ico* ]] ; then
189    ncrename --history --dimension cell,ycell grids_${CplModel}.nc
190    ncrename --history --dimension cell,ycell areas_${CplModel}.nc
191    ncrename --history --dimension cell,ycell masks_${CplModel}.nc
192fi
193# if [[ ${atm} = *lmd* ]] ; then
194#     ncrename --dimension lon,tlmdlon  grids_${CplModel}.nc
195#     ncrename --dimension lat,tlmdlat  grids_${CplModel}.nc
196#     ncrename --dimension lon,tlmdlon  masks_${CplModel}.nc
197#     ncrename --dimension lat,tlmdlat  masks_${CplModel}.nc
198#     ncrename --dimension lon,tlmdlon  areas_${CplModel}.nc
199#     ncrename --dimension lat,tlmdlat  areas_${CplModel}.nc
200# fi
201
202#
203# DYNAMICO grids :
204#  - Set correct order of dimensions
205#  - Add a dimension
206# ----------------------------------------------------------------------------
207if [[ ${atm} = dynamico* || ${atm} = ico* ]] ; then
208    mv grids_${CplModel}.nc grids_${CplModel}_tmp.nc
209    ncpdq --permute nvertex,ycell,xcell  grids_${CplModel}_tmp.nc grids_${CplModel}.nc
210   
211    mv grids_${CplModel}.nc grids_${CplModel}_tmp.nc
212    cat <<EOF >add_dim.nco
213//
214defdim("xcell",1) ;
215alat               [ycell,xcell] = alat       ;
216alon               [ycell,xcell] = alon       ;
217bounds_lat [nvertex,ycell,xcell] = bounds_lat ;
218bounds_lon [nvertex,ycell,xcell] = bounds_lon ;
219//
220EOF
221   
222    ncap2 --fl_fmt=${FL_FMT} --history --append --script-file add_dim.nco  grids_${CplModel}_tmp.nc grids_${CplModel}.nc
223   
224    mv masks_${CplModel}.nc masks_${CplModel}_tmp.nc
225    ncap2 --fl_fmt=${FL_FMT} --history --append --script 'defdim("xcell",1) ; OceMask[time_counter,ycell,xcell]=OceMask(:,:)'  masks_${CplModel}_tmp.nc masks_${CplModel}.nc
226
227    mv areas_${CplModel}.nc areas_${CplModel}_tmp.nc
228    ncap2 --fl_fmt=${FL_FMT} --history --append --script 'defdim("xcell",1) ; aire[ycell,xcell]=aire(:) ; '                       areas_${CplModel}_tmp.nc areas_${CplModel}.nc
229fi
230
231#
232# Some changes specific to LMDZ lon/lat grid
233# ----------------------------------------------------------------------------
234if [[ ${atm} = lmd* ]] ; then
235    # Spreads lon and lat to get 2D fields
236    # -----------------------------------------------------------------------
237    mv grids_${CplModel}.nc grids_${CplModel}_tmp.nc
238    ncap2 --fl_fmt=${FL_FMT} --history --append --script "alon[lat,lon]=alon; alat[lat,lon]=alat"  grids_${CplModel}_tmp.nc grids_${CplModel}.nc
239    ncatted --history \
240            --attribute long_name,alon,o,c,"Longitude"    \
241            --attribute long_name,alat,o,c,"Latitude"     \
242            --attribute units,alon,o,c,"degrees_east"     \
243            --attribute units,alat,o,c,"degrees_north"    \
244            --attribute grid_type,alon,o,c,"P"            \
245            --attribute grid_type,alat,o,c,"P"            \
246            --attribute overlap,alon,o,i,0                \
247            --attribute overlap,alat,o,i,0                grids_${CplModel}.nc
248   
249    # Generates cell corners
250    # -----------------------------------------------------------------------
251cat <<EOF > add_bounds.nco
252defdim ("nvertex_lmd", 4) ;
253*delta_x = alon(0,1) - alon(0,0) ;
254*delta_y = alat(1,0) - alat(0,0) ;
255//
256bounds_lon[nvertex_lmd,lat,lon] = 0.0 ;
257bounds_lat[nvertex_lmd,lat,lon] = 0.0 ;
258//
259bounds_lon (0,:,:) = alon + delta_x*0.5 ;
260bounds_lat (0,:,:) = alat + delta_y*0.5 ;
261bounds_lon (1,:,:) = alon + delta_x*0.5 ;
262bounds_lat (1,:,:) = alat - delta_y*0.5 ;
263bounds_lon (2,:,:) = alon - delta_x*0.5 ;
264bounds_lat (2,:,:) = alat - delta_y*0.5 ;
265bounds_lon (3,:,:) = alon - delta_x*0.5 ;
266bounds_lat (3,:,:) = alat + delta_y*0.5 ;
267//
268where (bounds_lat >  90.0 ) bounds_lat =  90.0 ;;
269where (bounds_lat < -90.0 ) bounds_lat = -90.0 ;;
270//
271EOF
272    mv grids_${CplModel}.nc grids_${CplModel}_tmp.nc
273    ncap2 --fl_fmt=${FL_FMT} --history --script-file add_bounds.nco -O grids_${CplModel}_tmp.nc grids_${CplModel}.nc
274fi
275
276#
277# Generates grid "o${atm}e" : same as t${atm} grid, with surfaces set to 1
278# and mask to 0 (ocean everywhere, to compute integral over the whole grid))
279# This grid is used when field are quantities instead of fluxes (i.e river flow)
280# ----------------------------------------------------------------------------
281mv grids_${CplModel}.nc grids_${CplModel}_tmp.nc
282ncap2 --fl_fmt=${FL_FMT} --history --script "o${atm}_lon=alon; o${atm}_lat=alat; bounds_o${atm}_lon=bounds_lon; bounds_o${atm}_lat=bounds_lat; " grids_${CplModel}_tmp.nc grids_${CplModel}.nc
283
284mv areas_${CplModel}.nc areas_${CplModel}_tmp.nc
285ncap2 --fl_fmt=${FL_FMT} --history --script "o${atm}_aire=aire*0.0d+1.0d; "   areas_${CplModel}_tmp.nc areas_${CplModel}.nc
286
287mv masks_${CplModel}.nc masks_${CplModel}_tmp.nc
288ncap2 --fl_fmt=${FL_FMT} --history --script "o${atm}_mask=int(OceMask)*0+0; " masks_${CplModel}_tmp.nc masks_${CplModel}.nc
289
290#
291# Final renaming
292# ----------------------------------------------------------------------------
293for OCEGRID in T U V
294do
295    ocegrid=${OCEGRID~} # To lowercase
296    ncrename --history --variable nav_lon_grid_${OCEGRID},${ocegrid}${oce}.lon     grids_${CplModel}.nc
297    ncrename --history --variable nav_lat_grid_${OCEGRID},${ocegrid}${oce}.lat     grids_${CplModel}.nc
298    ncrename --history --variable bounds_lon_grid_${OCEGRID},${ocegrid}${oce}.clo  grids_${CplModel}.nc
299    ncrename --history --variable bounds_lat_grid_${OCEGRID},${ocegrid}${oce}.cla  grids_${CplModel}.nc
300    ncrename --history --variable mask_${OCEGRID},${ocegrid}${oce}.msk             masks_${CplModel}.nc
301    ncrename --history --variable area_grid_${OCEGRID},${ocegrid}${oce}.srf        areas_${CplModel}.nc
302   
303    ncatted --history \
304            --attribute bounds,${ocegrid}${oce}.lon,m,c,"${ocegrid}${oce}.clo"  \
305            --attribute bounds,${ocegrid}${oce}.lat,m,c,"${ocegrid}${oce}.cla"  \
306            --attribute units,${ocegrid}${oce}.clo,o,c,"degrees_east"        \
307            --attribute units,${ocegrid}${oce}.clo,o,c,"degrees_north" grids_${CplModel}.nc
308done
309
310ncrename --history --variable alon,t${atm}.lon                grids_${CplModel}.nc
311ncrename --history --variable alat,t${atm}.lat                grids_${CplModel}.nc
312ncrename --history --variable o${atm}_lon,o${atm}.lon         grids_${CplModel}.nc
313ncrename --history --variable o${atm}_lat,o${atm}.lat         grids_${CplModel}.nc
314ncrename --history --variable OceMask,t${atm}.msk             masks_${CplModel}.nc
315ncrename --history --variable o${atm}_mask,o${atm}.msk        masks_${CplModel}.nc
316ncrename --history --variable aire,t${atm}.srf                areas_${CplModel}.nc
317ncrename --history --variable o${atm}_aire,o${atm}.srf        areas_${CplModel}.nc
318
319ncrename --history --variable bounds_lon,t${atm}.clo          grids_${CplModel}.nc
320ncrename --history --variable bounds_lat,t${atm}.cla          grids_${CplModel}.nc
321ncrename --history --variable bounds_o${atm}_lon,o${atm}.clo  grids_${CplModel}.nc
322ncrename --history --variable bounds_o${atm}_lat,o${atm}.cla  grids_${CplModel}.nc
323
324for atmgrid in t o ; do
325    ncatted --history \
326            --attribute bounds,${atmgrid}${atm}.lon,c,c,"${atmgrid}${atm}.clo"   \
327            --attribute bounds,${atmgrid}${atm}.lat,c,c,"${atmgrid}${atm}.cla"   \
328            --attribute units,${atmgrid}${atm}.clo,o,c,"degrees_east"            \
329            --attribute units,${atmgrid}${atm}.cla,o,c,"degrees_north"        grids_${CplModel}.nc
330done
331
332##
333## Change all NetCDF files to NetCDF 3 format
334## ===========================================================================
335for InFile in areas_${CplModel}.nc grids_${CplModel}.nc masks_${CplModel}.nc ; do
336    if [[ $(ncdump -k ${InFile}) = *netCDF-4* ]] ; then
337        mv ${InFile} tmp_${InFile}
338        ncks --fl_fmt=${FL_FMT} --history  tmp_${InFile}  ${InFile}
339    fi
340done
341
342##
343## Add some useful information in NetCDF headers
344## ===========================================================================
345UUID=$(uuid)
346NCO=$(ncks --version |& tail -1|sed 's/ncks //')
347for type in areas masks grids ; do
348    ncatted --history \
349            --attribute nco_openmp_thread_number,global,d,,            \
350            --attribute uuid,global,d,,                                \
351            --attribute LongName,global,d,,                            \
352            --attribute NCO,global,o,c,"NCO netCDF Operator ${NCO} http://nco.sourceforge.net" \
353            --attribute Conventions,global,o,c,"CF-1.6"                \
354            --attribute source,global,o,c,"IPSL Earth system model"    \
355            --attribute group,global,o,c,"ICMC IPSL Climate Modelling Center" \
356            --attribute production,global,o,c,"$(finger ${LOGNAME} | head -1 | awk '{print $4, $5}') " \
357            --attribute Institution,global,o,c,"IPSL https://www.ipsl.fr" \
358            --attribute Ocean,global,o,c,"${OCE} https://www.nemo-ocean.eu"  \
359            --attribute Atmosphere,global,o,c,"${ATM} http://lmdz.lmd.jussieu.fr" \
360            --attribute HOSTNAME,global,o,c,"$(hostname)"              \
361            --attribute LOGNAME,global,o,c,"$(whoami)"                 \
362            --attribute Program,global,o,c,"Generated by ${0}"         \
363            --attribute name,global,o,c,"${type}_${CplModel}.nc"       \
364            --attribute directory,global,o,c,"$(pwd)"                  \
365            --attribute description,global,o,c,"Fields needed by OASIS-MCT" \
366            --attribute title,global,o,c,"${type}_${CplModel}.nc"      \
367            --attribute timeStamp,global,o,c,"$(date)"                 \
368            --attribute associatedFiles,global,o,c,"grids_${CplModel}.nc areas_${CplModel}.nc masks_${CplModel}.nc" \
369            --attribute originalFiles,global,o,c,"${OCE}_coordinates_mask.nc ${ATM}_grid_mask.nc"\
370            --attribute uuid,global,o,c,"${UUID}"                      \
371            --attribute OS,global,o,c,"$(uname -o)"                    \
372            --attribute release,global,o,c,"$(uname -r)"               \
373            --attribute hardware,global,o,c,"$(uname -i)"              \
374            --attribute Comment,global,o,c,"Preliminary attempt - Do not trust !" \
375            --attribute SVN_Author,global,o,c,"$Author$"      \
376            --attribute SVN_Date,global,o,c,"$Date$" \
377            --attribute SVN_Revision,global,o,c,"$Revision$"                    \
378            --attribute SVN_Id,global,o,c,"$Id$" \
379            --attribute SVN_HeardURL,global,o,c,"$HeadURL $" \
380            ${type}_${CplModel}.nc
381done
382
383##
384## Cleaning
385## ===========================================================================
386#rm  *_tmp.nc ${OCE}_coordinates_mask_${FL_FMT}.nc ${ATM}_grid_maskFrom_${OCE}_${FL_FMT}.nc *.nco
387
388exit
389#
390# Name of the dimensions ?
391# ----------------------------------------------------------------------------
392for OCEGRID in T U V ; do
393    ocegrid=${OCEGRID~} # Downcase name of grids
394    ncrename --history --dimension  x_grid_${OCEGRID},x${ocegrid}${oce}         grids_${CplModel}.nc
395    ncrename --history --dimension  y_grid_${OCEGRID},y${ocegrid}${oce}         grids_${CplModel}.nc
396    ncrename --history --dimension  nvertex_grid_${OCEGRID},crn${ocegrid}${oce} grids_${CplModel}.nc
397done
398
399## ===========================================================================
400##
401##                               That's all folk's !!!
402##
403## ===========================================================================
Note: See TracBrowser for help on using the repository browser.