Subversion Repositories lagranto.20cr

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 michaesp 1
      subroutine clscdf (cdfid, error)
2
c-----------------------------------------------------------------------
3
c     Purpose:
4
c        This routine closes an open netCDF file.
5
c     Aguments :
6
c        cdfid  int  input   the id of the file to be closed.
7
c        error  int  output  indicates possible errors found in this
8
c                            routine.
9
c                            error = 0   no errors detected.
10
c                            error = 1   error detected.
11
c     History:
12
c        Nov. 91  PPM  UW  Created.
13
c-----------------------------------------------------------------------
14
 
15
      include "netcdf.inc"
16
 
17
c     Argument declarations.
18
      integer      cdfid, error
19
 
20
c     Local variable declarations.
21
      integer      ncopts
22
 
23
c     Get current value of error options.
24
      call ncgopt (ncopts)
25
 
26
c     Make sure netCDF errors do not abort execution.
27
      call ncpopt (NCVERBOS)
28
 
29
c     Close requested file.
30
      call ncclos (cdfid, error)
31
 
32
c     Reset error options.
33
      call ncpopt (ncopts)
34
 
35
      end
36
 
37
 
38
      subroutine crecdf (filnam, cdfid, phymin, phymax, ndim, cfn, 
39
     &                   error) 
40
c-----------------------------------------------------------------------
41
c     Purpose:
42
c        This routine is called to create a netCDF file for use with 
43
c        the UWGAP plotting package.
44
c           Any netCDF file written to must be closed with the call
45
c        'call clscdf(cdfid,error)', where cdfid and error are
46
c        as in the argumentlist below. 
47
c     Arguments:
48
c        filnam  char  input   the user-supplied netCDF file name.
49
c        cdfid   int   output  the file-identifier
50
c        phymin  real  input   the minimum physical dimension of the
51
c                              entire physical domain along each axis.
52
c                              phymin is dimensioned (ndim)
53
c        phymax  real  input   the maximum physical dimension of the
54
c                              entire physical domain along each axis.
55
c                              phymax is dimensioned (ndim)
56
c        ndim    int   input   the number of dimensions in the file
57
c                              (i.e. number of elements in phymin,
58
c                              phymax)
59
c        cfn     char  input   constants file name 
60
c                              ('0' = no constants file).
61
c        error   int   output  indicates possible errors found in this
62
c                              routine.
63
c                              error = 0   no errors detected.
64
c                              error = 1   error detected.
65
c     History:
66
c        Nov. 91  PPM  UW  Created cr3df.
67
c        Jan. 92  CS   UW  Created crecdf.
68
c-----------------------------------------------------------------------
69
 
70
      include "netcdf.inc"
71
 
72
c     Argument declarations.
73
      integer        MAXDIM
74
      parameter      (MAXDIM=4)
75
      integer        ndim, error
76
      character *(*) filnam,cfn
77
      real           phymin(*), phymax(*)
78
 
79
c     Local variable declarations.
80
      character *(20) attnam
81
      character *(1)  chrid(MAXDIM)
82
      integer         cdfid, k, ibeg, iend, lenfil, ncopts
83
      data            chrid/'x','y','z','a'/
84
 
85
c     Get current value of error options, and make sure netCDF-errors do
86
c     not abort execution
87
      call ncgopt (ncopts)
88
      call ncpopt(NCVERBOS)
89
 
90
c     Initially set error to indicate no errors.
91
      error = 0
92
 
93
c     create the netCDF file
94
      cdfid = nccre (trim(filnam), NCCLOB, error)
95
      if (error.ne.0) go to 920
96
 
97
c     define global attributes
98
      do k=1,ndim
99
        attnam(1:3)='dom'
100
        attnam(4:4)=chrid(k)
101
        attnam(5:7)='min'
102
        attnam=attnam(1:7)
103
        call ncapt(cdfid,NCGLOBAL,attnam,NCFLOAT,1,phymin(k),error)
104
        if (error.gt.0) goto 920
105
 
106
        attnam(1:3)='dom'
107
        attnam(4:4)=chrid(k)
108
        attnam(5:7)='max'
109
        attnam=attnam(1:7)
110
        call ncapt(cdfid,NCGLOBAL,attnam,NCFLOAT,1,phymax(k),error)
111
        if (error.gt.0) goto 920
112
      enddo
113
 
114
c     define constants file name
115
      if (cfn.ne.'0') then
116
        call ncaptc (cdfid, NCGLOBAL, 'constants_file_name',
117
c    &             NCCHAR, len_trim(cfn)+1, cfn // char(0) , error)
118
     &             NCCHAR, len_trim(cfn), cfn , error)
119
        if (error.gt.0) goto 920
120
      endif
121
 
122
c     End variable definitions.
123
      call ncendf (cdfid, error)
124
      if (error.gt.0) goto 920
125
 
126
c     normal exit
127
      call ncpopt (ncopts)
128
      return
129
 
130
c     error exit
131
 920  write (6, *) 'ERROR: An error occurred while attempting to ',
132
     &             'create the data file in subroutine crecdf.'
133
      call ncpopt (ncopts)
134
      call ncclos (cdfid, error)
135
      end
136
 
137
      subroutine opncdf(filnam, cdfid,
138
     &                 phymin, phymax, ndim, varnam, nvar, cfn, error) 
139
c-----------------------------------------------------------------------
140
c     Purpose:
141
c        This routine is called to open a netCDF file for read and write
142
c        with the UWGAP plotting package. 
143
c     Arguments:
144
c        filnam  char  input   the user-supplied netCDF file name.
145
c        cdfid   int   output  the file-identifier
146
c        phymin  real  output  the minimum physical dimension of the
147
c                              entire physical domain along each axis.
148
c                              phymin is dimensioned (ndim)
149
c        phymax  real  output  the maximum physical dimension of the
150
c                              entire physical domain along each axis.
151
c                              phymax is dimensioned (ndim)
152
c        ndim    int   output  the number of dimensions in the file
153
c                              (i.e. number of elements in phymin,
154
c                              phymax)
155
c        varnam  char  output  an array containing the variable names.
156
c                              varnam is dimensioned (nvar).
157
c        nvar    int   output  the number of variables in the file
158
c        cfn     char  output  constants file name 
159
c                              ('0'=no constants file).
160
c        error   int   output  indicates possible errors found in this
161
c                              routine.
162
c                              error = 0   no errors detected.
163
c                              error = 1   error detected.
164
c     History:
165
c        Nov. 91  PPM  UW  Created cr3df.
166
c        Jan. 92  CS   UW  Created opncdf.
167
c-----------------------------------------------------------------------
168
 
169
      include "netcdf.inc"
170
 
171
c     Argument declarations.
172
      integer        MAXDIM
173
      parameter      (MAXDIM=4)
174
      integer        ndim, nvar, error
175
      character *(*) filnam, varnam(*),cfn
176
      real           phymin(*), phymax(*)
177
 
178
c     Local variable declarations.
179
      character *(20) attnam,vnam
180
      character *(1)  chrid(MAXDIM)
181
      integer         cdfid, i,k
182
      integer         ncopts, ndims,ngatts,recdim
183
      integer         nvdims,vartyp,nvatts,vardim(MAXDIM)
184
      real            attval
185
      integer         lenstr
186
      data            chrid/'x','y','z','a'/
187
      data            lenstr/80/
188
 
189
c     Get current value of error options and make sure netCDF-errors do 
190
c     not abort execution
191
      call ncgopt (ncopts)
192
      call ncpopt(NCVERBOS)
193
 
194
c     Initially set error to indicate no errors.
195
      error = 0
196
 
197
c     open the netCDF file for write
198
      cdfid = ncopn (trim(filnam), NCWRITE, error)
199
      if (error.ne.0) then
200
c       try to open the netCDF file for read
201
        cdfid = ncopn (trim(filnam), NCNOWRIT, error)
202
        if (error.ne.0) go to 920
203
      endif
204
 
