File: jopnllsm.c

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 (539 lines) | stat: -rwxr-xr-x 11,717 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
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
538
539
/**
* Copyright 1981-2007 ECMWF
* 
* Licensed under the GNU Lesser General Public License which
* incorporates the terms and conditions of version 3 of the GNU
* General Public License.
* See LICENSE and gpl-3.0.txt for details.
*/

#ifdef linux
#ifndef __USE_LARGEFILE64
#define __USE_LARGEFILE64
#endif
#include <sys/stat.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include "fortint.h"



#ifndef CRAY
#ifdef FORTRAN_NO_UNDERSCORE
#define JOPNLLSM jopnllsm
#define JMAKLL jmakll
#define INTLOGT intlogt
#define PBOPEN pbopen
#define PBCLOSE pbclose
#else
#define JOPNLLSM jopnllsm_
#define JMAKLL jmakll_
#define INTLOGT intlogt_
#define PBOPEN pbopen_
#define PBCLOSE pbclose_
#endif
#endif

/*
//#ifdef POINTER_64
//#define pointer long
//#else
//#define pointer fortint
//#endif
*/

#ifdef linux
# if !defined __off64_t_defined
typedef __off64_t off64_t;
#define __off64_t_defined
#ifdef FOPEN64
void *mmap64(void *__addr, size_t __len, int __prot, int __flags, int __fd, __off64_t __offset);
#endif
#endif
#endif 

#ifdef FOPEN64
#define OFF_T off64_t
extern OFF_T ftello64(FILE *);
#else
#define OFF_T off_t
#endif

int iitimer(int *seconds)
{
   time_t timval1;
	  time(&timval1);

		 if (*seconds == 0) return (int)timval1;

			return (int)(timval1 - (time_t)*seconds);
}

void INTLOGT(char * message, long length);
FILE * pbfp(long);

#ifdef REAL_8
void JOPNLLSM(
  JPointer* ipdum,
  double* platinc,
  fortint* iktrunc,
  fortint* kunit,
  double* zbuild,
  fortint* kret)
#else
void JOPNLLSM(
  JPointer* ipdum,
  float* platinc,
  fortint* iktrunc,
  fortint* kunit,
  float* zbuild,
  fortint* kret)
