Subversion Repositories lagranto.ecmwf

Rev

Rev 21 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 michaesp 1
c     ************************************************************
2
c     * This package provides input routines to read the wind    *
3
c     * and other fields from IVE necdf files. The routines are  *
4
c     *                                                          *
5
c     * 1) input_open  : to open a data file                     *
6
c     * 2) input_grid  : to read the grid information, including *
7
c     *                  the vertical levels                     *
8
c     * 3) input_wind  : to read the wind components             *
9
c     * 4) input_close : to close an input file                  *
10
c     *                                                          *
11
c     * The file is characterised by an filename <filename> and  *
12
c     * a file identifier <fid>. The horizontal grid is given by *
13
c     * <xmin,xmax,ymin,ymax,dx,dy,nx,ny> where the pole of the  *
14
c     * rotated grid is given by <pollon,pollat>. The vertical   *
15
c     * grid is characterised by the surface pressure <ps> and   *
16
c     * the pressure at staggered <slev> and unstaggered <ulev>  *
17
c     * levels. The number of levels is given by <nz>. Finally,  *
18
c     * the retrieval of the wind <field> with name <fieldname>  *
19
c     * is characterised by a <time> and a missing data value    *
20
c     * <mdv>.                                                   *
21
c     *                                                          *
22
c     * Author: Michael Sprenger, Autumn 2008                    *
23
c     ************************************************************
24
 
25
c     ------------------------------------------------------------
26
c     Open input file
27
c     ------------------------------------------------------------
28
 
29
      subroutine input_open (fid,filename)
30
 
31
c     Open the input file with filename <filename> and return the
32
c     file identifier <fid> for further reference. 
33
 
34
      use netcdf
35
 
36
      implicit none
37
 
38
c     Declaration of subroutine parameters
39
      integer      fid              ! File identifier
40
      character*80 filename         ! Filename
41
 
42
c     Declaration of auxiliary variables
43
      integer      ierr
44
 
45
c     Open netcdf file
46
      ierr = NF90_OPEN(TRIM(filename),nf90_nowrite, fid)
47
      IF ( ierr /= nf90_NoErr ) PRINT *,NF90_STRERROR(ierr)
48
 
49
c     Exception handling
50
      return
51
 
52
      end
53
 
54
c     ------------------------------------------------------------
55
c     Read information about the grid
56
c     ------------------------------------------------------------
57
 
58
      subroutine input_grid 
59
     >                   (fid,fieldname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,
60
     >                    time,pollon,pollat,p3,ps,nz,ak,bk,stagz,
61
     >                    timecheck)
62
 
63
c     Read grid information at <time> from file with identifier <fid>. 
64
c     The horizontal grid is characterized by <xmin,xmax,ymin,ymax,dx,dy>
65
c     with pole position at <pollon,pollat> and grid dimension <nx,ny>.
66
c     The 3d arrays <p3(nx,ny,nz)> gives the vertical coordinates, either
67
c     on the staggered or unstaggered grid (with <stagz> as the flag).
68
c     The surface pressure is given in <ps(nx,ny)>. If <fid> is negative, 
69
c     only the grid dimensions and grid parameters (xmin...pollat,nz) are 
70
c     determined and returned (this is needed for dynamical allocation of 
71
c     memory).
72
 
73
      use netcdf
74
 
75
      implicit none
76
 
77
c     Declaration of subroutine parameters 
78
      integer      fid                 ! File identifier
79
      real         xmin,xmax,ymin,ymax ! Domain size
80
      real         dx,dy               ! Horizontal resolution
81
      integer      nx,ny,nz            ! Grid dimensions
82
      real         pollon,pollat       ! Longitude and latitude of pole
83
      real         p3(nx,ny,nz)        ! Staggered levels
84
      real         ps(nx,ny)           ! Surface pressure
85
      real         time                ! Time of the grid information
86
      real         ak(nz),bk(nz)       ! Ak and Bk for layers or levels
87
      real         stagz               ! Vertical staggering (0 or -0.5)
88
      character*80 fieldname           ! Variable from which to take grid info
89
      character*80 timecheck           ! Either 'yes' or 'no'
90
 
91
c     Numerical and physical parameters
92
      real          eps                 ! Numerical epsilon
93
      parameter    (eps=0.001)