205
c     inquire for number of variables
206
      call ncinq(cdfid,ndims,nvar,ngatts,recdim,error)
207
      if (error.eq.1) goto 920
208
 
209
c     read the variables
210
      do i=1,nvar
211
        call ncvinq(cdfid,i,varnam(i),vartyp,nvdims,vardim,
212
     &                         nvatts,error)
213
        if (vartyp.ne.NCFLOAT) error=1
214
        if (error.gt.0) goto 920
215
      enddo
216
 
217
c     get global attributes
218
      k=0
219
  100 continue
220
        k=k+1
221
        attnam(1:3)='dom'
222
        attnam(4:4)=chrid(k)
223
        attnam(5:7)='min'
224
        attnam=attnam(1:7)
225
 
226
c       switch off error message
227
        call ncpopt(0)
228
 
229
c       check whether dimension k is present
230
        call ncagt(cdfid,NCGLOBAL,attnam,attval,error)
231
        if (error.gt.0) goto 110
232
        phymin(k)=attval
233
 
234
        attnam(1:3)='dom'
235
        attnam(4:4)=chrid(k)
236
        attnam(5:7)='max'
237
        attnam=attnam(1:7)
238
        call ncagt(cdfid,NCGLOBAL,attnam,attval,error)
239
        if (error.gt.0) goto 920
240
        phymax(k)=attval
241
      if (k.lt.3) goto 100
242
      k=k+1
243
 
244
c     define ndim-parameter
245
 110  continue
246
      ndim=k-1
247
      error=0
248
 
249
c     switch on error messages
250
      call ncpopt(NCVERBOS)
251
 
252
c     get constants file name
253
c      call ncagt(cdfid,NCGLOBAL,'constants_file_name',cfn,error)
254
c     ! chrigel
255
      call ncagtc(cdfid,NCGLOBAL,'constants_file_name',cfn,lenstr,error)
256
      if (error.gt.0) cfn='0'
257
 
258
c     normal exit
259
      call ncpopt (ncopts)
260
      return
261
 
262
c     error exit
263
 920  write (6, *) 'ERROR: An error occurred while attempting to ',
264
     &             'read the data file in subroutine opncdf.'
265
      call ncclos (cdfid, error)
266
      call ncpopt (ncopts)
267
      end
268
 
269
 
270
      subroutine readcdf(filnam, cdfid,
271
     &                 phymin, phymax, ndim, varnam, nvar, cfn, error) 
272
c-----------------------------------------------------------------------
273
c     Purpose:
274
c        This routine is called to open a netCDF file for read 
275
c        with the UWGAP plotting package. 
276
c     Arguments:
277
c        filnam  char  input   the user-supplied netCDF file name.
278
c        cdfid   int   output  the file-identifier
279
c        phymin  real  output  the minimum physical dimension of the
280
c                              entire physical domain along each axis.
281
c                              phymin is dimensioned (ndim)
282
c        phymax  real  output  the maximum physical dimension of the
283
c                              entire physical domain along each axis.
284
c                              phymax is dimensioned (ndim)
285
c        ndim    int   output  the number of dimensions in the file
286
c                              (i.e. number of elements in phymin,
287
c                              phymax)
288
c        varnam  char  output  an array containing the variable names.
289
c                              varnam is dimensioned (nvar).
290
c        nvar    int   output  the number of variables in the file
291
c        cfn     char  output  constants file name 
292
c                              ('0'=no constants file).
293
c        error   int   output  indicates possible errors found in this
294
c                              routine.
295
c                              error = 0   no errors detected.
296
c                              error = 1   error detected.
297
c     History:
298
c        Nov. 91  PPM  UW  Created cr3df.
299
c        Jan. 92  CS   UW  Created opncdf.
300
c-----------------------------------------------------------------------
301
 
302
      include "netcdf.inc"
303
 
304
c     Argument declarations.
305
      integer        MAXDIM
306
      parameter      (MAXDIM=4)
307
      integer        ndim, nvar, error
308
      character *(*) filnam, varnam(*),cfn
309
      real           phymin(*), phymax(*)
310
 
311
 
312
c     Local variable declarations.
313
      character *(20) attnam
314
      character *(1)  chrid(MAXDIM)
315
      integer         cdfid, i,k
316
      integer         ncopts, ndims,ngatts,recdim
317
      integer         nvdims,vartyp,nvatts,vardim(MAXDIM)
318
      real            attval
319
      integer         lenstr
320
      data            chrid/'x','y','z','a'/
321
      data            lenstr/80/
322
 
323
c     Get current value of error options.
324
      call ncgopt (ncopts)
325
 
326
c     make sure netCDF-errors do not abort execution
327
      call ncpopt(NCVERBOS)
328
 
329
c     Initially set error to indicate no errors.
330
      error = 0
331
 
332
c     open the netCDF file for read
333
      cdfid = ncopn (trim(filnam), NCNOWRIT, error)
334
      if (error.ne.0) go to 920
335
 
336
c     inquire for number of variables
337
      call ncinq(cdfid,ndims,nvar,ngatts,recdim,error)
338
      if (error.eq.1) goto 920
339
 
340
c     read the variables
341
      do i=1,nvar
342
        call ncvinq(cdfid,i,varnam(i),vartyp,nvdims,vardim,
343
     &                         nvatts,error)
344
        if (vartyp.ne.NCFLOAT) error=1
345
c       print *,varnam(i),nvdims,nvatts
346
        if (error.gt.0) goto 920
347
      enddo
348
 
349
c     get global attributes
350
      k=0
351
  100 continue
352
        k=k+1
353
        attnam(1:3)='dom'
354
        attnam(4:4)=chrid(k)
355
        attnam(5:7)='min'
356
        attnam=attnam(1:7)
357
 
358
c       switch off error message
359
        call ncpopt(0)
360
 
361
c       check whether dimension k is present
362
        call ncagt(cdfid,NCGLOBAL,attnam,attval,error)
363
        if (error.gt.0) goto 110
364
        phymin(k)=attval
365
 
366
        attnam(1:3)='dom'
367
        attnam(4:4)=chrid(k)
368
        attnam(5:7)='max'
369
        attnam=attnam(1:7)
370
        call ncagt(cdfid,NCGLOBAL,attnam,attval,error)
371
        if (error.gt.0) goto 920
372
        phymax(k)=attval
373
      if (k.lt.4) goto 100
374
      k=k+1
375
 
376
c     define ndim-parameter
377
 110  continue
378
      ndim=k-1
379
      error=0
380
 
381
c     switch on error messages
382
      call ncpopt(NCVERBOS)
383
 
384
c     get constants file name
385
c      call ncagt(cdfid,NCGLOBAL,'constants_file_name',cfn,error)
386
c     ! chrigel
387
      call ncagtc(cdfid,NCGLOBAL,'constants_file_name',cfn,lenstr,error)
388
      if (error.gt.0) cfn='0'
389
c     print *,cfn
390
 
391
c     normal exit
392
      call ncpopt (ncopts)
393
      return
394
 
395
c     error exit
396
 920  write (6, *) 'ERROR: An error occurred while attempting to ',
397
     &             'read the data file in subroutine opncdf.'
398
      call ncclos (cdfid, error)
399
      call ncpopt (ncopts)
400
      end
401
 
402
 
403
 
404
      subroutine getcdf (cdfid, varnam, ndim, misdat, 
405
     &                       vardim, varmin, varmax, stag, dat, error)
