1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
Exporting band data
-------------------
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 (:download:`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:
.. code-block:: sh
$ python write_bands.py <envisat-product> \
<output directory for the raster file> <dataset name 1> \
[<dataset name 2> ... <dataset name N>]
.. _ENVISAT: http://envisat.esa.int
.. _PyEPR: https://github.com/avalentino/pyepr
.. _`write_bands.c`: https://github.com/bcdev/epr-api/blob/master/src/examples/write_bands.c
Import section
~~~~~~~~~~~~~~
To use the Python_ EPR API one have to import :mod:`epr` module.
At first import time the underlaying C library is opportunely initialized.
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 1-15
The main program
~~~~~~~~~~~~~~~~
The main program in quite simple (this is just an example).
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 55-
It performs some basic command line arguments handling and then open the
input product.
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 97-99
Finally the core function (:func:`write_raw_image`) is called on each band
specified on the command:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 100-101
The :func:`write_raw_image` core function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The core function is :func:`write_raw_image`.
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:pyobject: write_raw_image
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 :mod:`os` module.
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 23-25
Then the desired band is retrieved using the :meth:`epr.Product.get_band`
method and some of its parameters are loaded in to local variables:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 26-27
Band data are accessed by means of a :class:`epr.Raster` object.
.. seealso:: :func:`epr.Band.read_as_array`
The :meth:`epr.Band.create_compatible_raster` is a facility method that
allows to instantiate a :class:`epr.Raster` object with a data type compatible
with the band data:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 32-34
Then data are read using the :meth:`epr.Band.read_raster` method:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 36-37
Then the output file object is created (in binary mode of course)
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 39
and data are copied to the output file one line at time
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/write_bands.py
:language: python
:lines: 41-42
Please note that it has been used :data:`epr.Raster.data` attribute of the
:class:`epr.Raster` objects that exposes :class:`epr.Raster` data with the
powerful :class:`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:
.. code-block:: c
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)
.. _Python: http://www.python.org
.. _pythonic: http://www.cafepy.com/article/be_pythonic
|