source: TOOLS/MOSAIX/CreateOasisGrids.bash @ 3620

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

O.M. : continue first commit

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