institution.c 10.6 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "dmemory.h"
#include "cdi.h"
#include "stream_int.h"
8
9
10
11
12
#include "resource_handle.h"
#include "pio_util.h"
#include "resource_handle.h"
#include "pio_rpc.h"
#include "namespace.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
14
15
16
17
18

#undef  UNDEFID
#define UNDEFID  -1

int ECMWF  = UNDEFID;
int MPIMET = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
int DWD    = UNDEFID;
20
int MCH    = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23

typedef struct
{
24
25
26
  int    self;
  int    used;
  int    center;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
30
  int    subcenter;
  char  *name;
  char  *longname;
}
31
institute_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
33


34
35
36
37
38
static int    instituteCompareP ( void * instituteptr1, void * instituteptr2 );
static void   instituteDestroyP ( void * instituteptr );
static void   institutePrintP   ( void * instituteptr, FILE * fp );
#ifdef USE_MPI
static int    instituteGetSizeP ( void * instituteptr, MPI_Comm comm );
39
static void   institutePackP    ( void * instituteptr, void *buf, int size, int *position, MPI_Comm comm );
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
static int    instituteTxCode   ( void );
#endif

resOps instituteOps = { instituteCompareP, instituteDestroyP, institutePrintP
#ifdef USE_MPI
                        ,instituteGetSizeP, institutePackP, instituteTxCode
#endif
 };

static int * instituteInitializedNsp;

