Commit 35fd263a authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Refactor cdo_parse_cmor_file() to cmortable_to_pmlist().

parent ebe963d1
......@@ -171,7 +171,7 @@ void cdo_check_data(int vlistID2, int varID2, var_t *var, long gridsize, double
}
static
void apply_cmor_table(const char *filename, int nvars, int vlistID2, var_t *vars)
void apply_cmorlist(list_t *pmlist, int nvars, int vlistID2, var_t *vars)
{
const char *hentry[] = {"Header"};
const char *ventry[] = {"variable_entry"};
......@@ -179,9 +179,6 @@ void apply_cmor_table(const char *filename, int nvars, int vlistID2, var_t *vars
int nhentry = (int) sizeof(hentry)/sizeof(hentry[0]);
char varname[CDI_MAX_NAME];
list_t *pmlist = cdo_parse_cmor_file(filename);
if ( pmlist == NULL ) return;
// search for global missing value
bool lmissval = false;
double missval;
......@@ -311,11 +308,9 @@ void apply_cmor_table(const char *filename, int nvars, int vlistID2, var_t *vars
}
else
{
cdoPrint("Variable %s not found!", varname);
cdoPrint("Variable %s not found in cmor table!", varname);
}
}
list_destroy(pmlist);
}
......@@ -362,7 +357,14 @@ void *CMOR_lite(void *argument)
for ( varID = 0; varID < nvars; ++varID ) vars[varID].convert = true;
const char *filename = operatorArgv()[0];
apply_cmor_table(filename, nvars, vlistID2, vars);
FILE *fp = fopen(filename, "r");
if ( fp == NULL ) cdoAbort("Open failed on: %s\n", filename);
list_t *pmlist = cmortable_to_pmlist(fp, filename);
fclose(fp);
apply_cmorlist(pmlist, nvars, vlistID2, vars);
list_destroy(pmlist);
for ( int varID = 0; varID < nvars; ++varID )
if ( vars[varID].remove )
......
......@@ -26,19 +26,16 @@
static
int dump_cmor_table(const char *filename)
{
list_t *pml = cdo_parse_cmor_file(filename);
if ( pml == NULL ) return -1;
printf("# Number of lists: %d\n", list_size(pml));
void dump_cmor_table(list_t *pmlist)
{
printf("# Number of lists: %d\n", list_size(pmlist));
int i = 0;
for ( listNode_t *pmnode = pml->head; pmnode; pmnode = pmnode->next )
for ( listNode_t *pmnode = pmlist->head; pmnode; pmnode = pmnode->next )
{
list_t *kvl = *(list_t **)pmnode->data;
printf("# list ID: %d; Number of elements: %d\n", i, list_size(kvl));
printf("&%s\n", list_name(kvl));
for ( listNode_t *kvnode = kvl->head; kvnode; kvnode = kvnode->next )
list_t *kvlist = *(list_t **)pmnode->data;
printf("# list ID: %d; Number of elements: %d\n", i, list_size(kvlist));
printf("&%s\n", list_name(kvlist));
for ( listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next )
{
keyValues_t *kv = *(keyValues_t **)kvnode->data;
if ( kv ) printf(" %s = %s\n", kv->key, kv->values[0]);
......@@ -46,33 +43,26 @@ int dump_cmor_table(const char *filename)
printf("/\n");
++i;
}
list_destroy(pml);
return 0;
}
static
int conv_cmor_table(const char *filename)
void conv_cmor_table(list_t *pmlist)
{
const char *hname = "Header";
const char *vname = "variable";
//const char *aname = "axis";
list_t *pml = cdo_parse_cmor_file(filename);
if ( pml == NULL ) return -1;
bool hasmissval = false;
double missval;
for ( listNode_t *pmnode = pml->head; pmnode; pmnode = pmnode->next )
for ( listNode_t *pmnode = pmlist->head; pmnode; pmnode = pmnode->next )
{
list_t *kvl = *(list_t **)pmnode->data;
const char *listname = list_name(kvl);
list_t *kvlist = *(list_t **)pmnode->data;
const char *listname = list_name(kvlist);
if ( strncmp(listname, hname, strlen(hname)) == 0 )
{
for ( listNode_t *kvnode = kvl->head; kvnode; kvnode = kvnode->next )
for ( listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next )
{
keyValues_t *kv = *(keyValues_t **)kvnode->data;
const char *ename = kv->key;
......@@ -89,7 +79,7 @@ int conv_cmor_table(const char *filename)
else if ( strncmp(listname, vname, strlen(vname)) == 0 )
{
printf("&%s\n", "parameter");
for ( listNode_t *kvnode = kvl->head; kvnode; kvnode = kvnode->next )
for ( listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next )
{
keyValues_t *kv = *(keyValues_t **)kvnode->data;
const char *ename = kv->key;
......@@ -139,10 +129,6 @@ int conv_cmor_table(const char *filename)
printf("/\n");
}
}
list_destroy(pml);
return 0;
}
......@@ -159,9 +145,17 @@ void *CMOR_table(void *argument)
const char *filename = operatorArgv()[0];
if ( cdoVerbose ) cdoPrint("Parse file: %s", filename);
FILE *fp = fopen(filename, "r");
if ( fp == NULL ) cdoAbort("Open failed on: %s\n", filename);
list_t *pmlist = cmortable_to_pmlist(fp, filename);
fclose(fp);
if ( operatorID == DUMP_CMOR_TABLE ) dump_cmor_table(pmlist);
else if ( operatorID == CONV_CMOR_TABLE ) conv_cmor_table(pmlist);
if ( operatorID == DUMP_CMOR_TABLE ) dump_cmor_table(filename);
else if ( operatorID == CONV_CMOR_TABLE ) conv_cmor_table(filename);
list_destroy(pmlist);
cdoFinish();
......
......@@ -409,10 +409,10 @@ void *Setpartab(void *argument)
FILE *fp = fopen(filename, "r");
if ( fp == NULL ) cdoAbort("Open failed on: %s\n", filename);
list_t *pmlist = namelist_to_pmlist(fp, "filename");
list_t *pmlist = namelist_to_pmlist(fp, filename);
fclose(fp);
apply_parameterlist(ptmode, pmlist, nvars, vlistID2, vars);
list_destroy(pmlist);
for ( int varID = 0; varID < nvars; ++varID )
......
......@@ -171,9 +171,9 @@ void cdo_read_field(const char *name, char *pline, int size, double *field, int
double cdoZaxisInqLevel(int zaxisID, int levelID);
int cdoZaxisInqLevels(int zaxisID, double *levels);
list_t *cdo_parse_cmor_file(const char *filename);
list_t *namelistbuf_to_pmlist(listbuf_t *listbuf);
list_t *namelist_to_pmlist(FILE *fp, const char *name);
list_t *cmortable_to_pmlist(FILE *fp, const char *name);
int literal_get_datatype(const char *literal);
int literal_to_int(const char *literal);
......
......@@ -36,6 +36,7 @@ char *readLineFromBuffer(char *buffer, size_t *buffersize, char *line, size_t le
break;
}
}
line[ipos] = 0;
if ( *buffersize == 0 && ipos == 0 ) buffer = NULL;
......@@ -82,7 +83,7 @@ char *getElementValue(char *pline)
}
void parse_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
void cmortablebuf_to_pmlist(list_t *pmlist, size_t buffersize, char *buffer)
{
char line[4096];
char name[256];
......@@ -91,7 +92,7 @@ void parse_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
char listkey2[] = "variable_entry";
int linenumber = 0;
int listtype = 0;
list_t *kvl = NULL;
list_t *kvlist = NULL;
while ( (buffer = readLineFromBuffer(buffer, &buffersize, line, sizeof(line))) )
{
......@@ -108,13 +109,13 @@ void parse_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
listtype = 2;
kvl = kvlist_new(listkey1);
list_append(pml, &kvl);
kvlist = kvlist_new(listkey1);
list_append(pmlist, &kvlist);
pline = skipSeparator(pline);
pline = getElementValue(pline);
if ( *pline ) kvlist_append(kvl, "name", (const char **)&pline, 1);
if ( *pline ) kvlist_append(kvlist, "name", (const char **)&pline, 1);
}
else if ( strncmp(pline, listkey2, strlen(listkey2)) == 0 )
{
......@@ -122,13 +123,13 @@ void parse_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
listtype = 2;
kvl = kvlist_new(listkey2);
list_append(pml, &kvl);
kvlist = kvlist_new(listkey2);
list_append(pmlist, &kvlist);
pline = skipSeparator(pline);
pline = getElementValue(pline);
if ( *pline ) kvlist_append(kvl, "name", (const char **)&pline, 1);
if ( *pline ) kvlist_append(kvlist, "name", (const char **)&pline, 1);
}
else
{
......@@ -136,13 +137,13 @@ void parse_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
pline = skipSeparator(pline);
pline = getElementValue(pline);
if ( kvl == NULL )
if ( kvlist == NULL )
{
kvl = kvlist_new("Header");
list_append(pml, &kvl);
kvlist = kvlist_new("Header");
list_append(pmlist, &kvlist);
}
if ( *pline ) kvlist_append(kvl, name, (const char **)&pline, 1);
if ( *pline ) kvlist_append(kvlist, name, (const char **)&pline, 1);
}
}
}
......@@ -196,7 +197,7 @@ int dump_json(const char *js, jsmntok_t *t, size_t count, int level)
}
static
void kvlist_append_json(list_t *kvl, const char *key, const char *js, jsmntok_t *t, int nvalues)
void kvlist_append_json(list_t *kvlist, const char *key, const char *js, jsmntok_t *t, int nvalues)
{
keyValues_t *keyval = (keyValues_t *) malloc(sizeof(keyValues_t));
keyval->key = strdup(key);
......@@ -211,11 +212,11 @@ void kvlist_append_json(list_t *kvl, const char *key, const char *js, jsmntok_t
// printf("set %s: '%s'\n", key, value);
keyval->values[i] = value;
}
list_append(kvl, &keyval);
list_append(kvlist, &keyval);
}
static
int json_to_pml(list_t *pml, const char *js, jsmntok_t *t, int count)
int json_to_pmlist(list_t *pmlist, const char *js, jsmntok_t *t, int count)
{
bool debug = false;
char name[4096];
......@@ -235,8 +236,8 @@ int json_to_pml(list_t *pml, const char *js, jsmntok_t *t, int count)
snprintf(name, sizeof(name), "%.*s", t[pmlname].end - t[pmlname].start, js+t[pmlname].start);
name[sizeof(name)-1] = 0;
// printf("new object: %s\n", name);
list_t *kvl = kvlist_new(name);
list_append(pml, &kvl);
list_t *kvlist = kvlist_new(name);
list_append(pmlist, &kvlist);
if ( t[i+2].type == JSMN_OBJECT )
{
......@@ -244,7 +245,7 @@ int json_to_pml(list_t *pml, const char *js, jsmntok_t *t, int count)
else ic--;
++i;
kvlist_append_json(kvl, "name", js, &t[i], 1);
kvlist_append_json(kvlist, "name", js, &t[i], 1);
if ( debug ) printf(" name: '%.*s'\n", t[i].end - t[i].start, js+t[i].start);
++i;
}
......@@ -259,7 +260,7 @@ int json_to_pml(list_t *pml, const char *js, jsmntok_t *t, int count)
if ( t[i].type == JSMN_ARRAY )
{
int nae = t[i].size;
kvlist_append_json(kvl, name, js, &t[i+1], nae);
kvlist_append_json(kvlist, name, js, &t[i+1], nae);
while ( nae-- )
{
++i;
......@@ -268,7 +269,7 @@ int json_to_pml(list_t *pml, const char *js, jsmntok_t *t, int count)
}
else
{
kvlist_append_json(kvl, name, js, &t[i], 1);
kvlist_append_json(kvlist, name, js, &t[i], 1);
if ( debug ) printf(" '%.*s'", t[i].end - t[i].start, js+t[i].start);
}
if ( debug ) printf("\n");
......@@ -283,7 +284,7 @@ int json_to_pml(list_t *pml, const char *js, jsmntok_t *t, int count)
}
void parse_json_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
void cmortablebuf_to_pmlist_json(list_t *pmlist, size_t buffersize, char *buffer)
{
char *js = buffer;
size_t jslen = buffersize;
......@@ -309,51 +310,27 @@ void parse_json_buffer_to_pml(list_t *pml, size_t buffersize, char *buffer)
}
}
json_to_pml(pml, js, tok, (int)p.toknext);
json_to_pmlist(pmlist, js, tok, (int)p.toknext);
Free(tok);
}
list_t *cdo_parse_cmor_file(const char *filename)
list_t *cmortable_to_pmlist(FILE *fp, const char *name)
{
assert(filename != NULL);
size_t filesize = fileSize(filename);
if ( filesize == 0 )
{
fprintf(stderr, "Empty table file: %s\n", filename);
return NULL;
}
FILE *fp = fopen(filename, "r");
if ( fp == NULL )
{
fprintf(stderr, "Open failed on %s: %s\n", filename, strerror(errno));
return NULL;
}
char *buffer = (char*) Malloc(filesize);
size_t nitems = fread(buffer, 1, filesize, fp);
fclose(fp);
if ( nitems != filesize )
{
fprintf(stderr, "Read failed on %s!\n", filename);
return NULL;
}
list_t *pml = list_new(sizeof(list_t *), free_kvlist, filename);
listbuf_t *listbuf = listbuf_new();
if ( listbuf_read(listbuf, fp, name) ) cdoAbort("Read error on CMOR table %s!", name);
list_t *pmlist = list_new(sizeof(list_t *), free_kvlist, name);
if ( buffer[0] == '{' )
parse_json_buffer_to_pml(pml, filesize, buffer);
else if ( strncmp(buffer, "table_id:", 9) == 0 )
parse_buffer_to_pml(pml, filesize, buffer);
if ( listbuf->buffer[0] == '{' )
cmortablebuf_to_pmlist_json(pmlist, listbuf->size, listbuf->buffer);
else if ( strncmp(listbuf->buffer, "table_id:", 9) == 0 )
cmortablebuf_to_pmlist(pmlist, listbuf->size, listbuf->buffer);
else
cdoAbort("Invalid CMOR table (file: %s)!", filename);
cdoAbort("Invalid CMOR table (file: %s)!", name);
Free(buffer);
return pml;
listbuf_destroy(listbuf);
return pmlist;
}
......@@ -116,10 +116,11 @@ list_t *namelistbuf_to_pmlist(listbuf_t *listbuf)
return pmlist;
}
list_t *namelist_to_pmlist(FILE *fp, const char *name)
{
listbuf_t *listbuf = listbuf_new();
if ( listbuf_read(listbuf, fp, name) ) cdoAbort("Namelist read error!");
if ( listbuf_read(listbuf, fp, name) ) cdoAbort("Read error on namelist %s!", name);
list_t *pmlist = namelistbuf_to_pmlist(listbuf);
if ( pmlist == NULL ) cdoAbort("Namelist not found!");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment