source: TOOLS/MOSAIX/CreateOasisGrids.bash @ 3623

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

O.M.: add SVN properties

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