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

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

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