Examples for Extracting Time from RAF netCDF files
Test C Program
#include ‹netcdf.h›
#include ‹time.h›
int main(int argc, char *argv[])
{
int ncid, varID, nDims, dimids[3];
int i, total, len;
float *fp;
time_t StartFlightUnixTime;
nc_open(argv[1], NC_NOWRITE, &ncid);
StartFlightUnixTime = InitFlightTime(ncid);
nc_inq_varid(ncid, "Time", &varID);
nc_inq_varndims(ncid, varID, &nDims);
nc_inq_vardimid(ncid, varID, dimids);
total = 1;
for (i = 0; i ‹ nDims; ++i)
{
nc_inq_dimlen(ncid, dimids[i], &len);
total *= len;
}
fp = malloc(total * sizeof(float));
nc_get_var_float(ncid, varID, fp);
for (i = 0; i ‹ total; ++i)
{
printf("%5d %04d/%02d/%02d %02d:%02d:%02d\n", fp[i],
GetFlightYear((int)fp[i]),
GetFlightMonth((int)fp[i]),
GetFlightDay((int)fp[i]),
GetFlightHour((int)fp[i]),
GetFlightMinute((int)fp[i]),
GetFlightSecond((int)fp[i]));
}
nc_close(ncid);
}
Example C Routines (raf-time.c)
#include ‹netcdf.h›
#include ‹time.h›
#include ‹stdlib.h›
#include ‹stdio.h›
/* Given an open netCDF file (ncid), return the Unix time (seconds since
* 01/01/1970) of Time::units. Subsequent values from 'Time' should be added
* to this return value.
*/
static time_t start_t = 0;
time_t InitFlightTime(int ncid)
{
int id;
putenv("TZ=UTC"); // Force all time routines to work in UTC.
if (nc_inq_varid(ncid, "Time", &id) == NC_NOERR)
{
struct tm StartFlight;
char t_units[128], units_frmt[128];
nc_get_att_text(ncid, id, "units", t_units);
nc_get_att_text(ncid, id, "strptime_format", units_frmt);
strptime(t_units, units_frmt, &StartFlight);
start_t = mktime(&StartFlight);
}
else
{
fprintf(stderr, "No 'Time' variable, this is fatal.\n");
exit(1);
}
return start_t;
}
int GetFlightYear(int currentTimeOffset)
{
time_t x = start_t + currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_year + 1900;
}
int GetFlightMonth(int currentTimeOffset)
{
time_t x = start_t + currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_mon + 1;
}
int GetFlightDay(int currentTimeOffset)
{
time_t x = start_t + currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_mday;
}
int GetFlightHour(int currentTimeOffset)
{
time_t x = start_t + currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_hour;
}
int GetFlightMinute(int currentTimeOffset)
{
time_t x = start_t + currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_min;
}
int GetFlightSecond(int currentTimeOffset)
{
time_t x = start_t + currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_sec;
}
C Routines to compile with Fortran Programs
This is still under a little construction. cjw 12/14/06
#include ‹stdio.h›
#include ‹string.h›
#include ‹time.h›
static const char * default_format = "seconds since %F %T %z";
static time_t start_t = 0;
/*
* Call this function first with the flight start date and time. This
* can be found as the "units" attribute in the "Time" variable.
*/
void init_flight_date__(char * start, char * format)
{
struct tm start_tm;
putenv("TZ=UTC"); /* Perform all time calculations in/as UTC. */
start[39] = '\0';
format[22] = '\0';
printf("[%s] [%s]\n", start, format);
if (format == 0 || strlen(format) == 0)
strptime(start, default_format, &start_tm);
else
strptime(start, format, &start_tm);
start_t = mktime(&start_tm);
}
int get_flight_year__(int * currentTimeOffset)
{
time_t x = start_t + *currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_year + 1900;
}
int get_flight_month__(int * currentTimeOffset)
{
time_t x = start_t + *currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_mon + 1;
}
int get_flight_day__(int * currentTimeOffset)
{
time_t x = start_t + *currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_mday;
}
int get_flight_hour__(int * currentTimeOffset)
{
time_t x = start_t + *currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_hour;
}
int get_flight_minute__(int * currentTimeOffset)
{
time_t x = start_t + *currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_min;
}
int get_flight_second__(int * currentTimeOffset)
{
time_t x = start_t + *currentTimeOffset;
struct tm * stm = gmtime(&x);
return stm-›tm_sec;
}
Last update:
Wed Dec 13 22:51:22 MST 2006