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