1 |
C $Header: /u/gcmpack/MITgcm/pkg/mnc/mnc_file.F,v 1.18 2005/01/24 04:40:35 edhill Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "MNC_OPTIONS.h" |
5 |
|
6 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
7 |
CBOP 0 |
8 |
C !ROUTINE: MNC_FILE_CREATE |
9 |
|
10 |
C !INTERFACE: |
11 |
SUBROUTINE MNC_FILE_CREATE( |
12 |
I fname, |
13 |
I myThid ) |
14 |
|
15 |
C !DESCRIPTION: |
16 |
C Create a NetCDF file. |
17 |
|
18 |
C !USES: |
19 |
implicit none |
20 |
|
21 |
C !INPUT PARAMETERS: |
22 |
integer myThid |
23 |
character*(*) fname |
24 |
CEOP |
25 |
|
26 |
C !LOCAL VARIABLES: |
27 |
integer indf |
28 |
|
29 |
CALL MNC_FILE_OPEN(fname, 0, indf, myThid) |
30 |
|
31 |
RETURN |
32 |
END |
33 |
|
34 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
35 |
CBOP 0 |
36 |
C !ROUTINE: MNC_FILE_OPEN |
37 |
|
38 |
C !INTERFACE: |
39 |
SUBROUTINE MNC_FILE_OPEN( |
40 |
I fname, |
41 |
I itype, |
42 |
O indf, |
43 |
I myThid ) |
44 |
|
45 |
C !DESCRIPTION: |
46 |
C Open or create a NetCDF file. |
47 |
|
48 |
C !USES: |
49 |
implicit none |
50 |
#include "netcdf.inc" |
51 |
#include "mnc_common.h" |
52 |
#include "EEPARAMS.h" |
53 |
|
54 |
C !INPUT PARAMETERS: |
55 |
integer myThid,indf |
56 |
character*(*) fname |
57 |
integer itype |
58 |
C itype => [ 0=new | 1=append | 2=read-only ] |
59 |
CEOP |
60 |
|
61 |
C !LOCAL VARIABLES: |
62 |
integer n, err, fid, nf |
63 |
character*(MAX_LEN_MBUF) msgbuf |
64 |
|
65 |
C Functions |
66 |
integer ILNBLNK |
67 |
|
68 |
C Is the file already open? |
69 |
nf = ILNBLNK(fname) |
70 |
CALL MNC_GET_IND(MNC_MAX_ID, fname, mnc_f_names, indf, myThid) |
71 |
IF (indf .GT. 0) THEN |
72 |
write(msgbuf,'(3a)') 'MNC_FILE_OPEN ERROR: ''', fname(1:nf), |
73 |
& ''' is already open -- cannot open twice' |
74 |
CALL print_error(msgbuf, mythid) |
75 |
stop 'ABNORMAL END: package MNC' |
76 |
ENDIF |
77 |
|
78 |
write(msgbuf,'(3a)') 'opening ''', fname, '''' |
79 |
IF (itype .EQ. 0) THEN |
80 |
|
81 |
C Create new file |
82 |
err = NF_CREATE(fname, NF_CLOBBER, fid) |
83 |
CALL MNC_HANDLE_ERR(err, msgbuf, myThid) |
84 |
|
85 |
ELSEIF (itype .EQ. 1) THEN |
86 |
|
87 |
C Append to existing file |
88 |
CALL MNC_FILE_READALL(fname, myThid) |
89 |
|
90 |
ELSE |
91 |
C Error |
92 |
write(msgbuf,'(a,i5,a)') 'MNC_FILE_OPEN ERROR: ''', itype, |
93 |
& ''' is not defined--should be: [0|1]' |
94 |
CALL print_error( msgbuf, mythid ) |
95 |
stop 'ABNORMAL END: S/R MNC_FILE_ADD_ATTR_STR' |
96 |
ENDIF |
97 |
|
98 |
CALL MNC_GET_NEXT_EMPTY_IND(MNC_MAX_ID,mnc_f_names,indf, myThid) |
99 |
n = ILNBLNK(fname) |
100 |
mnc_f_names(indf)(1:n) = fname(1:n) |
101 |
mnc_f_info(indf,1) = 1 |
102 |
mnc_f_info(indf,2) = fid |
103 |
mnc_f_info(indf,3) = 0 |
104 |
mnc_fv_ids(indf,1) = 0 |
105 |
mnc_f_alld(indf,1) = 0 |
106 |
|
107 |
RETURN |
108 |
END |
109 |
|
110 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
111 |
CBOP 0 |
112 |
C !ROUTINE: MNC_FILE_ADD_ATTR_STR |
113 |
|
114 |
C !INTERFACE: |
115 |
SUBROUTINE MNC_FILE_ADD_ATTR_STR( |
116 |
I fname, |
117 |
I atname, |
118 |
I sval, |
119 |
I myThid ) |
120 |
|
121 |
C !DESCRIPTION: |
122 |
C Add a character string attribute to a NetCDF file. |
123 |
|
124 |
C !USES: |
125 |
implicit none |
126 |
|
127 |
C !INPUT PARAMETERS: |
128 |
integer myThid |
129 |
character*(*) fname, atname, sval |
130 |
CEOP |
131 |
real*4 sZero |
132 |
sZero = 0. |
133 |
|
134 |
CALL MNC_FILE_ADD_ATTR_ANY(fname,atname, 1, |
135 |
& sval, 0, 0.0D0, sZero, 0, myThid ) |
136 |
RETURN |
137 |
END |
138 |
|
139 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
140 |
CBOP 0 |
141 |
C !ROUTINE: MNC_FILE_ADD_ATTR_DBL |
142 |
|
143 |
C !INTERFACE: |
144 |
SUBROUTINE MNC_FILE_ADD_ATTR_DBL( |
145 |
I fname, |
146 |
I atname, |
147 |
I len, |
148 |
I dval, |
149 |
I myThid ) |
150 |
|
151 |
C !DESCRIPTION: |
152 |
C Add a double-precision real attribute to a NetCDF file. |
153 |
|
154 |
C !USES: |
155 |
implicit none |
156 |
|
157 |
C !INPUT PARAMETERS: |
158 |
integer myThid, len |
159 |
character*(*) fname, atname |
160 |
REAL*8 dval |
161 |
CEOP |
162 |
real*4 sZero |
163 |
sZero = 0. |
164 |
|
165 |
CALL MNC_FILE_ADD_ATTR_ANY(fname,atname, 2, |
166 |
& ' ', len, dval, sZero, 0, myThid ) |
167 |
RETURN |
168 |
END |
169 |
|
170 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
171 |
CBOP 0 |
172 |
C !ROUTINE: MNC_FILE_ADD_ATTR_REAL |
173 |
|
174 |
C !INTERFACE: |
175 |
SUBROUTINE MNC_FILE_ADD_ATTR_REAL( |
176 |
I fname, |
177 |
I atname, |
178 |
I len, |
179 |
I rval, |
180 |
I myThid ) |
181 |
|
182 |
C !DESCRIPTION: |
183 |
C Add a single-precision real attribute to a NetCDF file. |
184 |
|
185 |
C !USES: |
186 |
implicit none |
187 |
|
188 |
C !INPUT PARAMETERS: |
189 |
integer myThid, len |
190 |
character*(*) fname, atname |
191 |
REAL*4 rval |
192 |
CEOP |
193 |
|
194 |
CALL MNC_FILE_ADD_ATTR_ANY(fname,atname, 3, |
195 |
& ' ', len, 0.0D0, rval, 0, myThid ) |
196 |
RETURN |
197 |
END |
198 |
|
199 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
200 |
CBOP 0 |
201 |
C !ROUTINE: MNC_FILE_ADD_ATTR_INT |
202 |
|
203 |
C !INTERFACE: |
204 |
SUBROUTINE MNC_FILE_ADD_ATTR_INT( |
205 |
I fname, |
206 |
I atname, |
207 |
I len, |
208 |
I ival, |
209 |
I myThid ) |
210 |
|
211 |
C !DESCRIPTION: |
212 |
C Add an integer attribute to a NetCDF file. |
213 |
|
214 |
C !USES: |
215 |
implicit none |
216 |
|
217 |
C !INPUT PARAMETERS: |
218 |
integer myThid, len, ival |
219 |
character*(*) fname, atname |
220 |
CEOP |
221 |
real*4 sZero |
222 |
sZero = 0. |
223 |
|
224 |
CALL MNC_FILE_ADD_ATTR_ANY(fname,atname, 4, |
225 |
& ' ', len, 0.0D0, sZero, ival, myThid ) |
226 |
RETURN |
227 |
END |
228 |
|
229 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
230 |
CBOP 1 |
231 |
C !ROUTINE: MNC_FILE_ADD_ATTR_ANY |
232 |
|
233 |
C !INTERFACE: |
234 |
SUBROUTINE MNC_FILE_ADD_ATTR_ANY( |
235 |
I fname, |
236 |
I atname, |
237 |
I atype, sv, len,dv,rv,iv, |
238 |
I myThid ) |
239 |
|
240 |
C !DESCRIPTION: |
241 |
C Add all attributes to a NetCDF file. |
242 |
|
243 |
C !USES: |
244 |
implicit none |
245 |
#include "netcdf.inc" |
246 |
#include "mnc_common.h" |
247 |
#include "EEPARAMS.h" |
248 |
|
249 |
C !INPUT PARAMETERS: |
250 |
integer myThid, atype, len, iv |
251 |
character*(*) fname, atname, sv |
252 |
REAL*8 dv |
253 |
REAL*4 rv |
254 |
CEOP |
255 |
|
256 |
C !LOCAL VARIABLES: |
257 |
integer n, err, fid, ind, n1, lens |
258 |
character*(MNC_MAX_CHAR) s1 |
259 |
character*(MAX_LEN_MBUF) msgbuf |
260 |
|
261 |
C Functions |
262 |
integer ILNBLNK |
263 |
|
264 |
C Verify that the file is open |
265 |
CALL MNC_GET_IND(MNC_MAX_ID, fname, mnc_f_names, ind, myThid) |
266 |
IF (ind .LT. 0) THEN |
267 |
write(msgbuf,'(3a)') 'MNC ERROR: file ''', |
268 |
& fname, ''' must be opened first' |
269 |
CALL print_error( msgbuf, mythid ) |
270 |
stop 'ABNORMAL END: S/R MNC_FILE_ADD_ATTR_INT' |
271 |
ENDIF |
272 |
fid = mnc_f_info(ind,2) |
273 |
|
274 |
C Enter define mode |
275 |
CALL MNC_FILE_REDEF(fname, myThid) |
276 |
|
277 |
s1(1:MNC_MAX_CHAR) = mnc_blank_name(1:MNC_MAX_CHAR) |
278 |
n1 = ILNBLNK(atname) |
279 |
s1(1:n1) = atname(1:n1) |
280 |
|
281 |
IF (atype .EQ. 1) THEN |
282 |
lens = ILNBLNK(sv) |
283 |
err = NF_PUT_ATT_TEXT(fid, NF_GLOBAL, s1, lens, sv) |
284 |
CALL MNC_HANDLE_ERR(err, |
285 |
& 'adding TEXT attribute in S/R MNC_FILE_ADD_ATTR_ANY', |
286 |
& myThid) |
287 |
ELSEIF (atype .EQ. 2) THEN |
288 |
err = NF_PUT_ATT_DOUBLE(fid, NF_GLOBAL, s1, NF_DOUBLE, len, dv) |
289 |
CALL MNC_HANDLE_ERR(err, |
290 |
& 'adding DOUBLE attribute in S/R MNC_FILE_ADD_ATTR_ANY', |
291 |
& myThid) |
292 |
ELSEIF (atype .EQ. 3) THEN |
293 |
err = NF_PUT_ATT_REAL(fid, NF_GLOBAL, s1, NF_FLOAT, len, rv) |
294 |
CALL MNC_HANDLE_ERR(err, |
295 |
& 'adding REAL attribute in S/R MNC_FILE_ADD_ATTR_ANY', |
296 |
& myThid) |
297 |
ELSEIF (atype .EQ. 4) THEN |
298 |
err = NF_PUT_ATT_INT(fid, NF_GLOBAL, s1, NF_INT, len, iv) |
299 |
CALL MNC_HANDLE_ERR(err, |
300 |
& 'adding INT attribute in S/R MNC_FILE_ADD_ATTR_ANY', |
301 |
& myThid) |
302 |
ELSE |
303 |
write(msgbuf,'(a,i10,a)') 'MNC ERROR: atype = ''', atype, |
304 |
& ''' is invalid--must be: [1-4]' |
305 |
n = ILNBLNK(msgbuf) |
306 |
CALL print_error(msgbuf(1:n), mythid) |
307 |
stop 'ABNORMAL END: S/R MNC_VAR_ADD_ATTR_ANY' |
308 |
ENDIF |
309 |
|
310 |
RETURN |
311 |
END |
312 |
|
313 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
314 |
CBOP 1 |
315 |
C !ROUTINE: MNC_FILE_CLOSE |
316 |
|
317 |
C !INTERFACE: |
318 |
SUBROUTINE MNC_FILE_CLOSE( |
319 |
I fname, |
320 |
I myThid ) |
321 |
|
322 |
C !DESCRIPTION: |
323 |
C Close a NetCDF file. |
324 |
|
325 |
C !USES: |
326 |
implicit none |
327 |
#include "netcdf.inc" |
328 |
#include "mnc_common.h" |
329 |
#include "EEPARAMS.h" |
330 |
|
331 |
C !INPUT PARAMETERS: |
332 |
integer myThid |
333 |
character*(*) fname |
334 |
CEOP |
335 |
|
336 |
C !LOCAL VARIABLES: |
337 |
integer i,j,k,n, err, fid, indf, nf |
338 |
character*(MAX_LEN_MBUF) msgbuf |
339 |
|
340 |
C Functions |
341 |
integer ILNBLNK |
342 |
|
343 |
nf = ILNBLNK(fname) |
344 |
|
345 |
C Check that the file is open |
346 |
CALL MNC_GET_IND(MNC_MAX_ID, fname, mnc_f_names, indf, myThid) |
347 |
IF (indf .LT. 1) THEN |
348 |
write(msgbuf,'(3a)') 'MNC Warning: file ''', fname(1:nf), |
349 |
& ''' is already closed' |
350 |
CALL print_error( msgbuf, mythid ) |
351 |
RETURN |
352 |
ENDIF |
353 |
fid = mnc_f_info(indf,2) |
354 |
err = NF_CLOSE(fid) |
355 |
write(msgbuf,'(3a)') ' cannot close file ''', fname(1:nf), '''' |
356 |
CALL MNC_HANDLE_ERR(err, msgbuf, myThid) |
357 |
|
358 |
C Clear all the info associated with this file |
359 |
C variables |
360 |
n = mnc_fv_ids(indf,1) |
361 |
IF (n .GE. 1) THEN |
362 |
DO i = 1,n |
363 |
j = 2 + 3*(i - 1) |
364 |
k = mnc_fv_ids(indf,j) |
365 |
mnc_v_names(k)(1:MNC_MAX_CHAR)=mnc_blank_name(1:MNC_MAX_CHAR) |
366 |
ENDDO |
367 |
DO i = 1,MNC_MAX_INFO |
368 |
mnc_fv_ids(indf,i) = 0 |
369 |
ENDDO |
370 |
ENDIF |
371 |
C dims |
372 |
n = mnc_f_alld(indf,1) |
373 |
mnc_f_alld(indf,1) = 0 |
374 |
DO i = 1,n |
375 |
j = mnc_f_alld(indf,i+1) |
376 |
mnc_d_ids(j) = 0 |
377 |
mnc_d_size(j) = 0 |
378 |
mnc_d_names(j)(1:MNC_MAX_CHAR) = mnc_blank_name(1:MNC_MAX_CHAR) |
379 |
mnc_f_alld(indf,i+1) = 0 |
380 |
ENDDO |
381 |
C grids |
382 |
n = mnc_f_info(indf,3) |
383 |
IF (n .GT. 0) THEN |
384 |
DO i = 1,n |
385 |
j = 4 + 3*(i - 1) |
386 |
k = mnc_f_info(indf,j) |
387 |
mnc_g_names(k)(1:MNC_MAX_CHAR)=mnc_blank_name(1:MNC_MAX_CHAR) |
388 |
ENDDO |
389 |
DO i = 1,MNC_MAX_INFO |
390 |
mnc_fd_ind(indf,i) = 0 |
391 |
mnc_f_info(indf,i) = 0 |
392 |
ENDDO |
393 |
ENDIF |
394 |
C file name |
395 |
mnc_f_names(indf)(1:MNC_MAX_CHAR) = mnc_blank_name(1:MNC_MAX_CHAR) |
396 |
|
397 |
RETURN |
398 |
END |
399 |
|
400 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
401 |
CBOP 0 |
402 |
C !ROUTINE: MNC_FILE_CLOSE_ALL_MATCHING |
403 |
|
404 |
C !INTERFACE: |
405 |
SUBROUTINE MNC_FILE_CLOSE_ALL_MATCHING( |
406 |
I fname, |
407 |
I myThid ) |
408 |
|
409 |
C !DESCRIPTION: |
410 |
C Close all files matching a character string. |
411 |
|
412 |
C !USES: |
413 |
implicit none |
414 |
#include "netcdf.inc" |
415 |
#include "mnc_common.h" |
416 |
#include "EEPARAMS.h" |
417 |
|
418 |
C !INPUT PARAMETERS: |
419 |
integer myThid |
420 |
character*(*) fname |
421 |
CEOP |
422 |
|
423 |
C !LOCAL VARIABLES: |
424 |
integer i,n |
425 |
|
426 |
C Functions |
427 |
integer ILNBLNK |
428 |
|
429 |
n = ILNBLNK(fname) |
430 |
DO i = 1,MNC_MAX_ID |
431 |
|
432 |
C Check that the file is open |
433 |
IF (fname(1:n) .EQ. mnc_f_names(i)(1:n)) THEN |
434 |
CALL MNC_FILE_CLOSE(mnc_f_names(i), myThid) |
435 |
ENDIF |
436 |
|
437 |
ENDDO |
438 |
|
439 |
RETURN |
440 |
END |
441 |
|
442 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
443 |
CBOP 0 |
444 |
C !ROUTINE: MNC_FILE_CLOSE_ALL |
445 |
|
446 |
C !INTERFACE: |
447 |
SUBROUTINE MNC_FILE_CLOSE_ALL( |
448 |
I myThid ) |
449 |
|
450 |
C !DESCRIPTION: |
451 |
C Close all NetCDF files. |
452 |
|
453 |
C !USES: |
454 |
implicit none |
455 |
#include "netcdf.inc" |
456 |
#include "mnc_common.h" |
457 |
#include "EEPARAMS.h" |
458 |
|
459 |
C !INPUT PARAMETERS: |
460 |
integer myThid |
461 |
CEOP |
462 |
|
463 |
C !LOCAL VARIABLES: |
464 |
integer i |
465 |
|
466 |
DO i = 1,MNC_MAX_ID |
467 |
|
468 |
C Check that the file is open |
469 |
IF (mnc_f_names(i)(1:MNC_MAX_CHAR) |
470 |
& .NE. mnc_blank_name(1:MNC_MAX_CHAR)) THEN |
471 |
CALL MNC_FILE_CLOSE(mnc_f_names(i), myThid) |
472 |
ENDIF |
473 |
|
474 |
ENDDO |
475 |
|
476 |
RETURN |
477 |
END |
478 |
|
479 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
480 |
CBOP 0 |
481 |
C !ROUTINE: MNC_FILE_REDEF |
482 |
|
483 |
C !INTERFACE: |
484 |
SUBROUTINE MNC_FILE_REDEF( |
485 |
I fname, |
486 |
I myThid ) |
487 |
|
488 |
C !DESCRIPTION: |
489 |
C Set the NetCDF file to DEFINE mode. |
490 |
|
491 |
C !USES: |
492 |
implicit none |
493 |
#include "netcdf.inc" |
494 |
#include "mnc_common.h" |
495 |
#include "EEPARAMS.h" |
496 |
|
497 |
C !INPUT PARAMETERS: |
498 |
integer myThid |
499 |
character*(*) fname |
500 |
CEOP |
501 |
|
502 |
C !LOCAL VARIABLES: |
503 |
integer ind, fid, def, err, n |
504 |
character*(MAX_LEN_MBUF) msgbuf |
505 |
|
506 |
C Functions |
507 |
integer ILNBLNK |
508 |
|
509 |
C Verify that the file is open |
510 |
CALL MNC_GET_IND(MNC_MAX_ID, fname, mnc_f_names, ind, myThid) |
511 |
IF (ind .LT. 0) THEN |
512 |
n = ILNBLNK(fname) |
513 |
write(msgbuf,'(3a)') 'MNC ERROR: file ''', |
514 |
& fname(1:n), ''' must be opened first' |
515 |
CALL print_error( msgbuf, mythid ) |
516 |
stop 'ABNORMAL END: S/R MNC_FILE_REDEF' |
517 |
ENDIF |
518 |
def = mnc_f_info(ind,1) |
519 |
fid = mnc_f_info(ind,2) |
520 |
|
521 |
IF (def .NE. 1) THEN |
522 |
C Enter define mode |
523 |
err = NF_REDEF(fid) |
524 |
CALL MNC_HANDLE_ERR(err, |
525 |
& 'entering define mode in S/R MNC_FILE_REDEF', myThid) |
526 |
mnc_f_info(ind,1) = 1 |
527 |
ENDIF |
528 |
|
529 |
RETURN |
530 |
END |
531 |
|
532 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
533 |
CBOP 0 |
534 |
C !ROUTINE: MNC_FILE_ENDDEF |
535 |
|
536 |
C !INTERFACE: |
537 |
SUBROUTINE MNC_FILE_ENDDEF( |
538 |
I fname, |
539 |
I myThid ) |
540 |
|
541 |
C !DESCRIPTION: |
542 |
C End DEFINE mode for a NetCDF file. |
543 |
|
544 |
C !USES: |
545 |
implicit none |
546 |
#include "netcdf.inc" |
547 |
#include "mnc_common.h" |
548 |
#include "EEPARAMS.h" |
549 |
|
550 |
C !INPUT PARAMETERS: |
551 |
integer myThid |
552 |
character*(*) fname |
553 |
CEOP |
554 |
|
555 |
C !LOCAL VARIABLES: |
556 |
integer ind, fid, def, err, n |
557 |
character*(MAX_LEN_MBUF) msgbuf |
558 |
|
559 |
C Functions |
560 |
integer ILNBLNK |
561 |
|
562 |
C Verify that the file is open |
563 |
CALL MNC_GET_IND(MNC_MAX_ID, fname, mnc_f_names, ind, myThid) |
564 |
IF (ind .LT. 0) THEN |
565 |
n = ILNBLNK(fname) |
566 |
write(msgbuf,'(3a)') 'MNC ERROR: file ''', |
567 |
& fname(1:n), ''' must be opened first' |
568 |
CALL print_error( msgbuf, mythid ) |
569 |
stop 'ABNORMAL END: S/R MNC_FILE_REDEF' |
570 |
ENDIF |
571 |
def = mnc_f_info(ind,1) |
572 |
fid = mnc_f_info(ind,2) |
573 |
|
574 |
IF (def .NE. 2) THEN |
575 |
C Enter define mode |
576 |
err = NF_ENDDEF(fid) |
577 |
CALL MNC_HANDLE_ERR(err, |
578 |
& 'ending define mode in S/R MNC_FILE_ENDDEF', myThid) |
579 |
mnc_f_info(ind,1) = 2 |
580 |
ENDIF |
581 |
|
582 |
RETURN |
583 |
END |
584 |
|
585 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
586 |
CBOP 1 |
587 |
C !ROUTINE: MNC_FILE_READALL |
588 |
|
589 |
C !INTERFACE: |
590 |
SUBROUTINE MNC_FILE_READALL( |
591 |
I fname, |
592 |
I myThid ) |
593 |
|
594 |
C !DESCRIPTION: |
595 |
C Try to open and read a NetCDF file. |
596 |
|
597 |
C !USES: |
598 |
implicit none |
599 |
#include "netcdf.inc" |
600 |
#include "EEPARAMS.h" |
601 |
|
602 |
C !INPUT PARAMETERS: |
603 |
integer myThid |
604 |
character*(*) fname |
605 |
CEOP |
606 |
|
607 |
C Functions |
608 |
integer IFNBLNK, ILNBLNK |
609 |
|
610 |
C Local Variables |
611 |
integer ierr, nff,nlf, indf |
612 |
character*(MAX_LEN_MBUF) msgbuf |
613 |
|
614 |
nff = IFNBLNK(fname) |
615 |
nlf = ILNBLNK(fname) |
616 |
CALL MNC_FILE_TRY_READ(fname, ierr, indf, myThid) |
617 |
write(msgbuf,'(3a)') 'MNC ERROR: cannot open file ''', |
618 |
& fname(nff:nlf), ''' for read/write access' |
619 |
CALL MNC_HANDLE_ERR(ierr, msgbuf, myThid) |
620 |
|
621 |
RETURN |
622 |
END |
623 |
|
624 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
625 |
CBOP 1 |
626 |
C !ROUTINE: MNC_FILE_TRY_READ |
627 |
|
628 |
C !INTERFACE: |
629 |
SUBROUTINE MNC_FILE_TRY_READ( |
630 |
I fname, |
631 |
O ierr, |
632 |
O indf, |
633 |
I myThid ) |
634 |
|
635 |
C !DESCRIPTION: |
636 |
C Try to open and read a NetCDF file. |
637 |
|
638 |
C !USES: |
639 |
implicit none |
640 |
#include "netcdf.inc" |
641 |
#include "mnc_common.h" |
642 |
#include "EEPARAMS.h" |
643 |
|
644 |
C !INPUT PARAMETERS: |
645 |
integer myThid, ierr, indf |
646 |
character*(*) fname |
647 |
|
648 |
C !LOCAL VARIABLES: |
649 |
integer i,j,k, fid, err, ndim,nvar,ngat,unlimid |
650 |
integer dlen, id, xtype, nat, nff,nlf, iv |
651 |
integer ndv, did, ns,ne, n1,n2, indg, indv |
652 |
character*(MAX_LEN_MBUF) msgbuf |
653 |
character*(NF_MAX_NAME) name |
654 |
integer idlist(NF_MAX_VAR_DIMS) |
655 |
character*(MNC_MAX_CHAR) dnames(20) |
656 |
CEOP |
657 |
C Functions |
658 |
integer IFNBLNK, ILNBLNK |
659 |
|
660 |
C Open and save the filename and fID |
661 |
nff = IFNBLNK(fname) |
662 |
nlf = ILNBLNK(fname) |
663 |
err = NF_OPEN(fname, NF_WRITE, fid) |
664 |
ierr = NF_NOERR |
665 |
IF (err .NE. NF_NOERR) THEN |
666 |
ierr = err |
667 |
RETURN |
668 |
ENDIF |
669 |
CALL MNC_GET_NEXT_EMPTY_IND(MNC_MAX_ID,mnc_f_names,indf, myThid) |
670 |
mnc_f_names(indf)(1:(nlf-nff+1)) = fname(nff:nlf) |
671 |
mnc_f_info(indf,2) = fid |
672 |
|
673 |
C Get the overall number of entities |
674 |
err = NF_INQ(fid, ndim, nvar, ngat, unlimid) |
675 |
write(msgbuf,'(4a)') 'MNC ERROR: cannot read number of dims', |
676 |
& ' in file ''', fname(nff:nlf), '''' |
677 |
CALL MNC_HANDLE_ERR(err, msgbuf, myThid) |
678 |
|
679 |
C Read each dimension and save the information |
680 |
DO id = 1,ndim |
681 |
err = NF_INQ_DIM(fid, id, name, dlen) |
682 |
write(msgbuf,'(2a,i5,3a)') 'MNC ERROR: cannot read dimension', |
683 |
& ' info for dim ''', id, ''' in file ''', |
684 |
& fname(nff:nlf), '''' |
685 |
CALL MNC_HANDLE_ERR(err, msgbuf, myThid) |
686 |
IF (id .EQ. unlimid) THEN |
687 |
dlen = -1 |
688 |
ENDIF |
689 |
ns = IFNBLNK(name) |
690 |
ne = ILNBLNK(name) |
691 |
|
692 |
CALL MNC_DIM_INIT_ALL(fname,name(ns:ne),dlen,'N', myThid) |
693 |
DO i = 1,mnc_f_alld(indf,1) |
694 |
j = mnc_f_alld(indf,i+1) |
695 |
n1 = IFNBLNK(mnc_d_names(j)) |
696 |
n2 = ILNBLNK(mnc_d_names(j)) |
697 |
IF (((ne-ns) .EQ. (n2-n1)) |
698 |
& .AND. (mnc_d_names(j)(ns:ne) .EQ. name(ns:ne))) THEN |
699 |
mnc_d_ids(j) = id |
700 |
goto 10 |
701 |
ENDIF |
702 |
ENDDO |
703 |
10 CONTINUE |
704 |
ENDDO |
705 |
|
706 |
C Read and save each variable |
707 |
DO id = 1,nvar |
708 |
err = NF_INQ_VAR(fid, id, name, xtype, ndv, idlist, nat) |
709 |
write(msgbuf,'(2a,i5,3a)') 'MNC ERROR: cannot read variable', |
710 |
& ' info for variable ''', id, ''' in file ''', |
711 |
& fname(nff:nlf), '''' |
712 |
CALL MNC_HANDLE_ERR(err, msgbuf, myThid) |
713 |
n1 = IFNBLNK(name) |
714 |
n2 = ILNBLNK(name) |
715 |
|
716 |
C Create a grid for this variable |
717 |
DO i = 1,ndv |
718 |
did = idlist(i) |
719 |
dnames(i)(1:MNC_MAX_CHAR) = mnc_d_names(did)(1:MNC_MAX_CHAR) |
720 |
ENDDO |
721 |
|
722 |
CALL MNC_GRID_INIT_ALL(fname, name, ndv, dnames, indg, myThid) |
723 |
|
724 |
C Update the tables |
725 |
CALL MNC_GET_NEXT_EMPTY_IND(MNC_MAX_ID,mnc_v_names,indv,myThid) |
726 |
mnc_v_names(indv)(1:(n2-n1+1)) = name(n1:n2) |
727 |
iv = 2 + 3*mnc_fv_ids(indf,1) |
728 |
mnc_fv_ids(indf,iv) = indv |
729 |
mnc_fv_ids(indf,iv+1) = id |
730 |
DO i = 1,mnc_f_info(indf,3) |
731 |
j = 4 + 3*(i-1) |
732 |
k = mnc_f_info(indf,j) |
733 |
IF (k .EQ. indg) THEN |
734 |
mnc_fv_ids(indf,iv+2) = j |
735 |
GOTO 20 |
736 |
ENDIF |
737 |
ENDDO |
738 |
20 CONTINUE |
739 |
mnc_fv_ids(indf,1) = mnc_fv_ids(indf,1) + 1 |
740 |
|
741 |
ENDDO |
742 |
|
743 |
RETURN |
744 |
END |
745 |
|
746 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |