Commit b8de667b authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

stream_cdf: added cdfDefTimeUnits()

parent 05e9afd7
......@@ -749,10 +749,10 @@ int cdfReadRecord(stream_t *streamptr, double *data, int *nmiss)
return (ierr);
}
#if defined (HAVE_LIBNETCDF)
static
void cdfDefTimeValue(stream_t *streamptr, int tsID)
{
#if defined (HAVE_LIBNETCDF)
int fileID;
double timevalue;
int ncvarid;
......@@ -797,9 +797,86 @@ void cdfDefTimeValue(stream_t *streamptr, int tsID)
/*
printf("fileID = %d %d %d %f\n", fileID, time_varid, index, timevalue);
*/
#endif
}
static
int cdfDefTimeBounds(int fileID, int nctimevarid, int nctimedimid, char* taxis_name, taxis_t* taxis)
{
int time_bndsid = -1;
int dims[2];
char tmpstr[CDI_MAX_NAME];
dims[0] = nctimedimid;
/* fprintf(stderr, "time has bounds\n"); */
if ( nc_inq_dimid(fileID, "nb2", &dims[1]) != NC_NOERR )
cdf_def_dim(fileID, "nb2", 2, &dims[1]);
if ( taxis->climatology )
{
strcpy(tmpstr, "climatology");
strcat(tmpstr, "_bnds");
cdf_def_var(fileID, tmpstr, NC_DOUBLE, 2, dims, &time_bndsid);
cdf_put_att_text(fileID, nctimevarid, "climatology", strlen(tmpstr), tmpstr);
}
else
{
strcpy(tmpstr, taxis_name);
strcat(tmpstr, "_bnds");
cdf_def_var(fileID, tmpstr, NC_DOUBLE, 2, dims, &time_bndsid);
cdf_put_att_text(fileID, nctimevarid, "bounds", strlen(tmpstr), tmpstr);
}
return (time_bndsid);
}
static
void cdfDefTimeUnits(char *unitstr, taxis_t* taxis0, taxis_t* taxis)
{
unitstr[0] = 0;
if ( taxis0->type == TAXIS_ABSOLUTE )
{
if ( taxis0->unit == TUNIT_YEAR )
sprintf(unitstr, "year as %s", "%Y.%f");
else if ( taxis0->unit == TUNIT_MONTH )
sprintf(unitstr, "month as %s", "%Y%m.%f");
else
sprintf(unitstr, "day as %s", "%Y%m%d.%f");
}
else
{
int year, month, day, hour, minute, second;
int rdate, rtime;
int timeunit;
timeunit = taxis->unit;
if ( timeunit == -1 ) timeunit = TUNIT_HOUR;
rdate = taxis->rdate;
rtime = taxis->rtime;
if ( rdate == -1 )
{
rdate = taxis->vdate;
rtime = taxis->vtime;
}
cdiDecodeDate(rdate, &year, &month, &day);
cdiDecodeTime(rtime, &hour, &minute, &second);
if ( timeunit == TUNIT_QUARTER ) timeunit = TUNIT_MINUTE;
if ( timeunit == TUNIT_3HOURS ||
timeunit == TUNIT_6HOURS ||
timeunit == TUNIT_12HOURS ) timeunit = TUNIT_HOUR;
sprintf(unitstr, "%s since %d-%02d-%02d %02d:%02d:%02d",
tunitNamePtr(timeunit), year, month, day, hour, minute, second);
}
}
#endif
static
void cdfDefCalendar(int fileID, int ncvarid, int calendar)
{
......@@ -817,24 +894,25 @@ void cdfDefCalendar(int fileID, int ncvarid, int calendar)
len = strlen(calstr);
#if defined (HAVE_LIBNETCDF)
if ( len ) cdf_put_att_text(fileID, ncvarid, "calendar", len, calstr);
#endif
}
static
void cdfDefTime(stream_t *streamptr)
void cdfDefTime(stream_t* streamptr)
{
#if defined (HAVE_LIBNETCDF)
int fileID;
int time_varid;
int time_bndsid;
int dims[2];
int year, month, day, hour, minute, second;
char unitstr[80];
int time_dimid;
int time_bndsid = -1;
char unitstr[CDI_MAX_NAME];
char tmpstr[CDI_MAX_NAME];
char default_name[] = "time";
char *taxis_name = default_name;
char* taxis_name = default_name;
size_t len;
taxis_t *taxis;
taxis_t* taxis;
if ( streamptr->basetime.ncvarid != UNDEFID ) return;
......@@ -848,10 +926,10 @@ void cdfDefTime(stream_t *streamptr)
if ( taxis->name && taxis->name[0] ) taxis_name = taxis->name;
cdf_def_dim(fileID, taxis_name, NC_UNLIMITED, &streamptr->basetime.ncdimid);
cdf_def_dim(fileID, taxis_name, NC_UNLIMITED, &time_dimid);
streamptr->basetime.ncdimid = time_dimid;
dims[0] = streamptr->basetime.ncdimid;
cdf_def_var(fileID, taxis_name, NC_DOUBLE, 1, dims, &time_varid);
cdf_def_var(fileID, taxis_name, NC_DOUBLE, 1, &time_dimid, &time_varid);
streamptr->basetime.ncvarid = time_varid;
......@@ -863,69 +941,11 @@ void cdfDefTime(stream_t *streamptr)
if ( taxis->has_bounds )
{
/* fprintf(stderr, "time has bounds\n"); */
if ( nc_inq_dimid(fileID, "nb2", &dims[1]) != NC_NOERR )
cdf_def_dim(fileID, "nb2", 2, &dims[1]);
if ( taxis->climatology )
{
strcpy(tmpstr, "climatology");
strcat(tmpstr, "_bnds");
cdf_def_var(fileID, tmpstr, NC_DOUBLE, 2, dims, &time_bndsid);
streamptr->basetime.ncvarboundsid = time_bndsid;
cdf_put_att_text(fileID, time_varid, "climatology", strlen(tmpstr), tmpstr);
}
else
{
strcpy(tmpstr, taxis_name);
strcat(tmpstr, "_bnds");
cdf_def_var(fileID, tmpstr, NC_DOUBLE, 2, dims, &time_bndsid);
streamptr->basetime.ncvarboundsid = time_bndsid;
cdf_put_att_text(fileID, time_varid, "bounds", strlen(tmpstr), tmpstr);
}
}
unitstr[0] = 0;
if ( streamptr->tsteps[0].taxis.type == TAXIS_ABSOLUTE )
{
if ( streamptr->tsteps[0].taxis.unit == TUNIT_YEAR )
sprintf(unitstr, "year as %s", "%Y.%f");
else if ( streamptr->tsteps[0].taxis.unit == TUNIT_MONTH )
sprintf(unitstr, "month as %s", "%Y%m.%f");
else
sprintf(unitstr, "day as %s", "%Y%m%d.%f");
time_bndsid = cdfDefTimeBounds(fileID, time_varid, time_dimid, taxis_name, taxis);
streamptr->basetime.ncvarboundsid = time_bndsid;
}
else
{
int rdate, rtime;
int timeunit;
timeunit = taxis->unit;
if ( timeunit == -1 ) timeunit = TUNIT_HOUR;
rdate = taxis->rdate;
rtime = taxis->rtime;
if ( rdate == -1 )
{
rdate = taxis->vdate;
rtime = taxis->vtime;
}
cdiDecodeDate(rdate, &year, &month, &day);
cdiDecodeTime(rtime, &hour, &minute, &second);
if ( timeunit == TUNIT_QUARTER ) timeunit = TUNIT_MINUTE;
if ( timeunit == TUNIT_3HOURS ||
timeunit == TUNIT_6HOURS ||
timeunit == TUNIT_12HOURS ) timeunit = TUNIT_HOUR;
sprintf(unitstr, "%s since %d-%02d-%02d %02d:%02d:%02d",
tunitNamePtr(timeunit), year, month, day, hour, minute, second);
}
cdfDefTimeUnits(unitstr, &streamptr->tsteps[0].taxis, taxis);
len = strlen(unitstr);
if ( len )
......
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