source: trunk/SRC/Grid/micromeshmask.pro @ 394

Last change on this file since 394 was 394, checked in by smasson, 15 years ago

minor bugfix in micromeshmask

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