Subversion Repositories lagranto.ocean

Rev

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 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
      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 IVE netcdf file
44
      call cdfopn(filename,fid,ierr)
45
      if (ierr.ne.0) goto 900
46
 
47
c     Exception handling
48
      return
49
 
50
 900  print*,'Cannot open input file ',trim(filename)
51
      stop
52
 
53
      end
54
 
55
 
56
c     ------------------------------------------------------------
57
c     Read information about the grid
58
c     ------------------------------------------------------------
59
 
60
      subroutine input_grid 
61
     >                   (fid,fieldname,xmin,xmax,ymin,ymax,dx,dy,nx,ny,
62
     >                    time,pollon,pollat,z3,zb,nz,stagz,timecheck)
63
 
64
c     Read grid information at <time> from file with identifier <fid>. 
65
c     The horizontal grid is characterized by <xmin,xmax,ymin,ymax,dx,dy>
66
c     with pole position at <pollon,pollat> and grid dimension <nx,ny>.
67
c     The 3d arrays <z3(nx,ny,nz)> gives the vertical coordinates, either
68
c     on the staggered or unstaggered grid (with <stagz> as the flag).
69
c     The surface is given in <zs(nx,ny)>. If <fid> is negative, 
70
c     only the grid dimensions and grid parameters (xmin...pollat,nz) are 
71
c     determined and returned (this is needed for dynamical allocation of 
72
c     memory).
73
 
74
      implicit none
75
 
76
c     Declaration of subroutine parameters 
77
      integer      fid                 ! File identifier
78
      real         xmin,xmax,ymin,ymax ! Domain size
79
      real         dx,dy               ! Horizontal resolution
80
      integer      nx,ny,nz            ! Grid dimensions
81
      real         pollon,pollat       ! Longitude and latitude of pole
82
      real         z3(nx,ny,nz)        ! Staggered levels
83
      real         zb(nx,ny)           ! Surface height
84
      real         z1(nz)              ! Vertical level array
85
      real         time                ! Time of the grid information
86
      real         stagz               ! Vertical staggering (0 or -0.5)
87
      character(len=*) fieldname       ! Variable from which to take grid info
88
      character(len=*) timecheck           ! Either 'yes' or 'no'
89
 
90
c     Numerical and physical parameters
91
      real          eps                 ! Numerical epsilon
92
      parameter    (eps=0.001)
93
 
94
c     Netcdf variables
95
      integer      vardim(4)
96
      real         varmin(4),varmax(4)
97
      real         mdv
98
      real         stag(4)
99
      integer      ndim
100
      character*80 cstfile
101
      integer      cstid
102
      real         times(10)
103
      integer      ntimes
104
      integer      nvars
105
      character*80 vars(100)
106
 
107
c     Auxiliary varaibles
108
      integer      ierr       
109
      integer      i,j,k
110
      integer      isok
111
      real         tmp(200)
112
      character*80 varname
113
      real         rtime
114
      integer      is2d
115
 
116
c     Init the flag for 2D variables - assume a 3D field
117
      is2d = 0
118
 
119
c     Inquire dimensions and grid constants if <fid> is negative
120
      if (fid.lt.0) then
121
 
122
         varname = fieldname
123
 
124
         call getdef(-fid,varname,ndim,mdv,vardim,
125
     >               varmin,varmax,stag,ierr)
126
         if (ierr.ne.0) goto 900
127
 
128
 
129
c        Set the grid dimensions and constants - vardim(3) is taken from constants file
130
         nx   = vardim(1)
131
         ny   = vardim(2)
132
         nz   = vardim(3)
133
         xmin = varmin(1)
134
         ymin = varmin(2)
135
         xmax = varmax(1)
136
         ymax = varmax(2)
137
         dx   = (xmax-xmin)/real(nx-1)
138
         dy   = (ymax-ymin)/real(ny-1)
139
 
140
c        set default pole
141
         pollon = 0.
142
         pollat = 90.
143
 
144
c     Get non-constant grid parameters (surface pressure and vertical grid)
145
      else
146
 
147
c        Get full grid info - in particular staggering flag; set flag for 2D tracing
148
         varname=fieldname
149
         call getdef(fid,varname,ndim,mdv,vardim,
150
     >               varmin,varmax,stag,ierr)
151
         if (ierr.ne.0) goto 900
152
         if (vardim(3).eq.1) is2d = 1
153
 
154
c        Get time information (check if time is correct)
155
         if (trim(varname)/='BATH') then
156
          call gettimes(fid,times,ntimes,ierr)
157
          if (ierr.ne.0) goto 901
158
          isok=0
159
          do i=1,ntimes
160
            if (abs(time-times(i)).lt.eps) then
161
               isok = 1
162
               rtime = times(i)
163
            elseif (timecheck.eq.'no') then
164
               isok = 1
165
               rtime = times(1)
166
            endif
167
          enddo
168
          if ( isok.eq.0) goto 905
169
 
170
         end if
171
 
172
c        If 2D tracing requested: take dummay values for ZB
173
         if ( is2d.eq.1 ) then
174
         varname = fieldname
175
         call getdef(fid,varname,ndim,mdv,vardim,
176
     >            varmin,varmax,stag,ierr)
177
         if (ierr.ne.0) goto 906
178
         if ( vardim(3).ne.(nz) ) goto 907
179
         if ( fieldname == "lev" ) then
180
           call getdat(fid,varname,0.,0,z1,ierr)
181
         else
182
           call getdat(fid,varname,0.,0,zb,ierr)
183
         end if
184
         if (ierr.ne.0) goto 906
185
         if ( fieldname == "lev" ) then
186
            do i=1,nx
187
               do j=1,ny
188
                  zb(i,j) = z1(1)
189
               enddo
190
            enddo            
191
         end if
192
 
193
c        3D tracing 
194
         else
195
 
196
c	     Read 3d model height 
197
         varname = fieldname
198
         call getdef(fid,varname,ndim,mdv,vardim,
199
     >            varmin,varmax,stag,ierr)
200
         if (ierr.ne.0) goto 906
201
         if ( vardim(3).ne.(nz) ) goto 907
202
         if ( fieldname == "lev" ) then
203
          call getdat(fid,varname,0.,0,z1,ierr)
204
         else  
205
          call getdat(fid,varname,0.,0,z3,ierr)
206
         end if
207
         if (ierr.ne.0) goto 906
208
 
209
         if ( fieldname == "lev" ) then
210
         do i=1,nx
211
           do j=1,ny
212
              do k=1,nz
213
                 z3(i,j,k) = z1(k)
214
              enddo
215
                 zb(i,j)   = z1(1)
216
           enddo
217
         enddo
218
         end if
219
         end if
220
 
221
      endif
222
 
223
 
224
c     Exit point for subroutine
225
 800  continue
226
      return
227
 
228
c     Exception handling
229
 900  print*,'Cannot retrieve grid dimension from ',fid
230
      stop
231
 901  print*,'Cannot retrieve grid parameters from ',fid
232
      stop
233
 902  print*,'Grid inconsistency detected for ',fid
234
      stop
235
 903  print*,'Problem with level coefficients from ',trim(cstfile)
236
      stop
237
 904  print*,'Cannot read surface height from ',trim(cstfile)
238
      stop
239
 905  print*,'Cannot find time ',time,' on ',fid
240
      stop
241
 906  print*,'Unable to get grid info ',fid
242
      stop
243
 907  print*,'Grid inconsistency'
244
      stop
245
 
246
      end
247
 
248
c     ------------------------------------------------------------
249
c     Read wind information
250
c     ------------------------------------------------------------
251
 
252
      subroutine input_wind (fid,fieldname,field,time,stagz,mdv,
253
     >                       xmin,xmax,ymin,ymax,dx,dy,nx,ny,nz,
254
     >                       timecheck)
255
 
256
c     Read the wind component <fieldname> from the file with identifier
257
c     <fid> and save it in the 3d array <field>. The vertical staggering 
258
c     information is provided in <stagz> and gives the reference to either
259
c     the layer or level field from <input_grid>. A consistency check is
260
c     performed to have an agreement with the grid specified by <xmin,xmax,
261
c     ymin,ymax,dx,dy,nx,ny,nz>.
262
 
