Completed
Pull Request — develop (#25)
by Adam
27s
created

OliRSR.__init__()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# Copyright (c) 2017, 2018 Adam.Dybbroe
5
6
# Author(s):
7
8
#   Adam.Dybbroe <[email protected]>
9
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation, either version 3 of the License, or
13
# (at your option) any later version.
14
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19
20
# You should have received a copy of the GNU General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23
"""Landsat-8 OLI reader
24
"""
25
26
import os
27
from xlrd import open_workbook
28
import numpy as np
29
from pyspectral.utils import convert2hdf5 as tohdf5
30
from pyspectral.raw_reader import InstrumentRSR
31
import logging
32
LOG = logging.getLogger(__name__)
33
34
35
OLI_BAND_NAMES = {'CoastalAerosol': 'B1',
36
                  'Blue': 'B2',
37
                  'Green': 'B3',
38
                  'Red': 'B4',
39
                  'NIR': 'B5',
40
                  'Cirrus': 'B9',
41
                  'SWIR1': 'B6',
42
                  'SWIR2': 'B7',
43
                  'Pan': 'B8'}
44
45
46
class OliRSR(InstrumentRSR):
47
48
    """Class for Landsat OLI RSR"""
49
50
    def __init__(self, bandname, platform_name):
51
        """
52
        Read the Landsat OLI relative spectral responses for all channels.
53
54
        """
55
        super(OliRSR, self).__init__(bandname, platform_name)
56
57
        self.instrument = 'oli'
58
        self._get_options_from_config()
59
60
        LOG.debug("Filename: %s", str(self.path))
61
        if os.path.exists(self.path):
62
            self._load()
63
        else:
64
            raise IOError("Couldn't find an existing file for this band: " +
65
                          str(self.bandname))
66
67
    def _load(self, scale=0.001):
68
        """Load the Landsat OLI relative spectral responses
69
        """
70
71
        with open_workbook(self.path) as wb_:
72
            for sheet in wb_.sheets():
73
                if sheet.name in ['Plot of AllBands', ]:
74
                    continue
75
                ch_name = OLI_BAND_NAMES.get(sheet.name.strip())
76
77
                if ch_name != self.bandname:
78
                    continue
79
80
                wvl = sheet.col_values(0, 2)
81
                resp = sheet.col_values(1, 2)
82
83
                self.rsr = {'wavelength': np.array(wvl) / 1000.,
84
                            'response': np.array(resp)}
85
                break
86
87
88
def main():
89
    """Main"""
90
    bands = OLI_BAND_NAMES.values()
91
    bands.sort()
92
    for platform_name in ['Landsat-8', ]:
93
        tohdf5(OliRSR, platform_name, bands)
94
95
96
if __name__ == "__main__":
97
    main()
98