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

Last change on this file since 297 was 297, checked in by pinsard, 17 years ago

typo

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