source: TOOLS/MOSAIX/CreateOasisGrids.bash @ 3913

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

O.M. : supress computation for coastal grid

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