406
c-----------------------------------------------------------------------
407
c     Purpose:
408
c        This routine is called to get a variable and its attributes
409
c        from a netCDF file for use with the UWGAP plotting package. 
410
c        It is assumed that the data is floating-point data. Prior to
411
c        calling this routine, the file must be opened with a call to
412
c        opncdf.
413
c     Arguments:
414
c        cdfid   int   input   file-identifier
415
c                              (can be obtained by calling routine 
416
c                              opncdf)
417
c        varnam  char  input   the user-supplied variable name.
418
c                              (can be obtained by calling routine 
419
c                              opncdf)
420
c        ndim    int   output  the number of dimensions (ndim<=4)
421
c        misdat  real  output  missing data value for the variable. 
422
c        vardim  int   output  the dimensions of the variable.
423
c                              is dimensioned at least (ndim). 
424
c        varmin  real  output  the location in physical space of the
425
c                              origin of each variable.
426
c                              is dimensioned at least Min(ndim,3). 
427
c        varmax  real  output  the extent of each variable in physical
428
c                              space.
429
c                              is dimensioned at least Min(ndim,3). 
430
c        stag    real  output  the grid staggering for each variable.
431
c                              is dimensioned at least Min(ndim,3). 
432
c        dat     real  output  data-array dimensioned suffiecently 
433
c                              large, at least 
434
c                              vardim(1)* ... vardim(ndim)
435
c        error   int   output  indicates possible errors found in this
436
c                              routine.
437
c                              error = 0   no errors detected.
438
c                              error = 1   error detected.
439
c     History:
440
c        Nov. 91  PPM  UW  Created cr3df.
441
c        Jan. 92  CS   UW  Created getcdf.
442
c-----------------------------------------------------------------------
443
 
444
      include "netcdf.inc"
445
 
446
c     Argument declarations.
447
      integer        MAXDIM
448
      parameter      (MAXDIM=4)
449
      character *(*) varnam
450
      integer        vardim(*), ndim, error, cdfid
451
      real           misdat,  stag(*), varmin(*), varmax(*), dat(*)
452
 
453
c     Local variable declarations.
454
      character *(20) dimnam(100),attnam
455
      character *(1)  chrid(MAXDIM)
456
      integer         id,i,k,corner(MAXDIM)
457
      integer         ndims,nvars,ngatts,recdim,dimsiz(100)
458
      integer         vartyp,nvatts, ncopts
459
      data            chrid/'x','y','z','a'/
460
      data            corner/1,1,1,1/
461
 
462
c     Get current value of error options, and make sure netCDF-errors do 
463
c     not abort execution 
464
      call ncgopt (ncopts)
465
      call ncpopt(NCVERBOS)
466
 
467
c     Initially set error to indicate no errors.
468
      error = 0
469
 
470
c     inquire for number of dimensions
471
      call ncinq(cdfid,ndims,nvars,ngatts,recdim,error)
472
      if (error.eq.1) goto 920
473
 
474
c     read dimension-table
475
      do i=1,ndims 
476
        call ncdinq(cdfid,i,dimnam(i),dimsiz(i),error)
477
        if (error.gt.0) goto 920
478
      enddo
479
 
480
c     get id of the variable
481
      id=ncvid(cdfid,varnam,error)
482
      if (error.eq.1) goto 910
483
 
484
c     inquire about variable
485
      call ncvinq(cdfid,id,varnam,vartyp,ndim,vardim,nvatts,error)
486
      if (vartyp.ne.NCFLOAT) error=1
487
      if (error.gt.0) goto 920
488
 
489
c     Make sure ndim <= MAXDIM.
490
      if (ndim.gt.MAXDIM) then
491
         error = 1
492
         go to 900
493
      endif
494
 
495
c     get dimensions from dimension-table
496
      do k=1,ndim 
497
        vardim(k)=dimsiz(vardim(k))
498
      enddo
499
 
500
c     get attributes
501
      do k=1,min0(ndim,3)
502
c       get staggering
503
        attnam(1:1)=chrid(k)
504
        attnam(2:5)='stag'
505
        attnam=attnam(1:5)
506
        call ncagt(cdfid,id,attnam,stag(k),error)
507
        if (error.gt.0) goto 920
508
c       get min postion
509
        attnam(1:1)=chrid(k)
510
        attnam(2:4)='min'
511
        attnam=attnam(1:4)
512
        call ncagt(cdfid,id,attnam,varmin(k),error)
513
        if (error.gt.0) goto 920
514
c       get max position     
515
        attnam(1:1)=chrid(k)
516
        attnam(2:4)='max'
517
        attnam=attnam(1:4)
518
        call ncagt(cdfid,id,attnam,varmax(k),error)
519
        if (error.gt.0) goto 920     
520
      enddo
521
 
522
c     get missing data value
523
      call ncagt(cdfid,id,'missing_data',misdat,error)
524
      if (error.gt.0) goto 920     
525
 
526
c     get data
527
      call ncvgt(cdfid,id,corner,vardim,dat,error)
528
      if (error.gt.0) goto 920
529
 
530
c     normal exit
531
      call ncpopt (ncopts)
532
      return
533
 
534
 
535
c     Error exits.
536
 900  write (6, *) 'ERROR: When calling getcdf, the number of ',
537
     &             'variable dimensions must be less or equal 4.'
538
      call ncpopt (ncopts)
539
      call ncclos (cdfid, error)
540
      return
541
 
542
 910  write (6, *) 'ERROR: The selected variable could not be found ',       
543
     &             'in the file by getcdf.'
544
      call ncpopt (ncopts)
545
      call ncclos (cdfid, error)
546
      return
547
 
548
 920  write (6, *) 'ERROR: An error occurred while attempting to ',
549
     &             'read the data file in subroutine getcdf.'
550
      call ncpopt (ncopts)
551
      call ncclos (cdfid, error)
552
      return
553
 
554
      end
555
 
556
 
557
      subroutine putcdf (cdfid, varnam, ndim, misdat, 
558
     &                       vardim, varmin, varmax, stag, dat, error)
559
c-----------------------------------------------------------------------
560
c     Purpose:
561
c        This routine is called to put a variable and its attributes
562
c        onto a netCDF file for use with the UWGAP plotting package. 
563
c        It is assumed that the data is floating-point data. Prior to 
564
c        calling this routine, the file must be created (crecdf) or
565
c        opened (opncdf). 
566
c           Any netCDF file written to must be closed with the call
567
c        call ncclos(cdfid,error), where cdfid and error are
568
c        as in the argumentlist below.
569
c     Arguments:
570
c        cdfid   int   input   file-identifier
571
c                              (can be obtained by calling routine 
572
c                              opncdf)
573
c        varnam  char  input   the user-supplied variable name.
574
c                              (can be obtained by calling routine 
575
c                              opncdf)
576
c        ndim    int   input   the number of dimensions (ndim<=4)
577
c        misdat  real  input   missing data value for the variable. 
578
c        vardim  int   input   the dimensions of the variable.
579
c                              is dimensioned at least (ndim). 
580
c        varmin  real  input   the location in physical space of the
581
c                              origin of each variable.
582
c                              is dimensioned at least Min(ndim,3). 
583
c        varmax  real  input   the extent of each variable in physical
584
c                              space.
585
c                              is dimensioned at least Min(ndim,3).  
586
c        stag    real  input   the grid staggering for each variable.
587
c                              is dimensioned at least Min(ndim,3).  
588
c        dat     real  input   data-array dimensioned suffiecently 
589
c                              large, at least 
590
c                              vardim(1)* ... vardim(ndim)
591
c        error   int   output  indicates possible errors found in this
592
c                              routine.
593
c                              error = 0   no errors detected.
594
c                              error = 1   error detected.
595
c     History:
596
c        Nov. 91  PPM  UW  Created cr3df, wr3df.
597
c        Jan. 92  CS   UW  Created putcdf.
598
c-----------------------------------------------------------------------
599
 
600
      include "netcdf.inc"
601
 
602
c     Argument declarations.
603
      integer        MAXDIM
604
      parameter      (MAXDIM=4)
605
      character *(*) varnam
606
      integer        vardim(*), ndim, error, cdfid
607
      real           misdat,  stag(*), varmin(*), varmax(*), dat(*)
608
 
609
c     Local variable declarations.
610
      character *(20) dimnam,attnam,dimchk
611
      character *(1)  chrid(MAXDIM)
612
      character *(20) dimnams(MAXNCDIM)
613
      integer         dimvals(MAXNCDIM)
614
      integer         numdims,numvars,numgats,dimulim
615
      integer         id,did(MAXDIM),i,k,corner(MAXDIM)
