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

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

improvements/corrections of some *.pro headers

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