File: write_bands.py

package info (click to toggle)
pyepr 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,144 kB
  • sloc: python: 3,028; makefile: 124
file content (102 lines) | stat: -rwxr-xr-x 2,958 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

# This program is a direct translation of the sample program
# "write_bands.c" bundled with the EPR-API distribution.
#
# Source code of the C program is available at:
# https://github.com/bcdev/epr-api/blob/master/src/examples/write_bands.c

import os
import sys

import epr


def write_raw_image(output_dir, product, band_name):
    """Generate the ENVI binary pattern image file for an actual DS.

    The first parameter is the output directory path.
    """
    # Build ENVI file path, DS name specifically
    image_file_path = os.path.join(output_dir, band_name + ".raw")

    band = product.get_band(band_name)
    source_w = product.get_scene_width()
    source_h = product.get_scene_height()
    source_step_x = 1
    source_step_y = 1

    raster = band.create_compatible_raster(
        source_w, source_h, source_step_x, source_step_y
    )

    print(f"Reading band {band_name!r}...")
    raster = band.read_raster(0, 0, raster)

    with open(image_file_path, "wb") as out_stream:
        for line in raster.data:
            out_stream.write(line.tostring())
        # or better: raster.data.tofile(out_stream)

    print(f"Raw image data successfully written to {image_file_path!r}.")
    print(
        f"C data type is {epr.data_type_id_to_str(raster.data_type)!r}, "
        f"element size {raster.get_elem_size()} byte(s), "
        f"raster size is {raster.get_width()} x {raster.get_height()} pixels."
    )


def main(*argv):
    """A program for converting producing ENVI raster information from
    dataset.

    It generates as many raster as there are dataset entrance parameters.

    Call::

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

    Example::

      $ python3 write_bands.py \
        MER_RR__1PNPDK20020415_103725_000002702005_00094_00649_1059.N1 \
        . latitude

    """
    if not argv:
        argv = sys.argv

    if len(argv) <= 3:
        print(
            """Usage:

  python3 write_bands.py <envisat-product> <output-dir>  <dataset-name-1>
                         [<dataset-name-2> ... <dataset-name-N>]

where envisat-product is the input filename and output-dir is the
output directory and dataset-name-1 is the name of the first band
to be extracted (mandatory) and dataset-name-2 ... dataset-name-N
are the names of further bands to be extracted (optional)

Example:

  python3 write_bands MER_RR__2P_TEST.N1 . latitude

"""
        )
        sys.exit(1)

    product_file_path = argv[1]
    output_dir_path = argv[2]

    # Open the product; the argument is the path to product the data file
    with epr.open(product_file_path) as product:
        for band_name in argv[3:]:
            write_raw_image(output_dir_path, product, band_name)


if __name__ == "__main__":
    main()