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

#include "dmemory.h"
#include "cdi.h"
7
#include "cdi_int.h"
8
#include "resource_handle.h"
9
#include "resource_unpack.h"
10
#include "namespace.h"
11
#include "serialize.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
14
15
16
17

#undef  UNDEFID
#define UNDEFID -1

int ECHAM4 = UNDEFID;
int ECHAM5 = UNDEFID;
18
int COSMO  = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
20
21

typedef struct
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
  int      self;
23
24
  int      used;
  int      instID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
  int      modelgribID;
  char    *name;
}
28
model_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30


Uwe Schulzweida's avatar
Uwe Schulzweida committed
31
static int  MODEL_Debug = 0;   /* If set to 1, debugging */
32
static int * modelInitializedNsp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

34
static void modelInit(void);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36


37
38
static int    modelCompareP ( void * modelptr1, void * modelptr2 );
static void   modelDestroyP ( void * modelptr );
39
static void   modelPrintP   ( void * modelptr, FILE * fp );
40
static int    modelGetSizeP ( void * modelptr, void *context);
41
static void   modelPackP    ( void * modelptr, void * buff, int size,
42
                              int *position, void *context);
43
static int    modelTxCode   ( void );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44

45
resOps modelOps = { modelCompareP, modelDestroyP, modelPrintP
46
                    , modelGetSizeP, modelPackP, modelTxCode
47
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48

49
static
50
void modelDefaultValue ( model_t *modelptr )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
{
52
53
  modelptr->self        = UNDEFID;
  modelptr->used        = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55
56
  modelptr->instID      = UNDEFID;
  modelptr->modelgribID = UNDEFID;
  modelptr->name        = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
}

59
static
60
model_t *modelNewEntry ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
{
62
  model_t *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63

64
65
66
  modelptr = (model_t *) xmalloc(sizeof(model_t));
  modelDefaultValue ( modelptr );
  modelptr->self = reshPut (( void * ) modelptr, &modelOps );
67
  modelptr->used = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
69
70
71
72
73
74

  return (modelptr);
}


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

75
static
76
void modelDefaultEntries ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
{
78
79
  int instID, i;
  cdiResH resH[10];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81

  instID  = institutInq(  0,   0, "ECMWF", NULL);
82
83
  /* (void)    modelDef(instID, 131, "ERA15"); */
  /* (void)    modelDef(instID, 199, "ERA40"); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
  instID  = institutInq(  0,   0, "MPIMET", NULL);
85
86
87
88
89

  resH[0] = ECHAM5  = modelDef(instID,  64, "ECHAM5.4");
  resH[1] = modelDef(instID,  63, "ECHAM5.3");
  resH[2] = modelDef(instID,  62, "ECHAM5.2");
  resH[3] = modelDef(instID,  61, "ECHAM5.1");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91

  instID  = institutInq( 98, 255, "MPIMET", NULL);
92
93
94
  resH[4] = modelDef(instID,  60, "ECHAM5.0");
  resH[5] = ECHAM4  = modelDef(instID,  50, "ECHAM4");
  resH[6] = modelDef(instID, 110, "MPIOM1");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96

  instID  = institutInq(  0,   0, "DWD", NULL);
97
  resH[7] = modelDef(instID, 149, "GME");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98

99
  instID  = institutInq(  0,   0, "MCH", NULL);
100
  //(void)  = modelDef(instID, 137, "COSMO");
101
  resH[8] = COSMO   = modelDef(instID, 255, "COSMO");
102

Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
  instID  = institutInq(  0,   1, "NCEP", NULL);
104
  resH[9] = modelDef(instID,  80, "T62L28MRF");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105

Thomas Jahns's avatar
Thomas Jahns committed
106
107
  for ( i = 0; i < 10 ; i++ )
    reshSetStatus(resH[i], &modelOps, SUSPENDED);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
109
}

110
111
112
113
114
115
static
void modelFinalize ( void )
{
  free (   modelInitializedNsp );
}

116
static
117
void modelInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
{
119
  static int modelInitialized = 0;
120
  int nsp, nspc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
  char *env;

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

125
126
127
  if ( !modelInitialized )
    {
      modelInitialized = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
      modelInitializedNsp = (int*) xcalloc ( 1, nspc * sizeof ( int ));
129
130
      atexit ( modelFinalize );
      env = getenv("MODEL_DEBUG");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
      if ( env ) MODEL_Debug = atoi(env);
132
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133

134
  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135

136
  if ( modelInitializedNsp[nsp] ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137

138
  modelInitializedNsp[nsp] = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139

140
  modelDefaultEntries ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
}

143
int modelSize ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
{
145
  return reshCountType ( &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
}

148
struct modelLoc
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
{
150
151
152
  char *name;
  int instID, modelgribID, resID;
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153

154
155
156
static enum cdiApplyRet
findModelByID(int resID, void *res, void *data)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
  model_t *modelptr = (model_t*) res;
  struct modelLoc *ret = (struct modelLoc*) data;
159
160
161
162
  int instID = ret->instID, modelgribID = ret->modelgribID;
  if (modelptr->used
      && modelptr->instID == instID
      && modelptr->modelgribID == modelgribID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
    {
164
165
166
167
168
169
      ret->resID = resID;
      return CDI_APPLY_STOP;
    }
  else
    return CDI_APPLY_GO_ON;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170

171
172
173
static enum cdiApplyRet
findModelByName(int resID, void *res, void *data)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175
  model_t *modelptr = (model_t*) res;
  struct modelLoc *ret = (struct modelLoc*) data;
176
177
178
179
180
181
182
183
184
185
186
  int instID = ret->instID, modelgribID = ret->modelgribID;
  const char *name = ret->name;
  if (modelptr->used
      && (instID == -1 || modelptr->instID == instID)
      && (modelgribID == 0 || modelptr->modelgribID == modelgribID)
      && modelptr->name)
    {
      const char *p = name, *q = modelptr->name;
      while (*p != '\0' && *p == *q)
        ++p, ++q;
      if (*p == '\0' || *q == '\0')
187
        {
188
189
          ret->resID = resID;
          return CDI_APPLY_STOP;
190
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
    }
192
193
  return CDI_APPLY_GO_ON;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194

195
196
197
int modelInq(int instID, int modelgribID, char *name)
{
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198

199
200
201
202
203
204
205
206
  struct modelLoc searchState = { .name = name, .instID = instID,
                                  .modelgribID = modelgribID,
                                  .resID = UNDEFID };
  if (name && *name)
    cdiResHFilterApply(&modelOps, findModelByName, &searchState);
  else
    cdiResHFilterApply(&modelOps, findModelByID, &searchState);
  return searchState.resID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
209

Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
211
int modelDef(int instID, int modelgribID, const char *name)
{
212
  model_t *modelptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213

214
  modelInit ();
215

216
  modelptr = modelNewEntry();
217

218
  modelptr->instID      = instID;
219
  modelptr->modelgribID = modelgribID;
220
  if ( name && *name ) modelptr->name = strdupx(name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221

222
  return modelptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
224
225
226
227
}


int modelInqInstitut(int modelID)
{
228
  model_t *modelptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229

230
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231

232
233
  if ( modelID != UNDEFID )
    modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
234

235
  return modelptr ? modelptr->instID : UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
237
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
238

Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
240
int modelInqGribID(int modelID)
{
241
  model_t *modelptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242

243
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244

245
246
  if ( modelID != UNDEFID )
    modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247

248
  return modelptr ? modelptr->modelgribID : UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
250
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
251

Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
253
char *modelInqNamePtr(int modelID)
{
254
  model_t *modelptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

256
  modelInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257

258
259
  if ( modelID != UNDEFID )
    modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260

261
262
263
264
265
266
267
268
  return modelptr ? modelptr->name : NULL;
}


int  modelCompareP ( void * modelptr1, void * modelptr2 )
{
  return 0;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269

270
271
272
273
274
275

void modelDestroyP ( void * modelptr )
{
}


276
void modelPrintP   ( void * modelptr, FILE * fp )
277
278
279
280
281
282
283
{
  model_t * mp = ( model_t * ) modelptr;

  if ( !mp ) return;

  fprintf ( fp, "#\n");
  fprintf ( fp, "# modelID %d\n", mp->self);
284
  fprintf ( fp, "#\n");
285
286
287
288
289
  fprintf ( fp, "self          = %d\n", mp->self );
  fprintf ( fp, "used          = %d\n", mp->used );
  fprintf ( fp, "instID        = %d\n", mp->instID );
  fprintf ( fp, "modelgribID   = %d\n", mp->modelgribID );
  fprintf ( fp, "name          = %s\n", mp->name ? mp->name : "NN" );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
}
291
292


293
static int
294
modelTxCode ( void )
295
296
297
298
{
  return MODEL;
}

299
300
301
302
303
enum {
  model_nints = 4,
};


304
static int modelGetSizeP(void * modelptr, void *context)
305
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
  model_t *p = (model_t*) modelptr;
307
  int txsize = serializeGetSize(model_nints, DATATYPE_INT, context)
308
    + serializeGetSize(p->name?strlen(p->name) + 1:0, DATATYPE_TXT, context);
309
  return txsize;
310
311
312
}


313
static void modelPackP(void * modelptr, void * buf, int size, int *position, void *context)
314
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
  model_t *p = (model_t*) modelptr;
316
317
318
319
  int tempbuf[model_nints];
  tempbuf[0] = p->self;
  tempbuf[1] = p->instID;
  tempbuf[2] = p->modelgribID;
320
  tempbuf[3] = p->name ? (int)strlen(p->name) + 1 : 0;
321
  serializePack(tempbuf, model_nints, DATATYPE_INT, buf, size, position, context);
322
323
  if (p->name)
    serializePack(p->name, tempbuf[3], DATATYPE_TXT, buf, size, position, context);
324
325
326
}

int
327
modelUnpack(void *buf, int size, int *position, int nspTarget, void *context)
328
329
330
{
  int tempbuf[model_nints];
  int modelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
  char *name = NULL;
332
  serializeUnpack(buf, size, position, tempbuf, model_nints, DATATYPE_INT, context);
333
334
  if (tempbuf[3] != 0)
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
      name = (char*) xmalloc(tempbuf[3]);
336
337
      serializeUnpack(buf, size, position, name, tempbuf[3], DATATYPE_TXT, context);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338

339
  modelID = modelDef( namespaceAdaptKey ( tempbuf[1], nspTarget ), tempbuf[2], name);
340
341
342
  // FIXME: this should work, once all types are transferred
  //assert(modelID == tempbuf[0]);
  return modelID;
343
}
344

345
346
347
348
349
350
351
352
353
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */