Subversion Repositories lagranto.icon

Rev

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

Rev Author Line No. Line
3 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 height <zb> and     *
16
c     * the model level height (given on the W grid). The number *
17
c     * of levels is given by <nz>. Finally, the retrieval of the* 
18
c     * wind <field> with name <fieldname>  is characterised by  *
19
c     a <time> and a missing data value <mdv>.                   *
20
c     *                                                          *
21
c     * Author: Michael Sprenger, Spring 2011/2012               *
22
c     ************************************************************
23
 
24
c     ------------------------------------------------------------
25
c     Open input file
26
c     ------------------------------------------------------------
27
 
28
      subroutine input_open (fid,filename)
29
 
30
c     Open the input file with filename <filename> and return the
31
c     file identifier <fid> for further reference. 
32
 
33
      use netcdf
34
      implicit none
35
 
36
c     Declaration of subroutine parameters
37
      integer      fid              ! File identifier
38
      character*80 filename         ! Filename
39
 
40
c     Declaration of auxiliary variables
41
      integer      ierr
42
 
43
c     Open netcdf file
44
      ierr = NF90_OPEN(TRIM(filename),nf90_nowrite, fid)
45
      IF ( ierr /= nf90_NoErr ) PRINT *,NF90_STRERROR(ierr)
46
 
47
      end
48
 
49
 
50
c     ------------------------------------------------------------
51
c     Read information about the grid
52
c     ------------------------------------------------------------
53
 
54
      subroutine input_grid 
55
     >             (fid,fieldname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,
56
     >              time,pollon,pollat,polgam,z3,zb,nz,stagz,timecheck)
57
 
58
c     Read grid information at <time> from file with identifier <fid>. 
59
c     The horizontal grid is characterized by <xmin,xmax,ymin,ymax,dx,dy>
60
c     with pole position at <pollon,pollat> and grid dimension <nx,ny>.
61
c     The 3d arrays <z3(nx,ny,nz)> gives the vertical coordinates, either
62
c     on the staggered or unstaggered grid (with <stagz> as the flag).
63
c     The surface height is given in <zb(nx,ny)>. If <fid> is negative, 
64
c     only the grid dimensions and grid parameters (xmin...pollat,nz) are 
65
c     determined and returned (this is needed for dynamical allocation of 
66
c     memory).
67
 
68
      use netcdf
69
      implicit none
70
 
71
c     Declaration of subroutine parameters 
72
      integer      fid                  ! File identifier
73
      real         xmin,xmax,ymin,ymax  ! Domain size
74
      real         dx,dy                ! Horizontal resolution
75
      integer      nx,ny,nz             ! Grid dimensions
76
      real         pollon,pollat,polgam ! Longitude and latitude of pole
77
      real         z3(nx,ny,nz)         ! Staggered levels
78
      real         zb(nx,ny)            ! Surface pressure
79
      real         time                 ! Time of the grid information
80
      real         stagz                ! Vertical staggering (0 or -0.5)
81
      character*80 fieldname            ! Variable from which to take grid info
82
      character*80 timecheck            ! Either 'yes' or 'no'
83
 
84
c     Numerical and physical parameters
85
      real          eps                ! Numerical epsilon
86
      parameter    (eps=0.001)
87
 
88
c     Name of the constants file
89
      character*80  constfile
90
      parameter     (constfile = 'ICONCONST')
91
 
92
c     netCDF variables
93
      real         lon(5000)
94
      real         lat(5000)
95
 
96
c     Auxiliary variables
97
      integer      ierr   
98
      integer      varid
99
      integer      dimid
100
      integer      dimids(100)
101
      character*80 dimname(100)   
102
      integer      ndim
103
      integer      i,j,k
104
      real         tmp3(nx,ny,nz)
105
      integer      cdfid 
106
 
107
c     fid can be negative, take its absolute value
108
      cdfid = abs(fid)
109
 
110
c     Open ICONCONST      
111
      ierr = NF90_OPEN(constfile,nf90_nowrite, cdfid)
112
      IF ( ierr /= nf90_NoErr ) PRINT *,NF90_STRERROR(ierr)
113
 
114
c     Get <ndim>, <dimids> and <dimname>
115
      ierr = NF90_INQ_VARID(cdfid,'z_mc',varid)
116
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
117
      ierr = nf90_inquire_variable(cdfid, varid,ndims  = ndim)
118
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
119
      ierr = nf90_inquire_variable(cdfid, varid,dimids = dimids(1:ndim))
120
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
121
      do i=1,ndim
