model.c 7.66 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "dmemory.h"
#include "cdi.h"
#include "stream_int.h"

#undef  UNDEFID
#define UNDEFID -1

int ECHAM4 = UNDEFID;
int ECHAM5 = UNDEFID;

typedef struct
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
  int      self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
19
20
21
22
  int      used;  
  int      instID;  
  int      modelgribID;
  char    *name;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
MODEL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
25
26



Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
static int  MODEL_Debug = 0;   /* If set to 1, debugging */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
static int _model_max = MAX_MODELS;

static void model_initialize(void);

static int _model_init = FALSE;

#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>

static pthread_once_t _model_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _model_mutex;

#  define MODEL_LOCK           pthread_mutex_lock(&_model_mutex);
#  define MODEL_UNLOCK         pthread_mutex_unlock(&_model_mutex);
#  define MODEL_INIT                               \
   if ( _model_init == FALSE ) pthread_once(&_model_init_thread, model_initialize);

#else

#  define MODEL_LOCK
#  define MODEL_UNLOCK
#  define MODEL_INIT                               \
   if ( _model_init == FALSE ) model_initialize();

#endif


typedef struct _modelPtrToIdx {
  int idx;
  MODEL *ptr;
  struct _modelPtrToIdx *next;
} modelPtrToIdx;


