Commit 480424ff authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

read netCDF: cache time variable

parent ce4d3cf6
2014-10-01 Uwe Schulzweida
2014-10-23 Uwe Schulzweida
* Version 1.7.0 released
* using CGRIBEX library version 1.7.0
* Version 1.6.5 released
* using CGRIBEX library version 1.6.5
2014-10-07 Uwe Schulzweida
* read netCDF: cache time variable
2014-08-21 Uwe Schulzweida
......
......@@ -16,11 +16,12 @@ void basetimeInit(basetime_t *basetime)
if ( basetime == NULL )
Error("Internal problem! Basetime not allocated.");
(*basetime).ncvarid = UNDEFID;
(*basetime).ncdimid = UNDEFID;
(*basetime).ncvarboundsid = UNDEFID;
(*basetime).leadtimeid = UNDEFID;
(*basetime).lwrf = 0;
basetime->ncvarid = UNDEFID;
basetime->ncdimid = UNDEFID;
basetime->ncvarboundsid = UNDEFID;
basetime->leadtimeid = UNDEFID;
basetime->lwrf = 0;
basetime->timevar_cache = NULL;
}
/*
* Local Variables:
......
#ifndef _BASETIME_H
#define _BASETIME_H
#define MAX_TIMECACHE_SIZE 1024
typedef struct {
int size;
int startid;
int maxvals;
double cache[MAX_TIMECACHE_SIZE];
}
timecache_t;
typedef struct {
int ncvarid;
......@@ -8,6 +17,7 @@ typedef struct {
int ncvarboundsid;
int leadtimeid;
int lwrf; /* TRUE for time axis in WRF format */
timecache_t *timevar_cache;
}
basetime_t;
......
......@@ -1013,6 +1013,8 @@ void streamClose(int streamID)
if ( streamptr->tsteps ) free(streamptr->tsteps);
if ( streamptr->basetime.timevar_cache ) free(streamptr->basetime.timevar_cache);
if ( streamptr->nfiles > 0 )
{
for ( index = 0; index < streamptr->nfiles; ++index )
......
......@@ -7967,6 +7967,62 @@ int cdfInqContents(stream_t *streamptr)
return (0);
}
static
void wrf_read_timestep(int fileID, int nctimevarid, int tsID, taxis_t *taxis)
{
size_t start[2], count[2];
char stvalue[32];
start[0] = (size_t) tsID; start[1] = 0;
count[0] = 1; count[1] = 19;
stvalue[0] = 0;
cdf_get_vara_text(fileID, nctimevarid, start, count, stvalue);
stvalue[19] = 0;
{
int year = 1, month = 1, day = 1 , hour = 0, minute = 0, second = 0;
if ( strlen(stvalue) == 19 )
sscanf(stvalue, "%d-%d-%d_%d:%d:%d", &year, &month, &day, &hour, &minute, &second);
taxis->vdate = cdiEncodeDate(year, month, day);
taxis->vtime = cdiEncodeTime(hour, minute, second);
taxis->type = TAXIS_ABSOLUTE;
}
}
static
double get_timevalue(int fileID, int nctimevarid, int tsID, timecache_t *tcache)
{
double timevalue = 0;
size_t index = (size_t) tsID;
if ( tcache )
{
if ( tcache->size == 0 || (tsID < tcache->startid || tsID > (tcache->startid+tcache->size-1)) )
{
int maxvals = MAX_TIMECACHE_SIZE;
tcache->startid = (tsID/MAX_TIMECACHE_SIZE)*MAX_TIMECACHE_SIZE;
if ( (tcache->startid + maxvals) > tcache->maxvals ) maxvals = (tcache->maxvals)%MAX_TIMECACHE_SIZE;
tcache->size = maxvals;
index = (size_t) tcache->startid;
// fprintf(stderr, "fill time cache: %d %d %d %d %d\n", tcache->maxvals, tsID, tcache->startid, tcache->startid+maxvals-1, maxvals);
for ( int ival = 0; ival < maxvals; ++ival )
{
cdf_get_var1_double(fileID, nctimevarid, &index, &timevalue);
if ( timevalue >= NC_FILL_DOUBLE || timevalue < -NC_FILL_DOUBLE ) timevalue = 0;
tcache->cache[ival] = timevalue;
index++;
}
}
timevalue = tcache->cache[tsID%MAX_TIMECACHE_SIZE];
}
else
{
cdf_get_var1_double(fileID, nctimevarid, &index, &timevalue);
if ( timevalue >= NC_FILL_DOUBLE || timevalue < -NC_FILL_DOUBLE ) timevalue = 0;
}
return timevalue;
}
int cdfInqTimestep(stream_t * streamptr, int tsID)
{
......@@ -7974,7 +8030,6 @@ int cdfInqTimestep(stream_t * streamptr, int tsID)
#if defined (HAVE_LIBNETCDF)
double timevalue;
int fileID;
size_t index;
taxis_t *taxis;
if ( CDI_Debug ) Message("streamID = %d tsID = %d", streamptr->self, tsID);
......@@ -7995,31 +8050,21 @@ int cdfInqTimestep(stream_t * streamptr, int tsID)
if ( nctimevarid != UNDEFID )
{
fileID = streamptr->fileID;
index = (size_t)tsID;
size_t index = (size_t)tsID;
if ( streamptr->basetime.lwrf )
{
size_t start[2], count[2];
char stvalue[32];
start[0] = index; start[1] = 0;
count[0] = 1; count[1] = 19;
stvalue[0] = 0;
cdf_get_vara_text(fileID, nctimevarid, start, count, stvalue);
stvalue[19] = 0;
{
int year = 1, month = 1, day = 1 , hour = 0, minute = 0, second = 0;
if ( strlen(stvalue) == 19 )
sscanf(stvalue, "%d-%d-%d_%d:%d:%d", &year, &month, &day, &hour, &minute, &second);
taxis->vdate = cdiEncodeDate(year, month, day);
taxis->vtime = cdiEncodeTime(hour, minute, second);
taxis->type = TAXIS_ABSOLUTE;
}
wrf_read_timestep(fileID, nctimevarid, tsID, taxis);
}
else
{
cdf_get_var1_double(fileID, nctimevarid, &index, &timevalue);
if ( timevalue >= NC_FILL_DOUBLE || timevalue < -NC_FILL_DOUBLE ) timevalue = 0;
if ( streamptr->basetime.timevar_cache == NULL )
{
streamptr->basetime.timevar_cache = (timecache_t *) malloc(MAX_TIMECACHE_SIZE*sizeof(timecache_t));
streamptr->basetime.timevar_cache->size = 0;
streamptr->basetime.timevar_cache->maxvals = streamptr->ntsteps;
}
timevalue = get_timevalue(fileID, nctimevarid, tsID, streamptr->basetime.timevar_cache);
cdiDecodeTimeval(timevalue, taxis, &taxis->vdate, &taxis->vtime);
}
......@@ -8043,7 +8088,7 @@ int cdfInqTimestep(stream_t * streamptr, int tsID)
int leadtimeid = streamptr->basetime.leadtimeid;
if ( leadtimeid != UNDEFID )
{
cdf_get_var1_double(fileID, leadtimeid, &index, &timevalue);
timevalue = get_timevalue(fileID, leadtimeid, tsID, NULL);
cdiSetForecastPeriod(timevalue, taxis);
}
}
......
Markdown is supported
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