-
Notifications
You must be signed in to change notification settings - Fork 285
/
caliop_l2_cloud.py
116 lines (94 loc) · 3.79 KB
/
caliop_l2_cloud.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Satpy developers
#
# This file is part of satpy.
#
# satpy 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.
#
# satpy 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
# satpy. If not, see <http://www.gnu.org/licenses/>.
# type: ignore
"""Interface to CALIOP L2 HDF4 cloud products."""
import logging
import os.path
import re
from datetime import datetime
from pyhdf.SD import SD, SDC
from satpy.dataset import Dataset
from satpy.readers.file_handlers import BaseFileHandler
logger = logging.getLogger(__name__)
class HDF4BandReader(BaseFileHandler):
"""CALIOP v3 HDF4 reader."""
def __init__(self, filename, filename_info, filetype_info):
"""Initialze file handler."""
super(HDF4BandReader, self).__init__(filename,
filename_info,
filetype_info)
self.lons = None
self.lats = None
self._start_time = None
self._end_time = None
self.get_filehandle()
self._start_time = filename_info['start_time']
logger.debug('Retrieving end time from metadata array')
self.get_end_time()
def get_end_time(self):
"""Get observation end time from file metadata."""
mda_dict = self.filehandle.attributes()
core_mda = mda_dict['coremetadata']
end_time_str = self.parse_metadata_string(core_mda)
self._end_time = datetime.strptime(end_time_str, "%Y-%m-%dT%H:%M:%SZ")
@staticmethod
def parse_metadata_string(metadata_string):
"""Grab end time with regular expression."""
regex = r"STOP_DATE.+?VALUE\s*=\s*\"(.+?)\""
match = re.search(regex, metadata_string, re.DOTALL)
end_time_str = match.group(1)
return end_time_str
def get_filehandle(self):
"""Get HDF4 filehandle."""
if os.path.exists(self.filename):
self.filehandle = SD(self.filename, SDC.READ)
logger.debug("Loading dataset {}".format(self.filename))
else:
raise IOError("Path {} does not exist.".format(self.filename))
def get_dataset(self, key, info):
"""Read data from file and return the corresponding projectables."""
if key['name'] in ['longitude', 'latitude']:
logger.debug('Reading coordinate arrays.')
if self.lons is None or self.lats is None:
self.lons, self.lats = self.get_lonlats()
if key['name'] == 'latitude':
proj = Dataset(self.lats, id=key, **info)
else:
proj = Dataset(self.lons, id=key, **info)
else:
data = self.get_sds_variable(key['name'])
proj = Dataset(data, id=key, **info)
return proj
def get_sds_variable(self, name):
"""Read variable from the HDF4 file."""
sds_obj = self.filehandle.select(name)
data = sds_obj.get()
return data
def get_lonlats(self):
"""Get longitude and latitude arrays from the file."""
longitudes = self.get_sds_variable('Longitude')
latitudes = self.get_sds_variable('Latitude')
return longitudes, latitudes
@property
def start_time(self):
"""Get start time."""
return self._start_time
@property
def end_time(self):
"""Get end time."""
return self._end_time