Difference between revisions of "Encoding Relational Tables in NetCDF"

From Earth Science Information Partners (ESIP)
Line 179: Line 179:
 
=== NILU / EBAS ===
 
=== NILU / EBAS ===
  
The EBAS database schema is rather large, we're only presenting the essential central tables. Full schema outline looks like this:
+
The EBAS database schema is rather large, full schema outline looks like this:
  
 
[[Image:EBAS_thumbnail.png]]
 
[[Image:EBAS_thumbnail.png]]
  
Some basic information about the design by Paul Eckhardt:
 
  
* Table DS_DATA_SET describes what we call a dataset in ebas: one parameter measured at a certain station (in the full version some more complex dependecies on instrument, method etc.).
+
The essential schema is not that big, only four tables.  
* ER_REGIME_CODE is always 'IMG' for observations.
 
* EM_MATRIX_NAME defines which medium the parameter is measured in (e.g. precipitation, air, pm10, ...)
 
* EC_COMP_NAME is the name of the parameter (e.g. sulphate_total, ...)
 
* DS_STARTDATE and DS_ENDDATE provide the timestamp of the first and last measurement in the timeseries.
 
* A1_TIME contains the measurements. Relates n:1 to DS_DATA_SETS, FK=DS_SET_KEY.
 
* EB_STATION contains the location data. relates 1:n to DS_DATA_SETS.
 
 
 
'''This gives us the essential schema:'''
 
  
 
[[Image:EBAS_essential.png]]
 
[[Image:EBAS_essential.png]]
Line 199: Line 190:
 
The essential four tables are:
 
The essential four tables are:
  
* EB_STATION: This contains the station information EB_STATION. It has fields for station name; lat, lon and alt; and some miscellaneous fields about the history of the station.
+
* EB_STATION: This contains the station information EB_STATION.  
 
+
** EB_STATION_CODE is the unique code.
* EC_COMPONENTS: This table contains the measured parameters. Fields '''EM_MATRIX_NAME''' and '''EC_COMP_NAME''' (medium and substance) define what is being measured and various other fields, like '''EC_UNIT''' define more metadata.
+
** lat, lon, alt; some miscellaneous fields about the history of the station.  
 
 
* DS_DATA_SET: A dataset is one parameter measured in one station.  
 
  
SELECT [EB_STATION_CODE]
+
* EC_COMPONENTS: This table contains the measured parameters. Fields '''EM_MATRIX_NAME''' and '''EC_COMP_NAME''' define what is being measured.
      ,[EB_NAME]
+
** EM_MATRIX_NAME defines which medium the parameter is measured in (e.g. precipitation, air, pm10, ...)
      ,[EB_LONGITUDE]
+
** EC_COMP_NAME is the name of the parameter (e.g. sulphate_total, ...)
      ,[EB_LATITUDE]
+
** Various other fields, like '''EC_UNIT''' define more metadata.
      ,[EB_ALTITUDE_ASL]
 
  FROM [EBAS].[dbo].[EB_STATION]
 
  
 +
* DS_DATA_SET:
 +
** Table DS_DATA_SET describes what is called a '''dataset''' in '''EBAS''': One parameter measured at a certain station. The full version has some more complex dependecies on instrument, method etc.
 +
** The columns EM_MATRIX_NAME and EC_COMP_NAME refer to the EC_COMPONENTS table.
 +
** DS_STARTDATE and DS_ENDDATE provide the timestamp of the first and last measurement in the timeseries.
 +
** There are many other fields and references to many other tables.
  
* Table '''A1_TIME'''
+
You can imagine a dataset as an instrument on a fixed location measuring one parameter. This leads us to the main data table.
** Contains all the observations, all the parameters from all the stations
 
** Field '''A1_VALUE''' contains the data value
 
** '''A1_STARTTIME''' and '''A1_ENDTIME''' tell the time and A1_SUBST, A1_NUMFLAG, A1_FLAGS, A1_CODES are standard metadata for the observation.  
 
** '''DS_SETKEY''' refers to '''DS_DATA_SET'''
 
  
The '''DS_DATA_SET''' field tells which dataset this value belongs to
+
* Table A1_TIME Contains all the observations, all the parameters from all the stations
 +
