File: eps_avhrrl1b.py

package info (click to toggle)
python-mpop 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,516 kB
  • ctags: 1,877
  • sloc: python: 15,374; xml: 820; makefile: 90; sh: 8
file content (221 lines) | stat: -rw-r--r-- 6,449 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2012 SMHI

# Author(s):

#   Martin Raspaud <martin.raspaud@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/>.

"""New reader for eps avhrr level 1b
"""
from datetime import datetime
from xml.etree.ElementTree import ElementTree

import numpy as np

from test_scale import fill_scales

VARIABLES = {}

TYPEC = {"boolean": ">i1",
         "integer2": ">i2",
         "integer4": ">i4",
         "uinteger2": ">u2",
         "uinteger4": ">u4",
         }


def process_delimiter(elt, ascii=False):
    del elt, ascii

def process_field(elt, ascii=False):
    if elt.get("type") == "bitfield" and not ascii:
        current_type = ">u" + str(int(elt.get("length")) / 8)
    elif(elt.get("length") is not None):
        if ascii:
            add = 33
        else:
            add = 0
        current_type = "S" + str(int(elt.get("length")) + add)
    else:
        current_type = TYPEC[elt.get("type")]
    return ((elt.get("name"), current_type))


def process_array(elt, ascii=False):
    del ascii
    chld = elt.getchildren()
    if len(chld) > 1:
        print "stop"
        raise ValueError()
    chld = chld[0]
    try:
        name, current_type = CASES[chld.tag](chld)
        size = None
    except ValueError:
        name, current_type, size = CASES[chld.tag](chld)
    del name
    myname = elt.get("name") or elt.get("label")
    if elt.get("length").startswith("$"):
        length = int(VARIABLES[elt.get("length")[1:]])
    else:
        length = int(elt.get("length"))
    if size is not None:
        return (myname, current_type, (length, ) + size)
    else:
        return (myname, current_type, (length, ))

CASES = {"delimiter": process_delimiter,
         "field": process_field,
         "array": process_array,
         }


def parse_format(xml_file):

    global VARIABLES
    tree = ElementTree()
    tree.parse(xml_file)

    products = tree.find("product")


    params = tree.find("parameters")

    for param in params.getchildren():
        VARIABLES[param.get("name")] = param.get("value")


    types = {}

    for prod in products:
        ascii = (prod.tag in ["mphr", "sphr"])
        res = []
        for i in prod:
            lres = CASES[i.tag](i, ascii)
            if lres is not None:
                res.append(lres)
        types[(prod.tag, int(prod.get("subclass")))] = np.dtype(res)

    return types

def norm255(a):
    arr = a * 1.0
    arr = (arr - arr.min()) * 255.0 / (arr.max() - arr.min())
    return arr.astype(np.uint8)

def show(a):
    import Image
    print norm255(a).dtype
    Image.fromarray(norm255(a), "L").show()

C1 = 1.191062e-05 # mW/(m2*sr*cm-4)
C2 = 1.4387863 # K/cm-1

def to_bt(arr, wc_, a__, b__):
    """Convert to BT.
    """
    val = np.log(1 + (C1 * (wc_ ** 3) / arr))
    t_star = C2 * wc_ / val
    return a__ + b__ * t_star

def to_refl(arr, solar_flux):
    """Convert to reflectances.
    """
    return arr * math.pi * 100.0 / solar_flux

if __name__ == '__main__':
    tic = datetime.now()



    types = parse_format("eps_avhrrl1b_6.5.xml")

    #filename = "AVHR_xxx_1B_M02_20120321100103Z_20120321100403Z_N_O_20120321105619Z"
    filename = "AVHR_xxx_1B_M02_20120321100403Z_20120321100703Z_N_O_20120321105847Z"
    grh_dtype = np.dtype([("RECORD_CLASS", "|i1"),
                          ("INSTRUMENT_GROUP", "|i1"),
                          ("RECORD_SUBCLASS", "|i1"),
                          ("RECORD_SUBCLASS_VERSION", "|i1"),
                          ("RECORD_SIZE", ">u4"),
                          ("RECORD_START_TIME", "S6"),
                          ("RECORD_STOP_TIME", "S6")])

    RECORD_CLASS = ["Reserved", "mphr", "sphr",
                    "ipr", "geadr", "giadr",
                    "veadr", "viadr", "mdr"]


    records = []

    with open(filename, "rb") as fdes:
        while True:
            grh = np.fromfile(fdes, grh_dtype, 1)
            if not grh:
                break
            try:
                rec_class = RECORD_CLASS[grh["RECORD_CLASS"]]
                record = np.fromfile(fdes,
                                     types[(rec_class,
                                            grh["RECORD_SUBCLASS"][0])],
                                     1)
                records.append((rec_class, record, grh["RECORD_SUBCLASS"][0]))
            except KeyError:
                fdes.seek(grh["RECORD_SIZE"] - 20, 1)
                
    mdrs = [record[1]
            for record in records
            if record[0] == "mdr"]

    scales = np.empty((1, ), dtype=mdrs[0].dtype)
    print scales
    fill_scales(scales, "eps_avhrrl1b_6.5.xml")
    mdrs = np.concatenate(mdrs)


    giadr = [record[1]
              for record in records
              if record[0] == "giadr" and record[2] == 1][0]

    print mdrs["SCENE_RADIANCES"].shape

    three_a = mdrs["FRAME_INDICATOR"] & 2**15
    print three_a.shape

    # filter 3a and calibrate

    channels = np.empty((mdrs["SCENE_RADIANCES"].shape[0], 6, mdrs["SCENE_RADIANCES"].shape[2]))
    channels[:, 0, :] = mdrs["SCENE_RADIANCES"][:, 0, :]
    channels[:, 1, :] = mdrs["SCENE_RADIANCES"][:, 1, :]
    print (giadr["CH4_CENTRAL_WAVENUMBER"],
                              giadr["CH4_CONSTANT1"],
                              giadr["CH4_CONSTANT2_SLOPE"])
    channels[:, 4, :] = to_bt(mdrs["SCENE_RADIANCES"][:, 3, :] / 100.0,
                              giadr["CH4_CENTRAL_WAVENUMBER"] * 1e-3,
                              giadr["CH4_CONSTANT1"] * 1e-5,
                              giadr["CH4_CONSTANT2_SLOPE"] * 1e-6)
    channels[:, 5, :] = mdrs["SCENE_RADIANCES"][:, 4, :]
    channels[three_a, 2, :] = mdrs["SCENE_RADIANCES"][three_a, 2, :]
    channels[three_a, 3, :] = mdrs["SCENE_RADIANCES"][1 - three_a, 2, :]

    
    
    toc = datetime.now()

    print toc - tic

    show(channels[:, 4, :])