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
|
#!/usr/bin/env python
# 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
from __future__ import print_function
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('Reading band "%s"...' % band_name)
raster = band.read_raster(0, 0, raster)
out_stream = open(image_file_path, 'wb')
for line in raster.data:
out_stream.write(line.tostring())
# or better: raster.data.tofile(out_stream)
out_stream.close()
print('Raw image data successfully written to "%s".' % image_file_path)
print('C data type is "%s", element size %u byte(s), '
'raster size is %u x %u pixels.' % (
epr.data_type_id_to_str(raster.data_type),
raster.get_elem_size(),
raster.get_width(),
raster.get_height()))
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::
$ write_bands.py <envisat-product>
<output directory for the raster file>
<dataset name 1>
[<dataset name 2> ... <dataset name N>]
Example::
$ 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: write_bands.py <envisat-product> <output-dir> '
'<dataset-name-1>')
print(' [<dataset-name-2> ... <dataset-name-N>]')
print(' where envisat-product is the input filename')
print(' and output-dir is the output directory')
print(' and dataset-name-1 is the name of the first band to be '
'extracted (mandatory)')
print(' and dataset-name-2 ... dataset-name-N are the names of '
'further bands to be extracted (optional)')
print('Example:')
print(' write_bands MER_RR__2P_TEST.N1 . latitude')
print()
sys.exit(1)
product_file_path = argv[1]
output_dir_path = argv[2]
# Open the product; an argument is a path to product 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()
|