616
      integer         ncopts
617
      integer         ibeg,iend
618
      data            chrid/'x','y','z','t'/
619
      data            corner/1,1,1,1/
620
 
621
c     Get current value of error options.
622
      call ncgopt (ncopts)
623
 
624
c     make sure netCDF-errors do not abort execution
625
      call ncpopt(NCVERBOS)
626
 
627
c     Initially set error to indicate no errors.
628
      error = 0
629
 
630
c     Make sure ndim <= MAXDIM.
631
      if (ndim.gt.MAXDIM) then
632
         error = 1
633
         go to 900
634
      endif
635
 
636
c     Read existing dimensions-declarations from the file
637
      call ncinq(cdfid,numdims,numvars,numgats,dimulim,error)
638
      if (error.ne.0) numdims=0
639
      if (numdims.gt.0) then
640
        do i=1,numdims
641
          call ncdinq(cdfid,i,dimnams(i),dimvals(i),error)
642
c          print *,dimnams(i),dimvals(i)
643
        enddo
644
      endif
645
 
646
c     put file into define mode
647
      call ncredf(cdfid,error)
648
      if (error.ne.0) goto 920
649
 
650
c     define the dimension
651
      do k=1,ndim
652
c       define the dimension-name
653
        dimnam(1:3)='dim'
654
        dimnam(4:4)=chrid(k)
655
        dimnam(5:5)='_'
656
        dimnam(6:5+len_trim(varnam))=trim(varnam)
657
        dimnam=dimnam(1:5+len_trim(varnam))
658
        did(k)=-1
659
        if (numdims.gt.0) then
660
c         check if an existing dimension-declaration can be used
661
c         instead of defining a nuw dimension
662
          do i=1,numdims
663
            dimchk=dimnams(i)
664
            if ((vardim(k).eq.dimvals(i)).and.
665
     &        (dimnam(1:4).eq.dimchk(1:4))) then 
666
              did(k)=i
667
              goto 100
668
            endif
669
          enddo
670
 100      continue
671
        endif
672
        if (did(k).lt.0) then
673
c         define the dimension
674
          did(k)=ncddef(cdfid,dimnam,vardim(k),error)
675
          if (error.ne.0) goto 920
676
        endif
677
      enddo
678
 
679
c     define variable
680
      id=ncvdef(cdfid,varnam,NCFLOAT,ndim,did,error)
681
      if (error.ne.0) goto 920
682
 
683
c     define attributes
684
      do k=1,min0(ndim,3)
685
c       staggering
686
        attnam(1:1)=chrid(k)
687
        attnam(2:5)='stag'
688
        attnam=attnam(1:5)
689
        call ncapt(cdfid,id,attnam,NCFLOAT,1,stag(k),error)
690
        if (error.gt.0) goto 920
691
c       min postion
692
        attnam(1:1)=chrid(k)
693
        attnam(2:4)='min'
694
        attnam=attnam(1:4)
695
        call ncapt(cdfid,id,attnam,NCFLOAT,1,varmin(k),error)
696
        if (error.gt.0) goto 920
697
c       max position     
698
        attnam(1:1)=chrid(k)
699
        attnam(2:4)='max'
700
        attnam=attnam(1:4)
701
        call ncapt(cdfid,id,attnam,NCFLOAT,1,varmax(k),error)
702
        if (error.gt.0) goto 920     
703
      enddo
704
 
705
c     define missing data value
706
      call ncapt(cdfid,id,'missing_data',NCFLOAT,1,misdat,error)
707
      if (error.gt.0) goto 920     
708
 
709
c     leave define mode
710
      call ncendf(cdfid,error)
711
      if (error.gt.0) goto 920     
712
 
713
c     define data
714
      call ncvpt(cdfid,id,corner,vardim,dat,error)
715
      if (error.gt.0) goto 920
716
 
717
c     synchronyse output to disk and exit
718
      call ncsnc (cdfid,error)
719
      call ncpopt (ncopts)
720
      return
721
 
722
c     Error exits.
723
 900  write (6, *) 'ERROR: When calling putcdf, the number of ',
724
     &             'variable dimensions must be less or equal 4.'
725
      call ncpopt (ncopts)
726
      call ncclos (cdfid, error)
727
      return
728
 
729
 920  write (6, *) 'ERROR: An error occurred while attempting to ',
730
     &             'write the data file in subroutine putcdf.'
731
      call ncpopt (ncopts)
732
      call ncclos (cdfid, error)
733
      return
734
      end
735
c
736
c
737
      subroutine getdef (cdfid, varnam, ndim, misdat, 
738
     &                              vardim, varmin, varmax, stag, error)
739
c-----------------------------------------------------------------------
740
c     Purpose:
741
c        This routine is called to get the dimensions and attributes of 
742
c        a variable from an IVE-NetCDF file for use with the IVE plotting
743
c        package. Prior to calling this routine, the file must be opened
744
c        with a call to opncdf.
745
c     Arguments:
746
c        cdfid   int   input   file-identifier
747
c                              (can be obtained by calling routine 
748
c                              opncdf)
749
c        varnam  char  input   the user-supplied variable name.
750
c                              (can be obtained by calling routine 
751
c                              opncdf)
752
c        ndim    int   output  the number of dimensions (ndim<=4)
753
c        misdat  real  output  missing data value for the variable. 
754
c        vardim  int   output  the dimensions of the variable.
755
c                              Is dimensioned at least (ndim). 
756
c        varmin  real  output  the location in physical space of the
757
c                              origin of each variable. 
758
c                              Is dimensioned at least Min(3,ndim). 
759
c        varmax  real  output  the extend of each variable in physical
760
c                              space.
761
c                              Is dimensioned at least Min(3,ndim). 
762
c        stag    real  output  the grid staggering for each variable.
763
c                              Is dimensioned at least Min(3,ndim). 
764
c        error   int   output  indicates possible errors found in this
765
c                              routine.
766
c                              error = 0   no errors detected.
767
c                              error = 1   the variable is not on the file.
768
c                              error =10   other errors.
769
c     History:
770
c       Apr. 93    Christoph Schaer (ETHZ)     Created.
771
c-----------------------------------------------------------------------
772
 
773
      include "netcdf.inc"
774
 
775
c     Argument declarations.
776
      integer        MAXDIM
777
      parameter      (MAXDIM=4)
778
      character *(*) varnam
779
      integer        vardim(*), ndim, error, cdfid
780
      real           misdat,  stag(*), varmin(*), varmax(*)
781
 
782
c     Local variable declarations.
783
      character *(20) dimnam(MAXNCDIM),attnam,vnam
784
      character *(1)  chrid(MAXDIM)
785
      integer         id,i,k
786
      integer         ndims,nvars,ngatts,recdim,dimsiz(MAXNCDIM)
787
      integer         vartyp,nvatts, ncopts
788
      data            chrid/'x','y','z','t'/
789
 
790
c     Get current value of error options.
791
      call ncgopt (ncopts)
792
 
793
c     make sure NetCDF-errors do not abort execution
794
      call ncpopt(NCVERBOS)
795
 
796
c     Initially set error to indicate no errors.
797
      error = 0
798
 
799
c     inquire for number of dimensions
800
      call ncinq(cdfid,ndims,nvars,ngatts,recdim,error)
801
      if (error.eq.1) goto 920
802
 
803
c     read dimension-table
804
      do i=1,ndims 
805
        call ncdinq(cdfid,i,dimnam(i),dimsiz(i),error)
806
        if (error.gt.0) goto 920
807
      enddo
808
 
809
c     get id of the variable
810
      id=ncvid(cdfid,varnam,error)
811
      if (error.eq.1) goto 910
812
 
813
c     inquire about variable
814
      call ncvinq(cdfid,id,vnam,vartyp,ndim,vardim,nvatts,error)
815
      if (vartyp.ne.NCFLOAT) error=1
816
      if (error.gt.0) goto 920
817
 