94
 
95
c     Netcdf variables
96
      integer      vardim(4)
97
      real         varmin(4),varmax(4)
98
      real         mdv
99
      real         stag(4)
100
      integer      ndim
101
      character*80 cstfile
102
      integer      cstid
103
      integer      nvars
104
      character*80 vars(100)
105
      integer        dimids (nf90_max_var_dims)
106
      character*80   dimname(nf90_max_var_dims)
107
      real,allocatable, dimension (:)     :: lon,lat,lev
108
      real,allocatable, dimension (:)     :: times
109
      real,allocatable, dimension (:,:)   :: tmp2
110
      real,allocatable, dimension (:,:,:) :: tmp3
23 michaesp 111
      real,allocatable, dimension (:)     :: aktmp,bktmp
21 michaesp 112
      character*80  units
113
 
114
c     Auxiliary variables
115
      integer      ierr       
116
      integer      i,j,k
117
      integer      isok
118
      real         tmp(200)
119
      character*80 varname
120
      real         rtime
121
      integer      varid
122
      integer      cdfid
123
      integer      stat
124
      real         delta
125
      integer      closear
126
      real         maxps,minps
127
 
128
c     ------ Set file identifier --------------------------------------
129
      if (fid.lt.0) then
130
        cdfid = -fid
131
      else 
132
        cdfid = fid
133
      endif
134
 
135
c     Special handling if 3D pressure is
136
      if ( fieldname.eq.'P' ) then
137
         fieldname = 'U'
138
      endif
139
 
140
c     Get number of dimensions of variable -> ndim
141
      ierr = NF90_INQ_VARID(cdfid,fieldname,varid)
142
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
143
      ierr = nf90_inquire_variable(cdfid, varid, ndims  = ndim)
144
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
145
      if ( ndim.ne.4 ) then
146
          print*,' ERROR: netCDF variables need to be 4D'
147
          print*,'      ',trim(fieldname)
148
          stop
149
      endif
150
 
151
c     Get dimensions -> vardim(1:ndim),dimname(1:ndim)
152
      ierr = NF90_INQ_VARID(cdfid,fieldname,varid)
153
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
154
      ierr = nf90_inquire_variable(cdfid, varid, 
155
     >                                   dimids = dimids(1:ndim))
156
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
157
      do i=1,ndim
158
           ierr = nf90_inquire_dimension(cdfid, dimids(i), 
159
     >                               name = dimname(i) )
160
           IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
161
           ierr = nf90_inquire_dimension(cdfid, dimids(i),len=vardim(i))
162
           IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
163
      enddo
164
 
165
c     Check whether the list of dimensions is OK
166
      if ( ( dimname(1).ne.'lon'  ).or.
167
     >     ( dimname(2).ne.'lat'  ).or. 
168
     >     ( dimname(3).ne.'lev'  ).and.( dimname(3).ne.'lev_2'  ).or.
169
     >     ( dimname(4).ne.'time' ) )
170
     >then
171
        print*,' ERROR: the dimensions of the variable are not correct'
172
        print*,'        expected -> lon / lat / lev / time'
