1 | program expandstartfi |
---|
2 | |
---|
3 | ! program which takes a GCM startfi.nc file and puts it on the |
---|
4 | ! corresponding lonxlat grid (so it can be displayed using Grads, Ferret, etc.) |
---|
5 | ! usage: |
---|
6 | ! expandstartfi [infile.nc] [outfile.nc] |
---|
7 | ! if outfile is not specified it is built as "infile_ex.nc" |
---|
8 | ! if infile is not specified, "startfi.nc" is used as default |
---|
9 | |
---|
10 | use netcdf |
---|
11 | implicit none |
---|
12 | |
---|
13 | ! Input and output files: |
---|
14 | character(len=256) :: infile="startfi.nc" ! default input file |
---|
15 | character(len=256) :: outfile="startfi_ex.nc" ! default output file |
---|
16 | |
---|
17 | ! NetCDF stuff |
---|
18 | integer :: status ! NetCDF return code |
---|
19 | integer :: inid,outid ! NetCDF file IDs |
---|
20 | integer :: varid ! to store the ID of a variable |
---|
21 | integer :: datashape(4) ! to store dimension IDs of a given dataset |
---|
22 | integer :: corner(3),edges(3) ! to read data with a time axis |
---|
23 | character(len=90) :: varname ! name of a variable |
---|
24 | character(len=90) :: varatt ! name of attribute of a variable |
---|
25 | character(len=90) :: varattcontent ! content of the attribute |
---|
26 | ! Input file dimension IDs: |
---|
27 | integer :: physical_points_dimid |
---|
28 | integer :: subsurface_layers_dimid |
---|
29 | integer :: nlayer_plus_1_dimid |
---|
30 | integer :: number_of_advected_fields_dimid |
---|
31 | integer :: time_dimid |
---|
32 | integer :: nbindims ! number of dimensions in input file |
---|
33 | integer :: nbinvars ! number of variables in input file |
---|
34 | integer :: invarid ! to store ID of an input variable |
---|
35 | !Input file variables |
---|
36 | integer :: physical_points |
---|
37 | integer :: subsurface_layers |
---|
38 | integer :: nlayer_plus_1 |
---|
39 | integer :: number_of_advected_fields |
---|
40 | integer :: timelen |
---|
41 | real,allocatable :: surf_field(:) ! to store a 1D field of physical_points elements |
---|
42 | real,allocatable :: subsurf_field(:,:) ! to store subsurface (2D field) |
---|
43 | |
---|
44 | ! Output file dimensions: |
---|
45 | integer :: latlen |
---|
46 | integer :: lonlen |
---|
47 | ! Output file variables: |
---|
48 | real,allocatable :: longitude(:) |
---|
49 | real,allocatable :: latitude(:) |
---|
50 | real,allocatable :: depth(:) |
---|
51 | real,allocatable :: out_surf_field(:,:) |
---|
52 | real,allocatable :: out_subsurf_field(:,:,:) |
---|
53 | ! Output file dimension IDs |
---|
54 | integer :: lon_dimid |
---|
55 | integer :: lat_dimid |
---|
56 | integer :: depth_dimid |
---|
57 | ! IDs of Output file variables |
---|
58 | integer :: lon_varid |
---|
59 | integer :: lat_varid |
---|
60 | integer :: depth_varid |
---|
61 | |
---|
62 | integer :: i,j,k,ig0,ivar |
---|
63 | integer :: nbdim,nbatt,shape(4) |
---|
64 | integer :: nbarg ! # of program arguments |
---|
65 | character(len=256) :: arg ! to store a program argument |
---|
66 | real :: pi ! 3.14159... |
---|
67 | |
---|
68 | pi=2.*asin(1.) |
---|
69 | |
---|
70 | ! 0. Preliminary stuff, check arguments (input and output files) |
---|
71 | ! get number of arguments this program was called with |
---|
72 | nbarg=command_argument_count() |
---|
73 | |
---|
74 | if (nbarg.ge.1) then |
---|
75 | call get_command_argument(1,arg) ! get argument # 1 |
---|
76 | infile=trim(arg) |
---|
77 | if (nbarg.eq.2) then |
---|
78 | call get_command_argument(2,arg) ! get argument # 2 |
---|
79 | outfile=trim(arg) |
---|
80 | else |
---|
81 | ! build outfile from infile (replace ".nc" with "_ex.nc" |
---|
82 | outfile=trim(infile(1:index(infile,".nc",back=.true.)-1))//"_ex.nc" |
---|
83 | endif |
---|
84 | if (nbarg.ge.3) then |
---|
85 | write(*,*) ' Warning: Too many arguments...' |
---|
86 | write(*,*) ' will only use the first 2 ' |
---|
87 | endif |
---|
88 | endif |
---|
89 | |
---|
90 | ! 1. open input file |
---|
91 | status=nf90_open(infile,NF90_NOWRITE,inid) |
---|
92 | if (status.ne.nf90_noerr) then |
---|
93 | write(*,*)"Failed to open datafile ",trim(infile) |
---|
94 | write(*,*)trim(nf90_strerror(status)) |
---|
95 | stop |
---|
96 | endif |
---|
97 | write(*,*) "Reading input file: ",trim(infile) |
---|
98 | |
---|
99 | ! 1.2 Identify/load dimensions in input file |
---|
100 | status=nf90_inq_dimid(inid,"physical_points",physical_points_dimid) |
---|
101 | if (status.ne.nf90_noerr) then |
---|
102 | write(*,*)"Failed to find physical_points dimension" |
---|
103 | write(*,*)trim(nf90_strerror(status)) |
---|
104 | stop |
---|
105 | endif |
---|
106 | status=nf90_inquire_dimension(inid,physical_points_dimid,len=physical_points) |
---|
107 | if (status.ne.nf90_noerr) then |
---|
108 | write(*,*)"Failed to find physical_points value" |
---|
109 | write(*,*)trim(nf90_strerror(status)) |
---|
110 | stop |
---|
111 | else |
---|
112 | write(*,*) " physical_points = ",physical_points |
---|
113 | endif |
---|
114 | |
---|
115 | status=nf90_inq_dimid(inid,"subsurface_layers",subsurface_layers_dimid) |
---|
116 | if (status.ne.nf90_noerr) then |
---|
117 | write(*,*)"Failed to find subsurface_layers dimension" |
---|
118 | write(*,*)trim(nf90_strerror(status)) |
---|
119 | stop |
---|
120 | endif |
---|
121 | status=nf90_inquire_dimension(inid,subsurface_layers_dimid,len=subsurface_layers) |
---|
122 | if (status.ne.nf90_noerr) then |
---|
123 | write(*,*)"Failed to find subsurface_layers value" |
---|
124 | write(*,*)trim(nf90_strerror(status)) |
---|
125 | stop |
---|
126 | else |
---|
127 | write(*,*) " subsurface_layers = ",subsurface_layers |
---|
128 | endif |
---|
129 | |
---|
130 | status=nf90_inq_dimid(inid,"nlayer_plus_1",nlayer_plus_1_dimid) |
---|
131 | if (status.ne.nf90_noerr) then |
---|
132 | write(*,*)"Failed to find nlayer_plus_1 dimension" |
---|
133 | write(*,*)trim(nf90_strerror(status)) |
---|
134 | stop |
---|
135 | endif |
---|
136 | status=nf90_inquire_dimension(inid,nlayer_plus_1_dimid,len=nlayer_plus_1) |
---|
137 | if (status.ne.nf90_noerr) then |
---|
138 | write(*,*)"Failed to find nlayer_plus_1 value" |
---|
139 | write(*,*)trim(nf90_strerror(status)) |
---|
140 | stop |
---|
141 | else |
---|
142 | write(*,*) " nlayer_plus_1 = ",nlayer_plus_1 |
---|
143 | endif |
---|
144 | |
---|
145 | status=nf90_inq_dimid(inid,"number_of_advected_fields",number_of_advected_fields_dimid) |
---|
146 | if (status.ne.nf90_noerr) then |
---|
147 | write(*,*)"Failed to find number_of_advected_fields dimension" |
---|
148 | write(*,*)trim(nf90_strerror(status)) |
---|
149 | stop |
---|
150 | endif |
---|
151 | status=nf90_inquire_dimension(inid,number_of_advected_fields_dimid,len=number_of_advected_fields) |
---|
152 | if (status.ne.nf90_noerr) then |
---|
153 | write(*,*)"Failed to find number_of_advected_fields value" |
---|
154 | write(*,*)trim(nf90_strerror(status)) |
---|
155 | stop |
---|
156 | else |
---|
157 | write(*,*) " number_of_advected_fields = ",number_of_advected_fields |
---|
158 | endif |
---|
159 | |
---|
160 | status=nf90_inq_dimid(inid,"Time",time_dimid) |
---|
161 | if (status.ne.nf90_noerr) then |
---|
162 | write(*,*)"Failed to find Time dimension" |
---|
163 | write(*,*)trim(nf90_strerror(status)) |
---|
164 | timelen = 0 |
---|
165 | else |
---|
166 | status=nf90_inquire_dimension(inid,time_dimid,len=timelen) |
---|
167 | if (status.ne.nf90_noerr) then |
---|
168 | write(*,*)"Failed to read Time dimension" |
---|
169 | write(*,*)trim(nf90_strerror(status)) |
---|
170 | stop |
---|
171 | else |
---|
172 | write(*,*) " time length = ",timelen |
---|
173 | endif |
---|
174 | endif |
---|
175 | |
---|
176 | ! 1.3 Allocate memory for input fields |
---|
177 | allocate(surf_field(physical_points)) |
---|
178 | allocate(subsurf_field(physical_points,subsurface_layers)) |
---|
179 | |
---|
180 | ! 2.1. Create output file |
---|
181 | status=nf90_create(outfile,NF90_CLOBBER,outid) |
---|
182 | if (status.ne.nf90_noerr) then |
---|
183 | write(*,*) "Failed to create output file: ",trim(outfile) |
---|
184 | write(*,*)trim(nf90_strerror(status)) |
---|
185 | stop |
---|
186 | endif |
---|
187 | write(*,*) " " |
---|
188 | write(*,*) "Created output file: ",trim(outfile) |
---|
189 | |
---|
190 | ! 2.2. Build dimensions for output file |
---|
191 | |
---|
192 | ! 2.2.1 Compute lonlen and latlen from physical_points |
---|
193 | ! load "longitude(physical_points)" from input file |
---|
194 | status=nf90_inq_varid(inid,"longitude",varid) |
---|
195 | if (status.ne.nf90_noerr) then |
---|
196 | write(*,*) "Failed to find longitude ID" |
---|
197 | write(*,*)trim(nf90_strerror(status)) |
---|
198 | stop |
---|
199 | endif |
---|
200 | ! read longitude |
---|
201 | status=nf90_get_var(inid,varid,surf_field) |
---|
202 | if (status.ne.nf90_noerr) then |
---|
203 | write(*,*) "Failed to load longitude" |
---|
204 | write(*,*)trim(nf90_strerror(status)) |
---|
205 | stop |
---|
206 | endif |
---|
207 | |
---|
208 | ! count the number of points before longitude(i) wraps around |
---|
209 | i=3 |
---|
210 | lonlen=1 |
---|
211 | !write(*,*) "longitude(2)=",surf_field(2) |
---|
212 | do while (surf_field(i).ne.surf_field(2)) |
---|
213 | !write(*,*) "i:",i,"surf_field(i)=",surf_field(i) |
---|
214 | i=i+1 |
---|
215 | lonlen=lonlen+1 |
---|
216 | enddo |
---|
217 | ! and add 1 because we want a redundant lon=180 point |
---|
218 | lonlen=lonlen+1 |
---|
219 | write(*,*) " => lonlen=",lonlen |
---|
220 | |
---|
221 | allocate(longitude(lonlen)) |
---|
222 | ! fill longitude(1:lonlen) |
---|
223 | longitude(1:lonlen-1)=surf_field(2:lonlen) |
---|
224 | longitude(lonlen)=-longitude(1) ! redundant +Pi/2 |
---|
225 | ! convert to degrees |
---|
226 | longitude(:)=longitude(:)*(180./pi) |
---|
227 | |
---|
228 | ! knowing lonlen, compute latlen |
---|
229 | latlen=2+(physical_points-2)/(lonlen-1) |
---|
230 | write(*,*) " => latlen=",latlen |
---|
231 | |
---|
232 | allocate(latitude(latlen)) |
---|
233 | ! get field "latitude(physical_points)" from infile |
---|
234 | status=nf90_inq_varid(inid,"latitude",varid) |
---|
235 | if (status.ne.nf90_noerr) then |
---|
236 | write(*,*) "Failed to find latitude ID" |
---|
237 | write(*,*)trim(nf90_strerror(status)) |
---|
238 | stop |
---|
239 | endif |
---|
240 | ! read latitude |
---|
241 | status=nf90_get_var(inid,varid,surf_field) |
---|
242 | if (status.ne.nf90_noerr) then |
---|
243 | write(*,*) "Failed to load latitude" |
---|
244 | write(*,*)trim(nf90_strerror(status)) |
---|
245 | stop |
---|
246 | endif |
---|
247 | |
---|
248 | latitude(1)=surf_field(1) |
---|
249 | !write(*,*) "latitude(1)=",latitude(1) |
---|
250 | do i=2,latlen-1 |
---|
251 | latitude(i)=surf_field((i-1)*(lonlen-1)) |
---|
252 | ! write(*,*) "i:",i,"latitude(i)=",latitude(i) |
---|
253 | enddo |
---|
254 | latitude(latlen)=surf_field(physical_points) |
---|
255 | !write(*,*) "latitude(latlen)=",latitude(latlen) |
---|
256 | ! convert to degrees |
---|
257 | latitude(:)=latitude(:)*(180./pi) |
---|
258 | |
---|
259 | ! depth: |
---|
260 | allocate(depth(subsurface_layers)) |
---|
261 | ! load "soildepth(subsurface_layers)" from input file |
---|
262 | status=nf90_inq_varid(inid,"soildepth",varid) |
---|
263 | if (status.ne.nf90_noerr) then |
---|
264 | write(*,*) "Failed to find soildepth ID" |
---|
265 | write(*,*)trim(nf90_strerror(status)) |
---|
266 | stop |
---|
267 | endif |
---|
268 | ! read longitude |
---|
269 | status=nf90_get_var(inid,varid,depth) |
---|
270 | if (status.ne.nf90_noerr) then |
---|
271 | write(*,*) "Failed to load soildepth" |
---|
272 | write(*,*)trim(nf90_strerror(status)) |
---|
273 | stop |
---|
274 | endif |
---|
275 | |
---|
276 | ! 2.2.2 define dimensions to output file |
---|
277 | ! longitude: |
---|
278 | status=nf90_def_dim(outid,"longitude",lonlen,lon_dimid) |
---|
279 | if (status.ne.nf90_noerr) then |
---|
280 | write(*,*) "Failed creating longitude dimension" |
---|
281 | write(*,*)trim(nf90_strerror(status)) |
---|
282 | stop |
---|
283 | endif |
---|
284 | ! latitude: |
---|
285 | status=nf90_def_dim(outid,"latitude",latlen,lat_dimid) |
---|
286 | if (status.ne.nf90_noerr) then |
---|
287 | write(*,*) "Failed creating longitude dimension" |
---|
288 | write(*,*)trim(nf90_strerror(status)) |
---|
289 | stop |
---|
290 | endif |
---|
291 | ! depth: |
---|
292 | status=nf90_def_dim(outid,"depth",subsurface_layers,depth_dimid) |
---|
293 | if (status.ne.nf90_noerr) then |
---|
294 | write(*,*) "Failed creating depth dimension" |
---|
295 | write(*,*)trim(nf90_strerror(status)) |
---|
296 | stop |
---|
297 | endif |
---|
298 | |
---|
299 | !2.3. write variables to output file |
---|
300 | ! 2.3.1. define 1D variables |
---|
301 | ! longitude |
---|
302 | datashape(1)=lon_dimid |
---|
303 | status=nf90_def_var(outid,"longitude",NF90_REAL,lon_dimid,lon_varid) |
---|
304 | if (status.ne.nf90_noerr) then |
---|
305 | write(*,*) "Failed creating longitude variable" |
---|
306 | write(*,*)trim(nf90_strerror(status)) |
---|
307 | stop |
---|
308 | endif |
---|
309 | ! longitude attributes |
---|
310 | status=nf90_put_att(outid,lon_varid,"long_name","East longitude") |
---|
311 | status=nf90_put_att(outid,lon_varid,"units","degrees_east") |
---|
312 | |
---|
313 | !latitude |
---|
314 | datashape(2)=lat_dimid |
---|
315 | status=nf90_def_var(outid,"latitude",NF90_REAL,lat_dimid,lat_varid) |
---|
316 | if (status.ne.nf90_noerr) then |
---|
317 | write(*,*) "Failed creating latitude variable" |
---|
318 | write(*,*)trim(nf90_strerror(status)) |
---|
319 | stop |
---|
320 | endif |
---|
321 | ! latitude attributes |
---|
322 | status=nf90_put_att(outid,lat_varid,"long_name","North latitude") |
---|
323 | status=nf90_put_att(outid,lat_varid,"units","degrees_north") |
---|
324 | |
---|
325 | ! depth |
---|
326 | status=nf90_def_var(outid,"depth",NF90_REAL,depth_dimid,depth_varid) |
---|
327 | if (status.ne.nf90_noerr) then |
---|
328 | write(*,*) "Failed creating depth variable" |
---|
329 | write(*,*)trim(nf90_strerror(status)) |
---|
330 | stop |
---|
331 | endif |
---|
332 | !depth atributes |
---|
333 | status=nf90_put_att(outid,depth_varid,"long_name","Soil mid-layer depth") |
---|
334 | status=nf90_put_att(outid,depth_varid,"units","m") |
---|
335 | status=nf90_put_att(outid,depth_varid,"positive","down") |
---|
336 | |
---|
337 | ! 2.3.2 write 1D variable |
---|
338 | |
---|
339 | ! swich out of NetCDF define mode |
---|
340 | status=nf90_enddef(outid) |
---|
341 | if (status.ne.nf90_noerr) then |
---|
342 | write(*,*) "Failed to swich out of define mode" |
---|
343 | write(*,*)trim(nf90_strerror(status)) |
---|
344 | stop |
---|
345 | endif |
---|
346 | |
---|
347 | ! write longitude |
---|
348 | status=nf90_put_var(outid,lon_varid,longitude) |
---|
349 | if (status.ne.nf90_noerr) then |
---|
350 | write(*,*) "Failed writing longitude" |
---|
351 | write(*,*)trim(nf90_strerror(status)) |
---|
352 | stop |
---|
353 | endif |
---|
354 | |
---|
355 | ! write latitude |
---|
356 | status=nf90_put_var(outid,lat_varid,latitude) |
---|
357 | if (status.ne.nf90_noerr) then |
---|
358 | write(*,*) "Failed writing latitude" |
---|
359 | write(*,*)trim(nf90_strerror(status)) |
---|
360 | stop |
---|
361 | endif |
---|
362 | |
---|
363 | ! write depth |
---|
364 | status=nf90_put_var(outid,depth_varid,depth) |
---|
365 | if (status.ne.nf90_noerr) then |
---|
366 | write(*,*) "Failed writing depth" |
---|
367 | write(*,*)trim(nf90_strerror(status)) |
---|
368 | stop |
---|
369 | endif |
---|
370 | |
---|
371 | ! 2.3. 2D (surface) variables |
---|
372 | ! First find out how many variables there are in input file |
---|
373 | status=nf90_inquire(inid,nbindims,nbinvars) |
---|
374 | if (status.ne.nf90_noerr) then |
---|
375 | write(*,*) "Failed obtaining nbindims and nbinvars from input file" |
---|
376 | write(*,*)trim(nf90_strerror(status)) |
---|
377 | stop |
---|
378 | endif |
---|
379 | |
---|
380 | allocate(out_surf_field(lonlen,latlen)) |
---|
381 | |
---|
382 | shape(:) = 0 |
---|
383 | do ivar=1,nbinvars ! loop on all input variables |
---|
384 | ! find out what dimensions are linked to this variable |
---|
385 | status=nf90_inquire_variable(inid,ivar,name=varname,ndims=nbdim,& |
---|
386 | dimids=shape,natts=nbatt) |
---|
387 | if (((nbdim==1).and.(shape(1)==physical_points_dimid))& |
---|
388 | .or.((nbdim==2).and.(shape(1)==physical_points_dimid)& |
---|
389 | .and.(shape(2)==time_dimid))) then |
---|
390 | |
---|
391 | corner(1) = 1 |
---|
392 | corner(2) = timelen |
---|
393 | edges(1) = physical_points |
---|
394 | edges(2) = 1 |
---|
395 | |
---|
396 | ! skip "longitude" and "latitude" |
---|
397 | if (trim(varname)=="longitude") cycle |
---|
398 | if (trim(varname)=="latitude") cycle |
---|
399 | |
---|
400 | write(*,*) " processing: ",trim(varname) |
---|
401 | |
---|
402 | ! load input data: |
---|
403 | status=nf90_inq_varid(inid,varname,invarid) |
---|
404 | status=nf90_get_var(inid,invarid,surf_field,corner,edges) |
---|
405 | |
---|
406 | ! switch output file to to define mode |
---|
407 | status=nf90_redef(outid) |
---|
408 | if (status.ne.nf90_noerr) then |
---|
409 | write(*,*) "Failed to swich to define mode" |
---|
410 | write(*,*)trim(nf90_strerror(status)) |
---|
411 | stop |
---|
412 | endif |
---|
413 | !define the variable |
---|
414 | status=nf90_def_var(outid,trim(varname),NF90_REAL,& |
---|
415 | (/lon_dimid,lat_dimid/),varid) |
---|
416 | if (status.ne.nf90_noerr) then |
---|
417 | write(*,*) "Failed creating variable ",trim(varname) |
---|
418 | write(*,*)trim(nf90_strerror(status)) |
---|
419 | stop |
---|
420 | endif |
---|
421 | |
---|
422 | ! variable attributes |
---|
423 | do k=1,nbatt |
---|
424 | status=nf90_inq_attname(inid,invarid,k,varatt) |
---|
425 | if (status.ne.nf90_noerr) then |
---|
426 | write(*,*) "Failed getting attribute number",k," for ",trim(varname) |
---|
427 | write(*,*)trim(nf90_strerror(status)) |
---|
428 | stop |
---|
429 | endif |
---|
430 | status=nf90_get_att(inid,invarid,trim(varatt),varattcontent) |
---|
431 | if (status.ne.nf90_noerr) then |
---|
432 | write(*,*) "Failed loading attribute ",trim(varatt) |
---|
433 | write(*,*)trim(nf90_strerror(status)) |
---|
434 | stop |
---|
435 | endif |
---|
436 | |
---|
437 | ! write the attribute and its value to output |
---|
438 | status=nf90_put_att(outid,varid,trim(varatt),trim(varattcontent)) |
---|
439 | if (status.ne.nf90_noerr) then |
---|
440 | write(*,*) "Failed writing attribute ",trim(varatt) |
---|
441 | write(*,*)trim(nf90_strerror(status)) |
---|
442 | stop |
---|
443 | endif |
---|
444 | enddo ! do k=1,nbatt |
---|
445 | |
---|
446 | ! swich out of NetCDF define mode |
---|
447 | status=nf90_enddef(outid) |
---|
448 | if (status.ne.nf90_noerr) then |
---|
449 | write(*,*) "Failed to swich out of define mode" |
---|
450 | write(*,*)trim(nf90_strerror(status)) |
---|
451 | stop |
---|
452 | endif |
---|
453 | |
---|
454 | ! "convert" from surf_field(:) to out_surf_field(:,:) |
---|
455 | do i=1,lonlen |
---|
456 | out_surf_field(i,1)=surf_field(1) ! North Pole |
---|
457 | out_surf_field(i,latlen)=surf_field(physical_points) ! South Pole |
---|
458 | enddo |
---|
459 | do j=2,latlen-1 |
---|
460 | ig0=1+(j-2)*(lonlen-1) |
---|
461 | do i=1,lonlen-1 |
---|
462 | out_surf_field(i,j)=surf_field(ig0+i) |
---|
463 | enddo |
---|
464 | out_surf_field(lonlen,j)=out_surf_field(1,j) ! redundant lon=180 |
---|
465 | enddo |
---|
466 | |
---|
467 | ! write the variable |
---|
468 | status=nf90_put_var(outid,varid,out_surf_field) |
---|
469 | if (status.ne.nf90_noerr) then |
---|
470 | write(*,*) "Failed to write ",trim(varname) |
---|
471 | write(*,*)trim(nf90_strerror(status)) |
---|
472 | stop |
---|
473 | endif |
---|
474 | endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid)) |
---|
475 | enddo ! of do i=1,nbinvars |
---|
476 | |
---|
477 | ! 2.4. 3D (subsurface) variables |
---|
478 | allocate(out_subsurf_field(lonlen,latlen,subsurface_layers)) |
---|
479 | |
---|
480 | do ivar=1,nbinvars ! loop on all input variables |
---|
481 | ! find out what dimensions are linked to this variable |
---|
482 | status=nf90_inquire_variable(inid,ivar,name=varname,ndims=nbdim,& |
---|
483 | dimids=shape,natts=nbatt) |
---|
484 | if (((nbdim==2).and.(shape(1)==physical_points_dimid)& |
---|
485 | .and.(shape(2)==subsurface_layers_dimid))& |
---|
486 | .or.((nbdim==3).and.(shape(1)==physical_points_dimid)& |
---|
487 | .and.(shape(2)==subsurface_layers_dimid)& |
---|
488 | .and.(shape(3)==time_dimid))) then |
---|
489 | |
---|
490 | corner(1) = 1 |
---|
491 | corner(2) = 1 |
---|
492 | corner(3) = timelen |
---|
493 | edges(1) = physical_points |
---|
494 | edges(2) = subsurface_layers |
---|
495 | edges(3) = 1 |
---|
496 | |
---|
497 | write(*,*) " processing: ",trim(varname) |
---|
498 | |
---|
499 | ! load input data: |
---|
500 | status=nf90_inq_varid(inid,varname,invarid) |
---|
501 | status=nf90_get_var(inid,invarid,subsurf_field,corner,edges) |
---|
502 | |
---|
503 | ! switch output file to to define mode |
---|
504 | status=nf90_redef(outid) |
---|
505 | if (status.ne.nf90_noerr) then |
---|
506 | write(*,*) "Failed to swich to define mode" |
---|
507 | write(*,*)trim(nf90_strerror(status)) |
---|
508 | stop |
---|
509 | endif |
---|
510 | !define the variable |
---|
511 | status=nf90_def_var(outid,trim(varname),NF90_REAL,& |
---|
512 | (/lon_dimid,lat_dimid,depth_dimid/),varid) |
---|
513 | if (status.ne.nf90_noerr) then |
---|
514 | write(*,*) "Failed creating variable ",trim(varname) |
---|
515 | write(*,*)trim(nf90_strerror(status)) |
---|
516 | stop |
---|
517 | endif |
---|
518 | |
---|
519 | ! variable attributes |
---|
520 | do k=1,nbatt |
---|
521 | status=nf90_inq_attname(inid,invarid,k,varatt) |
---|
522 | if (status.ne.nf90_noerr) then |
---|
523 | write(*,*) "Failed getting attribute number",k," for ",trim(varname) |
---|
524 | write(*,*)trim(nf90_strerror(status)) |
---|
525 | stop |
---|
526 | endif |
---|
527 | status=nf90_get_att(inid,invarid,trim(varatt),varattcontent) |
---|
528 | if (status.ne.nf90_noerr) then |
---|
529 | write(*,*) "Failed loading attribute ",trim(varatt) |
---|
530 | write(*,*)trim(nf90_strerror(status)) |
---|
531 | stop |
---|
532 | endif |
---|
533 | |
---|
534 | ! write the attribute and its value to output |
---|
535 | status=nf90_put_att(outid,varid,trim(varatt),trim(varattcontent)) |
---|
536 | if (status.ne.nf90_noerr) then |
---|
537 | write(*,*) "Failed writing attribute ",trim(varatt) |
---|
538 | write(*,*)trim(nf90_strerror(status)) |
---|
539 | stop |
---|
540 | endif |
---|
541 | enddo ! do k=1,nbatt |
---|
542 | |
---|
543 | ! swich out of NetCDF define mode |
---|
544 | status=nf90_enddef(outid) |
---|
545 | if (status.ne.nf90_noerr) then |
---|
546 | write(*,*) "Failed to swich out of define mode" |
---|
547 | write(*,*)trim(nf90_strerror(status)) |
---|
548 | stop |
---|
549 | endif |
---|
550 | |
---|
551 | ! "convert" from subsurf_field(:,:) to out_subsurf_field(:,:,:) |
---|
552 | do i=1,lonlen |
---|
553 | out_subsurf_field(i,1,:)=subsurf_field(1,:) ! North Pole |
---|
554 | out_subsurf_field(i,latlen,:)=subsurf_field(1,:) ! South Pole |
---|
555 | enddo |
---|
556 | do j=2,latlen-1 |
---|
557 | ig0=1+(j-2)*(lonlen-1) |
---|
558 | do i=1,lonlen-1 |
---|
559 | out_subsurf_field(i,j,:)=subsurf_field(ig0+i,:) |
---|
560 | enddo |
---|
561 | out_subsurf_field(lonlen,j,:)=out_subsurf_field(1,j,:) ! redundant lon=180 |
---|
562 | enddo |
---|
563 | |
---|
564 | ! write the variable |
---|
565 | status=nf90_put_var(outid,varid,out_subsurf_field) |
---|
566 | if (status.ne.nf90_noerr) then |
---|
567 | write(*,*) "Failed to write ",trim(varname) |
---|
568 | write(*,*)trim(nf90_strerror(status)) |
---|
569 | stop |
---|
570 | endif |
---|
571 | endif ! of if ((nbdim==1).and.(shape(1)==physical_points_dimid)...) |
---|
572 | enddo ! of do i=1,nbinvars |
---|
573 | |
---|
574 | |
---|
575 | ! 3. Finish things and cleanup |
---|
576 | ! Close output file |
---|
577 | status=nf90_close(outid) |
---|
578 | if (status.ne.nf90_noerr) then |
---|
579 | write(*,*)"Failed to close file: ",trim(outfile) |
---|
580 | write(*,*)trim(nf90_strerror(status)) |
---|
581 | stop |
---|
582 | endif |
---|
583 | |
---|
584 | end program expandstartfi |
---|