ENVISAT Product Reader API for C |

ENVISAT Product Reader C API

## 1. Introduction

The ENVISAT Product Reader API for C (`epr-c-api`) is a library of data structures and functions for simple access to MERIS, AATSR and ASAR products. You can use them in your programs to retrieve directly the geophysically coded values, e.g. such as the chlorophyll concentration in mg/m³, in a data matrix.

The `epr-c-api` is generic, i.e. it has no instrument specific functions but utilises the generic ENVISAT product format. However, it is not necessary for a user of the `epr-c-api` to know the ENVISAT product format - the `epr-c-api` knows it, and that's sufficient.

All a user of the `epr-c-api` has to know are a few functions and the name of the geophysical variable he wants to get. In fact, the `epr-c-api` can do more: all information stored in ENVISAT products can be retrieved in a unique way, just by requesting it identified by its name, without worrying too much where and how it is stored in the world of the ENVISAT product format.

Now, here's the tricky bit: there are many different products and the number of items stored in each product is huge. Therefore a ENVISAT Data Products documentation is included in the BEAM software homepage, which describes the internal structure of each supported product. In order to access a certain item such as a dataset, record, band or flag you have to consult the data product documentation for he correct name or ID for the dataset, record, band or flag you want to access.

## 2. Using the API in your own C-Programs

The `epr-c-api` is written in pure ANSI C and should compile with every ANSI conformant C-compiler:

1. Copy all `*.h` `*.c` files contained in the distribution's `src` folder to your own source folder of your project and include them in your development environment (makefile or IDE).
2. Include the public API header `epr-api.h` in your C source files.
3. Optionally refer to the example code in `src/examples` directory

## 3. API Design

### 3.1 Basic Concepts

Given here are some of the basic concepts of the API.

- The API works exclusively with pointers to dynamically allocated structures. For every structure type in the API a constructor/desctructor function pair always exists: `X epr_create_X(...)` and `void epr_free_X(X)`. Example: For the type `EPR_SRecord*` these functions are `EPR_SRecord* epr_create_record(...)` and `void epr_free_record(EPR_SRecord*)`.
- In most cases, the first parameter is the object on which the function is operating. For example, `uint epr_get_num_fields(const EPR_SRecord* record)`.
- Access functions exist for all important members of structures. So, the user of the API should not need to work directly with the API structures but with functions to get values. For example, if you use the `epr_get_pixel_as_float` to access a pixel value of raster, your code is less dependent on API changes as if you would have directly accessed a structure member.
- A central concept is that all items, which can be retrieved from ENVISAT products, are identified by name. For example, to access the MERIS L1B radiance of band 1, you have to call `epr_get_dataset_id(product_id, "Radiance_1")` But how do you know, what name you should use for the data you want to retrieve? There are three possibilities:
  1. The ENVISAT Data Products documentation provides all valid names used in the all supported data products. All tables in the documentation have a similar structure, the first column is always contains the longed-for name.
  2. One can use VISAT to open a product. The names shown in VISAT in the product browser are the same as in the ENVISAT data product tables documentation and are hence those needed.
  3. One can open a product using the `epr-c-api`, scan all datasets in a look and print the names of the datasets and records by the access function. The same can be done with all fields of a record.

### 3.2 Data Access Types

The API provides two access types for ENVISAT data:

1. Access of data as it is stored in the product (we call this basic or raw data access) and
2. Access to the geophysical raster data (what we call this geophysical data access).

The difference between the two is how they treat the measurement data: Access type (1) returns the data in its native data product structure: as datasets, records and fields. This gives a direct read access to the raw data as it is stored in the data product, without any interpretation of the content. For example, the MERIS L1B measurement data set MDS 15 stores chlorophyll, vegetation index or cloud top height in the same byte. The content has to be interpreted depending on the surface type, which itself is coded in MDS 20. To get the true geophysical value one needs to retrieve the proper scaling factors from a certain GADS. Access type (2) decodes all this and provides, for example, a data matrix with the chlorophyll concentration in a float variable in its geophysical units, i.e. mg/m³. The data of the measurement datasets and the tie-point values, i.e. geometry, geo-location and meteorological data, are available by this method. The tie-point data are interpolated to the corresponding image grid.

### 3.3 Working with the Basic/Raw Data Access

Level | Correspondence in ENVISAT product
Product | ENVISAT product file
Dataset | Dataset, e.g. Main Product Header MPH, or a Measurement Data Set MDS. A dataset contains records. For example, the MERIS L1b MDS1 contains many records with radiance values.
Record | A single record within a dataset. A record contains many fields. For example, a record within the MERIS L1b MDS1 contains a time-stamp field, a quality flag field and then a field with as many radiance values as contained in the width of the image.
Field | A field within a record. Fields can be a scalar or a vector. In the example, the time stamp and quality flag are scalars while the radiance field is a vector of the length of an image line.
Element | Optional. If a field is a vector, these are the elements of that vector. In the example each element of the radiance field is the radiance of a certain pixel.