818
c     Make sure ndim <= MAXDIM.
819
      if (ndim.gt.MAXDIM) then
820
         error = 1
821
         go to 900
822
      endif
823
 
824
c     get dimensions from dimension-table
825
      do k=1,ndim 
826
        vardim(k)=dimsiz(vardim(k))
827
      enddo
828
 
829
c     get attributes
830
      do k=1,min0(3,ndim)
831
c       get min postion
832
        attnam(1:1)=chrid(k)
833
        attnam(2:4)='min'
834
        attnam=attnam(1:4)
835
        call ncagt(cdfid,id,attnam,varmin(k),error)
836
        if (error.gt.0) goto 920
837
c       get max position     
838
        attnam(1:1)=chrid(k)
839
        attnam(2:4)='max'
840
        attnam=attnam(1:4)
841
        call ncagt(cdfid,id,attnam,varmax(k),error)
842
        if (error.gt.0) goto 920     
843
c       get staggering
844
        attnam(1:1)=chrid(k)
845
        attnam(2:5)='stag'
846
        attnam=attnam(1:5)
847
        call ncagt(cdfid,id,attnam,stag(k),error)
848
        if (error.gt.0) goto 920
849
      enddo
850
 
851
c     get missing data value
852
      call ncagt(cdfid,id,'missing_data',misdat,error)
853
      if (error.gt.0) goto 920     
854
 
855
c     normal exit
856
      call ncpopt (ncopts)
857
      return
858
 
859
c     Error exits.
860
 900  write (6, *) '*ERROR*: When calling getcdf, the number of ',
861
     &             'variable dimensions must be less or equal 4.'
862
      call ncpopt (ncopts)
863
      call ncclos (cdfid, error)
864
      return
865
 
866
 910  write (6, *) '*ERROR*: The selected variable could not be found ',       
867
     &             'in the file by getcdf.'
868
      call ncpopt (ncopts)
869
      call ncclos (cdfid, error)
870
      return
871
 
872
 920  write (6, *) '*ERROR*: An error occurred while attempting to ',
873
     &             'read the data file in subroutine getcdf.'
874
      call ncpopt (ncopts)
875
      call ncclos (cdfid, error)
876
      end
877
 
878
 
879
 
880
      subroutine getdat(cdfid, varnam, time, level, dat, error)
881
c-----------------------------------------------------------------------
882
c     Purpose:
883
c        This routine is called to read the data of a variable
884
c        from an IVE-NetCDF file for use with the IVE plotting package. 
885
c        Prior to calling this routine, the file must be opened with 
886
c        a call to opncdf (for extension) or crecdf (for creation) or
887
c        readcdf (for readonly).
888
c     Arguments:
889
c        cdfid   int   input   file-identifier
890
c                              (must be obtained by calling routine 
891
c                              opncdf,readcdf  or crecdf)
892
c        varnam  char  input   the user-supplied variable name (must 
893
c                              previously be defined with a call to
894
c                              putdef)
895
c        time    real  input   the user-supplied time-level of the
896
c                              data to be read from the file (the time-
897
c                              levels stored in the file can be obtained
898
c                              with a call to gettimes). 
899
c        level   int   input   the horizontal level(s) to be read 
900
c                              to the NetCDF file. Suppose that the
901
c                              variable is defined as (nx,ny,nz,nt).
902
c                              level>0: the call reads the subdomain
903
c                                       (1:nx,1:ny,level,itimes)
904
c                              level=0: the call reads the subdomain
905
c                                       (1:nx,1:ny,1:nz,itimes)
906
c                              Here itimes is the time-index corresponding
907
c                              to the value of 'time'. 
908
c        dat     real  output  data-array dimensioned sufficiently 
909
c                              large. The dimensions (nx,ny,nz)
910
c                              of the variable must previously be defined
911
c                              with a call to putdef. No previous 
912
c                              definition of the time-dimension is
913
c                              required.
914
c        error   int   output  indicates possible errors found in this
915
c                              routine.
916
c                              error = 0   no errors detected.
917
c                              error = 1   the variable is not present on
918
c                                          the file.
919
c                              error = 2   the value of 'time' is not
920
c                                          known.to the file.
921
c                              error = 3   inconsistent value of level
922
c                              error =10   another error.
923
c     History:
924
c       March 93    Heini Wernli (ETHZ)      Created wr2cdf.
925
c       April 93    Bettina Messmer (ETHZ)   Created putdat.
926
c       June  93    Christoph Schaer (ETHZ)  Created getdat
927
c       Sept. 07    Johannes Jenkner (ETHZ)  Integer and double times
928
c-----------------------------------------------------------------------
929
 
930
      include "netcdf.inc"
931
 
932
C     Declaration of local variables
933
      character*(*) varnam
934
      character*(20) chars,dimnam
935
      integer cdfid
936
 
937
      real        dat(*)
938
      real        misdat,varmin(3),varmax(3),stag(3)
939
      real        time, timeval
940
 
941
      integer     corner(4),edgeln(4),didtim,vardim(4),ndims
942
      integer     error, ierr
943
      integer     level,ntime
944
      integer     idtime,idvar,iflag
945
      integer     i
946
 
947
      integer     vtyp,dn,nat
948
      integer     dims(4)
949
      integer     inttime
950
      double precision doubletime
951
 
952
      call ncpopt(NCVERBOS)
953
 
954
c     access the variable
955
      call getdef (cdfid, trim(varnam), ndims, misdat, 
956
     &                           vardim, varmin, varmax, stag, ierr)
957
      if (ierr.ne.0) then
958
        print *,'*ERROR* in getdef in getdat'
959
        error=1
960
        return
961
      endif
962
      idvar=ncvid(cdfid,trim(varnam),ierr)
963
      if (ierr.ne.0) then
964
        print *,'*ERROR* in ncvid in getdat'
965
        error=1
966
        return
967
      endif
968
 
969
C     Get times-array
970
      didtim=ncdid(cdfid,'time',ierr)
971
      if (ierr.ne.0) then
972
        print *,'*ERROR* didtim in getdat'
973
        error=10
974
        return
975
      endif
976
      call ncdinq(cdfid,didtim,chars,ntime,ierr)
977
      if (ierr.ne.0) then
978
        print *,'*ERROR* in ncdinq in getdat'
979
        error=10
980
        return
981
      endif
982
      idtime=ncvid(cdfid,'time',ierr)
983
      if (ierr.ne.0) then
984
        print *,'*ERROR* in ncvid for time in getdat'
985
        error=10
986
        return
987
      endif
988
c     find appropriate time-index
989
      iflag=0
990
      call ncvinq(cdfid,idtime,dimnam,vtyp,dn,dims,natt,ierr)
991
      do i=1,ntime
992
        if (vtyp.eq.5) then
993
          call ncvgt1(cdfid,idtime,i,timeval,ierr)
994
        elseif (vtyp.eq.4) then ! integer version 
995
          call ncvgt1(cdfid,idtime,i,inttime,ierr) 
996
          timeval=real(inttime)
997
        elseif (vtyp.eq.6) then ! double precision version
998
           call ncvgt1(cdfid,idtime,i,doubletime,ierr) 
999
           timeval=real(doubletime)   
1000
        endif      
1001
        if (ierr.ne.0) print *,'*ERROR* in ncvgt1 in getdat'
1002
        if (time.eq.timeval) iflag=i
1003
      enddo
1004
      if (iflag.eq.0) then
1005
        error=2
1006
        print *,'Error: Unknown time in getdat'
1007
        return
1008
      endif
1009
 
1010
C     Define data volume to be written (index space)
1011
      corner(1)=1
1012
      corner(2)=1
1013
      edgeln(1)=vardim(1)
1014
      edgeln(2)=vardim(2)
1015
      if (level.eq.0) then
1016
        corner(3)=1
1017
        edgeln(3)=vardim(3)
1018
      else if ((level.le.vardim(3)).and.(level.ge.1)) then
1019
        corner(3)=level
1020
        edgeln(3)=1
1021
      else
1022
        error=3
1023
        return
