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 | ; |
---|
27 | PRO 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 |
---|
35 | END |
---|
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 | ; |
---|
60 | PRO 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 | zgr_varlist = ncdf_listvars(cdfid) |
---|
105 | zgr_varlist = strtrim(strlowcase(zgr_varlist), 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(zgr_varlist EQ 'hdept'))[0] NE -1 THEN $ |
---|
138 | varid = [varid, ncdf_vardef(cdfidout, 'hdept', [dimidx, dimidy], /float)] |
---|
139 | IF (where(zgr_varlist 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(zgr_varlist 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(zgr_varlist EQ 'e3wp'))[0] NE -1 THEN $ |
---|
146 | varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)] |
---|
147 | ; |
---|
148 | IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN $ |
---|
149 | varid = [varid, ncdf_vardef(cdfidout, 'e3t_ps', [dimidx, dimidy], /float)] |
---|
150 | IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN $ |
---|
151 | varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)] |
---|
152 | ; IF (where(zgr_varlist EQ 'e3u_ps'))[0] NE -1 THEN $ |
---|
153 | ; varid = [varid, ncdf_vardef(cdfidout, 'e3u_ps', [dimidx, dimidy], /float)] |
---|
154 | ; IF (where(zgr_varlist EQ 'e3v_ps'))[0] NE -1 THEN $ |
---|
155 | ; varid = [varid, ncdf_vardef(cdfidout, 'e3v_ps', [dimidx, dimidy], /float)] |
---|
156 | IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $ |
---|
157 | varid = [varid, ncdf_vardef(cdfidout, 'mbathy', [dimidx, dimidy], /short)] |
---|
158 | ; mask variable |
---|
159 | msklist = ['tmask', 'umask', 'vmask', 'fmask'] |
---|
160 | FOR m = 0, n_elements(msklist)-1 DO BEGIN |
---|
161 | varid = [varid, ncdf_vardef(cdfidout, msklist[m] $ |
---|
162 | , [dimidx, dimidy_m, dimidz], /byte)] |
---|
163 | ; ncdf_attput, cdfidout, varid[n_elements(varid)-1] $ |
---|
164 | ; , 'Comment', 'the mask is stored as bit. You must use ' $ |
---|
165 | ; +'the binary representation of the byte to get back the data.' |
---|
166 | ENDFOR |
---|
167 | ;------------------------------------------------------ |
---|
168 | ;------------------------------------------------------ |
---|
169 | ncdf_control, cdfidout, /endef |
---|
170 | ;------------------------------------------------------ |
---|
171 | ; |
---|
172 | ; get the horizontal variables |
---|
173 | ; |
---|
174 | IF n_elements(filein_hgr) NE 0 THEN BEGIN |
---|
175 | ncdf_close, cdfid |
---|
176 | cdfid = ncdf_open(filein_hgr) |
---|
177 | ENDIF |
---|
178 | ; |
---|
179 | FOR h = 0, n_elements(hgrlist)-1 DO $ |
---|
180 | ncdf_transfer, cdfid, cdfidout, hgrlist[h] |
---|
181 | ; |
---|
182 | ; get the vertical variables |
---|
183 | ; |
---|
184 | IF n_elements(filein_zgr) NE 0 THEN BEGIN |
---|
185 | ncdf_close, cdfid |
---|
186 | cdfid = ncdf_open(filein_zgr) |
---|
187 | ENDIF |
---|
188 | ; |
---|
189 | inzgrlist = zgrlist |
---|
190 | IF (where(varlist EQ 'gdept_0'))[0] NE -1 THEN inzgrlist = inzgrlist+'_0' |
---|
191 | FOR z = 0, n_elements(zgrlist)-1 DO $ |
---|
192 | ncdf_transfer, cdfid, cdfidout, inzgrlist[z], zgrlist[z] |
---|
193 | ; partial step variables |
---|
194 | IF (where(zgr_varlist EQ 'hdept'))[0] NE -1 THEN $ |
---|
195 | ncdf_transfer, cdfid, cdfidout, 'hdept' |
---|
196 | IF (where(zgr_varlist EQ 'hdepw'))[0] NE -1 THEN $ |
---|
197 | ncdf_transfer, cdfid, cdfidout, 'hdepw' |
---|
198 | IF (where(zgr_varlist EQ 'e3tp'))[0] NE -1 THEN $ |
---|
199 | ncdf_transfer, cdfid, cdfidout, 'e3tp', 'e3t_ps' |
---|
200 | IF (where(zgr_varlist EQ 'e3wp'))[0] NE -1 THEN $ |
---|
201 | ncdf_transfer, cdfid, cdfidout, 'e3wp', 'e3w_ps' |
---|
202 | IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN $ |
---|
203 | ncdf_transfer, cdfid, cdfidout, 'e3t_ps' |
---|
204 | IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN $ |
---|
205 | ncdf_transfer, cdfid, cdfidout, 'e3w_ps' |
---|
206 | ; IF (where(zgr_varlist EQ 'e3u_ps'))[0] NE -1 THEN $ |
---|
207 | ; ncdf_transfer, cdfid, cdfidout, 'e3u_ps' |
---|
208 | ; IF (where(zgr_varlist EQ 'e3v_ps'))[0] NE -1 THEN $ |
---|
209 | ; ncdf_transfer, cdfid, cdfidout, 'e3v_ps' |
---|
210 | IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $ |
---|
211 | ncdf_transfer, cdfid, cdfidout, 'mbathy' |
---|
212 | ; |
---|
213 | ; mask |
---|
214 | ; |
---|
215 | IF n_elements(filein_msk) NE 0 THEN BEGIN |
---|
216 | ncdf_close, cdfid |
---|
217 | cdfid = ncdf_open(filein_msk) |
---|
218 | ENDIF |
---|
219 | ; loop on the vertical levels to limit the memory use |
---|
220 | FOR k = 0, jpk-1 DO BEGIN |
---|
221 | FOR m = 0, 3 DO BEGIN |
---|
222 | CASE (ncdf_varinq(cdfid, msklist[m])).ndims OF |
---|
223 | 3:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k] $ |
---|
224 | , count = [jpi, jpj, 1] |
---|
225 | 4:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k, 0] $ |
---|
226 | , count = [jpi, jpj, 1, 1] |
---|
227 | ENDCASE |
---|
228 | zzz = byte(temporary(zzz)) |
---|
229 | ; zzz must contain only 0 or 1 |
---|
230 | zzz = temporary(zzz) MOD 2 |
---|
231 | ; we transpose zzz because we need to work with the y dimension as the |
---|
232 | ; first dimension |
---|
233 | zzz = transpose(temporary(zzz)) |
---|
234 | ; extend jpj to be a multiple of 8 |
---|
235 | jpjadd = jpj_m*8-jpj |
---|
236 | IF jpjadd NE 0 THEN zzz = [temporary(zzz), bytarr(jpjadd, jpi)] |
---|
237 | ; reform zzz, to look like output of binary.pro |
---|
238 | zzz = reform(zzz, 8, 1, jpj_m, jpi, /over) |
---|
239 | ; convert into "its byte form" |
---|
240 | zzz = inverse_binary(temporary(zzz)) |
---|
241 | ncdf_varput, cdfidout, msklist[m], transpose(temporary(zzz)) $ |
---|
242 | , offset = [0, 0, k], count = [jpi, jpj_m, 1] |
---|
243 | ENDFOR |
---|
244 | ENDFOR |
---|
245 | ;------------------------------------------------------ |
---|
246 | ;------------------------------------------------------ |
---|
247 | ncdf_close, cdfid |
---|
248 | ncdf_close, cdfidout |
---|
249 | |
---|
250 | RETURN |
---|
251 | END |
---|