On each level of the hierarchy, functions exist to point to a certain instance of it, e.g. a certain product, dataset or record and so on. That function generally requires the name of the item to be retrieved. All possible names can be found in the DDDB , and the name of the most important items are listed here. The function returns an identifier of that instance. The identifier is not the instance but a pointer to it. It is used to get the values of the instance by using access functions.

For example,

my_product_id = epr_open_product("my_MERIS_product");

returns the identifier to the `my_product_id` which points to the product contained in the file "my_MERIS_product".

rad1_id = epr_get_dataset_id(my_product_id, "Radiance_1");

This `my_product_id` is used get the identifier `rad1_id` which points to the `Radiance_1` dataset. Now, one can call

num_rec = epr_get_num_records(rad1_id);

to get the number of records which this dataset contains. Finally one can loop over all records and gets its fields and elements. See the examples to get a complete code example.

### 3.4 Working with Geophysical Data Access

To work with geophysical data is easier than the basic access. No such deep hierarchy exists. Instead of accessing datasets, so called bands will be accessed. A band directly includes one single geophysical variable.

For example,

my_product_id = epr_open_product(my_product_file_path);
my_chl_id = epr_create_band_id(my_product_id, "algal_1");

returns the identifier to a band containing the chlorophyll product. Now, the actual data are read into a raster.

chl_raster = epr_create_compatible_raster(my_chl_id, ...);
status = epr_read_band_raster(chl_raster, ...);

The raster `chl_raster` now contains the data as two dimensional matrix of pixel. To get the value of a pixel at a certain index `(i,j)`, one should use the access function:

chl_pixel = epr_get_pixel_as_float(chl_raster, i, j);

(See also the examples `ndvi.c` for complete example codes.)

The concept of the raster allows spatial subsets and undersampling: A certain portion of the ENVISAT product will be read into the raster. This is called the source. The complete ENVISAT product can be much greater than the source. One can move the raster over the complete ENVISAT product and read in turn different parts - always of the size of the source - of it into the raster.

A typical example is a processing in blocks. Lets say, a block has 64x32 pixel. Then, the source has a width of 64 pixel and a height of 32 pixel. Another example is a processing of complete image lines. Then, the source has a widths of the complete product (for example 1121 for a MERIS RR product), and a height of 1. One can loop over all blocks or image lines, read into the raster and process it. It is, of course, also possible to define a raster of the size of the complete product.

In addition, it is possible to define a subsampling step for a raster. This means, that the source is not read 1:1 into the raster, but that only every 2nd or 3rd pixel is read. This step can be set differently for the across track and along track directions.

### 3.5 Bit masks

MERIS and AATSR provide many so called flags, which are binary information indicating a certain state of a pixel. For example, this can be a quality indicator, which, if set, indicates that the value of the pixel is invalid. Other example are a cloud flag, indicating that this pixel is a measurement above a cloud, or a coastline flag. The flags are stored in a packed format inside the ENVISAT products, but the `epr-c-api` provides a function to easily access the flags. It returns a bit-mask, which is a byte array that matches the corresponding image raster and is 1 where the flag is set and 0 elsewhere. Even more, this functions permits to formulate a bit-mask expression to combine any number of flags in a logical expression and returns the resulting combined bit-mask:

bm_expr = "flags.LAND OR flags.CLOUD";
status = epr_read_bitmask_raster(product_id, bm_expr, ..., bm_raster);

This is an example to get a bit-mask which masks out all land and cloud pixels. The names of the flags are found in the DDDB. The `epr_read_bitmask_raster` function read from product, identified by `product_id`, the flags and stores the resulting bit-mask in the raster `bm_raster`. See the examples for the complete code.

## 4. API Function Group Index

The `epr-c-api` provides the following group of functions:

(1) | Initialisation | Functions for setting up the environment of the API and releasing memory when the API is closed.
(2) | Logging | Functions to manage logging information
(3) | Error handling | Functions for determining the behaviour of the API in case of errors, and of getting information about runtime errors
(4) | Input / Output | Opening and closing ENVISAT products and writing data to a file or stdout.
(5) | Basic data access | Functions to retrieve raw data as stored in ENVISAT products
(6) | Geophysical data access | Functions to retrieve geophysically interpreted data in a raster matrix
(7) | Bit masks | Functions for generating bit masks from the flags included in ENVISAT products.