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
|
Update :class:`Field` elements
------------------------------
.. highlight:: ipython
.. index:: update, field, read-only, EPR-API, MERIS
pair: open; mode
The EPR C API has been designed to provide read-only features.
PyEPR_ provides and extra capability consisting in the possibility to
modify (*update*) an existing ENVISAT_ :class:`Product`.
Lets consider a MERIS Level 2 low resolution product (
`MER_LRC_2PTGMV20000620_104318_00000104X000_00000_00000_0001.N1`).
It has a :class:`Band` named `water_vapour` containing the water vapour
content at a specific position.
One can load water vapour and compute an histogram using the following
instructions:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/update_elements.py
:language: python
:lines: 8-13
.. index:: matplotlib
The resulting histogram can be plot using Matplotlib_:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/update_elements.py
:language: python
:lines: 15-19
.. figure:: images/water_vapour_histogram_01.*
:width: 60%
Histogram of the original water vapour content
The actual values of the water vapour content :class:`Band` are computed
starting form data stored in the `Vapour_Content` :class:`Dataset` using
scaling factors contained in the `Scaling_Factor_GADS` :class:`Dataset`.
In particular :class:`Field`\ s `sf_wvapour` and `off_wvapour` are used::
In [21]: dataset = product.get_dataset('Scaling_Factor_GADS')
In [22]: print(dataset)
epr.Dataset(Scaling_Factor_GADS) 1 records
sf_cl_opt_thick = 1.000000
sf_cloud_top_press = 4.027559
sf_wvapour = 0.100000
off_cl_opt_thick = -1.000000
off_cloud_top_press = -4.027559
off_wvapour = -0.100000
spare_1 = <<unknown data type>>
.. index:: band
pair: scaling; factor
Now suppose that for some reason one needs to update the `sf_wvapour` scaling
factor for the water vapour content.
Changing the scaling factor, of course, will change all values in the
`water_vapour` :class:`Band`.
The change can be performed using the :meth:`Field.set_elem` and
:meth:`Field.set_elems` methods of :class:`Field` objects:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/update_elements.py
:language: python
:lines: 22-30
Now the `sf_wvapour` scaling factor has been changed and it is possible to
compute and display the histogram of modified data in the `water_vapour`
:class:`Band`:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/update_elements.py
:language: python
:lines: 32-46
.. figure:: images/water_vapour_histogram_02.*
:width: 60%
Histogram of the water vapour content (original and modified)
Figure above shows the two different histograms, original data in blue and
modified data in red, demonstrating the effect of the change of the scaling
factor.
The new map of water vapour is showed in the following picture:
.. figure:: images/modified_water_vapour.*
Modified water vapour content map
.. important::
it is important to stress that it is necessary to close and re-open the
:class:`Product` in order to see changes in the scaling factors applied
to the `water_vapour`:class:`Band` data.
This is a limitation of the current implementation that could be removed
in future versions of the PyEPR_ package.
It has been showed that changing the `sf_wvapour` scaling factor modifies
all values of the `water_vapour` :class:`Band`.
Now suppose that one needs to modify only a specific area.
It can be done changing the contents of the `Vapour_Content` :class:`Dataset`.
The :class:`Dataset` size can be read form the :class:`Product`::
In [44]: product.get_scene_height(), product.get_scene_width()
Out[44]: (149, 281)
while information about the fields in each record can be retrieved
introspecting the :class:`Record` object::
In [49]: record = dataset.read_record(0)
In [50]: record.get_field_names()
Out[50]: ['dsr_time', 'quality_flag', 'wvapour_cont_pix']
In [51]: record.get_field('wvapour_cont_pix')
Out[51]: epr.Field("wvapour_cont_pix") 281 uchar elements
So the name of the :class:`Field` we need to change is the `wvapour_cont_pix`,
and its index is `2`.
It is possible to change a small box inside the :class:`Dataset` as follows:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/update_elements.py
:language: python
:lines: 58-66
Please note that when one modifies the content of a :class:`Dataset` he/she
should also take into account id the corresponding band has lines mirrored
or not::
In [59]: band = p.get_band('water_vapour')
In [60]: band.lines_mirrored
Out[60]: True
Finally the :class:`Product` can be re-opened to load and display the
modified :class:`Band`:
.. raw:: latex
\fvset{fontsize=\footnotesize}
.. literalinclude:: examples/update_elements.py
:language: python
:lines: 69-80
.. figure:: images/modified_water_vapour_with_box.*
Modified water vapour content map with zeroed box
Of course values in the box that has been set to zero in the :class:`Dataset`
are transformed according to the scaling factor and offset parameters
associated to `water_vapour` :class:`Band`.
The complete code of the example can be found at
:download:`examples/update_elements.py`.
.. _PyEPR: https://github.com/avalentino/pyepr
.. _ENVISAT: https://earth.esa.int/eogateway/missions/envisat
.. _Matplotlib: https://matplotlib.org
.. raw:: latex
\clearpage
|