122
         ierr = nf90_inquire_dimension(cdfid, dimids(i), 
123
     >                               name = dimname(i) )
124
         IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
125
      enddo
126
 
127
c     Check that dimensions are OK
128
      if ( ndim.ne.3 ) then
129
        print*,' ERROR: z_mc must be 3D ',ndim
130
        stop
131
      endif
132
      if ( dimname(2).ne.'lat' ) then
133
        print*,' ERROR: dimname(2) must be lat ',trim(dimname(2))
134
        stop
135
      endif
136
      if ( dimname(1).ne.'lon' ) then
137
        print*,' ERROR: dimname(1) must be lon ',trim(dimname(1))
138
        stop
139
      endif
140
 
141
c     Get lon coordinates        
142
      ierr = nf90_inq_dimid(cdfid,'lon', dimid)
143
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
144
      ierr = nf90_inquire_dimension(cdfid, dimid, len = nx)
145
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
146
      ierr = NF90_INQ_VARID(cdfid,'lon',varid)
147
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
148
      ierr = NF90_GET_VAR(cdfid,varid,lon(1:nx))
149
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr) 
150
 
151
c     Get lat coordinates        
152
      ierr = nf90_inq_dimid(cdfid,'lat', dimid)
153
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
154
      ierr = nf90_inquire_dimension(cdfid, dimid, len = ny)
155
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
156
      ierr = NF90_INQ_VARID(cdfid,'lat',varid)
157
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
158
      ierr = NF90_GET_VAR(cdfid,varid,lat(1:ny))
159
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr) 
160
 
161
c     Get number of vertical levels
162
      ierr = nf90_inquire_dimension(cdfid, dimids(3), len = nz)
163
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
164
 
165
c     Set parameters
166
      pollon = 0.
167
      pollat = 90.
168
      polgam = 0.
169
      xmin   = lon(1)
170
      ymin   = lat(1)
171
      xmax   = lon(nx)
172
      ymax   = lat(ny)
173
      dx     = ( xmax - xmin ) / real(nx-1) 
174
      dy     = ( ymax - ymin ) / real(ny-1)
175
      if ( xmin.lt.-180.) then
176
        xmin = xmin + 360.
177
        xmax = xmax + 360.
178
      else if ( xmax.gt.360. ) then
179
        xmin = xmin - 360.
180
        xmax = xmax - 360.
181
      endif
182
      stagz = 0.
183
 
184
c     If <fid<0>, nothing further to do - we have the grid dimensions 
185
      if ( fid.lt.0 ) goto 100
186
 
187
c     Read topography 
188
      ierr = NF90_INQ_VARID(cdfid,'TOPOGRAPHY',varid)
189
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
190
      ierr = NF90_GET_VAR(cdfid,varid,zb)
191
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr) 
192
 
193
c     Read 3D height field
194
      ierr = NF90_INQ_VARID(cdfid,'z_mc',varid)
195
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
196
      ierr = NF90_GET_VAR(cdfid,varid,tmp3)
197
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr) 
198
 
199
c     Check whether vertical axis is descending - vertical flip
200
      if ( tmp3(1,1,1).lt.tmp3(1,1,nz) ) then
201
        print*,' ERROR: vertical axis must be descending... Stop'
202
        stop
203
      endif
204
      do i=1,nx
205
        do j=1,ny
206
           do k=1,nz
207
              z3(i,j,k) = tmp3(i,j,nz-k+1)
208
           enddo
209
        enddo
210
      enddo
211
 
212
c     Exit point
213
 100  continue
214
 
215
c     Close constants file
216
      ierr = NF90_CLOSE(cdfid)
217
      IF( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)   
218
 
219
      end
220
 
221
c     ------------------------------------------------------------
222
c     Read wind information
223
c     ------------------------------------------------------------
224
 
225
      subroutine input_wind (fid,fieldname,field,time,stagz,mdv,
226
     >                       xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,
227
     >                       timecheck)
228
 
229
c     Read the wind component <fieldname> from the file with identifier
230
c     <fid> and save it in the 3d array <field>. The vertical staggering 
231
c     information is provided in <stagz> and gives the reference to either
232
c     the layer or level field from <input_grid>. A consistency check is
233
c     performed to have an agreement with the grid specified by <xmin,xmax,
234
c     ymin,ymax,dx,dy,nx,ny,nz>.
235
 
236
      use netcdf
237
      implicit none
238
 
239
c     Declaration of variables and parameters
240
      integer      fid                 ! File identifier
241
      character*80 fieldname           ! Name of the wind field
242
      integer      nx,ny,nz            ! Dimension of fields
