Skip to content

Latest commit

 

History

History
194 lines (122 loc) · 4.64 KB

bands_example.rst

File metadata and controls

194 lines (122 loc) · 4.64 KB

Exporting band data

ENVISAT, band, raster

This tutorial shows how to convert ENVISAT raster information from dataset and generate flat binary rasters using PyEPR.

The program generates as many raster as the dataset specified in input.

The example code (examples/write_bands.py) is a direct translation of the C sample program write_bands.c bundled with the EPR API distribution.

The program is invoked as follows:

$ python write_bands.py <envisat-product> \
<output directory for the raster file> <dataset name 1> \
[<dataset name 2> ... <dataset name N>]

Import section

To use the Python EPR API one have to import epr module.

At first import time the underlaying C library is opportunely initialized.

examples/write_bands.py

The main program

The main program in quite simple (this is just an example).

examples/write_bands.py

It performs some basic command line arguments handling and then open the input product.

examples/write_bands.py

Finally the core function (write_raw_image) is called on each band specified on the command:

examples/write_bands.py

The write_raw_image core function

The core function is write_raw_image.

examples/write_bands.py

It generates a flat binary file with data of a single band whose name is specified as input parameter.

First the output file name is computed using the os module.

examples/write_bands.py

Then the desired band is retrieved using the epr.Product.get_band method and some of its parameters are loaded in to local variables:

examples/write_bands.py

Band data are accessed by means of a epr.Raster object.

epr.Band.read_as_array

The epr.Band.create_compatible_raster is a facility method that allows to instantiate a epr.Raster object with a data type compatible with the band data:

examples/write_bands.py

Then data are read using the epr.Band.read_raster method:

examples/write_bands.py

Then the output file object is created (in binary mode of course) and data are copied to the output file one line at time

examples/write_bands.py

Please note that it has been used epr.Raster.data attribute of the epr.Raster objects that exposes epr.Raster data with the powerful numpy.ndarray interface.

Note

copying one line at time is not the best way to perform the task in Python. It has been done just to mimic the original C code:

out_stream = fopen(image_file_path, "wb");
if (out_stream == NULL) {
    printf("Error: can't open '%s'\n", image_file_path);
    return 3;
}

for (y = 0; y < (uint)raster->raster_height; ++y) {
    numwritten = fwrite(epr_get_raster_line_addr(raster, y),
                        raster->elem_size,
                        raster->raster_width,
                        out_stream);

    if (numwritten != raster->raster_width) {
        printf("Error: can't write to %s\n", image_file_path);
        return 4;
    }
}
fclose(out_stream);

A by far more "pythonic" solution would be:

raster.data.tofile(out_stream)