Subversion Repositories lagranto.ecmwf

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 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(4),varmax(4),stag(4)
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(4),varmax(4),stag(4)
1105
      real	time, timeval
1106
      data	stag/0.,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
c     get definitions of data
1116
      call getdef (cdfid, trim(varnam), ndims, misdat, 
1117
     &                           vardim, varmin, varmax, stag, ierr)
1118
      if (ierr.ne.0)  print *,'*ERROR* in getdef in putdat'
1119
 
1120
c     get id of variable
1121
      idvar=ncvid(cdfid,trim(varnam),ierr)
1122
      if (ierr.ne.0) print *,'*ERROR* in ncvid in putdat'
1123
 
1124
c     get times-array 
1125
      did(4)=ncdid(cdfid,'time',ierr)
1126
      if (ierr.ne.0) print *,'*ERROR* did(4) in putdat'
1127
      call ncdinq(cdfid,did(4),chars,ntime,ierr)
1128
      if (ierr.ne.0) print *,'*ERROR* in ncdinq in putdat'
1129
      idtime=ncvid(cdfid,'time',ierr)
1130
      if (ierr.ne.0) print *,'*ERROR* in ncvid in putdat'
1131
C     Check if a new time step is starting
1132
      iflag=0
1133
      do i=1,ntime
1134
        call ncvgt1(cdfid,idtime,i,timeval,ierr)
1135
        if (ierr.ne.0) print *,'*ERROR* in ncvgt1 in putdat'
1136
        if (time.eq.timeval) iflag=i
1137
      enddo
1138
      if (iflag.eq.0) then		! new time step
1139
        ntime=ntime+1
1140
        iflag=ntime
1141
        idtime=ncvid(cdfid,'time',ierr)
1142
        if (ierr.ne.0) print *, '*ERROR* in ncvid in putdat'
1143
        call ncvpt1(cdfid,idtime,ntime,time,ierr)
1144
        if (ierr.ne.0) print *, '*ERROR* in ncvpt1 in putdat'
1145
      endif
1146
 
1147
C     Define data volume to write on the NetCDF file in index space
1148
      corner(1)=1               ! starting corner of data volume
1149
      corner(2)=1
1150
      edgeln(1)=vardim(1)       ! edge lengthes of data volume
1151
      edgeln(2)=vardim(2)
1152
      if (level.eq.0) then
1153
        corner(3)=1
1154
        edgeln(3)=vardim(3)
1155
      else
1156
        corner(3)=level
1157
        edgeln(3)=1
1158
      endif
1159
      corner(4)=iflag
1160
      edgeln(4)=1
1161
 
1162
C     Put data on NetCDF file
1163
 
1164
c      print *,'vor Aufruf ncvpt d.h. Daten schreiben in putdat '
1165
c      print *,'cdfid ',cdfid
1166
c      print *,'idvar ',idvar
1167
c      print *,'corner ',corner
1168
c      print *,'edgeln ',edgeln
1169
 
1170
      call ncvpt(cdfid,idvar,corner,edgeln,dat,error)
1171
      if (error.ne.0) then
1172
        print *, '*ERROR* in ncvpt in putdat - Put data on NetCDF file'
1173
      endif
1174
 
1175
C     Synchronize output to disk and close the files
1176
 
1177
      call ncsnc(cdfid,ierr)
1178
      if (ierr.ne.0) print *, '*ERROR* in ncsnc in putdat'
1179
      end
1180
 
1181
 
1182
 
1183
 
1184
 
1185
 
1186
      subroutine putdef (cdfid, varnam, ndim, misdat, 
1187
     &                            vardim, varmin, varmax, stag, error)