243
      real         field(nx,ny,nz)     ! 3d wind field
244
      real         stagz               ! Staggering in the z direction
245
      real         mdv                 ! Missing data flag
246
      real         xmin,xmax,ymin,ymax ! Domain size
247
      real         dx,dy               ! Horizontal resolution
248
      real         time                ! Time
249
      character*80 timecheck           ! Either 'yes' or 'no'
250
 
251
c     Auxiliary variables
252
      integer      i,j,k
253
      integer      nlon,nlat,nlev
254
      real         tmp2(nx,ny)
255
      real         tmp3(nx,ny,nz)
256
      integer      ierr
257
      integer      varid
258
      integer      dimid
259
      integer      dimids(100)
260
      character*80 dimname(100)   
261
      integer      ndim
262
 
263
c     Get <ndim>, <dimids> and <dimname>
264
      ierr = NF90_INQ_VARID(fid,fieldname,varid)
265
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
266
      ierr = nf90_inquire_variable(fid, varid, ndims  = ndim)
267
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
268
      ierr = nf90_inquire_variable(fid, varid,dimids = dimids(1:ndim))
269
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
270
      do i=1,ndim
271
         ierr = nf90_inquire_dimension(fid, dimids(i), 
272
     >                               name = dimname(i) )
273
         IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
274
      enddo
275
 
276
c     Check that dimensions are OK
277
      if ( ndim.ne.4 ) then
278
        print*,' ERROR: ',trim(fieldname),' must be 4D'
279
        stop
280
      endif
281
      if ( dimname(4).ne.'time' ) then
282
        print*,' ERROR: dimname(4) must be time '
283
        print*,trim(fieldname),' / ',trim(dimname(4))
284
        stop
285
      endif
286
      if ( dimname(2).ne.'lat' ) then
287
        print*,' ERROR: dimname(2) must be lat ' 
288
        print*,trim(fieldname),' / ',trim(dimname(2))
289
        stop
290
      endif    
291
      if ( dimname(1).ne.'lon' ) then
292
        print*,' ERROR: dimname(1) must be lon ' 
293
        print*,trim(fieldname),' / ',trim(dimname(2))
294
        stop
295
      endif   
296
 
297
c     Check grid dimensions       
298
      ierr = nf90_inq_dimid(fid,dimname(1), dimid)
299
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
300
      ierr = nf90_inquire_dimension(fid, dimid, len = nlon)
301
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
302
      ierr = nf90_inq_dimid(fid,dimname(2), dimid)
303
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
304
      ierr = nf90_inquire_dimension(fid, dimid, len = nlat)
305
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
306
      ierr = nf90_inq_dimid(fid,dimname(3), dimid)
307
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
308
      ierr = nf90_inquire_dimension(fid, dimid, len = nlev)
309
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
310
      if ( (nlon.ne.nx).or.(nlat.ne.ny).or.
311
     >     (nlev.ne.nz).and.(nlev.ne.1) )
312
     >then
313
         print*,' ERROR: grid mismatch between ICONCOSNT and P file'
314
         stop
315
      endif
316
 
317
c     Read the field
318
      ierr = NF90_INQ_VARID(fid,fieldname,varid)
319
      IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
320
      if ( nlev.eq.2 ) then
321
        ierr = NF90_GET_VAR(fid,varid,tmp2)
322
        IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr) 
323
        do i=1,nx
324
            do j=1,ny
325
               do k=1,nz
326
                  tmp3(i,j,k) = tmp2(i,j)
327
               enddo
328
            enddo
329
         enddo
330
      else
331
        ierr = NF90_GET_VAR(fid,varid,tmp3)
332
        IF ( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr) 
333
      endif
334
 
335
c     Flip vertically 
336
      do i=1,nx
337
        do j=1,ny
338
           do k=1,nz
339
              field(i,j,k) = tmp3(i,j,nz-k+1)
340
           enddo
341
        enddo
342
      enddo
343
 
344
 
345
      end
346
 
347
c     ------------------------------------------------------------
348
c     Close input file
349
c     ------------------------------------------------------------
350
 
351
      subroutine input_close(fid)
352
 
353
c     Close the input file with file identifier <fid>.
354
 
355
      use netcdf
356
      implicit none
357
 
358
c     Declaration of subroutine parameters
359
      integer fid
360
 
361
c     Auxiliary variables
362
      integer ierr
363
 
364
c     Close file
365
      ierr = NF90_CLOSE(fid)
366
      IF( ierr /= nf90_NoErr) PRINT *,NF90_STRERROR(ierr)
367
 
368
      end