File: writing.rst

package info (click to toggle)
satpy 0.59.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,292 kB
  • sloc: python: 93,630; xml: 3,343; makefile: 143; javascript: 23
file content (172 lines) | stat: -rw-r--r-- 6,121 bytes parent folder | download
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
=======
Writing
=======

Satpy makes it possible to save datasets in multiple formats, with *writers* designed to save in a given format.
For details on additional arguments and features available for a specific Writer see the table below.
Most use cases will want to save datasets using the
:meth:`~satpy.scene.Scene.save_datasets` method::

    >>> scn.save_datasets(writer="simple_image")

The ``writer`` parameter defaults to using the ``geotiff`` writer.
One common parameter across almost all Writers is ``filename`` and
``base_dir`` to help automate saving files with custom filenames::

    >>> scn.save_datasets(
    ...     filename="{name}_{start_time:%Y%m%d_%H%M%S}.tif",
    ...     base_dir="/tmp/my_ouput_dir")

.. versionchanged:: 0.10

    The `file_pattern` keyword argument was renamed to `filename` to match
    the `save_dataset` method"s keyword argument.

.. _writer_table:

.. list-table:: Satpy Writers
    :header-rows: 1

    * - Description
      - Writer name
      - Status
      - Examples
    * - GeoTIFF
      - :class:`geotiff <satpy.writers.geotiff.GeoTIFFWriter>`
      - Nominal
      -
    * - Simple Image (PNG, JPEG, etc)
      - :class:`simple_image <satpy.writers.simple_image.PillowWriter>`
      - Nominal
      -
    * - NinJo TIFF (using ``pyninjotiff`` package)
      - :class:`ninjotiff <satpy.writers.ninjotiff.NinjoTIFFWriter>`
      - Deprecated from NinJo 7 (use ninjogeotiff)
      -
    * - NetCDF (Standard CF)
      - :class:`cf <satpy.writers.cf_writer.CFWriter>`
      - Beta
      - :mod:`Usage example <satpy.writers.cf_writer>`
    * - AWIPS II Tiled NetCDF4
      - :class:`awips_tiled <satpy.writers.awips_tiled.AWIPSTiledWriter>`
      - Beta
      -
    * - GeoTIFF with NinJo tags (from NinJo 7)
      - :class:`ninjogeotiff <satpy.writers.ninjogeotiff.NinJoGeoTIFFWriter>`
      - Beta
      -

Available Writers
=================

To get a list of available writers use the `available_writers` function::

    >>> from satpy import available_writers
    >>> available_writers()


Colorizing and Palettizing using user-supplied colormaps
========================================================

.. note::

    In the future this functionality will be added to the ``Scene`` object.

It is possible to create single channel "composites" that are then colorized
using users' own colormaps.  The colormaps are Numpy arrays with shape
(num, 3), see the example below how to create the mapping file(s).

This example creates a 2-color colormap, and we interpolate the colors between
the defined temperature ranges.  Beyond those limits the image clipped to
the specified colors.

    >>> import numpy as np
    >>> from satpy.composites.core import BWCompositor
    >>> from satpy.enhancements.colormap import colorize
    >>> from trollimage.xrimage import XRImage
    >>> arr = np.array([[0, 0, 0], [255, 255, 255]])
    >>> np.save("/tmp/binary_colormap.npy", arr)
    >>> compositor = SingleBandCompositor("test", standard_name="colorized_ir_clouds")
    >>> composite = compositor((local_scene[10.8], ))
    >>> img = XRImage(composite)
    >>> kwargs = {"palettes": [{"filename": "/tmp/binary_colormap.npy",
    ...           "min_value": 223.15, "max_value": 303.15}]}
    >>> colorize(img, **kwargs)
    >>> img.show()

Similarly it is possible to use discrete values without color interpolation
using `palettize()` instead of `colorize()`.

You can define several colormaps and ranges in the `palettes` list and they
are merged together.  See trollimage_ documentation for more information how
colormaps and color ranges are merged.

The above example can be used in enhancements YAML config like this:

.. code-block:: yaml

  hot_or_cold:
    standard_name: hot_or_cold
    operations:
      - name: colorize
        method: &colorizefun !!python/name:satpy.enhancements.colormap.colorize ''
        kwargs:
          palettes:
            - {filename: /tmp/binary_colormap.npy, min_value: 223.15, max_value: 303.15}


.. _trollimage: http://trollimage.readthedocs.io/en/latest/

.. _scene_multiple_saves:

Saving multiple Scenes in one go
================================

As mentioned earlier, it is possible to save `Scene` datasets directly
using :meth:`~satpy.scene.Scene.save_datasets` method.  However,
sometimes it is beneficial to collect more `Scene`\ s together and process
and save them all at once.

::



    >>> from satpy.writers.core.compute import compute_writer_results
    >>> res1 = scn.save_datasets(filename="/tmp/{name}.png",
    ...                          writer="simple_image",
    ...                          compute=False)
    >>> res2 = scn.save_datasets(filename="/tmp/{name}.tif",
    ...                          writer="geotiff",
    ...                          compute=False)
    >>> results = [res1, res2]
    >>> compute_writer_results(results)


Adding text to images
=====================

Satpy, via :doc:`pydecorate <pydecorate:index>`, can add text to images when they're being saved.
To use this functionality, you must create a dictionary describing the text
to be added.

.. code-block:: python

    >>> decodict = {"decorate": [{"text": {"txt": "my_text",
    ...                                    "align": {"top_bottom": "top", "left_right": "left"},
    ...                                    "font": <path_to_font>,
    ...                                    "font_size": 48,
    ...                                    "line": "white",
    ...                                    "bg_opacity": 255,
    ...                                    "bg": "black",
    ...                                    "height": 30,
    ...                                     }}]}

Where `my_text` is the text you wish to add and `<path_to_font>` is the
location of the font file you wish to use, often in `/usr/share/fonts/`

This dictionary can then be passed to the :meth:`~satpy.scene.Scene.save_dataset` or :meth:`~satpy.scene.Scene.save_datasets` command.

.. code-block:: python

    >>> scene.save_dataset(my_dataset, writer="simple_image", fill_value=False,
    ...                    decorate=decodict)