File: jopnllf.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 (390 lines) | stat: -rwxr-xr-x 10,564 bytes parent folder | download | duplicates (2)
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
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

      SUBROUTINE JOPNLLF( IALEG, PINTVL, KTRUNC, KUNIT, PBUILD, KRET)
C
C---->
C**** JOPNLLF
C
C     Purpose
C     _______
C
C     This routine finds a file of legendre polynomials corresponding
C     to a given grid interval and truncation, allocates memory, reads
C     the coefficients into the memory and returns a unit number.
C
C     Interface
C     _________
C
C     CALL JOPNLLF( IALEG, PINTVL, KTRUNC, KUNIT, PBUILD, KRET)
C
C     Input parameters
C     ________________
C
C     PINTVL  - Grid interval in degrees
C     KTRUNC  - Spherical truncation
C
C     Output parameters
C     _________________
C
C     IALEG   - Pointer for memory array for legendre coefficients
C     KUNIT   - Unit number from PBOPEN
C               0 , open failed
C     PBUILD  - Grid interval used to build the legendre 
C               coefficients file (degrees)
C     KRET    - Return status code
C               0 = OK
C
C     Common block usage
C     __________________
C
C     Uses common JDCSPGP for size NISIZE6
C
C     Method
C     ______
C
C     Builds a file name from the truncation and grid interval and
C     tries to open a file of that name.
C
C     If the file is already open (from a previous call), the
C     previous unit number is returned.
C     If a different file is already open (from a previous call), the
C     the existing file is closed.
C
C     If no file can be located, a file is created. Memory is dynamically
C     allocted and the coefficients are read into the memory array.
C
C     Externals
C     _________
C
C     PBOPEN3  - Open binary file and return unit number
C     PBREAD3  - Read binary file
C     PBCLOSE3 - Close binary file
C     JFINDIR  - Find the file of legendre coefficients
C     JMAKLL   - Makes a file of legendre coefficients
C     INTLOG   - Output log message
C     JCHMOD   - Change file permissions
C     RENAME   - Rename file
C
C     Reference
C     _________
C
C     NONE
C
C     Comments
C     ________
C
C     The Legendre polynomials for the transforms may be held in
C     a ready-made file whose name is held in an environment variable:
C         cf_txxxx_raabbbbb       Txxxx   aa.bbbbb degrees
C     For example,
C         cf_t0213_r0050000       T213    0.5 degrees
C         cf_t0106_r0250000       T106    2.5 degrees
C
C     On the C90, the file of polynomials may be cached in /owrk/marsint
C
C     Otherwise the file is located in (or will be created in) the first
C     directory given by one of the following (in the order listed, if
C     they exist):
C         environment variable PPDIR
C     or
C         the current working directory.
C
C     Author
C     ______
C
C     J.D.Chambers      *ECMWF*      Jun 1996
C
C     Modifications
C     _____________
C
C     J.D.Chambers     ECMWF        Feb 1997
C     Allow for 64-bit pointers
C
C----<
C     _______________________________________________________
C
      IMPLICIT NONE
C
#include "parim.h"
#include "jparams.h"
#include "jparam2.h"
C
C     Parameters
C
      INTEGER JPROUTINE
      PARAMETER ( JPROUTINE = 30910 )
C
C     Subroutine arguments
C
      REAL      PINTVL, PBUILD
      INTEGER   KTRUNC, KUNIT, KRET
C
C     Local variables
C
      INTEGER IRET, IOFFSET, ISIZE, NPBYTES
#ifdef REAL_8
      DATA NPBYTES/8/
#else
      DATA NPBYTES/4/
#endif
      LOGICAL LFOUND
      CHARACTER*256 DIRNAME
      CHARACTER*256 FILENAME, FILEDUM
      CHARACTER*512 FULLPATH
      CHARACTER*20 YPFN
#ifdef REAL_8
      DATA YPFN/'CF_Txxxx_Raabbbbb'/
#else
      DATA YPFN/'cf_txxxx_raabbbbb'/
#endif
      INTEGER NUNIT
      DATA NUNIT/0/
#ifdef POINTER_64
      INTEGER*8 IALEG
#endif
      REAL ALEG
      DIMENSION ALEG(1)
      POINTER ( IALEG, ALEG )
C
      SAVE NUNIT
