Examples for Extracting Time from RAF netCDF files


This document provides routines and test programs for extracting time from the RAF netCDF data files (files starting in the year 2006). There are both C examples and Fortran examples (under construction).

The C Standard Library contains a function called strptime(3). This function can easily decode the "units" field below into a "struct tm" and the examples below demonstrate this.

For descriptions of strptime and "struct tm", see:


Example netCDF Time variable declaration

        int Time(Time) ;
                Time:long_name = "time of measurement" ;
                Time:standard_name = "time" ;
                Time:units = "seconds since 2001-09-20 14:38:33 +0000" ;
                Time:strptime_format = "seconds since %F %T %z" ;


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;
  int	*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(int));
  nc_get_var_int(ncid, varID, fp);

  for (i = 0; i ‹ total; ++i)
  {
    printf("%5d %04d/%02d/%02d %02d:%02d:%02d\n", fp[i],
	GetFlightYear(fp[i]),
	GetFlightMonth(fp[i]),
	GetFlightDay(fp[i]),
	GetFlightHour(fp[i]),
	GetFlightMinute(fp[i]),
	GetFlightSecond(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: Fri Jan 5 06:11:58 MST 2007