2
This file is part of CDO. CDO is a collection of Operators to
3
manipulate and analyse Climate model Data.
5
Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
6
See COPYING file for copying and redistribution conditions.
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; version 2 of the License.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
41
void nml_error(int status, const char *nml_name)
47
cdoAbort("Namelist %s not found!", nml_name);
52
cdoAbort("Namelist %s unknown error!", nml_name);
58
int pos_nml(FILE *fp_nml, const char *nml_name)
62
char line[MAX_LINE_LEN];
67
fgetpos(fp_nml, &fpos);
68
while ( readline(fp_nml, line, MAX_LINE_LEN) )
70
if ( line[0] == '#' ) continue;
71
if ( line[0] == '\0' ) continue;
72
if ( line[0] == '&' && strlen(line) > 2 )
75
if ( strncmp(line+1, nml_name, strlen(nml_name)) == 0 )
78
fsetpos(fp_nml, &fpos);
82
fgetpos(fp_nml, &fpos);
89
void nml_control_init(nml_control_t *nml_control)
91
strcpy(nml_control->nml_name, "control");
92
nml_control->debug_level = 0;
93
nml_control->force_remap = 0;
97
void nml_control_print(nml_control_t nml_control)
99
fprintf(stdout, "Namelist: %s\n", nml_control.nml_name);
100
fprintf(stdout, " debug_level = %d\n", nml_control.debug_level);
101
fprintf(stdout, " force_remap = %d\n", nml_control.force_remap);
105
int nml_control_read(FILE *fp_nml, nml_control_t *nml_control)
109
status = pos_nml(fp_nml, nml_control->nml_name);
114
NML_DEF_INT(debug_level, 1, 0);
115
NML_DEF_INT(force_remap, 1, 0);
117
nml = namelistNew(nml_control->nml_name);
119
NML_ADD_INT(nml, debug_level);
120
NML_ADD_INT(nml, force_remap);
122
namelistRead(fp_nml, nml);
124
if ( (NML_NUM(nml, debug_level)) ) nml_control->debug_level = seldebug_level[0];
125
if ( (NML_NUM(nml, force_remap)) ) nml_control->force_remap = selforce_remap[0];
134
void read_param(const char *paramfile, param_t *param, int maxparam, int *nparam)
136
static const char *func = "read_param";
139
int nml_itype, nml_name, nml_pos;
141
int code, new_code, table, pos;
143
int codenum, tabnum, levtype;
145
char *datatype = NULL;
146
char *name = NULL, *itype = NULL, *stdname = NULL, longname[256] = "", units[256] = "";
149
fp = fopen(paramfile, "r");
150
if ( fp == NULL ) cdoAbort("Open failed on %s!", paramfile);
152
nml = namelistNew("parameter");
155
nml_name = namelistAdd(nml, "name", NML_WORD, 0, &name, 1);
156
nml_pos = namelistAdd(nml, "pos", NML_INT, 0, &pos, 1);
157
nml_itype = namelistAdd(nml, "itype", NML_WORD, 0, &itype, 1);
162
namelistRead(fp, nml);
165
for ( i = 0; i < nml->size; i++ )
167
if ( nml->entry[i]->occ ) { locc = TRUE; break; }
172
/* namelistPrint(nml); */
176
if ( nml->entry[nml_name]->occ == 0 )
178
cdoWarning("Parameter %d skipped, variable name not found!", nml_index);
182
if ( numparam >= maxparam )
184
cdoWarning("Too many parameter (limit=%d)!", maxparam);
188
param[numparam].name = strdup(name);
190
if ( nml->entry[nml_pos]->occ )
191
param[numparam].pos = pos;
193
param[numparam].pos = 0;
195
if ( nml->entry[nml_itype]->occ )
196
param[numparam].itype = (int) *itype;
198
param[numparam].itype = 'Q';
202
for ( varID = 0; varID < nvars; varID++ )
204
vlistInqVarName(vlistID2, varID, varname);
205
if ( strcmp(varname, name) == 0 ) break;
210
if ( nml->entry[nml_code]->occ ) vlistDefVarCode(vlistID2, varID, code);
211
if ( nml->entry[nml_new_code]->occ ) vlistDefVarCode(vlistID2, varID, new_code);
212
if ( nml->entry[nml_name]->occ ) vlistDefVarName(vlistID2, varID, name);
213
if ( nml->entry[nml_new_name]->occ ) vlistDefVarName(vlistID2, varID, new_name);
214
if ( nml->entry[nml_stdname]->occ ) vlistDefVarStdname(vlistID2, varID, stdname);
215
if ( nml->entry[nml_longname]->occ ) vlistDefVarLongname(vlistID2, varID, longname);
216
if ( nml->entry[nml_units]->occ ) vlistDefVarUnits(vlistID2, varID, units);
222
cdoPrint("Variable %s not found!", name);
239
void *IFS2ICON(void *argument)
241
static const char *func = "IFS2ICON";
242
int streamID1, streamID2 = CDI_UNDEFID;
243
int nrecs, nvars, newval = -1, tabnum = 0;
244
int tsID1, recID, varID, levelID;
245
int vlistID1, vlistID2;
246
int taxisID1, taxisID2;
249
int index, zaxisID1, zaxisID2, nzaxis, nlevs;
256
const char *paramfile;
259
double *levels = NULL;
260
double *array = NULL;
261
param_t param[MAX_PARAM];
262
nml_control_t nml_control;
265
cdoInitialize(argument);
267
operatorInputArg("namelist file and parameter file");
268
operatorCheckArgc(2);
271
nmlfile = operatorArgv()[0];
273
fp_nml = fopen(nmlfile, "r");
274
if ( fp_nml == NULL ) cdoAbort("Open failed on %s", nmlfile);
276
nml_control_init(&nml_control);
277
status = nml_control_read(fp_nml, &nml_control);
278
if ( status > 0 ) nml_error(status, nml_control.nml_name);
284
nml_control_print(nml_control);
287
paramfile = operatorArgv()[1];
288
read_param(paramfile, param, MAX_PARAM, &nparam);
290
for ( i = 0; i < nparam; ++i )
291
printf("%s %d %c\n", param[i].name, param[i].pos, param[i].itype);
293
streamID1 = streamOpenRead(cdoStreamName(0));
294
if ( streamID1 < 0 ) cdiError(streamID1, "Open failed on %s", cdoStreamName(0));
296
vlistID1 = streamInqVlist(streamID1);
297
vlistID2 = vlistDuplicate(vlistID1);
298
vlistPrint(vlistID2);
300
taxisID1 = vlistInqTaxis(vlistID1);
301
taxisID2 = taxisDuplicate(taxisID1);
302
vlistDefTaxis(vlistID2, taxisID2);
304
streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());
305
if ( streamID2 < 0 ) cdiError(streamID2, "Open failed on %s", cdoStreamName(1));
307
streamDefVlist(streamID2, vlistID2);
309
gridsize = vlistGridsizeMax(vlistID2);
310
array = (double *) malloc(gridsize*sizeof(double));
313
while ( (nrecs = streamInqTimestep(streamID1, tsID1)) )
315
taxisCopyTimestep(taxisID2, taxisID1);
317
streamDefTimestep(streamID2, tsID1);
319
for ( recID = 0; recID < nrecs; recID++ )
321
streamInqRecord(streamID1, &varID, &levelID);
322
streamDefRecord(streamID2, varID, levelID);
324
streamReadRecord(streamID1, array, &nmiss);
325
streamWriteRecord(streamID2, array, nmiss);
330
streamClose(streamID2);
331
streamClose(streamID1);
333
if ( array ) free(array);