1188
c-----------------------------------------------------------------------
1189
c     Purpose:
1190
c        This routine is called to define the dimensions and the
1191
c        attributes of a variable on an IVE-NetCDF file for use with the
1192
c        IVE plotting package. Prior to calling this routine, the file must
1193
c        be opened with a call to opncdf (extend an existing file) or
1194
c        crecdf (create a new file).
1195
c     Arguments:
1196
c        cdfid   int   input   file-identifier
1197
c                              (can be obtained by calling routine 
1198
c                              opncdf)
1199
c        varnam  char  input   the user-supplied variable name.
1200
c        ndim    int   input   the number of dimensions (ndim<=4). 
1201
c                              Upon ndim=4, the fourth dimension of the
1202
c                              variable is specified as 'unlimited'
1203
c                              on the file (time-dimension). It can 
1204
c                              later be extended to arbitrary length.
1205
c        misdat  real  input   missing data value for the variable. 
1206
c        vardim  int   input   the dimensions of the variable.
1207
c                              Is dimensioned at least Min(3,ndim). 
1208
c        varmin  real  input   the location in physical space of the
1209
c                              origin of each variable.
1210
c                              Is dimensioned at least Min(3,ndim). 
1211
c        varmax  real  input   the extent of each variable in physical
1212
c                              space.
1213
c                              Is dimensioned at least Min(ndim). 
1214
c        stag    real  input   the grid staggering for each variable.
1215
c                              Is dimensioned at least Min(3,ndim). 
1216
c        error   int   output  indicates possible errors found in this
1217
c                              routine.
1218
c                              error = 0   no errors detected.
1219
c                              error =10   other errors detected.
1220
c     History:
1221
c       Apr. 93    Christoph Schaer (ETHZ)     Created.
1222
c-----------------------------------------------------------------------
1223
 
1224
      include "netcdf.inc"
1225
 
1226
c     Argument declarations.
1227
      integer        MAXDIM
1228
      parameter      (MAXDIM=4)
1229
      character *(*) varnam
1230
      integer        vardim(*), ndim, error, cdfid
1231
      real           misdat,  stag(*), varmin(*), varmax(*)
1232
 
1233
c     Local variable declarations.
1234
      character *(20) dimnam,attnam,dimchk
1235
      character *(1)  chrid(MAXDIM)
1236
      character *(20) dimnams(MAXNCDIM)
1237
      integer         dimvals(MAXNCDIM)
1238
      integer         numdims,numvars,numgats,dimulim
1239
      integer         id,did(MAXDIM),idtime,i,k,ierr
1240
      integer         ncopts
1241
      integer         ibeg,iend
1242
      data            chrid/'x','y','z','t'/
1243
 
1244
c     Get current value of error options.
1245
      call ncgopt (ncopts)
1246
 
1247
c     make sure NetCDF-errors do not abort execution
1248
      call ncpopt(NCVERBOS)
1249
 
1250
c     Initially set error to indicate no errors.
1251
      error = 0
1252
 
1253
c     Make sure ndim <= MAXDIM.
1254
      if (ndim.gt.MAXDIM) then
1255
         error = 10
1256
         go to 900
1257
      endif
1258
 
1259
c     Read existing dimensions-declarations from the file
1260
      call ncinq(cdfid,numdims,numvars,numgats,dimulim,error)
1261
      if (numdims.gt.0) then
1262
        do i=1,numdims
1263
          call ncdinq(cdfid,i,dimnams(i),dimvals(i),error)
1264
c         print *,dimnams(i),dimvals(i)
1265
        enddo
1266
      endif
1267
 
1268
c     put file into define mode
1269
      call ncredf(cdfid,error)
1270
      if (error.ne.0) goto 920
1271
 
1272
c     define spatial dimensions
1273
      do k=1,min0(3,ndim)
1274
c       define the default dimension-name
1275
        dimnam(1:3)='dim'
1276
        dimnam(4:4)=chrid(k)
1277
        dimnam(5:5)='_'
1278
        dimnam(6:5+len_trim(varnam))=trim(varnam)
1279
        dimnam=dimnam(1:5+len_trim(varnam))
1280
        did(k)=-1
1281
        if (numdims.gt.0) then
1282
c         check if an existing dimension-declaration can be used
1283
c         instead of defining a new dimension
1284
          do i=1,numdims
1285
            dimchk=dimnams(i)
1286
            if ((vardim(k).eq.dimvals(i)).and.
1287
     &        (dimnam(1:4).eq.dimchk(1:4))) then 
1288
              did(k)=i
1289
              goto 100
1290
            endif
1291
          enddo
1292
 100      continue
1293
        endif
1294
        if (did(k).lt.0) then
1295
c         define the dimension
1296
          did(k)=ncddef(cdfid,dimnam,vardim(k),error)
1297
          if (error.ne.0) goto 920
1298
        endif
1299
      enddo
1300
 
1301
c     define the times-array
1302
      if (ndim.eq.4) then
1303
c       define dimension and variable 'time'
1304
        if (numdims.ge.4) then
1305
          did(4)=ncdid(cdfid,'time',ierr)
1306
          idtime=ncvid(cdfid,'time',ierr)
1307
        else
1308
c         this dimension must first be defined
1309
          did(4) = ncddef (cdfid,'time',NCUNLIM,ierr)
1310
          idtime = ncvdef (cdfid,'time',NCFLOAT,1,did(4),ierr)
1311
        endif
1312
      endif
1313
 
1314
c     define variable
1315
      id=ncvdef(cdfid,varnam,NCFLOAT,ndim,did,error)
1316
      if (error.ne.0) goto 920
1317
 
1318
c     define attributes
1319
      do k=1,min0(ndim,3)
1320
c       min postion
1321
        attnam(1:1)=chrid(k)
1322
        attnam(2:4)='min'
1323
        attnam=attnam(1:4)
1324
        call ncapt(cdfid,id,attnam,NCFLOAT,1,varmin(k),error)
1325
        if (error.gt.0) goto 920
1326
c       max position     
1327
        attnam(1:1)=chrid(k)
1328
        attnam(2:4)='max'
1329
        attnam=attnam(1:4)
1330
        call ncapt(cdfid,id,attnam,NCFLOAT,1,varmax(k),error)
1331
        if (error.gt.0) goto 920     
1332
c       staggering
1333
        attnam(1:1)=chrid(k)
1334
        attnam(2:5)='stag'
1335
        attnam=attnam(1:5)
1336
        call ncapt(cdfid,id,attnam,NCFLOAT,1,stag(k),error)
1337
        if (error.gt.0) goto 920
1338
      enddo
1339
 
1340
c     define missing data value
1341
      call ncapt(cdfid,id,'missing_data',NCFLOAT,1,misdat,error)
1342
      if (error.gt.0) goto 920     
1343
 
1344
c     leave define mode
1345
      call ncendf(cdfid,error)
1346
      if (error.gt.0) goto 920     
1347
 
1348
c     synchronyse output to disk and exit
1349
      call ncsnc (cdfid,error)
1350
      call ncpopt (ncopts)
1351
      return
1352
 
1353
c     Error exits.
1354
 900  write (6, *) '*ERROR*: When calling putcdf, the number of ',
1355
     &             'variable dimensions must be less or equal 4.'
1356
      call ncpopt (ncopts)
1357
      call ncclos (cdfid, error)
1358
      return
1359
 
1360
 920  write (6, *) '*ERROR*: An error occurred while attempting to ',
1361
     &             'write the data file in subroutine putcdf.'
1362
      call ncpopt (ncopts)
1363
      call ncclos (cdfid, error)
1364
      return
1365
      end
1366
 
1367
 
1368
      subroutine puttimes(cdfid,times,ntimes,ierr)
1369
C------------------------------------------------------------------------
1370
C     Purpose:
1371
C        Redefine all times on the specified NetCDF file
1372
C     Arguments: 
1373
C        cdfid  int  input   identifier for NetCDF file
1374
C        times	real input   array contains all time values on the file,
1375
C                            dimensioned at least times(ntimes)
1376
C        ntimes int  input   number of times on the file
1377
C        error  int  output  errorflag 
1378
C     History:
1379
C        Heini Wernli, ETHZ  
1380
C        Christoph Schaer, ETHZ
1381
C        Johannes Jenkner, ETHZ (adjustment for integer and double times)
1382
C     Note:
1383
C        This preliminary version does not define the times-array, but only
1384
C        overwrites or extends an existing times-array.
1385
C------------------------------------------------------------------------
1386
 
1387
      integer	ierr,i
1388
      real times(*)
1389
      integer didtim,ntimes
