File: oceanp.F

package info (click to toggle)
emoslib 000380%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 47,712 kB
  • ctags: 11,551
  • sloc: fortran: 89,643; ansic: 24,200; makefile: 370; sh: 355
file content (537 lines) | stat: -rwxr-xr-x 14,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
C Copyright 1981-2007 ECMWF
C 
C Licensed under the GNU Lesser General Public License which
C incorporates the terms and conditions of version 3 of the GNU
C General Public License.
C See LICENSE and gpl-3.0.txt for details.
C

      INTEGER FUNCTION OCEANP( INOCEAN, INLEN, OTOCEAN, OUTLEN)
C
C---->
C**** OCEANP
C
C     Purpose
C     -------
C
C     Interpolate GRIB format input ocean field to GRIB format ocean
C     field.
C
C
C     Interface
C     ---------
C
C     IRET = OCEANP( INOCEAN, INLEN, OTOCEAN, OUTLEN)
C
C     Input
C     -----
C
C     INOCEAN - Input ocean field  (GRIB format).
C     INLEN   - Input field length (words).
C
C
C     Output
C     ------
C
C     OTOCEAN - Output ocean field  (GRIB format).
C     OUTLEN  - Output field length (words).
C
C
C     Method
C     ------
C
C     Interpolate ocean field.
C
C     If requested X grid increment does not match input GRIB
C     increment, do a full interpolation in both x and y. Otherwise,
C     interpolate only to make grid regular.
C
C     Externals
C     ---------
C
C     JDEBUG  - Tests if debug output required.
C     GRIBEX  - GRIB decoding/encoding.
C     INTLOG  - Log error message.
C     JMEMHAN - Allocate/deallocate scratch memory.
C     INTOCN  - Carry out ocean field to field interpolation.
C     IGSETUP - Setup GRIB sections for the output product.
C
C
C     Author
C     ------
C
C     J.D.Chambers     ECMWF     May 1996
C
C     J.D.Chambers     ECMWF        Feb 1997
C     Allow for 64-bit pointers
C
C     J.D.Chambers     ECMWF        Mar 1997
C     Allow for full or partial interpolation
C     (F or R option in call to INTOCN).
C
C----<
C
      IMPLICIT NONE
C
C     Function arguments
C
      INTEGER INOCEAN, INLEN, OTOCEAN, OUTLEN
C
#include "parim.h"
#include "nifld.common"
#include "nofld.common"
#include "grspace.h"
#include "intf.h"
C
C     Parameters
C
      INTEGER JPROUTINE, JPALLOC, JPDEALL, JPSCR3, JPSCR4
      INTEGER JP_LAT, JP_LONG, JP_GUESS
      PARAMETER (JPROUTINE = 36900 )
      PARAMETER (JPALLOC = 1) 
      PARAMETER (JPDEALL = 0) 
      PARAMETER (JPSCR3 = 3) 
      PARAMETER (JPSCR4 = 4) 
      PARAMETER (JP_LONG = 3) 
      PARAMETER (JP_LAT  = 4) 
      PARAMETER (JP_GUESS  = 18049691) 