#endif
/*
C
C**** JOPNLLSM
C
C     Purpose
C     _______
C
C     This routine finds a file of legendre polynomials corresponding
C     to a given grid interval and truncation and maps the file to an
C     array and returns a file descriptor.
C
C     Interface
C     _________
C
C     CALL JOPNLLSM( IPDUM, PLATINC, KTRUNC, KUNIT, ZBUILD, KRET)
C
C     Input parameters
C     ________________
C
C     IPDUM   - Dummy array for mapping legendre function file
C     PLATINC - Grid interval in degrees
C     KTRUNC  - Spherical truncation
C
C     Output parameters
C     _________________
C
C     KUNIT   - file descriptor from PBOPEN
C               NULL , open failed
C     ZBUILD  - Grid interval of the lat/long grid
C               underlying the legendre function file
C     KRET    - Return status, 0 = OK.
C
C     Common block usage
C     __________________
C
C     None
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     existing file is closed first.
C
C     If no file can be located, a file is created.
C
C     Externals
C     _________
C
C     GETENV     - Get value of an environment variable
C     JMAKLL     - Makes a file of legendre coefficients
C     PBOPEN     - Opens a file
C     PBCLOSE    - Closes a file
C
C     Reference
C     _________
C
C     NONE
C
C     Comments
C     ________
C
C     The filename for the legendre polynomials has the form:
C         cf_txxxx_raabbbb       Txxxx   aa.bbbb 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*      Feb 1996
C
C     Modifications
C     _____________
C
C     J.D.Chambers      *ECMWF*      Mar 1996
C     Standardise the search order for the environment variables.
C
C     J.D.Chambers      *ECMWF*      Sept 1999
C     Use PB routines for OPEN and CLOSE (use index into table of FILE *)
C
*/
{
  int time1, time2;
#if (defined hpR64) || (defined hpiaR64)
long l1, l2;
#else
fortint l1, l2;
#endif
char filedum[128], filename[128];
#ifdef REAL_8
char ypfn[18] =        "CF_Txxxx_Raabbbb";
#else
char ypfn[18] =        "cf_txxxx_raabbbb";
#endif
static char yold[18] = "xxxxxxxxxxxxxxxx";
static char yarray[12][18]; 
static int ncnt = 0;
static int nunit = -1;
int i;
OFF_T len, len_pleg, knum;
static OFF_T oldlen;
int kbuild ;
char * fn;
static fortint fpindex;
static FILE * fp;
OFF_T off = 0;
static void * result = 0;
static void * oldresult = 0;
static void * resultarray[12];
static int unitarray[12];
#ifdef REAL_8
double plat = 0.0;
double * pleg;
#else
float plat = 0.0;
float * pleg;
#endif
int exist = 0;
char message[] = "JOPNLLSM: creating coefficients file";
int status;
pid_t process_id;
int ktrunc = (int) (*iktrunc);

/*
//  Setup the filename: cf_txxxx_raabbbbb
*/
#ifdef REAL_8
  len_pleg =  sizeof(double) * (ktrunc+1) * (ktrunc+4) /2;
#else
  len_pleg =  sizeof(float) * (ktrunc+1) * (ktrunc+4) /2;
#endif

  *zbuild = *platinc;

  knum = (OFF_T) ( (90.0/(*zbuild)) + 0.5) + 1;
#ifdef REAL_8
  len =  (OFF_T)(knum * sizeof(double) * (ktrunc+1) * (ktrunc+4)/2);
#else
  len =  (OFF_T)(knum * sizeof(float) * (ktrunc+1) * (ktrunc+4)/2);
#endif

/*
printf("################# len: %lld, knum: %lld, sizeof(double): %d, ktrunc+1: %d, ktrunc+4: %d\n",len,knum,sizeof(double),(ktrunc+1),(ktrunc+4));
*/

/*
//  Setup the file name
*/
  sprintf( ypfn+4, "%04d", ktrunc);
  kbuild = (int) (((*zbuild)*100000.0) + 0.5);
#ifdef REAL_8
  sprintf( ypfn+8, "_R%07d", kbuild);
#else
  sprintf( ypfn+8, "_r%07d", kbuild);
#endif

/*
//  If file already open, return the existing unit number
*/
  for(i=0;i<ncnt;i++) {
    strncpy(yold,yarray[i],17);
    strcat(yold,"\0");
    if( !strcmp( ypfn, yold ) ) {
      *kunit = (fortint) unitarray[i];
      *ipdum = (JPointer) resultarray[i];
      return;
    }
  }

/*
//  Otherwise, unmap and close the existing file
*/
  result = 0;
/*                
  if( nunit != -1 ) {
    if( munmap((caddr_t) oldresult, oldlen) ) {
      perror("JOPNLLSM: munmap error");
      *kret = (fortint) 999;
      return;
    }
    PBCLOSE(&fpindex,kret);
    if( *kret != 0 ) {
      perror("JOPNLLSM: PBCLOSE error");
      *kret = (fortint) 999;
      return;
    }

  }
*/

/*
  If more than 12 files are memory mapped , error
*/
  if(ncnt==13) {
    fprintf(stderr,"JOPNLLSM: attempt to memory map more than 12 files\n");
    *kret = (fortint) 999;
    return;
  }

/*
//  See if the  file has already been created.
*/
  fn = getenv("PPDIR");
  if( (fn != NULL) && (strlen(fn) != 0) ) {
    strcpy( filename, fn );
    strcat( filename, "/");
    strcat( filename, ypfn);
    l1 = strlen(filename);
    l2 = 1;
    PBOPEN(&fpindex,filename,"r",kret,l1,l2);
    if( *kret == 0 ) {
      fp = pbfp((long)fpindex);
      if( fp != NULL ) exist = 1;
    }
  }

  if( !exist ) {
    strcpy( filename, "./" );
    strcat( filename, ypfn);
    l1 = strlen(filename);
    l2 = 1;
    PBOPEN(&fpindex,filename,"r",kret,l1,l2);
    if( *kret == 0 ) {
      fp = pbfp((long)fpindex);
      if( fp != NULL ) exist = 1;
    }
  }

/*
//  If file doesn't exist, find a suitable directory for it.
*/
  if( !exist ) {
    fn = getenv( "PPDIR" );
    if( (fn != NULL) && (strlen(fn) != 0) ) {
      strcpy( filename, fn );
      strcat( filename, "/");
    }
    else
      strcpy( filename, "./" );

    strcat( filename, ypfn);

/*
//  Open it with write access, change mode to 'read only', and make it
*/
    INTLOGT(message,strlen(message));
    INTLOGT(filename,strlen(filename));

    process_id = getpid();

    strcpy(filedum, filename);
    sprintf(&filedum[strlen(filename)],"_%07d",process_id);

    l1 = strlen(filedum);
    l2 = 1;
    PBOPEN(&fpindex,filedum,"w",kret,l1,l2);
    if( (*kret) || ( fp = pbfp((long)fpindex) ) == NULL ) {
      strcpy( filename, "./" );
      strcat( filename, ypfn);
      strcpy(filedum, filename);
      sprintf(&filedum[strlen(filename)],"_%07d",process_id);
      l1 = strlen(filedum);
      l2 = 1;
      PBOPEN(&fpindex,filedum,"w",kret,l1,l2);
      if( *kret ) {
        perror("JOPNLLSM: PBOPEN error");
        return;
      }
      fp = pbfp((long)fpindex);
      if( fp == NULL ) {
        perror("JOPNLLSM: file pointer after PBOPEN is NULL");
        *kret = (fortint) 998;
        return;
      }
    }

    status = chmod( filedum, (mode_t) 0444 );
    if( status ) {
      perror("JOPNLLSM: chmod error");
      *kret = (fortint) 997;
      return;
    }

/*
//  Setup scratch legendre file.
*/
#ifdef REAL_8
    pleg = (double *) malloc(len_pleg*2);
#else
    pleg = (float *) malloc(len_pleg*2);
#endif

    if( pleg == NULL ) {
      perror("JOPNLLSM: malloc error.");
      *kret = (fortint) 996;
      return;
    }

    JMAKLL( &fpindex, iktrunc, zbuild, &plat, pleg, kret);
    if( *kret != 0 ) {
      free(pleg);
      return;
    }

/*
//  Close it, rename it, and open it again with read access
*/
    free(pleg);
    PBCLOSE(&fpindex,kret);
    status = rename(filedum, filename);
    if( status ) {
      perror("JOPNLLSM: rename error");
      *kret = (fortint) 995;
      return;
    }

    l1 = strlen(filename);
    l2 = 1;
    PBOPEN(&fpindex,filename,"r",kret,l1,l2);
    if( *kret ) {
      perror("JOPNLLSM: PBOPEN error");
      return;
    }
    fp = pbfp((long)fpindex);
    if( fp == NULL ) {
      perror("JOPNLLSM: file pointer after PBOPEN is NULL");
      *kret = (fortint) 994;
      return;
    }
  }

/*
//  Check the file size
*/
#ifdef FOPEN64

#ifdef linux
/* On Linux -m32, lseek64 doesn't work. Use fstat64 instead */

{

    struct stat st;
    
    if(fstat64(fileno(fp),&st) == -1)
	{
		fprintf(stderr,"JOPNLLSM: coefficients file fstat failed\n");
		*kret = (fortint) 993;
		return;
	}
    else
	{
		if(len != st.st_size)
		{
			fprintf(stderr,"JOPNLLSM: coefficients file size should be %lld bytes, it is %lld\n", len,st.st_size);
			*kret = (fortint) 993;
			return;
		}
	}
}
#else
  fseeko64(fp, 0, 2);
  if( len != ftello64(fp) ) {
    long long ftello = ftello64(fp);
    fprintf(stderr,"JOPNLLSM: coefficients file size should be %lld bytes, it is %lld\n", len,ftello);
    *kret = (fortint) 993;
    return;
  }
#endif

#else
  fseek(fp, 0, 2);
  if( len != ftell(fp) ) {
    long ftello = ftell(fp);
    
    fprintf(stderr,"JOPNLLSM: coefficients file size should be %ld bytes, it is %ld\n", len,ftello);
    *kret = (fortint) 993;
    return;
  }
#endif

#ifdef FOPEN64
  fseeko64(fp, 0, 0);
#else
  fseek(fp, 0, 0);
#endif

/*
//  Map the file
*/
  nunit = fileno(fp);
  if( nunit >= 0 ) {
    char *p      = getenv("PPMAPADDR");
    caddr_t addr = (caddr_t) (p?atol(p):0);

    /* time1 = 0; */
	/* time2 = iitimer(&time1); */

#ifdef FOPEN64
    result =  (void *) mmap64(addr,(size_t) len, (PROT_READ),
                            MAP_SHARED,(int) nunit, (OFF_T)off);
#else
    result =  (void *) mmap(addr,(size_t) len, (PROT_READ),
                            MAP_SHARED,(int) nunit,(OFF_T) off);
#endif

    if( (caddr_t) result == (caddr_t) -1 ) {
      perror("JOPNLLSM: mmap64 error");
      *kret = (fortint) 992;
      return;
    }
    oldresult = result;
    oldlen = len;
  }

  *ipdum = (JPointer) result;
  *kunit = (fortint) nunit;
  strcpy( yarray[ncnt], ypfn);
  resultarray[ncnt] = result;
  unitarray[ncnt] = nunit;
  ncnt+=1;

  /* time2 = iitimer(&time1); */
  /* printf("MMAP-timer %d\n",iiii_time); */

  return;
}