1024
      endif
1025
      corner(4)=iflag
1026
      edgeln(4)=1
1027
 
1028
C     Read data from NetCDF file
1029
c      print *,'getdat vor Aufruf ncvgt'
1030
c      print *,'cdfid ',cdfid
1031
c      print *,'idvar ',idvar
1032
c      print *,'corner ',corner
1033
c      print *,'edgeln ',edgeln
1034
 
1035
      call ncvgt(cdfid,idvar,corner,edgeln,dat,error)
1036
      if (error.ne.0) then
1037
        print *, '*ERROR* in ncvgt in getdat'
1038
        error=10
1039
      endif
1040
      end
1041
 
1042
 
1043
      subroutine putdat(cdfid, varnam, time, level, dat, error)
1044
c-----------------------------------------------------------------------
1045
c     Purpose:
1046
c        This routine is called to write the data of a variable
1047
c        to an IVE-NetCDF file for use with the IVE plotting package. 
1048
c        Prior to calling this routine, the file must be opened with 
1049
c        a call to opncdf (for extension) or crecdf (for creation), the 
1050
c        variable must be defined with a call to putdef.
1051
c     Arguments:
1052
c        cdfid   int   input   file-identifier
1053
c                              (must be obtained by calling routine 
1054
c                              opncdf or crecdf)
1055
c        varnam  char  input   the user-supplied variable name (must 
1056
c                              previously be defined with a call to
1057
c                              putdef)
1058
c        time    real  input   the user-supplied time-level of the
1059
c                              data to be written to the file (the time-
1060
c                              levels stored in the file can be obtained
1061
c                              with a call to gettimes). If 'time' is not
1062
c                              yet known to the file, a knew time-level is
1063
c                              allocated and appended to the times-array.
1064
c        level   int input     the horizontal level(s) to be written 
1065
c                              to the NetCDF file. Suppose that the
1066
c                              variable is defined as (nx,ny,nz,nt).
1067
c                              level>0: the call writes the subdomain
1068
c                                       (1:nx,1:ny,level,itimes)
1069
c                              level=0: the call writes the subdomain
1070
c                                       (1:nx,1:ny,1:nz,itimes)
1071
c                              Here itimes is the time-index corresponding
1072
c                              to the value of 'time'. 
1073
c        dat     real  output  data-array dimensioned sufficiently 
1074
c                              large. The dimensions (nx,ny,nz)
1075
c                              of the variable must previously be defined
1076
c                              with a call to putdef. No previous 
1077
c                              definition of the time-dimension is
1078
c                              required.
1079
c        error   int output    indicates possible errors found in this
1080
c                              routine.
1081
c                              error = 0   no errors detected.
1082
c                              error = 1   the variable is not present on
1083
c                                          the file.
1084
c                              error = 2   the value of 'time' is new, but
1085
c                                          appending it would yield a non
1086
c                                          ascending times-array.
1087
c                              error = 3   inconsistent value of level
1088
c                              error =10   another error.
1089
c     History:
1090
c       March 93    Heini Wernli (ETHZ)      Created wr2cdf.
1091
c       April 93    Bettina Messmer (ETHZ)   Created putdat.
1092
c-----------------------------------------------------------------------
1093
 
1094
      include "netcdf.inc"
1095
 
1096
C     Declaration of local variables
1097
 
1098
      character*(*) varnam
1099
      character*(20) chars
1100
      integer cdfid
1101
 
1102
 
1103
      real 	dat(*)
1104
      real	misdat,varmin(3),varmax(3),stag(3)
1105
      real	time, timeval
1106
      data	stag/0.,0.,0./
1107
 
1108
      integer	corner(4),edgeln(4),did(4),vardim(4),ndims
1109
      integer	error, ierr
1110
      integer	level,ntime
1111
      integer	idtime,idvar,iflag
1112
      integer	i
1113
 
1114
      call ncpopt(NCVERBOS)
1115
 
1116
c     get definitions of data
1117
      call getdef (cdfid, trim(varnam), ndims, misdat, 
1118
     &                           vardim, varmin, varmax, stag, ierr)
1119
      if (ierr.ne.0)  print *,'*ERROR* in getdef in putdat'
1120
 
1121
c     get id of variable
1122
      idvar=ncvid(cdfid,trim(varnam),ierr)
1123
      if (ierr.ne.0) print *,'*ERROR* in ncvid in putdat'
1124
 
1125
c     get times-array 
1126
      did(4)=ncdid(cdfid,'time',ierr)
1127
      if (ierr.ne.0) print *,'*ERROR* did(4) in putdat'
1128
      call ncdinq(cdfid,did(4),chars,ntime,ierr)
1129
      if (ierr.ne.0) print *,'*ERROR* in ncdinq in putdat'
1130
      idtime=ncvid(cdfid,'time',ierr)
1131
      if (ierr.ne.0) print *,'*ERROR* in ncvid in putdat'
1132
C     Check if a new time step is starting
1133
      iflag=0
1134
      do i=1,ntime
1135
        call ncvgt1(cdfid,idtime,i,timeval,ierr)
1136
        if (ierr.ne.0) print *,'*ERROR* in ncvgt1 in putdat'
1137
        if (time.eq.timeval) iflag=i
1138
      enddo
1139
      if (iflag.eq.0) then		! new time step
1140
        ntime=ntime+1
1141
        iflag=ntime
1142
        idtime=ncvid(cdfid,'time',ierr)
1143
        if (ierr.ne.0) print *, '*ERROR* in ncvid in putdat'
1144
        call ncvpt1(cdfid,idtime,ntime,time,ierr)
1145
        if (ierr.ne.0) print *, '*ERROR* in ncvpt1 in putdat'
1146
      endif
1147
 
1148
C     Define data volume to write on the NetCDF file in index space
1149
      corner(1)=1               ! starting corner of data volume
1150
      corner(2)=1
1151
      edgeln(1)=vardim(1)       ! edge lengthes of data volume
1152
      edgeln(2)=vardim(2)
1153
      if (level.eq.0) then
1154
        corner(3)=1
1155
        edgeln(3)=vardim(3)
1156
      else
1157
        corner(3)=level
1158
        edgeln(3)=1
1159
      endif
1160
      corner(4)=iflag
1161
      edgeln(4)=1
1162
 
1163
C     Put data on NetCDF file
1164
 
1165
c      print *,'vor Aufruf ncvpt d.h. Daten schreiben in putdat '
1166
c      print *,'cdfid ',cdfid
1167
c      print *,'idvar ',idvar
1168
c      print *,'corner ',corner
1169
c      print *,'edgeln ',edgeln
1170
 
1171
      call ncvpt(cdfid,idvar,corner,edgeln,dat,error)
1172
      if (error.ne.0) then
1173
        print *, '*ERROR* in ncvpt in putdat - Put data on NetCDF file'
1174
      endif
1175
 
1176
C     Synchronize output to disk and close the files
1177
 
1178
      call ncsnc(cdfid,ierr)
1179
      if (ierr.ne.0) print *, '*ERROR* in ncsnc in putdat'
1180
      end
1181
 
1182
 
1183
 
1184
 
1185
 
1186
 
1187
      subroutine putdef (cdfid, varnam, ndim, misdat, 
1188
     &                            vardim, varmin, varmax, stag, error)