cs    PARAMETER (JP_GUESS  = 1675245) 
cs    PARAMETER (JP_GUESS  = 1237104) 
cs    PARAMETER (JP_GUESS  = 1038240) 
C                          `---> allow for 0.25*0.25 interpolated grid
C
C     Local variables
C
      LOGICAL LHORIZN, LFLIP
      CHARACTER*1 HFUNC
      CHARACTER*1 HINTOPT
      CHARACTER*6 YFLAG
      INTEGER IOLDN, IOLDW, IOLDS, IOLDE, IOLDEW, IOLDNS
      INTEGER LOOPLAT, LOOPLON, I, J , IOS,LOOP
      REAL ZTEMP
      INTEGER IERR, KPR, IWORD, IOLDSIZ, INEWSIZ, IINTPOL, ISTAGP
      INTEGER NXP, NYP, INEWP, ITEMP
      REAL RTOP, RBOTT, RRIGHT, RLEFT, RYRES, RXRES
      REAL XARR, YARR
      DIMENSION XARR(JPGRIB_ISEC1), YARR(JPGRIB_ISEC1)
#ifdef POINTER_64
      INTEGER*8 INEW, IOLD
#endif
      REAL NEW, OLD
      POINTER ( INEW, NEW )
      POINTER ( IOLD, OLD )
      DIMENSION NEW( 1 ), OLD( 1 )
C
C     Externals
C
      INTEGER RESET_C, IGSETUP, INTOCN, FIXAREA
C
C     -----------------------------------------------------------------|
C*    Section 1.   Initialise
C     -----------------------------------------------------------------|
C
  100 CONTINUE
C
C     Test if debug output required.
C
      CALL JDEBUG()
C
      CALL INTLOG(JP_DEBUG,
     X  'OCEANP: Trying to interpolate an ocean field.',JPQUIET)
C
      IF( .NOT.LNOGRID ) THEN
        CALL INTLOG(JP_FATAL,
     X    'OCEANP: GRID must be specified to interpolate an ocean field'
     X    , JPQUIET)
        OCEANP = JPROUTINE + 1
        GOTO 960
      ENDIF
C
      OCEANP = 0
      IERR   = 0
      KPR    = 0
      LFLIP = .FALSE.
C
C     Save the original area and grid definitions
C
      IOLDN  = NOAREA(1)
      IOLDW  = NOAREA(2)
      IOLDS  = NOAREA(3)
      IOLDE  = NOAREA(4)
      IOLDEW = NOGRID(1)
      IOLDNS = NOGRID(2)
C
C     -----------------------------------------------------------------|
C*    Section 2.   Unpack the input ocean field.
C     -----------------------------------------------------------------|
C
  200 CONTINUE
C
C     Decode first four sections of GRIB code to find number of values
C
      IWORD   = INLEN
      IERR    = 1
      IOLDSIZ = OUTLEN
      CALL GRIBEX(ISEC0, ISEC1, ISEC2, ZSEC2, ISEC3, ZSEC3, ISEC4,
     X            OLD, IOLDSIZ, INOCEAN, INLEN, IWORD, 'I',IERR)
C
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR,
     X    'OCEANP: GRIBEX option J decoding failed.',IERR)
        OCEANP = IERR
        GOTO 950
      ENDIF
C
C     Check it is an ocean field
C
      IF( (ISEC1(24).NE.1).OR.(ISEC1(37).NE.4) ) THEN
        CALL INTLOG(JP_ERROR, 'OCEANP: Not an ocean field.',JPQUIET)
        OCEANP = JPROUTINE + 3
        GOTO 950
      ENDIF
C
C     Horizontal (lat/long) field GRIB setup different from others
C
      LHORIZN = (ISEC1(60).EQ.JP_LONG).AND.(ISEC1(61).EQ.JP_LAT)
      IF( LHORIZN ) CALL INTLOG(JP_DEBUG,
     X  'OCEANP: Horizontal (lat/long) field interpolation.',JPQUIET)
C
C     Get scratch memory for unpacking the input ocean field.
C
      IOLDSIZ = ISEC2(2)*ISEC2(3)
      CALL JMEMHAN( JPSCR4, IOLD, IOLDSIZ, JPALLOC, IERR)
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR,
     X    'OCEANP: Scratch memory(4) allocation failed.',JPQUIET)
        OCEANP = IERR
        GOTO 950
      ENDIF
C
C     Decode data from GRIB code: -999.9 is a missing data value
C
      IWORD = INLEN
      IERR  = 1
      ZSEC3(2) = -999.9
      CALL GRIBEX(ISEC0, ISEC1, ISEC2, ZSEC2, ISEC3, ZSEC3, ISEC4,
     X            OLD, IOLDSIZ, INOCEAN, INLEN, IWORD, 'D',IERR)
C
      IF( (IERR.NE.0).AND.(IERR.NE.-2).AND.(IERR.NE.-4) ) THEN
        CALL INTLOG(JP_ERROR,'OCEANP: GRIBEX decoding failed.',IERR)
        OCEANP = IERR
        GOTO 940
      ENDIF
C
C     -----------------------------------------------------------------|
C*    Section 3.   Prepare the output ocean field.
C     -----------------------------------------------------------------|
C
  300 CONTINUE
C
C     Setup interpolation options from input GRIB characteristics.
C
      IERR = RESET_C(ISEC1, ISEC2, ZSEC2, ISEC4)
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR,
     X    'OCEANP: Setup interp. options from GRIB failed.',JPQUIET)
        OCEANP = IERR
        GOTO 940
      ENDIF
C
C     For horizontal field, match the desired area to the grid
C
      IERR = FIXAREA()
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR, 'OCEANP: FIXAREA failed.',JPQUIET)
        OCEANP = IERR
        GOTO 940
      ENDIF
C
C     Get scratch memory for output ocean field.
C     Have to guess how big the interpolated grid will be.
C
      INEWSIZ = JP_GUESS
      CALL JMEMHAN(JPSCR3, INEW, INEWSIZ*2, JPALLOC, IERR)
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR,
     X    'OCEANP: Scratch memory(3) allocation failed.',JPQUIET)
        OCEANP = IERR
        GOTO 940
      ENDIF
      INEWP = 1
      ITEMP = 1 + INEWSIZ
C
C     -----------------------------------------------------------------|
C*    Section 4.   Interpolate ocean field.
C     -----------------------------------------------------------------|
C
  400 CONTINUE
C
      RTOP   = FLOAT( NOAREA(1) ) / PPMULT
      RBOTT  = FLOAT( NOAREA(3) ) / PPMULT
      RRIGHT = FLOAT( NOAREA(4) ) / PPMULT
      RLEFT  = FLOAT( NOAREA(2) ) / PPMULT
C
      RYRES = FLOAT( NOGRID(2) ) / PPMULT
      RXRES = FLOAT( NOGRID(1) ) / PPMULT
C
C     Select the interpolation option
C     (default = 'R' = interpolate only to make grid regular)
C     If requested X grid increment does not match input GRIB
C     increment, interpolate in x and y ('F' = full)
C
      HINTOPT = 'R'
      IF( NOGRID(1).NE.ISEC2(9) ) HINTOPT = 'F'
C
      IERR = INTOCN(HINTOPT,'D',
     X              RTOP, RBOTT, RRIGHT, RLEFT, RYRES, RXRES,
     X              ISEC1, ISEC2, OLD, IOLDSIZ,
     X              INEWSIZ, NEW(ITEMP), XARR, YARR, NXP, NYP,
     X              NEW(INEWP),
     X              IINTPOL, ISTAGP)
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR, 'OCEANP: Interpolation failed.',JPQUIET)
        OCEANP = IERR
        GOTO 900
      ENDIF
C
      NOAREA(1) = NINT( (YARR(1) + YARR(2)*(NYP-1)) * PPMULT )
      NOAREA(2) = NINT( XARR(1) * PPMULT )
      NOAREA(3) = NINT( YARR(1) * PPMULT )
      NOAREA(4) = NINT( (XARR(1) + XARR(2)*(NXP-1)) * PPMULT )
C
      NOGRID(1) = NINT( ABS(XARR(2)) * PPMULT )
      NOGRID(2) = NINT( ABS(YARR(2)) * PPMULT )
      NONS = NYP
      NOWE = NXP
C
C     -----------------------------------------------------------------|
C*    Section 5.   Code new field into GRIB.
C     -----------------------------------------------------------------|
C
  500 CONTINUE
C
C     Re-order the rows from south to north to be north to south
C     (ECMWF convention).
C
      IF( LHORIZN.OR.(NOAREA(1).LT.NOAREA(3)) ) THEN
        LFLIP = .TRUE.
        DO LOOPLAT = 1, NYP/2
          I = (LOOPLAT - 1)*NXP
          J = (NYP - LOOPLAT)*NXP
          DO LOOPLON = 1, NXP
            I = I + 1
            J = J + 1
            ZTEMP  = NEW(I)
            NEW(I) = NEW(J)
            NEW(J) = ZTEMP
          ENDDO
        ENDDO
C
        IF( .NOT.LHORIZN ) THEN
          ITEMP     = NOAREA(1)
          NOAREA(1) = NOAREA(3)
          NOAREA(3) = ITEMP
C
          ITEMP    = ISEC2(4)
          ISEC2(4) = ISEC2(7)
          ISEC2(7) = ITEMP
        ENDIF
      ENDIF
C
C     Setup GRIB sections for the output product:
C
C     Use local definition 4 in GRIB section 1 ...
C
      ISEC1(73) = 0
      ITEMP = 75 + ISEC1(71) + ISEC1(72) + ISEC1(73) + ISEC1(74)
      ISEC1(ITEMP) = 0

C
      IF( LHORIZN ) THEN
        ISEC1(62) = NOAREA(1)*10
        ISEC1(63) = NOAREA(2)*10
        ISEC1(64) = NOAREA(3)*10
        ISEC1(65) = NOAREA(4)*10
        ISEC1(66) = NOGRID(1)*10
        ISEC1(67) = NOGRID(2)*10
        IF( LFLIP ) ISEC1(67) = - ISEC1(67)
        ISEC1(68) = 0
        ISEC1(69) = 0
C
C       Standard section 2 format for horizontal sections
C
        ISEC2(1) = 0
        ISEC2(2) = NXP
        ISEC2(3) = NYP
        ISEC2(4) = NOAREA(1)/100
        ISEC2(5) = NOAREA(2)/100
        ISEC2(6) = 128
        ISEC2(7) = NOAREA(3)/100
        ISEC2(8) = NOAREA(4)/100
        ISEC2(9)  = NOGRID(1)/100
        ISEC2(10) = NOGRID(2)/100
Cjdc    ISEC2(17) = 0
        DO LOOPLAT = 11, 22
          ISEC2(LOOPLAT) = 0
        ENDDO
      ELSE
C Tim-Correct headers for vertical sections
        IF( ISEC1(60).EQ.3 ) THEN
          ISEC1(63) = NOAREA(2)*10
        ELSEIF( ISEC1(60).EQ.4 ) THEN
          ISEC1(63) = NOAREA(2)*10
        ENDIF
        IF( ISEC1(61).EQ.3 ) THEN
          ISEC1(62) = NOAREA(1)*10
        ELSEIF( ISEC1(61).EQ.4 ) THEN
          ISEC1(62) = NOAREA(1)*10
        ENDIF
C
        IF( ISEC1(61).EQ.1 ) THEN
          ISEC1(64) = ISEC1(62) + NINT((NYP-1)*RYRES*1.0)
          ISEC1(65) = ISEC1(63) + NINT((NXP-1)*RXRES*1000000.0)
          ISEC1(66) = NOGRID(1)*NINT(1000000.0/PPMULT)
          ISEC1(67) = NOGRID(2)/(PPMULT/1.0)
        ELSE
          ISEC1(64) = ISEC1(62) + NINT((NYP-1)*RYRES*1000.0)
          ISEC1(65) = ISEC1(63) + NINT((NXP-1)*RXRES*1000000.0)
          ISEC1(66) = NOGRID(1)*NINT(1000000.0/PPMULT)
          ISEC1(67) = NOGRID(2)/(PPMULT/1000.0)
        ENDIF
C
        IF( LFLIP ) ISEC1(67) = - ISEC1(67)
        ISEC1(68) = 0
C
C       Special ECMWF ocean short form of GRIB section 2
C
        ISEC2(1) = 192
        ISEC2(2) = NXP
        ISEC2(3) = NYP
Cjdc    ISEC2(4) = NOAREA(1)/100000
Cjdc    ISEC2(5) = NOAREA(2)/100
Cjdc    ISEC2(6) = 0
Cjdc    ISEC2(7) = NOAREA(3)/100000
Cjdc    ISEC2(8) = NOAREA(4)/100
Cjdc    DO LOOPLAT = 9, 22
Cjdc      ISEC2(LOOPLAT) = 0
Cjdc    ENDDO
      ENDIF
C
C     Ensure there is a section 3 in case there is missing data in the
C     output area
C
      ISEC1(5) = 192
      ISEC3(1) = 0
      ZSEC3(2) = -999.9
C
      ISEC4(1) = NXP *  NYP
C
C     If grid-point output, setup for 2nd order packing if requested.
C
      IF( (NOREPR.NE.JPSPHERE).AND.(NOREPR.NE.JPSPHROT) ) THEN
        HFUNC = 'C'
        IF( NOHFUNC.EQ.'K' ) THEN
          HFUNC = 'K'
          ISEC4(4)  = 64
          ISEC4(6)  = 16
          ISEC4(9)  = 32
          ISEC4(10) = 16
          ISEC4(12) = 8
          ISEC4(13) = 4
          ISEC4(14) = 0
          ISEC4(15) = -1
        ELSE
          ISEC4(4)  = 0
          ISEC4(6)  = 0
        ENDIF
      ELSE
        HFUNC = 'C'
        IF( NOHFUNC.EQ.'C' ) THEN
          ISEC2(6) = 2
          ISEC4(4) = 64
        ELSE IF( NOHFUNC.EQ.'S' ) THEN
          ISEC2(6) = 1
          ISEC4(4) = 0
        ENDIF
      ENDIF
C
C     Switch off GRIBEX checking (sigh)
C
      CALL INTLOG(JP_DEBUG,
     X  'OCEANP: switching OFF GRIBEX checking',JPQUIET)
      CALL GRSVCK(0)
        CALL GETENV('DATA_CHECK', YFLAG)
        IF( YFLAG(1:1).EQ.'1' ) THEN
          OPEN (UNIT=18,FILE='./out_ocean_gribex.dat',STATUS='UNKNOWN',
     &    FORM='UNFORMATTED',IOSTAT=IOS)
          IF (IOS.EQ.0) THEN    
            WRITE (18)(NEW(LOOP),LOOP=1,ISEC4(1))
          ENDIF
		  print*,' a few OUT gribex OCEAN => ',NEW(1),NEW(2)
          CLOSE(18)
        ENDIF
C
      IERR = 1
      CALL GRIBEX( ISEC0, ISEC1, ISEC2, ZSEC2, ISEC3, ZSEC3, ISEC4,
     X             NEW,INEWSIZ,OTOCEAN,OUTLEN,IWORD,HFUNC,IERR)
C
      IF( IERR.NE.0 ) THEN
        CALL INTLOG(JP_ERROR,'OCEANP: GRIBEX encoding failed.',IERR)
        OCEANP = JPROUTINE + 5
        GOTO 900
      ENDIF
      OUTLEN = IWORD
C
C     Switch on GRIBEX checking (sigh again)
C
      CALL INTLOG(JP_DEBUG,
     X  'OCEANP: switching ON GRIBEX checking',JPQUIET)
      CALL GRSVCK(1)
C
C     -----------------------------------------------------------------|
C*    Section 9.   Closedown.
C     -----------------------------------------------------------------|
C
  900 CONTINUE
C
C     Clear change flags for next product processing
C
      LCHANGE = .FALSE.
      LSMCHNG = .FALSE.
C
C     Return the scratch memory.
C
      CALL JMEMHAN( JPSCR3, INEW, INEWSIZ, JPDEALL, IERR)
      IF ( IERR .NE. 0 ) THEN
        CALL INTLOG(JP_ERROR,
     X    'OCEANP: Scratch memory(3) reallocation failed.',JPQUIET)
        OCEANP = IERR
      ENDIF
C
  940 CONTINUE
C
      CALL JMEMHAN( JPSCR4, IOLD, IOLDSIZ, JPDEALL, IERR)
      IF ( IERR .NE. 0 ) THEN
        CALL INTLOG(JP_ERROR,
     X    'OCEANP: Scratch memory(4) reallocation failed.',JPQUIET)
        OCEANP = IERR
      ENDIF
C
  950 CONTINUE
C
C     Restore the original area and grid definitions
C
      NOAREA(1) = IOLDN
      NOAREA(2) = IOLDW
      NOAREA(3) = IOLDS
      NOAREA(4) = IOLDE
      NOGRID(1) = IOLDEW
      NOGRID(2) = IOLDNS
C
  960 CONTINUE
C
      CALL INTLOG(JP_DEBUG,
     X  'OCEANP: Returning from interpolating an ocean field.',JPQUIET)
C
      RETURN
      END