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

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

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • 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; IDL> meshdir='/d1fes2-raid2/smasson/DATA/ORCA05/'
55; IDL> micromeshmask, 'meshmask_ORCA_R05.nc',iodir=meshdir
56;
57;-
58PRO micromeshmask, ncfilein, ncfileout, IODIR = iodir
59;
60  compile_opt idl2, strictarrsubs
61;
62  filein = isafile(FILE = ncfilein, IODIR = iodir, /NEW)
63  test = (findfile(filein))[0]
64  IF test EQ '' THEN BEGIN
65    filein_hgr = (findfile(filein+'.mesh_hgr.nc'))[0]
66    filein_zgr = (findfile(filein+'.mesh_zgr.nc'))[0]
67    filein_msk = (findfile(filein+'.mask.nc'))[0]
68    IF filein_hgr EQ '' OR filein_zgr EQ '' OR filein_msk EQ ''  THEN BEGIN
69      ras = report(['meshmask file(s) not found...', $
70      filein+' does not exist', $
71      filein+'.mesh_hgr.nc does not exist', $
72      filein+'.mesh_zgr.nc does not exist', $
73      filein+'.mask.nc does not exist'])
74      return
75    ENDIF
76  ENDIF ELSE filein = test
77;------------------------------------------------------
78;------------------------------------------------------
79; get the horizontal dimensions
80  IF n_elements(filein_hgr) NE 0  THEN cdfid = ncdf_open(filein_hgr) $
81  ELSE cdfid = ncdf_open(filein)
82  ncdf_diminq, cdfid, 'x', name, jpi
83  ncdf_diminq, cdfid, 'y', name, jpj
84; for the mask, we use "its byte" representation -> its y dimension
85; will be extended to be a multiple of 8, then it will be divided by
86; 8. -> if (jpj mod 8) eq 0 the jpj_m=jpi/8 else jpj_m=jpi/8 + 1
87  jpj_m = (jpj+7)/8
88; get the vertical dimensions
89  IF n_elements(filein_zgr) NE 0  THEN BEGIN
90    ncdf_close, cdfid
91    cdfid = ncdf_open(filein_zgr)
92  ENDIF
93  listdims = strlowcase(ncdf_listdims(cdfid))
94  IF (where(listdims EQ 'z'))[0] NE -1 THEN ncdf_diminq, cdfid, 'z', name, jpk ELSE BEGIN
95    dimid = (where(strmid(listdims, 0, 5) EQ 'depth'))[0]
96    IF dimid NE -1 THEN ncdf_diminq, cdfid, dimid, name, jpk ELSE BEGIN
97      dummy = report('We could not find the vertical dimension..., its name must be z or start with depth')
98      return
99    ENDELSE
100  ENDELSE
101; get the variables list related to the partial steps
102  zgr_varlist = ncdf_listvars(cdfid)
103  zgr_varlist = strtrim(strlowcase(zgr_varlist), 2)
104;------------------------------------------------------
105;------------------------------------------------------
106;
107;------------------------------------------------------
108; define the output file
109;------------------------------------------------------
110  IF n_elements(ncfileout) EQ 0  THEN ncfileout = 'micromeshmask.nc'
111  cdfidout = ncdf_create(isafile(FILE = ncfileout, IODIR = iodir, /NEW), /clobber)
112  ncdf_control, cdfidout, /nofill
113; dimension
114  dimidx = ncdf_dimdef(cdfidout, 'x', jpi)
115  dimidy = ncdf_dimdef(cdfidout, 'y', jpj)
116  dimidy_m = ncdf_dimdef(cdfidout, 'y_m', jpj_m)
117  dimidz = ncdf_dimdef(cdfidout, 'z', jpk)
118; global attributs
119  ncdf_attput, cdfidout, 'IDL_Program_Name', 'micromeshmask.pro', /GLOBAL
120  ncdf_attput, cdfidout, 'Creation_Date', systime(), /GLOBAL
121; declaration des variables
122  varid = lonarr(20)
123; horizontal variables
124  hgrlist = ['glamt', 'glamu', 'glamv', 'glamf' $
125             , 'gphit', 'gphiu', 'gphiv', 'gphif' $
126             , 'e1t', 'e1u', 'e1v', 'e1f' $
127             , 'e2t', 'e2u', 'e2v', 'e2f']
128  FOR h = 0, n_elements(hgrlist)-1 DO $
129    varid[h] = ncdf_vardef(cdfidout, hgrlist[h], [dimidx, dimidy], /float)
130; vertical variables
131  zgrlist = ['e3t', 'e3w', 'gdept', 'gdepw']
132  FOR z = 0, n_elements(zgrlist)-1 DO $
133    varid[16+z] = ncdf_vardef(cdfidout, zgrlist[z], [dimidz], /float)
134; variables related to the partial steps
135  IF (where(zgr_varlist EQ 'hdept'))[0] NE -1 THEN $
136    varid = [varid, ncdf_vardef(cdfidout, 'hdept', [dimidx, dimidy], /float)]
137  IF (where(zgr_varlist EQ 'hdepw'))[0] NE -1 THEN $
138    varid = [varid, ncdf_vardef(cdfidout, 'hdepw', [dimidx, dimidy], /float)]
139; old variable name. keep for compatibility with old run. Change e3tp to e3t_ps
140  IF (where(zgr_varlist EQ 'e3tp'))[0] NE -1 THEN $
141    varid = [varid, ncdf_vardef(cdfidout, 'e3t_ps', [dimidx, dimidy], /float)]
142; old variable name. keep for compatibility with old run. Change e3wp to e3w_ps
143  IF (where(zgr_varlist EQ 'e3wp'))[0] NE -1 THEN $
144    varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)]
145;
146  IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN $
147    varid = [varid, ncdf_vardef(cdfidout, 'e3t_ps', [dimidx, dimidy], /float)]
148  IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN $
149    varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)]
150;   IF (where(zgr_varlist EQ 'e3u_ps'))[0] NE -1 THEN $
151;     varid = [varid, ncdf_vardef(cdfidout, 'e3u_ps', [dimidx, dimidy], /float)]
152;   IF (where(zgr_varlist EQ 'e3v_ps'))[0] NE -1 THEN $
153;     varid = [varid, ncdf_vardef(cdfidout, 'e3v_ps', [dimidx, dimidy], /float)]
154  IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $
155    varid = [varid, ncdf_vardef(cdfidout, 'mbathy', [dimidx, dimidy], /short)]
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  inzgrlist =  zgrlist
188  IF (where(varlist EQ 'gdept_0'))[0] NE -1 THEN inzgrlist =  inzgrlist+'_0'
189  FOR z = 0, n_elements(zgrlist)-1 DO $
190    ncdf_transfer, cdfid, cdfidout, inzgrlist[z], zgrlist[z]
191; partial step variables
192  IF (where(zgr_varlist EQ 'hdept'))[0] NE -1 THEN $
193    ncdf_transfer, cdfid, cdfidout, 'hdept'
194  IF (where(zgr_varlist EQ 'hdepw'))[0] NE -1 THEN $
195    ncdf_transfer, cdfid, cdfidout, 'hdepw'
196  IF (where(zgr_varlist EQ 'e3tp'))[0] NE -1 THEN $
197    ncdf_transfer, cdfid, cdfidout, 'e3tp', 'e3t_ps'
198  IF (where(zgr_varlist EQ 'e3wp'))[0] NE -1 THEN $
199    ncdf_transfer, cdfid, cdfidout, 'e3wp', 'e3w_ps'
200  IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN $
201    ncdf_transfer, cdfid, cdfidout, 'e3t_ps'
202  IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN $
203    ncdf_transfer, cdfid, cdfidout, 'e3w_ps'
204;   IF (where(zgr_varlist EQ 'e3u_ps'))[0] NE -1 THEN $
205;     ncdf_transfer, cdfid, cdfidout, 'e3u_ps'
206;   IF (where(zgr_varlist EQ 'e3v_ps'))[0] NE -1 THEN $
207;     ncdf_transfer, cdfid, cdfidout, 'e3v_ps'
208  IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $
209    ncdf_transfer, cdfid, cdfidout, 'mbathy'
210;
211; mask
212;
213  IF n_elements(filein_msk) NE 0  THEN BEGIN
214    ncdf_close, cdfid
215    cdfid = ncdf_open(filein_msk)
216  ENDIF
217; loop on the vertical levels to limit the memory use
218  FOR k = 0, jpk-1 DO BEGIN
219    FOR m = 0, 3 DO BEGIN
220      CASE (ncdf_varinq(cdfid, msklist[m])).ndims OF
221        3:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k] $
222        , count = [jpi, jpj, 1]
223        4:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k, 0] $
224        , count = [jpi, jpj, 1, 1]
225      ENDCASE
226      zzz = byte(temporary(zzz))
227; zzz must contain only 0 or 1
228      zzz = temporary(zzz) MOD 2
229; we transpose zzz because we need to work with the y dimension as the
230; first dimension
231      zzz = transpose(temporary(zzz))
232; extend jpj to be a multiple of 8
233      jpjadd = jpj_m*8-jpj
234      IF jpjadd NE 0 THEN zzz = [temporary(zzz), bytarr(jpjadd, jpi)]
235; reform zzz, to look like output of binary.pro
236      zzz = reform(zzz, 8, 1, jpj_m, jpi, /over)
237; convert into "its byte form"
238      zzz = inverse_binary(temporary(zzz))
239      ncdf_varput, cdfidout, msklist[m], transpose(temporary(zzz)) $
240        , offset = [0, 0, k], count = [jpi, jpj_m, 1]
241    ENDFOR
242  ENDFOR
243;------------------------------------------------------
244;------------------------------------------------------
245  ncdf_close, cdfid
246  ncdf_close, cdfidout
247
248  RETURN
249END
Note: See TracBrowser for help on using the repository browser.