** DS_SETKEY refers to the dataset.
 +
** A1_STARTTIME and A1_ENDTIME define the measurement time.
 +
** A1_VALUE contains the data value
 +
** The rest of the fields are metadata.

Revision as of 12:25, August 31, 2011

Back to Questions and Comments about CF-1.6 Station Data Convention

Rational Behind this Proposal

The WCS service has potential to grow beyond regular, gridded coverages. Coverage types that need to be described are for example:

  • station point data
  • station trajectory data
  • lidar data
  • moving station data, like aeroplane or ship

With a relational system, it is easy to design a schema that captures all the data and the metadata.

The netCDF data format was designed to contain mainly gridded multidimensional data. The format does not have a special support for tables, which sounds like a severe restriction, but it's not.

  • It's possible to agree about a simple way to encode tables in a netCDF file.
    • Tables with multiple fields.
    • Proper primary keys for tables.
    • Foreign keys for referential integrity.
    • Common relationships: many-to-many, 1-to-many, 1-to-1.
  • It's possible to be efficient, the encoding does not have to be one size fits all.
    • Sparse tables can be encoded row-by-row manner.
    • Dense tables can be encoded as multidimensional hypercubes.
    • Mix and match the two above at will.
  • It's possible to be elegant.
    • No mysterious indirections.
    • No complex offset calculations.
    • A reader-writer library can take care of following the rules, letting a programmer to operate on higher level.

Encoding Table Columns

Table is a collection of columns. These are indicated with table_name attribute.

Every table needs a primary key. It is indicated with primary_key = "T" attribute.

Example: A station table with three columns: station_code, lat and lon. There are ten rows with indexes 0..9

   dimensions:
       station = 10 ;  // station table dimension
       station_code_length = 4 ; // artificial dimension for station code, with max four character length.
   variables:
       char station_code(station, station_code_length) ;
           station_code:table_name = "station";
           station_code:primary_key = "T";
       float lon(station) ; 
           lon:table_name = "station";
       float lat(station) ; 
           lat:table_name = "station";

The dimension name station can be anything. It does not have to equal the table name station, but should be for readability. Same for station_code_length, it can be called anything.

A table can have multiple fields as a primary key.

Encoding Multiple Tables

Adding the second table is similar. Since new data is usually added by time, it can be made the unlimited dimension.

   dimensions:
       station = 10 ;  // station table dimension
       station_code_length = 4 ; // artificial dimension for station code, with max four character length.
       time = UNLIMITED ;
   variables:
       char station_code(station, station_code_length) ;
           station_code:table_name = "station";
           station_code:primary_key = "T";
       float lon(station) ; 
           lon:table_name = "station";
       float lat(station) ; 
           lat:table_name = "station";
       int time(time) ;
           time:table_name = "time";
           time:primary_key = "T";

Encoding Relations

A typical station data instance is a relation (station_code, time, temperature, humidity). The pair (station_code, time) is the primary key; data is recorded at a certain station, at a certain time. Temperature and humidity are the recorded parameters.

Encoding this into netCDF can be done in two ways, using either variable or a dimension.

Encoding Relation as a Record Array

So far we have the two unrelated tables for our two dimensions: location and time. Now let's add humidity and temperature, which refers to both of the dimensions:

   dimensions:
       station = 10 ;  // station table dimension
       station_code_length = 4 ; // artificial dimension for station code, with max four character length.
       data = UNLIMITED // each row in the data table. 
       time = 2000 ; we can't have two unlimited dimensions. 
   variables:
       char station_code(station, station_code_length) ;
           station_code:table_name = "station";
           station_code:primary_key = "T";
       float lon(station) ; 
           lon:table_name = "station";
       float lat(station) ; 
           lat:table_name = "station";
       int time(time) ;
           time:table_name = "time";
           time:primary_key = "T";
       int data_station_code(data) ;
           data_station_code:table_name = "data";
           data_station_code:foreign_key = "station_code";
           data_station_code:primary_key = "T";
       int data_time(data) ;
           data_time:table_name = "data";
           data_time:foreign_key = "time";
           data_time:primary_key = "T";
       float temperature(data) ; 
           temperature:table_name = "data";
       float humidity(data) ; 
           humidity:table_name = "data";