1189
c-----------------------------------------------------------------------
1190
c     Purpose:
1191
c        This routine is called to define the dimensions and the
1192
c        attributes of a variable on an IVE-NetCDF file for use with the
1193
c        IVE plotting package. Prior to calling this routine, the file must
1194
c        be opened with a call to opncdf (extend an existing file) or
1195
c        crecdf (create a new file).
1196
c     Arguments:
1197
c        cdfid   int   input   file-identifier
1198
c                              (can be obtained by calling routine 
1199
c                              opncdf)
1200
c        varnam  char  input   the user-supplied variable name.
1201
c        ndim    int   input   the number of dimensions (ndim<=4). 
1202
c                              Upon ndim=4, the fourth dimension of the
1203
c                              variable is specified as 'unlimited'
1204
c                              on the file (time-dimension). It can 
1205
c                              later be extended to arbitrary length.
1206
c        misdat  real  input   missing data value for the variable. 
1207
c        vardim  int   input   the dimensions of the variable.
1208
c                              Is dimensioned at least Min(3,ndim). 
1209
c        varmin  real  input   the location in physical space of the
1210
c                              origin of each variable.
1211
c                              Is dimensioned at least Min(3,ndim). 
1212
c        varmax  real  input   the extent of each variable in physical
1213
c                              space.
1214
c                              Is dimensioned at least Min(ndim). 
1215
c        stag    real  input   the grid staggering for each variable.
1216
c                              Is dimensioned at least Min(3,ndim). 
1217
c        error   int   output  indicates possible errors found in this
1218
c                              routine.
1219
c                              error = 0   no errors detected.
1220
c                              error =10   other errors detected.
1221
c     History:
1222
c       Apr. 93    Christoph Schaer (ETHZ)     Created.
1223
c-----------------------------------------------------------------------
1224
 
1225
      include "netcdf.inc"
1226
 
1227
c     Argument declarations.
1228
      integer        MAXDIM
1229
      parameter      (MAXDIM=4)
1230
      character *(*) varnam
1231
      integer        vardim(*), ndim, error, cdfid
1232
      real           misdat,  stag(*), varmin(*), varmax(*)
1233
 
1234
c     Local variable declarations.
1235
      character *(20) dimnam,attnam,dimchk
1236
      character *(1)  chrid(MAXDIM)
1237
      character *(20) dimnams(MAXNCDIM)
1238
      integer         dimvals(MAXNCDIM)
1239
      integer         numdims,numvars,numgats,dimulim
1240
      integer         id,did(MAXDIM),idtime,i,k,ierr
1241
      integer         ncopts
1242
      integer         ibeg,iend
1243
      data            chrid/'x','y','z','t'/
1244
 
1245
c     Get current value of error options.
1246
      call ncgopt (ncopts)
1247
 
1248
c     make sure NetCDF-errors do not abort execution
1249
      call ncpopt(NCVERBOS)
1250
 
1251
c     Initially set error to indicate no errors.
1252
      error = 0
1253
 
1254
c     Make sure ndim <= MAXDIM.
1255
      if (ndim.gt.MAXDIM) then
1256
         error = 10
1257
         go to 900
1258
      endif
1259
 
1260
c     Read existing dimensions-declarations from the file
1261
      call ncinq(cdfid,numdims,numvars,numgats,dimulim,error)
1262
      if (numdims.gt.0) then
1263
        do i=1,numdims
1264
          call ncdinq(cdfid,i,dimnams(i),dimvals(i),error)
1265
c         print *,dimnams(i),dimvals(i)
1266
        enddo
1267
      endif
1268
 
1269
c     put file into define mode
1270
      call ncredf(cdfid,error)
1271
      if (error.ne.0) goto 920
1272
 
1273
c     define spatial dimensions
1274
      do k=1,min0(3,ndim)
1275
c       define the default dimension-name
1276
        dimnam(1:3)='dim'
1277
        dimnam(4:4)=chrid(k)
1278
        dimnam(5:5)='_'
1279
        dimnam(6:5+len_trim(varnam))=trim(varnam)
1280
        dimnam=dimnam(1:5+len_trim(varnam))
1281
        did(k)=-1
1282
        if (numdims.gt.0) then
1283
c         check if an existing dimension-declaration can be used
1284
c         instead of defining a new dimension
1285
          do i=1,numdims
1286
            dimchk=dimnams(i)
1287
            if ((vardim(k).eq.dimvals(i)).and.
1288
     &        (dimnam(1:4).eq.dimchk(1:4))) then 
1289
              did(k)=i
1290
              goto 100
1291
            endif
1292
          enddo
1293
 100      continue
1294
        endif
1295
        if (did(k).lt.0) then
1296
c         define the dimension
1297
          did(k)=ncddef(cdfid,dimnam,vardim(k),error)
1298
          if (error.ne.0) goto 920
1299
        endif
1300
      enddo
1301
 
1302
c     define the times-array
1303
      if (ndim.eq.4) then
1304
c       define dimension and variable 'time'
1305
        if (numdims.ge.4) then
1306
          did(4)=ncdid(cdfid,'time',ierr)
1307
          idtime=ncvid(cdfid,'time',ierr)
1308
        else
1309
c         this dimension must first be defined
1310
          did(4) = ncddef (cdfid,'time',NCUNLIM,ierr)
1311
          idtime = ncvdef (cdfid,'time',NCFLOAT,1,did(4),ierr)
1312
        endif
1313
      endif
1314
 
1315
c     define variable
1316
      id=ncvdef(cdfid,varnam,NCFLOAT,ndim,did,error)
1317
      if (error.ne.0) goto 920
1318
 
1319
c     define attributes
1320
      do k=1,min0(ndim,3)
1321
c       min postion
1322
        attnam(1:1)=chrid(k)
1323
        attnam(2:4)='min'
1324
        attnam=attnam(1:4)
1325
        call ncapt(cdfid,id,attnam,NCFLOAT,1,varmin(k),error)
1326
        if (error.gt.0) goto 920
1327
c       max position     
1328
        attnam(1:1)=chrid(k)
1329
        attnam(2:4)='max'
1330
        attnam=attnam(1:4)
1331
        call ncapt(cdfid,id,attnam,NCFLOAT,1,varmax(k),error)
1332
        if (error.gt.0) goto 920     
1333
c       staggering
1334
        attnam(1:1)=chrid(k)
1335
        attnam(2:5)='stag'
1336
        attnam=attnam(1:5)
1337
        call ncapt(cdfid,id,attnam,NCFLOAT,1,stag(k),error)
1338
        if (error.gt.0) goto 920
1339
      enddo
1340
 
1341
c     define missing data value
1342
      call ncapt(cdfid,id,'missing_data',NCFLOAT,1,misdat,error)
1343
      if (error.gt.0) goto 920     
1344
 
1345
c     leave define mode
1346
      call ncendf(cdfid,error)
1347
      if (error.gt.0) goto 920     
1348
 
1349
c     synchronyse output to disk and exit
1350
      call ncsnc (cdfid,error)
1351
      call ncpopt (ncopts)
1352
      return
1353
 
1354
c     Error exits.
1355
 900  write (6, *) '*ERROR*: When calling putcdf, the number of ',
1356
     &             'variable dimensions must be less or equal 4.'
1357
      call ncpopt (ncopts)
1358
      call ncclos (cdfid, error)
1359
      return
1360
 
1361
 920  write (6, *) '*ERROR*: An error occurred while attempting to ',
1362
     &             'write the data file in subroutine putcdf.'
1363
      call ncpopt (ncopts)
1364
      call ncclos (cdfid, error)
1365
      return
1366
      end
1367
 
1368
 
1369
      subroutine puttimes(cdfid,times,ntimes,ierr)
1370
C------------------------------------------------------------------------
1371
C     Purpose:
1372
C        Redefine all times on the specified NetCDF file
1373
C     Arguments: 
1374
C        cdfid  int  input   identifier for NetCDF file
1375
C        times	real input   array contains all time values on the file,
1376
C                            dimensioned at least times(ntimes)
1377
C        ntimes int  input   number of times on the file
1378
C        error  int  output  errorflag 
1379
C     History:
1380
C        Heini Wernli, ETHZ  
1381
C        Christoph Schaer, ETHZ
1382
C        Johannes Jenkner, ETHZ (adjustment for integer and double times)
1383
C     Note:
1384
C        This preliminary version does not define the times-array, but only
1385
C        overwrites or extends an existing times-array.
1386
C------------------------------------------------------------------------
1387
 
1388
      integer	ierr,i
1389
      real times(*)
1390
      integer didtim,ntimes
1391
 
1392
      integer	cdfid,idtime,nfiltim
1393
      integer	ncdid,ncvid
