source: TOOLS/MOSAIX/CreateOasisGrids.bash @ 3640

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

O.M. : remove erroneous keywod $Log

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