File: oli_reader.py

package info (click to toggle)
pyspectral 0.8.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,040 kB
  • sloc: python: 5,697; makefile: 123
file content (97 lines) | stat: -rw-r--r-- 2,789 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2017, 2018 Adam.Dybbroe

# Author(s):

#   Adam.Dybbroe <a000680@c20671.ad.smhi.se>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Landsat-8 OLI reader
"""

import os
from xlrd import open_workbook
import numpy as np
from pyspectral.utils import convert2hdf5 as tohdf5
from pyspectral.raw_reader import InstrumentRSR
import logging
LOG = logging.getLogger(__name__)


OLI_BAND_NAMES = {'CoastalAerosol': 'B1',
                  'Blue': 'B2',
                  'Green': 'B3',
                  'Red': 'B4',
                  'NIR': 'B5',
                  'Cirrus': 'B9',
                  'SWIR1': 'B6',
                  'SWIR2': 'B7',
                  'Pan': 'B8'}


class OliRSR(InstrumentRSR):

    """Class for Landsat OLI RSR"""

    def __init__(self, bandname, platform_name):
        """
        Read the Landsat OLI relative spectral responses for all channels.

        """
        super(OliRSR, self).__init__(bandname, platform_name)

        self.instrument = 'oli'
        self._get_options_from_config()

        LOG.debug("Filename: %s", str(self.path))
        if os.path.exists(self.path):
            self._load()
        else:
            raise IOError("Couldn't find an existing file for this band: " +
                          str(self.bandname))

    def _load(self, scale=0.001):
        """Load the Landsat OLI relative spectral responses
        """

        with open_workbook(self.path) as wb_:
            for sheet in wb_.sheets():
                if sheet.name in ['Plot of AllBands', ]:
                    continue
                ch_name = OLI_BAND_NAMES.get(sheet.name.strip())

                if ch_name != self.bandname:
                    continue

                wvl = sheet.col_values(0, 2)
                resp = sheet.col_values(1, 2)

                self.rsr = {'wavelength': np.array(wvl) / 1000.,
                            'response': np.array(resp)}
                break


def main():
    """Main"""
    bands = OLI_BAND_NAMES.values()
    bands.sort()
    for platform_name in ['Landsat-8', ]:
        tohdf5(OliRSR, platform_name, bands)


if __name__ == "__main__":
    main()