263
      implicit none
264
 
265
c     Declaration of variables and parameters
266
      integer      fid                 ! File identifier
267
      character*80 fieldname           ! Name of the wind field
268
      integer      nx,ny,nz            ! Dimension of fields
269
      real         field(nx,ny,nz)     ! 3d wind field
270
      real         stagz               ! Staggering in the z direction
271
      real         mdv                 ! Missing data flag
272
      real         xmin,xmax,ymin,ymax ! Domain size
273
      real         dx,dy               ! Horizontal resolution
274
      real         time                ! Time
275
      character*80 timecheck           ! Either 'yes' or 'no'
276
 
277
c     Numerical and physical parameters
278
      real        eps                 ! Numerical epsilon
279
      parameter  (eps=0.001)
280
      real        notimecheck         ! 'Flag' for no time check
281
      parameter  (notimecheck=7.26537)
282
 
283
c     Netcdf variables
284
      integer      ierr
285
      character*80 varname
286
      integer      vardim(4)
287
      real         varmin(4),varmax(4)
288
      real         stag(4)
289
      integer      ndim
290
      real         times(10)
291
      integer      ntimes
292
      character*80 cstfile
293
      integer      cstid
294
      real         aklay(200),bklay(200),aklev(200),bklev(200)
295
      real         ps(nx,ny)
296
 
297
c     Auxiliary variables
298
      integer      isok
299
      integer      i,j,k
300
      integer      nz1
301
      real         rtime
302
 
303
c     Read variable definition - for P, PLEV and PLAY: load also ak,bk
304
      varname = fieldname
305
      call getdef(fid,varname,ndim,mdv,vardim,
306
     >               varmin,varmax,stag,ierr)
307
      if (ierr.ne.0) goto 900
308
      stagz=stag(3)
309
 
310
 
311
c     Get time information (set time to first one in the file)
312
      call gettimes(fid,times,ntimes,ierr)
313
      if (ierr.ne.0) goto 902
314
      isok=0
315
      do i=1,ntimes
316
         if (abs(time-times(i)).lt.eps) then
317
            isok = 1
318
            rtime = times(i)
319
         elseif (timecheck.eq.'no') then
320
            isok = 1
321
            rtime = times(1)
322
         endif
323
      enddo
324
      if ( isok.eq.0 ) goto 904
325
 
326
C     Read in variables, no de-staggering required                                           
327
         varname=fieldname
328
         call getdat(fid,varname,rtime,0,field,ierr)
329
         if (ierr.ne.0) goto 903
330
 
331
 
332
c     If the field is 2D, expand it to 3D - simple handling of 2D tracing
333
      if ( vardim(3).eq.1 ) then
334
         do i=1,nx
335
            do j=1,ny
336
               do k=1,nz
337
                  field(i,j,k) = field(i,j,1)
338
               enddo
339
            enddo
340
         enddo
341
      endif
342
 
343
 
344
 
345
c     Exception handling
346
      return
347
 
348
 900  print*,'Cannot retrieve definition for ',trim(varname),'  ',fid
349
      stop
350
 901  print*,'Grid inconsistency detected for ',trim(varname),'  ',fid
351
      stop
352
 902  print*,'Cannot retrieve time for ',trim(varname),'  ',fid
353
      stop
354
 903  print*,'Cannot load wind component ',trim(varname),'  ',fid
355
      stop
356
 904  print*,'Cannot load time ',time,' for ',trim(varname),'  ',fid
357
      stop
358
 905  print*,'Cannot load time vertical grid AK, BK from file  ',fid
359
      stop
360
 
361
      end
362
 
363
c     ------------------------------------------------------------
364
c     Close input file
365
c     ------------------------------------------------------------
366
 
367
      subroutine input_close(fid)
368
 
369
c     Close the input file with file identifier <fid>.
370
 
371
      implicit none
372
 
373
c     Declaration of subroutine parameters
374
      integer fid
375
 
376
c     Auxiliary variables
377
      integer ierr
378
 
379
c     Close file
380
      call clscdf(fid,ierr)
381
 
382
      end