C
C     Externals
C
      LOGICAL JFINDIR
      LOGICAL JFINDFN3
      INTEGER JCHMOD, RENAME
C
C     _______________________________________________________
C
C*    Section 1. See if required file already in use.
C     _______________________________________________________
C
  100 CONTINUE
C
C     Setup the filename: cf_txxxx_raabbbbb
C
      WRITE(YPFN(5:8),'(I4.4)') KTRUNC
      WRITE(YPFN(11:17),'(I7.7)') NINT(PINTVL*JPMULT)
      CALL INTLOG(JP_DEBUG,
     X  'JOPNLLF: Coefficients file to open is:', JPQUIET)
      CALL INTLOG(JP_DEBUG, YPFN, JPQUIET)
      PBUILD = PINTVL
C
C     If file already open, return the existing unit number
C
      IF( YPFN.EQ.YOLDLL ) THEN
        CALL INTLOG(JP_DEBUG,'JOPNLLF: File already open.',JPQUIET)
        KUNIT = NUNIT
        GOTO 900
      ENDIF
C
C     Otherwise, ensure existing unit is closed
C
      IF( NUNIT.NE.0 ) THEN
        CALL PBCLOSE3(NUNIT, IRET)
        IF( IRET.NE.0 ) THEN
          CALL INTLOG(JP_ERROR,'JOPNLLF: PBCLOSE3 error',IRET)
          KRET = JPROUTINE + 1
          GOTO 990
        ENDIF
        NUNIT = 0
      ENDIF
C     _______________________________________________________
C
C*    Section 2. See if the file has already been created.
C     _______________________________________________________
C
  200 CONTINUE
C
C     Set appropriate build inteval 
C
      WRITE(YPFN(11:17),'(I7.7)') NINT(PBUILD*JPMULT)
C
      IOFFSET = INDEX(YPFN,' ') - 1
      FILENAME = YPFN(1:IOFFSET)
C
      LFOUND = JFINDIR('PPDIR', DIRNAME)
      IF( LFOUND ) THEN
        LFOUND = JFINDFN3(DIRNAME,FILENAME,IOFFSET,NUNIT)
        IF( LFOUND ) GOTO 500
      ENDIF
C
C     Try present working directory
C
      IOFFSET = INDEX(YPFN, ' ') - 1
      FILENAME = YPFN(1:IOFFSET)
      CALL PBOPEN3( NUNIT, FILENAME(1:IOFFSET), 'r', IRET)
      IF( IRET.EQ.0 ) GOTO 500
C     _______________________________________________________
C
C*    Section 3. File doesn't exist, find a suitable directory for it.
C     _______________________________________________________
C
  300 CONTINUE
C
      LFOUND = JFINDIR('PPDIR', DIRNAME)
      IF ( LFOUND ) THEN
        IOFFSET = INDEX(DIRNAME, ' ') - 1
        FULLPATH = DIRNAME(1:IOFFSET) // '/' // FILENAME
        IOFFSET = INDEX(FULLPATH, ' ') - 1
        FILENAME(1:IOFFSET) = FULLPATH(1:IOFFSET)
        FILEDUM(1:IOFFSET) = FILENAME(1:IOFFSET)
#ifdef REAL_8
        FILEDUM(IOFFSET-16:IOFFSET-15) = 'XX'
#else
        FILEDUM(IOFFSET-16:IOFFSET-15) = 'xx'
#endif
        CALL PBOPEN3(NUNIT, FILEDUM(1:IOFFSET), 'w', IRET)
        IF( IRET.EQ.0 ) GOTO 400
      ENDIF
C
C     Try present working directory
C
      IOFFSET = INDEX(YPFN,' ') - 1
      FILENAME = YPFN(1:IOFFSET)
      FILEDUM(1:IOFFSET) = FILENAME(1:IOFFSET)
#ifdef REAL_8
      FILEDUM(IOFFSET-16:IOFFSET-15) = 'XX'
#else
      FILEDUM(IOFFSET-16:IOFFSET-15) = 'xx'