static
void instituteDefaultValue ( institute_t * instituteptr )
{
  instituteptr->self       = UNDEFID;
  instituteptr->used       = 0;
  instituteptr->center     = UNDEFID;
  instituteptr->subcenter  = UNDEFID;
  instituteptr->name       = NULL;
  instituteptr->longname   = NULL;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61

62
63
64
65
static
institute_t * instituteNewEntry ( void )
{
  institute_t *instituteptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66

67
  instituteptr = ( institute_t * ) xmalloc ( sizeof ( institute_t ));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68

69
70
71
72
73
74
  instituteDefaultValue ( instituteptr );
  instituteptr->self = reshPut (( void * ) instituteptr, &instituteOps );
  instituteptr->used = 1;

  return  instituteptr;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75

76
77
static
void instituteDefaultEntries ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
{
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  cdiResH resH[12];
  int i;

  resH[0]  = ECMWF   = institutDef( 98,   0, "ECMWF",     "European Centre for Medium-Range Weather Forecasts");
  resH[1]  = MPIMET  = institutDef( 98, 232, "MPIMET",    "Max-Planck-Institute for Meteorology");
  resH[2]  =           institutDef( 98, 255, "MPIMET",    "Max-Planck-Institute for Meteorology");
  resH[3]  =           institutDef( 98, 232, "MPIMET",    "Max-Planck Institute for Meteorology");
  resH[4]  =           institutDef( 78, 255, "DWD",       "Deutscher Wetterdienst");
  resH[5]  = MCH     = institutDef(215, 255, "MCH",       "MeteoSwiss");
  resH[6]  =           institutDef(  7,   0, "NCEP",      "National Centers for Environmental Prediction");
  resH[7]  =           institutDef(  7,   1, "NCEP",      "National Centers for Environmental Prediction");
  resH[8]  =           institutDef( 60,   0, "NCAR",      "National Center for Atmospheric Research");
  resH[9]  =           institutDef( 74,   0, "METOFFICE", "U.K. Met Office");
  resH[10] =           institutDef( 97,   0, "ESA",       "European Space Agency ");
  resH[11] =           institutDef( 99,   0, "KNMI",      "Royal Netherlands Meteorological Institute");
  /*     (void) institutDef(  0,   0, "IPSL", "IPSL (Institut Pierre Simon Laplace, Paris, France)"); */

  if ( ! namespaceHasLocalFile ( namespaceGetActive ()))
    for ( i = 0; i < 12 ; i++ )
      reshSetStatus ( resH[i], &instituteOps, SUSPENDED );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
100
}

101
102
103
104
105
static
void instituteFinalize ( void )
{
  free ( instituteInitializedNsp );
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106

107
108
static
void instituteInit (void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
{
110
111
112
113
  static int instituteInitialized = 0;
  int nsp, nspc;

  nspc = namespaceGetNumber ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114

115
  if ( !instituteInitialized )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
    {
117
118
119
      instituteInitialized = 1;
      instituteInitializedNsp = xcalloc ( 1, nspc * sizeof ( int ));
      atexit ( instituteFinalize );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
    }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

  nsp = namespaceGetActive ();
  if ( instituteInitializedNsp[nsp] ) return;
  instituteInitializedNsp[nsp] = 1;

  instituteDefaultEntries();
}


int instituteCount ( void )
{
  return reshCountType ( &instituteOps );
}


int instituteCompareKernel ( institute_t *  ip1, institute_t * ip2 )
{
  int differ = 0;
  size_t len;

  if ( ip1->name )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
    {
143
144
145
146
147
148
149
150
151
152
153
      if ( ip1->center    > 0 && ip2->center    != ip1->center )    differ = 1;
      if ( ip1->subcenter > 0 && ip2->subcenter != ip1->subcenter ) differ = 1;

      if ( !differ )
        {
          if ( ip2->name )
            {
              len = strlen(ip2->name);
              if ( memcmp(ip2->name, ip1->name, len)) differ = 1;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
    }
155
  else if ( ip1->longname )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
    {
157
158
159
160
161
162
163
164
165
166
      if ( ip2->longname )
        {
          len = strlen(ip2->longname);
          if ( memcmp(ip2->longname, ip1->longname, len)) differ = 1;
        }
    }
  else
    {
      if ( !( ip2->center    == ip1->center &&
              ip2->subcenter == ip1->subcenter )) differ = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
    }

169
  return differ;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
}


173
static int instituteCompareP ( void *  instituteptr1, void * instituteptr2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
{
175
  institute_t * i1, * i2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176

177
178
179
180
181
182
183
  i1 = ( institute_t * ) instituteptr1;
  i2 = ( institute_t * ) instituteptr2;

  xassert(i1);
  xassert(i2);

  return instituteCompareKernel ( i1, i2 );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
186
187
188
}


int institutInq(int center, int subcenter, const char *name, const char *longname)
{
189
190
191
192
193
194
195
  int instID = UNDEFID, instCount, * instResHs, i;
  institute_t * ip1;
  institute_t * ip2;

  instituteInit ();

  ip1 = xmalloc ( sizeof ( institute_t ));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196

197
198
199
200
  ip1->self       = UNDEFID;
  ip1->used       = 0;
  ip1->center     = center;
  ip1->subcenter  = subcenter;
201
202
203
204
205
206
  ip1->name       = NULL;
  ip1->longname   = NULL;
  if ( name && *name )
    ip1->name     = ( char * ) name;
  if ( longname && *longname )
    ip1->longname = ( char * ) longname;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207

208
209
210
211
212
  instCount = instituteCount ();
  instResHs = xmalloc ( instCount * sizeof ( int ));
  reshGetResHListOfType ( instCount, instResHs, &instituteOps );

  for ( i = 0; i < instCount; i++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
    {
214
      ip2 = ( institute_t * ) reshGetVal ( instResHs[i], &instituteOps );
215
216
217
218
219
220
221
      xassert ( ip2 );

      if ( ip2->used && ! instituteCompareKernel ( ip1, ip2 ))
        {
          instID = ip2->self;
          break;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
223
    }

224
225
  free ( instResHs );
  free ( ip1 );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226

227
  return  (instID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
230
231
232
}


int institutDef(int center, int subcenter, const char *name, const char *longname)
{
233
  institute_t * instituteptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234

235
  instituteInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236

237
  instituteptr = instituteNewEntry();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238

239
240
  instituteptr->center    = center;
  instituteptr->subcenter = subcenter;
241
242
  if ( name && *name )         instituteptr->name     = strdupx(name);
  if ( longname && *longname ) instituteptr->longname = strdupx(longname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243

244
  return instituteptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
246
247
}


248
int institutInqCenter(int instID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
{
250
  institute_t * instituteptr = NULL;
251
252
253

  instituteInit ();

254
255
  if ( instID != UNDEFID )
    instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
256

257
  return  instituteptr ? instituteptr->center : UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
260
}


261
int institutInqSubcenter(int instID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
{
263
  institute_t * instituteptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264

265
  instituteInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266

267
268
  if ( instID != UNDEFID )
    instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269

270
  return instituteptr ? instituteptr->subcenter: UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
272
273
}


274
char *institutInqNamePtr(int instID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
{
276
  institute_t * instituteptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277

278
  instituteInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279

280
281
  if ( instID != UNDEFID )
    instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282

283
284
285
286
287
288
  return instituteptr ? instituteptr->name : NULL;
}


char *institutInqLongnamePtr(int instID)
{
289
  institute_t * instituteptr = NULL;
290
291

  instituteInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292

293
294
  if ( instID != UNDEFID )
    instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
295
296

  return instituteptr ? instituteptr->longname : NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
298
299
}


300
int institutInqNumber(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
{
302
303
  int i, instCount, * instResHs, instNum = 0;
  institute_t * instituteptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304

305
  instituteInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306

307
308
309
  instCount = reshCountType ( &instituteOps );
  instResHs = xmalloc ( instCount * sizeof ( int ));
  reshGetResHListOfType ( instCount, instResHs, &instituteOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310

311
312
313
314
315
  for ( i = 0; i < instCount; i++ )
    {
      instituteptr = ( institute_t * ) reshGetVal ( instResHs[i], &instituteOps );
      if ( instituteptr && instituteptr->used )
        instNum++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
317
    }

318
319
320
  if ( instResHs ) free ( instResHs );

  return instNum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
322
323
}


324
void instituteDestroyP ( void * instituteptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
{
326
327
  int id;
  institute_t * i1 = ( institute_t * ) instituteptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328

329
  xassert ( i1 );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330

331
  id = i1->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332

333
  if ( instituteptr ) free ( instituteptr );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
334

335
  reshRemove ( id, &instituteOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
336
337
338
}


339
void institutePrintP   ( void * instituteptr, FILE * fp )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
{
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  institute_t * ip = ( institute_t * ) instituteptr;

  if ( !ip ) return;

  fprintf ( fp, "#\n");
  fprintf ( fp, "# instituteID %d\n", ip->self);
  fprintf ( fp, "#\n");
  fprintf ( fp, "self          = %d\n", ip->self );
  fprintf ( fp, "used          = %d\n", ip->used );
  fprintf ( fp, "center        = %d\n", ip->center );
  fprintf ( fp, "subcenter     = %d\n", ip->subcenter );
  fprintf ( fp, "name          = %s\n", ip->name ? ip->name : "NN" );
  fprintf ( fp, "longname      = %s\n", ip->longname ? ip->longname : "NN" );
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
356

357
358
359
360
361
#ifdef USE_MPI
static int
instituteTxCode ( void )
{
  return INSTITUTE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362
363
}

364
365
366
enum {
  institute_nints = 5,
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367

368
369
static
int instituteGetSizeP ( void * instituteptr, MPI_Comm comm )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
{
371
372
373
374
375
376
377
378
  institute_t *p = instituteptr;
  int txsize = 0, txinc;
  xmpi(MPI_Pack_size(institute_nints, MPI_INT, comm, &txsize));
  xmpi(MPI_Pack_size(strlen(p->name) + 1, MPI_CHAR, comm, &txinc));
  txsize += txinc;
  xmpi(MPI_Pack_size(strlen(p->longname) + 1, MPI_CHAR, comm, &txinc));
  txsize += txinc;
  return txsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

static
void institutePackP ( void * instituteptr, void *buf, int size,
                      int *position, MPI_Comm comm )
{
  institute_t *p = instituteptr;
  int tempbuf[institute_nints];
  tempbuf[0] = p->self;
  tempbuf[1] = p->center;
  tempbuf[2] = p->subcenter;
  tempbuf[3] = (int)strlen(p->name) + 1;
  tempbuf[4] = (int)strlen(p->longname) + 1;
  xmpi(MPI_Pack(tempbuf, institute_nints, MPI_INT, buf, size, position, comm));
  xmpi(MPI_Pack(p->name, tempbuf[3], MPI_CHAR, buf, size, position, comm));
  xmpi(MPI_Pack(p->longname, tempbuf[4], MPI_CHAR, buf, size, position, comm));
}

int
instituteUnpack(void *buf, int size, int *position, int nspTarget,
                MPI_Comm comm)
{
  int tempbuf[institute_nints];
  int instituteID;
  char *name, *longname;
  xmpi(MPI_Unpack(buf, size, position, tempbuf, institute_nints, MPI_INT, comm));
  name = xmalloc(tempbuf[3]);
  longname = xmalloc(tempbuf[4]);
  xmpi(MPI_Unpack(buf, size, position, name, tempbuf[3], MPI_CHAR, comm));
  xmpi(MPI_Unpack(buf, size, position, longname, tempbuf[4], MPI_CHAR, comm));
  instituteID = institutDef(tempbuf[1], tempbuf[2], name, longname);
  // FIXME: this should work, once all types are transferred
  //xassert(instituteID == tempbuf[0]);
  return instituteID;
}

#endif
416
417
418
419
420
421
422
423
424
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */