source: trunk/SRC/Grid/smallmeshmask.pro @ 451

Last change on this file since 451 was 451, checked in by smasson, 13 years ago

update meshmask related file for full compatibility with partial steps

  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1;+
2;
3; @categories
4; For OPA
5;
6; @param inid {in}{required}
7;
8; @param outid {in}{required}
9;
10; @param inname {in}{required}
11;
12; @param outname {in}{optional}
13;
14; @history
15;      July 2004 Sebastien Masson (smasson\@lodyc.jussieu.fr)
16;
17; @version
18; $Id$
19;
20;-
21PRO ncdf_transfer, inid, outid, inname, outname
22;
23  compile_opt idl2, strictarrsubs
24;
25  IF n_elements(outname) EQ 0 THEN outname = inname
26  ncdf_varget, inid, inname, zzz
27  ncdf_varput, outid, outname, float(reform(zzz, /over))
28  RETURN
29END
30;
31;+
32;
33; @file_comments
34; Reduce the size of the NetCDF meshmask created by OPA by
35; using byte format for the masks and the float format for the other
36; fields.
37;
38; @keyword IODIR
39; to define the files path.
40;
41; @param ncfilein {in}{required}
42; 1) the name of the meshmask file to be reduced. In that case,
43; there is only one meshmask file
44;
45; OR
46;
47; 2) the xxx part in the names: xxx.mesh_hgr.nc xxx.mesh_zgr.nc
48; xxx.mask.nc. In that case, the meshmask is split into 3 files.
49;
50; @param ncfileout {in}{optional}{default=smallmeshmask.nc}
51; the name of the reduced meshmask file.
52;
53; @examples
54;
55;   IDL> meshdir='/d1fes2-raid2/smasson/DATA/ORCA05/'
56;   IDL> smallmeshmask, 'meshmask_ORCA_R05.nc',iodir=meshdir
57;
58; @categories
59; For OPA
60;
61; @history
62;      July 2004 Sebastien Masson (smasson\@lodyc.jussieu.fr)
63;
64; @version
65; $Id$
66;
67;-
68PRO smallmeshmask, ncfilein, ncfileout, IODIR=iodir
69;
70  compile_opt idl2, strictarrsubs
71;
72  filein = isafile(FILE = ncfilein, IODIR = iodir, /NEW)
73  test = (findfile(filein))[0]
74  IF test EQ '' THEN BEGIN
75    filein_hgr = (findfile(filein+'.mesh_hgr.nc'))[0]
76    filein_zgr = (findfile(filein+'.mesh_zgr.nc'))[0]
77    filein_msk = (findfile(filein+'.mask.nc'))[0]
78    IF filein_hgr EQ '' OR filein_zgr EQ '' OR filein_msk EQ ''  THEN BEGIN
79      ras = report(['meshmask file(s) not found...', $
80      filein+' does not exist', $
81      filein+'.mesh_hgr.nc does not exist', $
82      filein+'.mesh_zgr.nc does not exist', $
83      filein+'.mask.nc does not exist'])
84      return
85    ENDIF
86  ENDIF ELSE filein = test
87;------------------------------------------------------
88;------------------------------------------------------
89; get the horizontal dimensions
90  IF n_elements(filein_hgr) NE 0  THEN cdfid = ncdf_open(filein_hgr) $
91  ELSE cdfid = ncdf_open(filein)
92  ncdf_diminq, cdfid, 'x', name, jpi
93  ncdf_diminq, cdfid, 'y', name, jpj
94; get the vertical dimensions
95  IF n_elements(filein_zgr) NE 0  THEN BEGIN
96    ncdf_close, cdfid
97    cdfid = ncdf_open(filein_zgr)
98  ENDIF
99  listdims = strlowcase(ncdf_listdims(cdfid))
100  IF (where(listdims EQ 'z'))[0] NE -1 THEN ncdf_diminq, cdfid, 'z', name, jpk ELSE BEGIN
101    dimid = (where(strmid(listdims, 0, 5) EQ 'depth'))[0]
102    IF dimid NE -1 THEN ncdf_diminq, cdfid, dimid, name, jpk ELSE BEGIN
103      dummy = report('We could not find the vertical dimension..., its name must be z or start with depth')
104      return
105    ENDELSE
106  ENDELSE
107; get the variables list related to the partial steps
108  zgr_varlist = ncdf_listvars(cdfid)
109  zgr_varlist = strtrim(strlowcase(zgr_varlist), 2)
110; is e3t a real 3D array?
111  IF (where(zgr_varlist EQ 'e3t'))[0] NE -1 THEN BEGIN
112    varinq = ncdf_varinq(cdfid, 'e3t')
113    if varinq.ndims GE 3 THEN BEGIN
114      ncdf_diminq, cdfid, varinq.dim[0], name, iii
115      ncdf_diminq, cdfid, varinq.dim[1], name, jjj
116      ncdf_diminq, cdfid, varinq.dim[2], name, kkk
117      IF iii EQ jpi AND jjj EQ jpj AND kkk EQ jpk THEN key_e3_3d = 1
118    ENDIF   
119  ENDIF
120; is gdept a real 3D array?
121  IF (where(zgr_varlist EQ 'gdept'))[0] NE -1 THEN BEGIN
122    varinq = ncdf_varinq(cdfid, 'gdept')
123    if varinq.ndims GE 3 THEN BEGIN
124      ncdf_diminq, cdfid, varinq.dim[0], name, iii
125      ncdf_diminq, cdfid, varinq.dim[1], name, jjj
126      ncdf_diminq, cdfid, varinq.dim[2], name, kkk
127      IF iii EQ jpi AND jjj EQ jpj AND kkk EQ jpk THEN key_gdep_3d = 1
128    ENDIF   
129  ENDIF
130;------------------------------------------------------
131;
132;------------------------------------------------------
133; define the output file
134;------------------------------------------------------
135  IF n_elements(ncfileout) EQ 0  THEN ncfileout = 'smallmeshmask.nc'
136
137  cdfidout = ncdf_create(isafile(FILE = ncfileout, IODIR = iodir, /NEW), /clobber)
138  ncdf_control, cdfidout, /nofill
139; dimension
140  dimidx = ncdf_dimdef(cdfidout, 'x', jpi)
141  dimidy = ncdf_dimdef(cdfidout, 'y', jpj)
142  dimidz = ncdf_dimdef(cdfidout, 'z', jpk)
143; global attributs
144  ncdf_attput, cdfidout, 'IDL_Program_Name', 'smallmeshmask.pro', /GLOBAL
145  ncdf_attput, cdfidout, 'Creation_Date', systime(), /GLOBAL
146; declaration des variables
147  varid = lonarr(20)
148; horizontal variables
149  hgrlist = ['glamt', 'glamu', 'glamv', 'glamf' $
150             , 'gphit', 'gphiu', 'gphiv', 'gphif' $
151             , 'e1t', 'e1u', 'e1v', 'e1f' $
152             , 'e2t', 'e2u', 'e2v', 'e2f']
153  FOR h = 0, n_elements(hgrlist)-1 DO $
154    varid[h] = ncdf_vardef(cdfidout, hgrlist[h], [dimidx, dimidy], /float)
155; vertical variables
156  zgrlist = ['e3t', 'e3w', 'gdept', 'gdepw']
157  FOR z = 0, n_elements(zgrlist)-1 DO $
158    varid[16+z] = ncdf_vardef(cdfidout, zgrlist[z], [dimidz], /float)
159; variables related to the partial steps
160  with_partial = keyword_set(key_e3_3d) OR (where(zgr_varlist EQ 'hdept'))[0] NE -1 $
161                 OR (where(zgr_varlist EQ 'e3tp'))[0] NE -1 $
162                 OR (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1
163  IF with_partial THEN BEGIN
164    varid = [varid, ncdf_vardef(cdfidout, 'hdept', [dimidx, dimidy], /float)]
165    varid = [varid, ncdf_vardef(cdfidout, 'hdepw', [dimidx, dimidy], /float)]
166    varid = [varid, ncdf_vardef(cdfidout, 'e3t_ps', [dimidx, dimidy], /float)]
167    varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)]
168  ENDIF
169;
170  IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $
171    varid = [varid, ncdf_vardef(cdfidout, 'mbathy', [dimidx, dimidy], /short)]
172; mask variable
173  msklist = ['tmask', 'umask', 'vmask', 'fmask']
174  FOR m = 0, n_elements(msklist)-1 DO $
175    varid = [varid, ncdf_vardef(cdfidout, msklist[m], [dimidx, dimidy, dimidz], /byte)]
176;------------------------------------------------------
177;------------------------------------------------------
178  ncdf_control, cdfidout, /endef
179;------------------------------------------------------
180;
181; get the horizontal variables
182;
183  IF n_elements(filein_hgr) NE 0  THEN BEGIN
184    ncdf_close, cdfid
185    cdfid = ncdf_open(filein_hgr)
186  ENDIF
187;
188  FOR h = 0, n_elements(hgrlist)-1 DO $
189    ncdf_transfer, cdfid, cdfidout, hgrlist[h]
190;
191; get the vertical variables
192;
193  IF n_elements(filein_zgr) NE 0  THEN BEGIN
194    ncdf_close, cdfid
195    cdfid = ncdf_open(filein_zgr)
196  ENDIF
197;
198  inzgrlist =  zgrlist
199  IF (where(zgr_varlist EQ 'gdept_0'))[0] NE -1 THEN inzgrlist =  inzgrlist+'_0'
200  FOR z = 0, n_elements(zgrlist)-1 DO ncdf_transfer, cdfid, cdfidout, inzgrlist[z], zgrlist[z]
201; partial step variables
202  IF (where(zgr_varlist EQ 'hdept' ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'hdept'
203  IF (where(zgr_varlist EQ 'hdepw' ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'hdepw'
204  IF (where(zgr_varlist EQ 'e3tp'  ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3tp', 'e3t_ps'
205  IF (where(zgr_varlist EQ 'e3wp'  ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3wp', 'e3w_ps'
206  IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3t_ps'
207  IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3w_ps'
208  IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'mbathy'
209;
210  IF keyword_set(key_e3_3d)  THEN BEGIN
211; get the bottom
212    CASE (ncdf_varinq(cdfid, 'mbathy')).ndims OF
213      2:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj]
214      3:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1]
215      4:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1, 1]
216    ENDCASE
217    bottom = 0L > ( long(bat) - 1L )
218    bottom = lindgen(jpi, jpj) + jpi*jpj*temporary(bottom)
219;
220    CASE (ncdf_varinq(cdfid, 'e3t')).ndims OF
221      3:ncdf_varget, cdfid, 'e3t', res, count = [jpi, jpj, jpk]
222      4:ncdf_varget, cdfid, 'e3t', res, count = [jpi, jpj, jpk, 1]
223    ENDCASE
224    ncdf_varput, cdfidout, 'e3t_ps', (temporary(res))[bottom] * (bat NE 0L)
225;
226    CASE (ncdf_varinq(cdfid, 'e3w')).ndims OF
227      3:ncdf_varget, cdfid, 'e3w', res, count = [jpi, jpj, jpk]
228      4:ncdf_varget, cdfid, 'e3w', res, count = [jpi, jpj, jpk, 1]
229    ENDCASE
230    ncdf_varput, cdfidout, 'e3w_ps', (temporary(res))[bottom] * (bat NE 0L)
231  ENDIF
232;
233  IF keyword_set(key_gdep_3d)  THEN BEGIN
234; get the bottom
235    CASE (ncdf_varinq(cdfid, 'mbathy')).ndims OF
236      2:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj]
237      3:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1]
238      4:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1, 1]
239    ENDCASE
240    bottom = 0L > ( long(bat) - 1L )
241    bottom = lindgen(jpi, jpj) + jpi*jpj*temporary(bottom)
242;
243    CASE (ncdf_varinq(cdfid, 'gdept')).ndims OF
244      3:ncdf_varget, cdfid, 'gdept', res, count = [jpi, jpj, jpk]
245      4:ncdf_varget, cdfid, 'gdept', res, count = [jpi, jpj, jpk, 1]
246    ENDCASE
247    ncdf_varput, cdfidout, 'hdept', (temporary(res))[bottom] * (bat NE 0L)
248;
249    bottom = jpi*jpj + temporary(bottom)
250    CASE (ncdf_varinq(cdfid, 'gdepw')).ndims OF
251      3:ncdf_varget, cdfid, 'gdepw', res, count = [jpi, jpj, jpk]
252      4:ncdf_varget, cdfid, 'gdepw', res, count = [jpi, jpj, jpk, 1]
253    ENDCASE
254    ncdf_varput, cdfidout, 'hdepw', (temporary(res))[bottom] * (bat NE 0L)
255  ENDIF
256;
257; mask
258;
259  IF n_elements(filein_msk) NE 0  THEN BEGIN
260    ncdf_close, cdfid
261    cdfid = ncdf_open(filein_msk)
262  ENDIF
263; loop on the vertical levels to limit the memory use
264  FOR k = 0, jpk-1 DO BEGIN
265    FOR m = 0, 3 DO BEGIN
266      CASE (ncdf_varinq(cdfid, msklist[m])).ndims OF
267        3:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k   ], count = [jpi, jpj, 1]
268        4:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k, 0], count = [jpi, jpj, 1, 1]
269      ENDCASE
270      ncdf_varput, cdfidout, msklist[m], byte(temporary(zzz)), offset = [0, 0, k], count = [jpi, jpj, 1]
271    ENDFOR
272  ENDFOR
273;------------------------------------------------------
274;------------------------------------------------------
275  ncdf_close, cdfid
276  ncdf_close, cdfidout
277
278  RETURN
279END
Note: See TracBrowser for help on using the repository browser.