1390
 
1391
      integer	cdfid,idtime,nfiltim
1392
      integer	ncdid,ncvid
1393
 
1394
      integer   vtyp,dn,nat
1395
      integer dims(4)
1396
 
1397
      idtime=ncvid(cdfid,'time',ierr)   ! inquire id for time array
1398
      if (ierr.ne.0) return
1399
      didtim=ncdid(cdfid,'time',ierr)	! inquire id for time dimension
1400
      if (ierr.ne.0) return
1401
 
1402
      call ncdinq(cdfid,didtim,'time',nfiltim,ierr)   ! inquire # of times
1403
      if (ierr.ne.0) return
1404
 
1405
      call ncvinq(cdfid,idtime,dimnam,vtyp,dn,dims,natt,ierr)
1406
 
1407
      if (nfiltim.lt.ntimes) then
1408
        print *,'Warning: puttimes is extending times-array'
1409
      else if (nfiltim.gt.ntimes) then
1410
        print *,'Warning: puttimes does not cover range of times-array'
1411
      endif
1412
 
1413
      if (vtyp.eq.5) then
1414
        do i=1,ntimes
1415
          call ncvpt1(cdfid,idtime,i,times(i),ierr)
1416
          if (ierr.ne.0) return
1417
        enddo
1418
      elseif (vtyp.eq.4) then ! integer version  
1419
        do i=1,ntimes
1420
          call ncvpt1(cdfid,idtime,i,int(times(i)),ierr) 
1421
          if (ierr.ne.0) return
1422
        enddo
1423
      elseif (vtyp.eq.6) then ! double precision version
1424
        do i=1,ntimes
1425
          call ncvgt1(cdfid,idtime,i,dble(times(i)),ierr) 
1426
          if (ierr.ne.0) return
1427
        enddo        
1428
      else
1429
        return
1430
      endif
1431
 
1432
      end
1433
 
1434
 
1435
 
1436
      subroutine gettimes(cdfid,times,ntimes,ierr)
1437
C------------------------------------------------------------------------
1438
C     Purpose:
1439
C        Get all times on the specified NetCDF file
1440
C     Arguments: 
1441
C        cdfid  int  input   identifier for NetCDF file
1442
C        times	real output  array contains all time values on the file,
1443
C                            dimensioned at least times(ntimes)
1444
C        ntimes int  output  number of times on the file
1445
C        error  int  output  errorflag 
1446
C     History:
1447
C        Heini Wernli, ETHZ  
1448
C        Johannes Jenkner, ETHZ (adjustment for integer and double times)
1449
C------------------------------------------------------------------------
1450
 
1451
      include "netcdf.inc"
1452
 
1453
      integer	ierr,i
1454
      real times(*)
1455
      integer didtim,ntimes
1456
 
1457
      integer	cdfid,idtime
1458
      integer	ncopts
1459
      character*(20) dimnam
1460
 
1461
      integer   vtyp,dn,nat
1462
      integer dims(4)
1463
 
1464
      integer,dimension(:),allocatable :: inttimes
1465
      double precision,dimension(:),allocatable :: doubletimes
1466
 
1467
c     Get current value of error options, and make sure netCDF-errors do 
1468
c     not abort execution
1469
      call ncgopt (ncopts)
1470
      call ncpopt(NCVERBOS)
1471
 
1472
      didtim=ncdid(cdfid,'time',ierr)	! inquire id for time dimension
1473
      if (ierr.ne.0) goto 900
1474
      idtime=ncvid(cdfid,'time',ierr)   ! inquire id for time array
1475
      if (ierr.ne.0) goto 900
1476
      call ncdinq(cdfid,didtim,dimnam,ntimes,ierr)      ! inquire # of times
1477
      if (ierr.ne.0) goto 900
1478
 
1479
      call ncvinq(cdfid,idtime,dimnam,vtyp,dn,dims,natt,ierr)
1480
 
1481
      if (vtyp.eq.5) then
1482
        do i=1,ntimes
1483
          call ncvgt1(cdfid,idtime,i,times(i),ierr) ! get times
1484
          if (ierr.ne.0) goto 900