173
        print*, ( trim(dimname(i))//' / ',i=1,ndim )
174
        stop
175
      endif
176
 
177
c     Allocate memory for reading arrays
178
      allocate(tmp2(vardim(1),vardim(2)),stat=stat)
179
      if (stat.ne.0) print*,'*** error allocating array tmp2     ***'
180
      allocate(tmp3(vardim(1),vardim(2),vardim(3)),stat=stat)
181
      if (stat.ne.0) print*,'*** error allocating array tmp3     ***'
182
      allocate(lon(vardim(1)),stat=stat)
183
      if (stat.ne.0) print*,'*** error allocating array lon     ***' 
184
      allocate(lat(vardim(2)),stat=stat)
185
      if (stat.ne.0) print*,'*** error allocating array lat     ***' 
186
      allocate(times(vardim(4)),stat=stat)
187
      if (stat.ne.0) print*,'*** error allocating array times   ***'
23 michaesp 188
      allocate(aktmp(vardim(3)),stat=stat)
189
      if (stat.ne.0) print*,'*** error allocating array aktmp   ***'
190
      allocate(bktmp(vardim(3)),stat=stat)
191
      if (stat.ne.0) print*,'*** error allocating array bktmp   ***'
21 michaesp 192
 
193
c     Get domain longitudes and latitudes
194
      varname = dimname(1)
195
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
196
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
197
      ierr = nf90_get_var(cdfid,varid,lon)
198
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
199
      varname = dimname(2)
200
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
201
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
202
      ierr = nf90_get_var(cdfid,varid,lat)
203
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
204
 
205
c     Get ak and bk
206
      varname='hyam'
207
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
208
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
23 michaesp 209
      ierr = nf90_get_var(cdfid,varid,aktmp)
21 michaesp 210
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
211
      varname='hybm'
212
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
213
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
23 michaesp 214
      ierr = nf90_get_var(cdfid,varid,bktmp)
21 michaesp 215
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
216
 
217
c     Check that unit of ak is in hPa - if necessary correct it
218
      varname='hyam'
219
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
220
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
221
      ierr = nf90_get_att(cdfid, varid, "units", units)
222
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
223
      if ( units.eq.'Pa' ) then
23 michaesp 224
         do k=1,vardim(3)
225
            aktmp(k) = 0.01 * aktmp(k)
21 michaesp 226
         enddo
227
      endif
228
 
229
c     Get time information (check if time is correct)
230
      varname = 'time'
231
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
232
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
233
      ierr = nf90_get_var(cdfid,varid,times)
234
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
235
      isok=0
236
      do i=1,vardim(4)
237
        if (abs(time-times(i)).lt.eps) then
238
               isok = 1
239
               rtime = times(i)
240
        elseif (timecheck.eq.'no') then
241
               isok = 1
242
               rtime = times(1)
243
        endif
244
      enddo
245
      if ( isok.eq.0 ) then
246
         print*,' ERROR: time ',rtime,' not found on netCDF file' 
247
         stop
248
      endif
249
 
250
c     Read surface pressure
251
      varname='PS'
252
      ierr = NF90_INQ_VARID(cdfid,varname,varid)
253
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
254
      ierr = nf90_get_var(cdfid,varid,tmp2)
255
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
256
 
257
c     Check that surface pressure is in hPa
258
      maxps = -1.e39
259
      minps =  1.e39
260
      do i=1,vardim(1)
261
        do j=1,vardim(2)
262
             if (tmp2(i,j).gt.maxps) maxps = tmp2(i,j)
263
             if (tmp2(i,j).lt.minps) minps = tmp2(i,j)
264
        enddo
265
      enddo
266
      if ( (maxps.gt.1500.).or.(minps.lt.300.) ) then
267
         print*,' ERROR: surface pressre PS must be in hPa'
268
         print*,'       ',maxps,minps
269
         stop
270
      endif
271
 
272
c     Calculate layer and level pressures
273
      do i=1,vardim(1)
274
         do j=1,vardim(2)
275
               do k=1,vardim(3)
23 michaesp 276
                  tmp3(i,j,k)=aktmp(k)+bktmp(k)*tmp2(i,j)
21 michaesp 277
               enddo
278
         enddo
279
      enddo
280
 
281
c     Set the grid dimensions and constants
282
      nx      = vardim(1)
283
      ny      = vardim(2)
284
      nz      = vardim(3)
285
      xmin    = lon(1)
286
      ymin    = lat(1)
287
      xmax    = lon(nx)
288
      ymax    = lat(ny)
289
      dx      = (xmax-xmin)/real(nx-1)
290
      dy      = (ymax-ymin)/real(ny-1)
291
      pollon  = 0.
292
      pollat  = 90.
293
      stagz   = 0.
294
      delta   = xmax-xmin-360.
295
      if (abs(delta+dx).lt.eps) then
296
          xmax    = xmax + dx
297
          nx      = nx + 1
298
          closear = 1
299
      else
300
          closear = 0
301
      endif
302
 
303
c     Save the output arrays (if fid>0) - close arrays on request
304
      if ( fid.gt.0 ) then
305
 
306
         do j=1,vardim(2)
307
           do i=1,vardim(1)
308
             ps(i,j) = tmp2(i,j)
309
           enddo
310
           if (closear.eq.1) ps(vardim(1)+1,j) = ps(1,j)
311
         enddo
312
 
313
         do j=1,vardim(2)
314
           do k=1,vardim(3)
315
             do i=1,vardim(1)
23 michaesp 316
               p3(i,j,k) = tmp3(i,j,vardim(3)-k+1)
21 michaesp 317
             enddo
318
             if (closear.eq.1) p3(vardim(1)+1,j,k) = p3(1,j,k)
319
           enddo
320
         enddo
23 michaesp 321
 
322
         do k=1,vardim(3)
323
            ak(k) = aktmp(vardim(3)-k+1)
324
            bk(k) = bktmp(vardim(3)-k+1)
325
         enddo
326
 
21 michaesp 327
      endif
328
 
329
      return
330
 
331
      end
332
 
333
c     ------------------------------------------------------------
334
c     Read wind information
335
c     ------------------------------------------------------------
336
 
337
      subroutine input_wind (fid,fieldname,field,time,stagz,mdv,
338
     >                       xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,
339
     >                       timecheck)
340
 
341
c     Read the wind component <fieldname> from the file with identifier
342
c     <fid> and save it in the 3d array <field>. The vertical staggering 
343
c     information is provided in <stagz> and gives the reference to either
344
c     the layer or level field from <input_grid>. A consistency check is
345
c     performed to have an agreement with the grid specified by <xmin,xmax,
346
c     ymin,ymax,dx,dy,nx,ny,nz>.
347
 
348
      use netcdf
349
 
350
      implicit none
351
 
352
c     Declaration of variables and parameters
353
      integer      fid                 ! File identifier
354
      character*80 fieldname           ! Name of the wind field
355
      integer      nx,ny,nz            ! Dimension of fields
356
      real         field(nx,ny,nz)     ! 3d wind field
357
      real         stagz               ! Staggering in the z direction
358
      real         mdv                 ! Missing data flag
359
      real         xmin,xmax,ymin,ymax ! Domain size
360
      real         dx,dy               ! Horizontal resolution
361
      real         time                ! Time
362
      character*80 timecheck           ! Either 'yes' or 'no'
363
 
364
c     Numerical and physical parameters
365
      real        eps                 ! Numerical epsilon
366
      parameter  (eps=0.001)
367
      real        notimecheck         ! 'Flag' for no time check
368
      parameter  (notimecheck=7.26537)
369
 
370
c     Netcdf variables
371
      integer      ierr
372
      character*80 varname
373
      integer      vardim(4)
374
      real         varmin(4),varmax(4)
375
      real         stag(4)
376
      integer      ndim
377
      real         times(10)
378
      integer      ntimes
379
      character*80 cstfile
380
      integer      cstid
381
      real         aklay(200),bklay(200),aklev(200),bklev(200)
382
      real         ps(nx,ny)
383
      integer      dimids (nf90_max_var_dims)
384
      character*80 dimname(nf90_max_var_dims)
385
      integer      varid
386
      integer      cdfid
387
      real,allocatable, dimension (:)     :: lon,lat
388
      real,allocatable, dimension (:,:)   :: tmp2
389
      real,allocatable, dimension (:,:,:) :: tmp3
390
 
391
c     Auxiliary variables
392
      integer      isok
393
      integer      i,j,k
394
      integer      nz1
395
      real         rtime
396
      integer      closear
397
      integer      stat
398
      real         delta
399
 
400
c     Get the number of dimensions -> ndim
401
      ierr = NF90_INQ_VARID(fid,fieldname,varid)
402
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
403
      ierr = nf90_inquire_variable(fid, varid, ndims  = ndim)
404
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
405
 
406
c     Get the dimensions of the arrays -> varid(1:ndim)
407
      ierr = NF90_INQ_VARID(fid,fieldname,varid)
408
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
409
      ierr = nf90_inquire_variable(fid, varid, 
410
     >                                   dimids = dimids(1:ndim))
411
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
412
      do i=1,ndim
413
           ierr = nf90_inquire_dimension(fid, dimids(i), 
414
     >                               name = dimname(i) )
415
           IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
416
           ierr = nf90_inquire_dimension(fid, dimids(i),len=vardim(i))
417
           IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
418
      enddo
419
 
420
c     Check whether the list of dimensions is OK
421
      if ( ( dimname(1).ne.'lon'  ).or.
422
     >     ( dimname(2).ne.'lat'  ).or.
423
     >     ( dimname(3).ne.'lev'  ).and.( dimname(3).ne.'lev_2'  ).or.
424
     >     ( dimname(4).ne.'time' ) )
425
     >then
426
        print*,' ERROR: the dimensions of the variable are not correct'
427
        print*,'        expected -> lon / lat / lev / time'
428
        print*, ( trim(dimname(i))//' / ',i=1,ndim )
429
        stop
430
      endif
431
 
432
c     Allocate memory for reading arrays - depending on <closear>
433
      allocate(tmp2(vardim(1),vardim(2)),stat=stat)
434
      if (stat.ne.0) print*,'*** error allocating array tmp2     ***'
435
      allocate(tmp3(vardim(1),vardim(2),vardim(3)),stat=stat)
436
      if (stat.ne.0) print*,'*** error allocating array tmp3     ***'
437
      allocate(lon(vardim(1)),stat=stat)
438
      if (stat.ne.0) print*,'*** error allocating array lon     ***'
439
      allocate(lat(vardim(2)),stat=stat)
440
      if (stat.ne.0) print*,'*** error allocating array lat     ***'
441
 
442
c     Get domain boundaries
443
      varname = dimname(1)
444
      ierr = NF90_INQ_VARID(fid,varname,varid)
445
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
446
      ierr = nf90_get_var(fid,varid,lon)
447
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
448
      varname = dimname(2)
449
      ierr = NF90_INQ_VARID(fid,varname,varid)
450
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
451
      ierr = nf90_get_var(fid,varid,lat)
452
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
453
 
454
c     Read data 
455
      ierr = NF90_INQ_VARID(fid,fieldname,varid)
456
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
457
      ierr = nf90_get_var(fid,varid,tmp3)
458
      IF(ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
459
 
460
c     If the field is 2D, expand it to 3D - simple handling of 2D tracing
461
      if ( vardim(3).eq.1 ) then
462
         do i=1,vardim(1)
463
            do j=1,vardim(2)
464
               do k=1,vardim(3)
465
                  tmp3(i,j,k) = tmp3(i,j,1)
466
               enddo
467
            enddo
468
         enddo
469
      endif
470
 
471
c     Save the ouput array - close on request
472
      delta = varmax(1)-varmin(1)-360.
473
      if (abs(delta+dx).lt.eps) then
474
          closear = 1
475
      else
476
          closear = 0
477
      endif
478
 
479
      do j=1,vardim(2)
480
        do k=1,vardim(3)
481
          do i=1,vardim(1)
23 michaesp 482
             field(i,j,k) = tmp3(i,j,vardim(3)-k+1)
21 michaesp 483
          enddo
484
          if (closear.eq.1) field(vardim(1)+1,j,k) = field(1,j,k)
485
        enddo
486
      enddo
487
 
488
c     Exit point
489
      return
490
 
491
      end
492
 
493
c     ------------------------------------------------------------
494
c     Close input file
495
c     ------------------------------------------------------------
496
 
497
      subroutine input_close(fid)
498
 
499
c     Close the input file with file identifier <fid>.
500
 
501
      use netcdf
502
 
503
      implicit none
504
 
505
c     Declaration of subroutine parameters
506
      integer fid
507
 
508
c     Auxiliary variables
509
      integer ierr
510
 
511
c     Close file
512
      ierr = NF90_CLOSE(fid)
513
      IF( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
514
 
515
      end
516
 
517
c     ------------------------------------------------------------
518
c     Get a list of variables on netCDF file
519
c     ------------------------------------------------------------
520
 
521
      subroutine input_getvars(fid,vnam,nvars)
522
 
523
c     List of variables on netCDF file
524
 
525
      use netcdf
526
 
527
      implicit none
528
 
529
c     Declaration of subroutine parameters
530
      integer      fid
531
      integer      nvars
532
      character*80 vnam(200)
533
 
534
c     Auxiliary variables
535
      integer ierr
536
      integer i
537
      integer nDims,nGlobalAtts,unlimdimid
538
 
539
      ierr = nf90_inquire(fid, nDims, nVars, nGlobalAtts, unlimdimid)
540
      IF( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
541
 
542
      do i=1,nVars
543
         ierr = nf90_inquire_variable(fid, i, name = vnam(i))
544
      enddo
545
 
546
      end