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

Last change on this file since 216 was 157, checked in by navarro, 18 years ago

header improvements + xxx doc

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