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

netCDF: added support for time axis name and long_name

parent 1616b4aa
......@@ -2,6 +2,10 @@
* Version 1.5.9 released
2012-11-26 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* netCDF: added support for time axis name and long_name
2012-11-17 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* stream_cdf::define_all_vars: added txt attributes to vlistDefAttTxt() without trailing 0 [Bug #3004]
......
......@@ -876,7 +876,7 @@ int main(int argc, char *argv[])
int recID;
int tsID;
int ntsteps = 0;
int taxisID;
int taxisID1, taxisID2 = CDI_UNDEFID;
int gridtype;
int vlistID1, vlistID2 = CDI_UNDEFID;
......@@ -897,13 +897,18 @@ int main(int argc, char *argv[])
}
nvars = vlistNvars(vlistID1);
taxisID = vlistInqTaxis(vlistID1);
taxisID1 = vlistInqTaxis(vlistID1);
ntsteps = vlistNtsteps(vlistID1);
if ( Debug ) fprintf(stderr, "nvars = %d\n", nvars);
if ( Debug ) fprintf(stderr, "ntsteps = %d\n", ntsteps);
if ( fname2 ) vlistID2 = vlistDuplicate(vlistID1);
if ( fname2 )
{
vlistID2 = vlistDuplicate(vlistID1);
taxisID2 = taxisDuplicate(taxisID1);
vlistDefTaxis(vlistID2, taxisID2);
}
for ( varID = 0; varID < nvars; varID++)
{
......@@ -966,10 +971,12 @@ int main(int argc, char *argv[])
while ( (nrecs = streamInqTimestep(streamID1, tsID)) > 0 )
{
if ( fname2 /* && ntsteps != 0*/ )
streamDefTimestep(streamID2, tsID);
vdate = taxisInqVdate(taxisID);
vtime = taxisInqVtime(taxisID);
{
taxisCopyTimestep(taxisID2, taxisID1);
streamDefTimestep(streamID2, tsID);
}
vdate = taxisInqVdate(taxisID1);
vtime = taxisInqVtime(taxisID1);
if ( Debug )
fprintf(stdout, "tsID = %d nrecs = %d date = %d time = %d\n", tsID, nrecs, vdate, vtime);
......
......@@ -835,6 +835,8 @@ void cdfDefTime(int streamID)
char unitstr[80];
char calstr[80];
char tmpstr[256];
char default_name[] = "time";
char *taxis_name = default_name;
size_t len;
taxis_t *taxis;
stream_t *streamptr;
......@@ -849,19 +851,23 @@ void cdfDefTime(int streamID)
if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
cdf_def_dim(fileID, "time", NC_UNLIMITED, &streamptr->basetime.ncdimid);
taxis = &streamptr->tsteps[0].taxis;
dims[0] = streamptr->basetime.ncdimid;
cdf_def_var(fileID, "time", NC_DOUBLE, 1, dims, &time_varid);
if ( taxis->name && taxis->name[0] ) taxis_name = taxis->name;
streamptr->basetime.ncvarid = time_varid;
cdf_def_dim(fileID, taxis_name, NC_UNLIMITED, &streamptr->basetime.ncdimid);
taxis = &streamptr->tsteps[0].taxis;
dims[0] = streamptr->basetime.ncdimid;
cdf_def_var(fileID, taxis_name, NC_DOUBLE, 1, dims, &time_varid);
streamptr->basetime.ncvarid = time_varid;
strcpy(tmpstr, "time");
cdf_put_att_text(fileID, time_varid, "standard_name", strlen(tmpstr), tmpstr);
if ( taxis->longname && taxis->longname[0] )
cdf_put_att_text(fileID, time_varid, "long_name", strlen(taxis->longname), taxis->longname);
if ( taxis->has_bounds )
{
/* fprintf(stderr, "time has bounds\n"); */
......@@ -869,11 +875,13 @@ void cdfDefTime(int streamID)
if ( nc_inq_dimid(fileID, "nb2", &dims[1]) != NC_NOERR )
cdf_def_dim(fileID, "nb2", 2, &dims[1]);
cdf_def_var(fileID, "time_bnds", NC_DOUBLE, 2, dims, &time_bndsid);
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", 9, "time_bnds");
cdf_put_att_text(fileID, time_varid, "bounds", strlen(tmpstr), tmpstr);
}
unitstr[0] = 0;
......@@ -6758,6 +6766,16 @@ int cdfInqContents(int streamID)
if ( time_has_bounds )
streamptr->tsteps[0].taxis.has_bounds = TRUE;
if ( ncvarid != -1 )
{
taxis_t *taxis;
taxis = &streamptr->tsteps[0].taxis;
taxis->name = strdup(ncvars[ncvarid].name);
if ( ncvars[ncvarid].longname[0] )
taxis->longname = strdup(ncvars[ncvarid].longname);
}
if ( ncvarid != -1 )
if ( ncvars[ncvarid].calendar == TRUE )
{
......
......@@ -625,7 +625,7 @@ void streamPrintP ( void * streamptr, FILE * fp )
fprintf ( fp, "rtsteps = %d\n", sp->rtsteps );
fprintf ( fp, "//long ntsteps;\n" );
fprintf ( fp, "numTimestep = %d\n", sp->numTimestep );
fprintf ( fp, "// TSTEPS *tsteps;\n" );
fprintf ( fp, "// tsteps_t *tsteps;\n" );
fprintf ( fp, "tstepsTableSize= %d\n", sp->tstepsTableSize );
fprintf ( fp, "tstepsNextID = %d\n", sp->tstepsNextID );
fprintf ( fp, "//BaseTime basetime;\n" );
......
......@@ -32,9 +32,6 @@ char *strdup(const char *s);
#endif
#ifndef _CDI_H
# include "cdi.h"
#endif
#ifndef _ERROR_H
# include "error.h"
#endif
......@@ -178,7 +175,7 @@ typedef struct {
off_t position; /* timestep file position */
taxis_t taxis;
}
TSTEPS;
tsteps_t;
typedef struct {
......@@ -227,7 +224,7 @@ typedef struct {
int rtsteps; /* number of tsteps accessed */
long ntsteps; /* number of tsteps : only set if all records accessed */
int numTimestep; /* number of tsteps : only set if all records accessed */
TSTEPS *tsteps;
tsteps_t *tsteps;
int tstepsTableSize;
int tstepsNextID;
BaseTime basetime;
......
......@@ -122,8 +122,8 @@ void taxisDefaultValue ( taxis_t *taxisptr )
taxisptr->vtime_lb = 0;
taxisptr->vdate_ub = 0;
taxisptr->vtime_ub = 0;
taxisptr->name[0] = 0;
taxisptr->longname[0] = 0;
taxisptr->name = NULL;
taxisptr->longname = NULL;
}
static
......@@ -255,6 +255,9 @@ void taxisDestroy(int taxisID)
taxisptr = ( taxis_t * ) reshGetVal ( taxisID, &taxisOps );
if ( taxisptr->name ) free(taxisptr->name);
if ( taxisptr->longname ) free(taxisptr->longname);
taxisDestroyKernel ( taxisptr );
}
......@@ -538,7 +541,6 @@ void taxisCopyTimestep(int taxisID2, int taxisID1)
taxis_t *taxisptr1;
taxis_t *taxisptr2;
taxisptr1 = ( taxis_t * ) reshGetVal ( taxisID1, &taxisOps );
taxisptr2 = ( taxis_t * ) reshGetVal ( taxisID2, &taxisOps );
......@@ -561,6 +563,9 @@ void taxisCopyTimestep(int taxisID2, int taxisID1)
taxisptr2->vtime_ub = taxisptr1->vtime_ub;
}
if ( taxisptr2->name == NULL && taxisptr1->name && taxisptr1->name[0] )
taxisptr2->name = strdup(taxisptr1->name);
reshUnlock ();
}
......@@ -1058,7 +1063,7 @@ void conv_timeval(double timevalue, int *rvdate, int *rvtime)
daysec = (int) (-(timevalue - vdate)*86400 + 0.01);
else
daysec = (int) ( (timevalue - vdate)*86400 + 0.01);
hour = daysec / 3600;
minute = (daysec - hour*3600)/60;
second = daysec - hour*3600 - minute*60;
......@@ -1220,8 +1225,8 @@ void ptaxisCopy(taxis_t *dest, taxis_t *source)
dest->vdate_ub = source->vdate_ub;
dest->vtime_ub = source->vtime_ub;
strcpy(dest->name, source->name);
strcpy(dest->longname, source->longname);
if ( source->name ) dest->name = strdup(source->name);
if ( source->longname ) dest->longname = strdup(source->longname);
reshUnlock ();
}
......
......@@ -20,8 +20,8 @@ typedef struct {
int vtime_lb; /* lower bounds of vtime */
int vdate_ub; /* upper bounds of vdate */
int vtime_ub; /* upper bounds of vtime */
char name[CDI_MAX_NAME];
char longname[CDI_MAX_NAME];
char *name;
char *longname;
}
taxis_t;
......
......@@ -30,7 +30,7 @@ int tstepsNewEntry(int streamID)
{
int tsID = 0;
int tstepsTableSize;
TSTEPS *tstepsTable;
tsteps_t *tstepsTable;
stream_t *streamptr;
streamptr = stream_to_pointer(streamID);
......@@ -46,11 +46,11 @@ int tstepsNewEntry(int streamID)
{
if ( tstepsTableSize == 0 ) tstepsTableSize = 1;
tstepsTableSize = 2*tstepsTableSize;
tstepsTable = (TSTEPS *) realloc(tstepsTable, tstepsTableSize*sizeof(TSTEPS));
tstepsTable = (tsteps_t *) realloc(tstepsTable, tstepsTableSize*sizeof(tsteps_t));
if ( tstepsTable == NULL )
{
Message("tstepsTableSize = %d", tstepsTableSize);
SysError("Reallocation of TSTEPS failed");
SysError("Reallocation of tsteps_t failed");
}
}
......@@ -78,9 +78,9 @@ void cdiCreateTimesteps(int streamID)
if ( streamptr->ntsteps == 0 ) ntsteps = 1; /* <<<<<-------- */
else ntsteps = streamptr->ntsteps;
streamptr->tsteps = (TSTEPS *) malloc(ntsteps*sizeof(TSTEPS));
streamptr->tsteps = (tsteps_t *) malloc(ntsteps*sizeof(tsteps_t));
if ( streamptr->tsteps == NULL )
SysError("Allocation of TSTEPS failed");
SysError("Allocation of tsteps_t failed");
streamptr->tstepsTableSize = ntsteps;
streamptr->tstepsNextID = ntsteps;
......
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