static modelPtrToIdx *_modelList  = NULL;
static modelPtrToIdx *_modelAvail = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66


Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
static void model_list_new(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
  static char func[] = "model_list_new";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70

Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
  assert(_modelList == NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72

Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
  _modelList = (modelPtrToIdx *) malloc(_model_max*sizeof(modelPtrToIdx));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
77

static void model_list_delete(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
  static char func[] = "model_list_delete";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80

Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
82
  if ( _modelList ) free(_modelList);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84


Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
86
87
88
89
90
91
92
93
static void model_init_pointer(void)
{
  int  i;
  
  for ( i = 0; i < _model_max; i++ )
    {
      _modelList[i].next = _modelList + i + 1;
      _modelList[i].idx  = i;
      _modelList[i].ptr  = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

  _modelList[_model_max-1].next = 0;

  _modelAvail = _modelList;
}


MODEL *model_to_pointer(int idx)
{
  static char func[] = "model_to_pointer";
  MODEL *modelptr = NULL;

  MODEL_INIT

  if ( idx >= 0 && idx < _model_max )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
112
113
114
115
      MODEL_LOCK

      modelptr = _modelList[idx].ptr;

      MODEL_UNLOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  else
    Error(func, "model index %d undefined!", idx);

  return (modelptr);
}


/* Create an index from a pointer */
static int model_from_pointer(MODEL *ptr)
{
  static char func[] = "model_from_pointer";
  int      idx = -1;
  modelPtrToIdx *newptr;

  if ( ptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
      MODEL_LOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134

Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
      if ( _modelAvail )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140
141
142
143
144
	  newptr       = _modelAvail;
	  _modelAvail  = _modelAvail->next;
	  newptr->next = 0;
	  idx	       = newptr->idx;
	  newptr->ptr  = ptr;
      
	  if ( MODEL_Debug )
	    Message(func, "Pointer %p has idx %d from model list", ptr, idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
      else
	Warning(func, "Too many open models (limit is %d)!", _model_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
      MODEL_UNLOCK
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
  else
    Error(func, "Internal problem (pointer %p undefined)", ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153

Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155
  return (idx);
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156

Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
159
160
161
162
163
164
165
166

static void model_init_entry(MODEL *modelptr)
{
  modelptr->self        = model_from_pointer(modelptr);

  modelptr->used        = 1;

  modelptr->instID      = UNDEFID;
  modelptr->modelgribID = UNDEFID;
  modelptr->name        = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

static MODEL *model_new_entry(void)
{
  static char func[] = "model_new_entry";
  MODEL *modelptr;

  modelptr = (MODEL *) malloc(sizeof(MODEL));

  if ( modelptr ) model_init_entry(modelptr);

  return (modelptr);
}


static void model_delete_entry(MODEL *modelptr)
{
  static char func[] = "model_delete_entry";
  int idx;

  idx = modelptr->self;

  MODEL_LOCK

  free(modelptr);

  _modelList[idx].next = _modelAvail;
  _modelList[idx].ptr  = 0;
  _modelAvail          = &_modelList[idx];

  MODEL_UNLOCK

  if ( MODEL_Debug )
    Message(func, "Removed idx %d from model list", idx);
}

int modelDef(int instID, int modelgribID, const char *name);

static void model_defaults(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209
210
211
212
213
214
{
  int instID;

  instID  = institutInq(  0,   0, "ECMWF", NULL);
  (void)    modelDef(instID, 131, "ERA15");
  (void)    modelDef(instID, 199, "ERA40");

  instID  = institutInq(  0,   0, "MPIMET", NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
  ECHAM5  = modelDef(instID,  64, "ECHAM5.4");
  (void)    modelDef(instID,  63, "ECHAM5.3");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  (void)    modelDef(instID,  62, "ECHAM5.2");
  (void)    modelDef(instID,  61, "ECHAM5.1");

  instID  = institutInq( 98, 255, "MPIMET", NULL);
  (void)    modelDef(instID,  60, "ECHAM5.0");
  ECHAM4  = modelDef(instID,  50, "ECHAM4");
  (void)    modelDef(instID, 110, "MPIOM1");

  instID  = institutInq(  0,   0, "DWD", NULL);
  (void)    modelDef(instID, 149, "GME");

  instID  = institutInq(  0,   1, "NCEP", NULL);
  (void)    modelDef(instID,  80, "T62L28MRF");
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

static void model_initialize(void)
{
  char *env;

#if  defined  (HAVE_LIBPTHREAD)
  /* initialize global API mutex lock */
  pthread_mutex_init(&_model_mutex, NULL);
#endif

  env = getenv("MODEL_DEBUG");
  if ( env ) MODEL_Debug = atoi(env);

  model_list_new();
  atexit(model_list_delete);

248
249
  MODEL_LOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
251
  model_init_pointer();

252
253
  MODEL_UNLOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
  _model_init = TRUE;

  model_defaults();
}


static void model_check_ptr(const char *func, MODEL *modelptr)
{
  if ( modelptr == NULL )
    Error(func, "model undefined!");
}


int modelSize(void)
{
  int modelsize = 0;
  int i;
  
  MODEL_INIT

  MODEL_LOCK

  for ( i = 0; i < _model_max; i++ )
    if ( _modelList[i].ptr ) modelsize++;

  MODEL_UNLOCK

  return (modelsize);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286
287
288
289
int modelInq(int instID, int modelgribID, char *name)
{
  int modelID = UNDEFID;
  size_t len;
  int found;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
291
  int model_size;
  MODEL *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292

Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
  MODEL_INIT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294

Uwe Schulzweida's avatar
Uwe Schulzweida committed
295
296
297
  model_size = modelSize();

  for( modelID = 0; modelID < model_size; modelID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
300
301
      modelptr = model_to_pointer(modelID);

      if ( modelptr->used )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
303
304
305
	{
	  if ( name )
	    {
	      found = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307
	      if ( instID      != -1 && modelptr->instID      != instID )      found = 0;
	      if ( modelgribID !=  0 && modelptr->modelgribID != modelgribID ) found = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
309
310

	      if ( found )
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
		  if ( modelptr->name )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
		    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
		      len = strlen(modelptr->name);
		      if ( strncmp(modelptr->name, name, len) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
		      len = strlen(name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
		      if ( strncmp(modelptr->name, name, len) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
318
319
320
321
		    }
		}
	    }
	  else
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
	      if ( modelptr->instID      == instID &&
		   modelptr->modelgribID == modelgribID ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
326
327
	    }
	}
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
  if ( modelID == model_size ) modelID = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
329
330
331
332

  return (modelID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
333

Uwe Schulzweida's avatar
Uwe Schulzweida committed
334
335
336
337
int modelDef(int instID, int modelgribID, const char *name)
{
  static char func[] = "modelDef";
  int modelID = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
339
340
  MODEL *modelptr;

  MODEL_INIT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
344
345
346

  /*
  modelID = modelInq(instID, modelgribID, name);
  */
  if ( modelID == UNDEFID )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
      modelptr = model_new_entry();
      if ( ! modelptr ) Error(func, "No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349

Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
      modelID = modelptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351

Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
353
354
355
      modelptr->instID      = instID;
      modelptr->modelgribID = modelgribID;

      if ( name ) modelptr->name = strdupx(name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
357
358
359
360
361
362
363
364
365
    }

  return (modelID);
}


int modelInqInstitut(int modelID)
{
  static char func[] = "modelInqInstitut";
  int instID = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
  MODEL *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367

Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
  MODEL_INIT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371

  if ( modelID != UNDEFID )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374
      modelptr = model_to_pointer(modelID);

      model_check_ptr(func, modelptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
      instID = modelptr->instID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
378
379
380
381
    }

  return (instID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
382

Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
384
385
386
int modelInqGribID(int modelID)
{
  static char func[] = "modelInqGribID";
  int modelgribID = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
  MODEL *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388

Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
  MODEL_INIT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
392

  if ( modelID != UNDEFID )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
      modelptr = model_to_pointer(modelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394

Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
      model_check_ptr(func, modelptr);
  
      modelgribID = modelptr->modelgribID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
398
399
400
401
402
    }

  return (modelgribID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
403

Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
405
406
407
char *modelInqNamePtr(int modelID)
{
  static char func[] = "modelInqNamePtr";
  char *name = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
  MODEL *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409

Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
  MODEL_INIT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411
412
413

  if ( modelID != UNDEFID )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
414
      modelptr = model_to_pointer(modelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415

Uwe Schulzweida's avatar
Uwe Schulzweida committed
416
417
418
419
      model_check_ptr(func, modelptr);
  
      if ( modelptr->name )
	name = modelptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
421
422
423
    }

  return (name);
}