File: split_metop_avhrr_rsrfile.py

package info (click to toggle)
pyspectral 0.13.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,184 kB
  • sloc: python: 6,070; makefile: 123
file content (81 lines) | stat: -rw-r--r-- 2,610 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018-2022 Pytroll developers
#
#
# 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/>.

"""Spitting original Metop-C AVHRR RSR data to one file per band.

Take the 'original' Metop-C AVHRR spectral responses from the file
AVHRR_A309_METOPC_SRF_PRELIMINARY.TXT and split it in one file per band and
convert the wavenumbers to wavelengths.

"""

import os

import numpy as np

DATAFILE = "/home/a000680/data/SpectralResponses/avhrr/AVHRR_A309_METOPC_SRF_PRELIMINARY.TXT"
OUTPUT_DIR = "/home/a000680/data/SpectralResponses/avhrr"

CH1_NAME = "Metop_C_A309C001.txt"
CH2_NAME = "Metop_C_A309C002.txt"
CH3A_NAME = "Metop_C_A309C03A.txt"
CH3B_NAME = "Metop_C_A309C03B.txt"
CH4_NAME = "Metop_C_A309C004.txt"
CH5_NAME = "Metop_C_A309C005.txt"

CH_FILES = {'ch1': CH1_NAME,
            'ch2': CH2_NAME,
            'ch3A': CH3A_NAME,
            'ch3B': CH3B_NAME,
            'ch4': CH4_NAME,
            'ch5': CH5_NAME
            }

HEADERLINE = "Wavelegth (um)      Normalized RSF"

with open(DATAFILE) as fpt:
    lines = fpt.readlines()

    idx = 0
    for counter, channel in enumerate(['ch1', 'ch2', 'ch3A', 'ch3B', 'ch4', 'ch5']):
        for line in lines[idx::]:
            if line.startswith('AVHRR'):
                break
            idx = idx + 1

        # print(lines[idx+2])
        wvn = []
        response = []
        for line in lines[idx+2:-1]:
            try:
                waven, resp = line.split()
            except ValueError:
                break

            wvn.append(float(waven))
            response.append(float(resp))
            idx = idx + 1

        # Convert from wavenumbers (cm-1) to wavelength (microns, um)
        wvl = 1./np.array(wvn) * 10000.0
        response = np.array(response)
        filename = os.path.join(OUTPUT_DIR, CH_FILES[channel])
        data = np.stack((wvl[::-1], response[::-1]), axis=-1)
        print(counter, filename)
        np.savetxt(filename, data, fmt="%8.6f     %10.8f", header=HEADERLINE)