1 | pro read_dat, output, x, y, z, t, desc_field, nodata=nodata |
---|
2 | |
---|
3 | |
---|
4 | ;--------------------------------------------------------------------------- |
---|
5 | ; |
---|
6 | ; Read the files generated by ARWpost |
---|
7 | ; |
---|
8 | ; Please create symbolic link 'input.ctl' and 'input.dat' |
---|
9 | ; --- or rename files |
---|
10 | ; |
---|
11 | ; - output contains all fields |
---|
12 | ; - x,y,z,t contains coordinates |
---|
13 | ; - desc_field contains strings describing fields |
---|
14 | ; - if nodata is not blank, do not read the fields |
---|
15 | ; |
---|
16 | ;--------------------------------------------------------------------------- |
---|
17 | ; A. Spiga, August 2007 |
---|
18 | ;--------------------------------------------------------------------------- |
---|
19 | |
---|
20 | |
---|
21 | ; |
---|
22 | ; info for localtime |
---|
23 | ; |
---|
24 | interv=0. |
---|
25 | openr, 99, 'timefil' |
---|
26 | readf,99,interv |
---|
27 | close, 99 |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | ; get preliminar info on coordinates |
---|
32 | ; |
---|
33 | wnx=1 |
---|
34 | wny=1 |
---|
35 | wnz=1 |
---|
36 | wnt=1 |
---|
37 | txt='' |
---|
38 | param=[0.,0.,0.,0.,0.,0.,0.,0.] |
---|
39 | openr, 1, 'input.ctl' |
---|
40 | while not eof(1) do begin |
---|
41 | readf,1,txt |
---|
42 | match=STRPOS(txt,'linear') |
---|
43 | if (match ne -1) then begin |
---|
44 | wnx=(STRPOS(txt,'xdef') eq -1)*wnx ;0 if linear |
---|
45 | wny=(STRPOS(txt,'ydef') eq -1)*wny ;0 if linear |
---|
46 | wnz=(STRPOS(txt,'zdef') eq -1)*wnz ;0 if linear |
---|
47 | wnt=(STRPOS(txt,'tdef') eq -1)*wnt ;0 if linear |
---|
48 | paramstart=STRSPLIT(txt, 'r', LENGTH=paramlength) |
---|
49 | paramchar=STRMID(txt,paramstart(1),paramlength(1)) |
---|
50 | READS, paramchar, start, step |
---|
51 | param=param+[start*(STRPOS(txt,'xdef') ne -1),$ |
---|
52 | step*(STRPOS(txt,'xdef') ne -1),$ |
---|
53 | start*(STRPOS(txt,'ydef') ne -1),$ |
---|
54 | step*(STRPOS(txt,'ydef') ne -1),$ |
---|
55 | start*(STRPOS(txt,'zdef') ne -1),$ |
---|
56 | step*(STRPOS(txt,'zdef') ne -1),$ |
---|
57 | start*(STRPOS(txt,'tdef') ne -1),$ |
---|
58 | step*(STRPOS(txt,'tdef') ne -1)] |
---|
59 | endif |
---|
60 | endwhile |
---|
61 | close, 1 |
---|
62 | timebegin=param(6) |
---|
63 | |
---|
64 | ;--------------------------------- |
---|
65 | ; READ .CTL FILE INFORMATIONS |
---|
66 | ;--------------------------------- |
---|
67 | |
---|
68 | info=read_ascii('input.ctl', missing_value=1e37) |
---|
69 | infodat=info.field1 |
---|
70 | |
---|
71 | ; |
---|
72 | ; second column : dimensions |
---|
73 | ; |
---|
74 | |
---|
75 | w=where((infodat(0,*) eq 1e37) and (infodat(1,*) lt 1e37)) |
---|
76 | dimensions=reform(infodat(1,w)) |
---|
77 | |
---|
78 | nx=round(dimensions(0)) |
---|
79 | ny=round(dimensions(1)) |
---|
80 | nz=round(dimensions(2)) |
---|
81 | nt=round(dimensions(3)) |
---|
82 | fields=round(dimensions(4)) |
---|
83 | vertical=intarr(fields) |
---|
84 | for i=0,fields-1 do begin |
---|
85 | vertical(i)=round(dimensions(5+i)) |
---|
86 | endfor |
---|
87 | |
---|
88 | |
---|
89 | ; |
---|
90 | ; first column : coordinates |
---|
91 | ; |
---|
92 | |
---|
93 | w=where((infodat(1,*) eq 1e37) and (infodat(0,*) lt 1e37)) |
---|
94 | if (w(0) ne -1) then coordinates=reform(infodat(0,w)) |
---|
95 | |
---|
96 | if (wnx ne 0) then begin |
---|
97 | wnx=nx |
---|
98 | x=coordinates(0:nx-1) |
---|
99 | endif else begin |
---|
100 | xmin=param(0) |
---|
101 | xmax=param(0)+param(1)*(nx-1) MOD 360 |
---|
102 | ;inc=param(0)+param(1)*(nx-1) ;cas mercator |
---|
103 | ;x=start+inc*findgen(nx) |
---|
104 | x=xmin+(xmax-xmin)*findgen(nx)/(nx-1) |
---|
105 | ;print, x |
---|
106 | ;stop |
---|
107 | endelse |
---|
108 | if (wny ne 0) then begin |
---|
109 | wny=ny |
---|
110 | y=coordinates(wnx:wnx+wny-1) |
---|
111 | endif else begin |
---|
112 | start=param(2) |
---|
113 | step=param(3) |
---|
114 | y=start+step*findgen(ny) |
---|
115 | endelse |
---|
116 | if (wnz ne 0) then begin |
---|
117 | wnz=nz |
---|
118 | z=coordinates(wnx+wny:wnx+wny+wnz-1) |
---|
119 | endif else begin |
---|
120 | start=param(4) |
---|
121 | step=param(5) |
---|
122 | z=start+step*findgen(nz) |
---|
123 | endelse |
---|
124 | t=interv*findgen(nt)/3700. + timebegin + mean(x)/15. |
---|
125 | |
---|
126 | |
---|
127 | ; |
---|
128 | ; get info on fields |
---|
129 | ; |
---|
130 | openr, 1, 'input.ctl' |
---|
131 | toto=5+wnx+1+wny+1+wnz+2+fields |
---|
132 | infofields=strarr(toto) |
---|
133 | readf, 1, infofields |
---|
134 | close, 1 |
---|
135 | |
---|
136 | ; |
---|
137 | desc_field=transpose(infofields(toto-fields:toto-1)) |
---|
138 | |
---|
139 | |
---|
140 | if (n_elements(nodata) eq 0) then begin |
---|
141 | ;--------------------------------- |
---|
142 | ; READ .DAT FILE DATA |
---|
143 | ;--------------------------------- |
---|
144 | ; GrADS views gridded data sets as 5-dimensional arrays |
---|
145 | ; varying in longitude, latitude, vertical level, variable, and time. |
---|
146 | ;--------------------------------- |
---|
147 | |
---|
148 | print, '----------------' |
---|
149 | print, 'reading data ...' |
---|
150 | print, '----------------' |
---|
151 | print, 'longitudes: ', min(x), max(x), nx, ' points' |
---|
152 | print, 'latitudes: ', min(y), max(y), ny, ' points' |
---|
153 | print, 'altitudes: ', min(z), max(z), nz, ' points' |
---|
154 | print, 'local times: ', min(t), max(t), nt, ' points' |
---|
155 | print, fields, ' recorded fields :' |
---|
156 | print, desc_field |
---|
157 | print, '----------------' |
---|
158 | |
---|
159 | ;;very slow method |
---|
160 | ;;---------------- |
---|
161 | ;data=read_ascii('input.dat', missing_value=1e37) |
---|
162 | ;datadat=data.field1 |
---|
163 | |
---|
164 | ; see http://www.dfanning.com/tips/ascii_column_data.html |
---|
165 | OPENR, lun, 'input.dat', /GET_LUN |
---|
166 | |
---|
167 | ;count=0L |
---|
168 | output=fltarr(fields,nx,ny,nz,nt) |
---|
169 | dummy=0. |
---|
170 | |
---|
171 | for l=0,nt-1 do begin ;time loop |
---|
172 | print, 'time loop ...', l+1, ' / ', string(nt,'(I0)') |
---|
173 | for f=0, fields-1 do begin ;fields (whether 2D or 3D) ;variable loop |
---|
174 | nzf=vertical(f) |
---|
175 | print, 'reading field ...', f+1, ' / ', string(fields,'(I0)') |
---|
176 | for k=0,nzf-1 do begin ;vertical loop |
---|
177 | for j=0,ny-1 do begin ;latitude loop |
---|
178 | for i=0,nx-1 do begin ;longitude loop |
---|
179 | |
---|
180 | ;;very slow method |
---|
181 | ;output(f,i,j,k,l)=datadat(count) |
---|
182 | ;count=count+1 |
---|
183 | |
---|
184 | READF, lun, dummy |
---|
185 | output(f,i,j,k,l)=dummy |
---|
186 | |
---|
187 | endfor |
---|
188 | endfor |
---|
189 | endfor |
---|
190 | endfor |
---|
191 | endfor |
---|
192 | |
---|
193 | ; missing values |
---|
194 | w=where(output eq 1e20) |
---|
195 | if (w(0) ne -1) then begin |
---|
196 | print, 'change missing values ...' |
---|
197 | output[w]=1e37 |
---|
198 | endif |
---|
199 | |
---|
200 | |
---|
201 | CLOSE, lun |
---|
202 | ;SPAWN, '\rm input.dat_tmp' |
---|
203 | print, '...done !' |
---|
204 | help, /memory |
---|
205 | print, '---------' |
---|
206 | |
---|
207 | ;if (count ne n_elements(datadat)) then begin |
---|
208 | ; print, 'some data were not read !' |
---|
209 | ; stop |
---|
210 | ;endif |
---|
211 | |
---|
212 | ;; |
---|
213 | ;; Manage missing data so as IDL recognize them |
---|
214 | ;; |
---|
215 | ;w=where(output eq 1e37) |
---|
216 | ;if (w(0) ne -1) then output[w]=!Values.F_NAN |
---|
217 | |
---|
218 | endif |
---|
219 | |
---|
220 | end |
---|