#endif
      CALL PBOPEN3(NUNIT, FILEDUM(1:IOFFSET), 'w', IRET)
      IF( IRET.NE.0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: PBOPEN3 error.',IRET)
        KRET = JPROUTINE + 2
        GOTO 990
      ENDIF
C     _______________________________________________________
C
C*    Section 4. Create the coefficients file.
C     _______________________________________________________
C
  400 CONTINUE
C
C     Let user know that a new file is being created.
C
      IOFFSET = INDEX(FILENAME, ' ') - 1
      CALL INTLOG(JP_DEBUG,'JOPNLLF: Creating new coefficients file:',
     X            JPQUIET)
      CALL INTLOG(JP_DEBUG,FILENAME(1:IOFFSET), JPQUIET)
C
C     Change access mode to 'read only' for all users.
C
      IRET = JCHMOD(FILEDUM(1:IOFFSET),'0444')
      IF( IRET.NE.0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: JCHMOD error.',IRET)
        KRET = JPROUTINE + 3
        GOTO 990
      ENDIF
C
C     Allocate memory for the array used in making the file
C
      ISIZE = (KTRUNC+1)*(KTRUNC+4)*(NINT(90.0/PBUILD)+1)/2
      IF( ISIZE.GT.NISIZE6 ) THEN
        CALL JMEMHAN( 6, IALEG, ISIZE, 1, KRET)
        IF( KRET.NE.0 ) THEN
          CALL INTLOG(JP_FATAL,'JOPNLLF: memory allocation error',IALEG)
          KRET = JPROUTINE + 3
          GOTO 990
        ENDIF
        NISIZE6 = ISIZE
      ENDIF
C
C     Make coefficients file
C
      CALL JMAKLL3( NUNIT, KTRUNC, PBUILD, 0.0, ALEG, KRET)
      IF ( KRET .NE. 0 ) GOTO 990
C
C     De-allocate memory for the array used in making the file
C
      CALL JMEMHAN( 6, IALEG, ISIZE, 0, KRET)
      IF( KRET.NE.0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: memory dealloc error',IALEG)
        KRET = JPROUTINE + 4
        GOTO 990
      ENDIF
      NISIZE6 = 0
C
C     Close rename it, re-open for reading, leave it open.
C
      CALL PBCLOSE3( NUNIT, IRET)
      IF( IRET.NE.0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: PBCLOSE3 error.',IRET)
        KRET = JPROUTINE + 5
        GOTO 990
      ENDIF
      NUNIT=0
C
      IRET = RENAME(FILEDUM(1:IOFFSET),FILENAME(1:IOFFSET))
      IF( IRET.NE.0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: RENAME of file failed',JPQUIET)
        KRET = JPROUTINE + 5
        GOTO 990
      ENDIF
C
      CALL PBOPEN3( NUNIT, FILENAME(1:IOFFSET), 'r', IRET)
      IF( IRET.NE.0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: PBOPEN3 error.',IRET)
        KRET = JPROUTINE + 6
        GOTO 990
      ENDIF
C     _______________________________________________________
C
C*    Section 5. File now open with read access.
C     _______________________________________________________
C
  500 CONTINUE
C
C     Allocate memory for the file and read it into memory
C
      ISIZE = (KTRUNC+1)*(KTRUNC+4)*(NINT(90.0/PBUILD)+1)/2
      IF( ISIZE.GT.NISIZE6 ) THEN
        CALL JMEMHAN( 6, IALEG, ISIZE, 1, KRET)
        IF( KRET.NE.0 ) THEN
          CALL INTLOG(JP_FATAL,'JOPNLLF: memory allocation error',IALEG)
          KRET = JPROUTINE + 7
          GOTO 990
        ENDIF
        NISIZE6 = ISIZE
      ENDIF
C
      CALL PBREAD3(NUNIT, ALEG, (ISIZE*NPBYTES), IRET)
      IF( IRET.NE.(ISIZE*NPBYTES) ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: PBREAD3 error.',IRET)
        KRET = JPROUTINE + 8
        GOTO 990
      ENDIF
C
C     The file is no longer required
C
      CALL PBCLOSE3( NUNIT, IRET)
      IF ( IRET .NE. 0 ) THEN
        CALL INTLOG(JP_FATAL,'JOPNLLF: PBCLOSE3 error.',IRET)
        NUNIT = 0
        KRET = JPROUTINE + 9
        GOTO 990
      ENDIF
      NUNIT=0
C
      KUNIT = NUNIT
      YOLDLL  = YPFN
C
C     _______________________________________________________
C
C*    Section 9. Return to calling routine. Format statements
C     _______________________________________________________
C
  900 CONTINUE
C
      KRET = 0
C
 990  CONTINUE
C
      RETURN
      END