The variable data_station_code is marked foreign_key = "station_code", which means, that the variable is an index to the station_code variable. Similar for time. Both are marked primary_key = "T" which means, that the combination of (data_station_code, data_time) is unique.

   To read the full table (station_code, lat, lon, time, temperature, humidity), row by row:
   for idx in range of data
       read station:
           read data_station_code(idx)
           read variables station_code, lat and lon at that index
       read time:
           read data_time(idx)
           read variable time at that index
       read temperature(idx)
       read humidity(idx)

This is an space-efficient encoding for sparse tables.

Encoding Relation as a Multidimensional Array

This is identical to CF-1.6 encoding. Both variables are 2-dimensional cubes.

   dimensions:
       station = 10 ;  // station table dimension
       station_code_length = 4 ; // artificial dimension for station code, with max four character length.
       time = UNLIMITED ; // we can again add one time slice at a time
   variables:
       char station_code(station, station_code_length) ;
           station_code:table_name = "station";
           station_code:primary_key = "T";
       float lon(station) ; 
           lon:table_name = "station";
       float lat(station) ; 
           lat:table_name = "station";
       int time(time) ;
           time:table_name = "time";
           time:primary_key = "T";
       float temperature(time,station) ; 
           temperature:table_name = "data";
       float humidity(time,station) ; 
           humidity:table_name = "data";

This declares two arrays, one for temperature and one for humidity. Both are two-dimensional. The dimensions time and station refer to the time and station variables. They implicitly became part of the primary key.

   To find temperature at "ACAD" in "2004-17-22":
       Find index for "ACAD" from the "station_code" array. Let's say it's 2.
       Find index for "2004-17-22" from the time array. Lets say it's 317.
       Read temperature(317,2)

This is a very space-efficient encoding, if most of the stations have data most of the time.

Summa Summarum

  • A typical schema of any relational database can be encoded.
  • If the data is sparse, foreign keys can be expressed as variables.
  • If the data is dense, foreign keys can be expressed as dimensions, resulting in vastly smaller files than flat CSV would be.
  • There can be any combination of the two, enabling the best of both worlds whatever your data will look like.

More Real World Examples

These examples are not full real-life examples of real live databases, but rather examples how to encode a typical schema. The center is usually the data table, and the data record refers to the auxliary metadata tables. Typically cases are a star or a snowflake schema.

CIRA / VIEWS

All the data is stored at the center table AirData3. The


NILU / EBAS

The EBAS database schema is rather large, full schema outline looks like this:

EBAS thumbnail.png


The essential schema is not that big, only four tables.

EBAS essential.png

The essential four tables are:

  • EB_STATION: This contains the station information EB_STATION.
    • EB_STATION_CODE is the unique code.
    • lat, lon, alt; some miscellaneous fields about the history of the station.
  • EC_COMPONENTS: This table contains the measured parameters. Fields EM_MATRIX_NAME and EC_COMP_NAME define what is being measured.
    • EM_MATRIX_NAME defines which medium the parameter is measured in (e.g. precipitation, air, pm10, ...)
    • EC_COMP_NAME is the name of the parameter (e.g. sulphate_total, ...)
    • Various other fields, like EC_UNIT define more metadata.
  • DS_DATA_SET:
    • Table DS_DATA_SET describes what is called a dataset in EBAS: One parameter measured at a certain station. The full version has some more complex dependecies on instrument, method etc.
    • The columns EM_MATRIX_NAME and EC_COMP_NAME refer to the EC_COMPONENTS table.
    • DS_STARTDATE and DS_ENDDATE provide the timestamp of the first and last measurement in the timeseries.
    • There are many other fields and references to many other tables.

You can imagine a dataset as an instrument on a fixed location measuring one parameter. This leads us to the main data table.

  • Table A1_TIME Contains all the observations, all the parameters from all the stations
    • DS_SETKEY refers to the dataset.
    • A1_STARTTIME and A1_ENDTIME define the measurement time.
    • A1_VALUE contains the data value
    • The rest of the fields are metadata.