1485
        enddo   
1486
      elseif (vtyp.eq.4) then ! integer version  
1487
        allocate(inttimes(ntimes))
1488
        do i=1,ntimes
1489
          call ncvgt1(cdfid,idtime,i,inttimes(i),ierr) ! get times
1490
          if (ierr.ne.0) goto 900
1491
        enddo
1492
        times(1:ntimes)=real(inttimes(1:ntimes))
1493
      elseif (vtyp.eq.6) then ! double precision version
1494
        allocate(doubletimes(ntimes))
1495
        do i=1,ntimes
1496
          call ncvgt1(cdfid,idtime,i,doubletimes(i),ierr) ! get times
1497
          if (ierr.ne.0) goto 900
1498
        enddo
1499
        times(1:ntimes)=real(doubletimes(1:ntimes))        
1500
      else
1501
        goto 900
1502
      endif
1503
 
1504
c     normal exit
1505
      call ncpopt (ncopts)
1506
      return
1507
 
1508
c     error exit
1509
 900  ntimes=1
1510
      times(1)=0.
1511
      call ncpopt (ncopts)
1512
      end
1513
 
1514
 
1515
 
1516
 
1517
      subroutine cpp_crecdf(filnam,filnam_len,cdfid,phymin,phymax,ndim,
1518
     &     cfn,cfn_len,error)
1519
C------------------------------------------------------------------------
1520
C     Purpose:
1521
C        allows to call crecdf from c++
1522
C     Arguments: 
1523
C        see crecdf
1524
C        additionally: fname_len and cfn_len, the length of the 
1525
C           strings
1526
C        
1527
C        
1528
C     History:
1529
C        981221  Mark A. Liniger ETHZ
1530
C        
1531
C     Note:
1532
C        
1533
C        
1534
C------------------------------------------------------------------------
1535
      integer        filnam_len,ndim,cfn_len,error,cdfid
1536
      character *(*) filnam,cfn
1537
      real           phymin(*),phymax(*)
1538
 
1539
      call crecdf (filnam(1:filnam_len),cdfid,phymin,phymax,ndim,
1540
     &     cfn(1:cfn_len),error)
1541
 
1542
      end
1543
 
1544
 
1545
      subroutine cpp_putdef(cdfid,varnam,varnam_len,ndim,misdat,
1546
     &     vardim,varmin,varmax,stag,error)
1547
C------------------------------------------------------------------------
1548
C     Purpose:
1549
C        allows to call putdef from c++
1550
C     Arguments: 
1551
C        see crecdf
1552
C        additionally: varnam_len, the length of the 
1553
C           strings
1554
C        
1555
C        
1556
C     History:
1557
C        981221  Mark A. Liniger ETHZ
1558
C        
1559
C     Note:
1560
C        
1561
C        
1562
C------------------------------------------------------------------------
1563
      integer        varnam_len,ndim,error,vardim(*),cdfid
1564
      character *(*) varnam
1565
      real           misdat,stag(*),varmin(*),varmax(*)
1566
 
1567
      call putdef (cdfid, varnam(1:varnam_len), ndim, misdat, 
1568
     &     vardim, varmin, varmax, stag, error)
1569
 
1570
      end
1571
 
1572
 
1573
      subroutine cpp_putdat(cdfid, varnam,varnam_len, 
1574
     &     time, level, dat, error)
1575
C------------------------------------------------------------------------
1576
C     Purpose:
1577
C        allows to call putdef from c++
1578
C     Arguments: 
1579
C        see crecdf
1580
C        additionally: varnam_len, the length of the 
1581
C           strings
1582
C        
1583
C        
1584
C     History:
1585
C        981221  Mark A. Liniger ETHZ
1586
C        
1587
C     Note:
1588
C        
1589
C        
1590
C------------------------------------------------------------------------
1591
      integer        varnam_len,cdfid,error,level
1592
      character *(*) varnam
1593
      real           dat(*)
1594
      real           time
1595
 
1596
      call putdat(cdfid, varnam(1:varnam_len), time, level, dat, error)
1597
 
1598
 
1599
 
1600
      end