1394
 
1395
      integer   vtyp,dn,nat
1396
      integer dims(4)
1397
 
1398
      idtime=ncvid(cdfid,'time',ierr)   ! inquire id for time array
1399
      if (ierr.ne.0) return
1400
      didtim=ncdid(cdfid,'time',ierr)	! inquire id for time dimension
1401
      if (ierr.ne.0) return
1402
 
1403
      call ncdinq(cdfid,didtim,'time',nfiltim,ierr)   ! inquire # of times
1404
      if (ierr.ne.0) return
1405
 
1406
      call ncvinq(cdfid,idtime,dimnam,vtyp,dn,dims,natt,ierr)
1407
 
1408
      if (nfiltim.lt.ntimes) then
1409
        print *,'Warning: puttimes is extending times-array'
1410
      else if (nfiltim.gt.ntimes) then
1411
        print *,'Warning: puttimes does not cover range of times-array'
1412
      endif
1413
 
1414
      if (vtyp.eq.5) then
1415
        do i=1,ntimes
1416
          call ncvpt1(cdfid,idtime,i,times(i),ierr)
1417
          if (ierr.ne.0) return
1418
        enddo
1419
      elseif (vtyp.eq.4) then ! integer version  
1420
        do i=1,ntimes
1421
          call ncvpt1(cdfid,idtime,i,int(times(i)),ierr) 
1422
          if (ierr.ne.0) return
1423
        enddo
1424
      elseif (vtyp.eq.6) then ! double precision version
1425
        do i=1,ntimes
1426
          call ncvgt1(cdfid,idtime,i,dble(times(i)),ierr) 
1427
          if (ierr.ne.0) return
1428
        enddo        
1429
      else
1430
        return
1431
      endif
1432
 
1433
      end
1434
 
1435
 
1436
 
1437
      subroutine gettimes(cdfid,times,ntimes,ierr)
1438
C------------------------------------------------------------------------
1439
C     Purpose:
1440
C        Get all times on the specified NetCDF file
1441
C     Arguments: 
1442
C        cdfid  int  input   identifier for NetCDF file
1443
C        times	real output  array contains all time values on the file,
1444
C                            dimensioned at least times(ntimes)
1445
C        ntimes int  output  number of times on the file
1446
C        error  int  output  errorflag 
1447
C     History:
1448
C        Heini Wernli, ETHZ  
1449
C        Johannes Jenkner, ETHZ (adjustment for integer and double times)
1450
C------------------------------------------------------------------------
1451
 
1452
      include "netcdf.inc"
1453
 
1454
      integer	ierr,i
1455
      real times(*)
1456
      integer didtim,ntimes
1457
 
1458
      integer	cdfid,idtime
1459
      integer	ncopts
1460
      character*(20) dimnam
1461
 
1462
      integer   vtyp,dn,nat
1463
      integer dims(4)
1464
 
1465
      integer,dimension(:),allocatable :: inttimes
1466
      double precision,dimension(:),allocatable :: doubletimes
1467
 
1468
c     Get current value of error options, and make sure netCDF-errors do 
1469
c     not abort execution
1470
      call ncgopt (ncopts)
1471
      call ncpopt(NCVERBOS)
1472
 
1473
      didtim=ncdid(cdfid,'time',ierr)	! inquire id for time dimension
1474
      if (ierr.ne.0) goto 900
1475
      idtime=ncvid(cdfid,'time',ierr)   ! inquire id for time array
1476
      if (ierr.ne.0) goto 900
1477
      call ncdinq(cdfid,didtim,dimnam,ntimes,ierr)      ! inquire # of times
1478
      if (ierr.ne.0) goto 900
1479
 
1480
      call ncvinq(cdfid,idtime,dimnam,vtyp,dn,dims,natt,ierr)
1481
 
1482
      if (vtyp.eq.5) then
1483
        do i=1,ntimes
1484
          call ncvgt1(cdfid,idtime,i,times(i),ierr) ! get times
1485
          if (ierr.ne.0) goto 900
1486
        enddo   
1487
      elseif (vtyp.eq.4) then ! integer version  
1488
        allocate(inttimes(ntimes))
1489
        do i=1,ntimes
1490
          call ncvgt1(cdfid,idtime,i,inttimes(i),ierr) ! get times
1491
          if (ierr.ne.0) goto 900
1492
        enddo
1493
        times(1:ntimes)=real(inttimes(1:ntimes))
1494
      elseif (vtyp.eq.6) then ! double precision version
1495
        allocate(doubletimes(ntimes))
1496
        do i=1,ntimes
1497
          call ncvgt1(cdfid,idtime,i,doubletimes(i),ierr) ! get times
1498
          if (ierr.ne.0) goto 900
1499
        enddo
1500
        times(1:ntimes)=real(doubletimes(1:ntimes))        
1501
      else
1502
        goto 900
1503
      endif
1504
 
1505
c     normal exit
1506
      call ncpopt (ncopts)
1507
      return
1508
 
1509
c     error exit
1510
 900  ntimes=1
1511
      times(1)=0.
1512
      call ncpopt (ncopts)
1513
      end
1514
 
1515
 
1516
 
1517
 
1518
      subroutine cpp_crecdf(filnam,filnam_len,cdfid,phymin,phymax,ndim,
1519
     &     cfn,cfn_len,error)
1520
C------------------------------------------------------------------------
1521
C     Purpose:
1522
C        allows to call crecdf from c++
1523
C     Arguments: 
1524
C        see crecdf
1525
C        additionally: fname_len and cfn_len, the length of the 
1526
C           strings
1527
C        
1528
C        
1529
C     History:
1530
C        981221  Mark A. Liniger ETHZ
1531
C        
1532
C     Note:
1533
C        
1534
C        
1535
C------------------------------------------------------------------------
1536
      integer        filnam_len,ndim,cfn_len,error,cdfid
1537
      character *(*) filnam,cfn
1538
      real           phymin(*),phymax(*)
1539
 
1540
      call crecdf (filnam(1:filnam_len),cdfid,phymin,phymax,ndim,
1541
     &     cfn(1:cfn_len),error)
1542
 
1543
      end
1544
 
1545
 
1546
      subroutine cpp_putdef(cdfid,varnam,varnam_len,ndim,misdat,
1547
     &     vardim,varmin,varmax,stag,error)
1548
C------------------------------------------------------------------------
1549
C     Purpose:
1550
C        allows to call putdef from c++
1551
C     Arguments: 
1552
C        see crecdf
1553
C        additionally: varnam_len, the length of the 
1554
C           strings
1555
C        
1556
C        
1557
C     History:
1558
C        981221  Mark A. Liniger ETHZ
1559
C        
1560
C     Note:
1561
C        
1562
C        
1563
C------------------------------------------------------------------------
1564
      integer        varnam_len,ndim,error,vardim(*),cdfid
1565
      character *(*) varnam
1566
      real           misdat,stag(*),varmin(*),varmax(*)
1567
 
1568
      call putdef (cdfid, varnam(1:varnam_len), ndim, misdat, 
1569
     &     vardim, varmin, varmax, stag, error)
1570
 
1571
      end
1572
 
1573
 
1574
      subroutine cpp_putdat(cdfid, varnam,varnam_len, 
1575
     &     time, level, dat, error)
1576
C------------------------------------------------------------------------
1577
C     Purpose:
1578
C        allows to call putdef from c++
1579
C     Arguments: 
1580
C        see crecdf
1581
C        additionally: varnam_len, the length of the 
1582
C           strings
1583
C        
1584
C        
1585
C     History:
1586
C        981221  Mark A. Liniger ETHZ
1587
C        
1588
C     Note:
1589
C        
1590
C        
1591
C------------------------------------------------------------------------
1592
      integer        varnam_len,cdfid,error,level
1593
      character *(*) varnam
1594
      real           dat(*)
1595
      real           time
1596
 
1597
      call putdat(cdfid, varnam(1:varnam_len), time, level, dat, error)
1598
 
1599
 
1600
 
1601
      end