File: sharedMemoryCharacteristics.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 (155 lines) | stat: -rwxr-xr-x 4,024 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
/**
* 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.
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>

extern int debugSet;

#include "sharedmemory.h"
#include "fortdefs.h"

#define MAC 1264

int sharedMemoryCharacteristics(
  int type,
  int truncation,
  float grid,
  int * numlat,
  int * size,
  key_t * key )
{
/*
// Input:
//
//   type - indicates the spectral to grid transformation:
//          SP2LL for spectral to lat/long
//          SP2RG for spectral to regular gaussian
//          SP2QG for spectral to quasi-regular (reduced) gaussian
//
//   truncation - the spectral truncation (eg 511 for T511)
//
//   grid - specifies the desired grid:
//          the grid spacing along a line of longitude for lat/long grids
//          the gaussian grid number for gaussian grids (eg 80.0 for N80)
//
// Output:
//
//   numlat - the number of latitudes from pole to pole in the grid
//
//   size - size in bytes of the transformation coefficients
//
//   key - the key for the shared memory and its associated semaphore
//
// Function returns 0 if all OK.
//
// Files setup to generate shared memory and semaphore keys are held
// in a desginated directory (see defaultDirectory below). This
// directory can be specified using environment variable SHARED_DIRECTORY.
//
*/
int status, gaussianNumber;
char defaultDirectory[] = "/home/ma/emos/data/BlueStorm";
char * directory;
char filename[] = "SP2LL_Tnnnn_to_Grid_xxxxxxxxx";
char * fullFilename;
key_t Key;

/*
// Build the name of the file which is used to define a key.
// If the file does not already exist, create it.
*/
    directory = getenv("SHARED_DIRECTORY");
    if( directory == NULL ) directory = defaultDirectory;
    fullFilename = (char *) malloc(strlen(directory)+strlen(filename));
    if( fullFilename == NULL ) {
      perror("malloc error");
      exit(1);
    }
    strcpy(fullFilename, directory);
    strcat(fullFilename, "/");

    switch( type) {

      case SP2LL:
        sprintf(filename,"SP2LL_T%04d_to_Grid_%09.6f",truncation,grid);
        break;

      case SP2RG:
        sprintf(filename,"SP2RG_T%04d_to_Regular_N%04.0f",truncation,grid);
        break;

      case SP2QG:
        sprintf(filename,"SP2QG_T%04d_to_Reduced_N%04.0f",truncation,grid);
        break;

      default:
        return (int) 1;
    }
    strcat(fullFilename, filename);
    
    if( DEBUG ) printf("fullFilename = %s\n", fullFilename);
    Key = ftok(fullFilename, MAC);
/*
//  If file does not exist, ..
*/
    if( Key == -1 ) {
      char * command = (char*) malloc(6+strlen(fullFilename));
      if( command == NULL ) {
        perror("malloc error");
        exit(1);
      }
      strcpy(command,"touch ");
      strcat(command,fullFilename);
      if( DEBUG ) printf("%s\n", command);
      status = system(command);
      if( status ) {
        printf("Unable to create file %s\n", fullFilename);
        perror("File creation problem");
        exit(1);
      }
      free(command);
      Key = ftok(fullFilename, MAC);
      if( Key == -1 ) {
        perror("Error getting key from newly created file");
        exit(1);
      }
    }
    free(fullFilename);

    *key = Key;
    if( DEBUG ) printf("sharedMemoryCharacteristics: Key = %0x\n", Key);
/*
// Calculate the file characteristics
*/
    switch( type) {

      case SP2LL:
        *numlat = (int)(90.0/grid + 1.0);
        break;

      case SP2RG:
      case SP2QG:
        gaussianNumber = (int) grid;
        *numlat = gaussianNumber*2;
        break;

      default:
        printf("sharedMemoryCharacteristics: Type %d not yet handled\n", type);
        return (int) 1;
    }     

    *size = ((truncation+1)*(truncation+4))/2 * (*numlat) * sizeof(fortreal);

    if( DEBUG ) printf("sharedMemoryCharacteristics: Size = %d bytes\n", *size);

    return (int) 0;
}