-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathghrsst_l2.py
80 lines (70 loc) · 3.2 KB
/
ghrsst_l2.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
# -*- coding: utf-8 -*-
# Copyright (c) 2017 - 2020 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/>.
"""Reader for the GHRSST level-2 formatted data."""
from datetime import datetime
from satpy.readers.file_handlers import BaseFileHandler
from satpy import CHUNK_SIZE
import xarray as xr
import tarfile
import os
import tempfile
class GHRSSTL2FileHandler(BaseFileHandler):
"""File handler for GHRSST L2 netCDF files."""
def __init__(self, filename, filename_info, filetype_info, engine=None):
"""Initialize the file handler for GHRSST L2 netCDF data."""
super(GHRSSTL2FileHandler, self).__init__(filename, filename_info, filetype_info)
if filename.endswith('tar'):
with tempfile.TemporaryDirectory() as tempdir:
with tarfile.open(name=filename, mode='r') as tf:
sst_filename = next((name for name in tf.getnames()
if name.endswith('nc') and 'GHRSST-SSTskin' in name))
tf.extract(sst_filename, tempdir)
fullpath = os.path.join(tempdir, sst_filename)
self.nc = xr.open_dataset(fullpath,
decode_cf=True,
mask_and_scale=True,
engine=engine,
chunks={'ni': CHUNK_SIZE,
'nj': CHUNK_SIZE})
else:
self.nc = xr.open_dataset(filename,
decode_cf=True,
mask_and_scale=True,
engine=engine,
chunks={'ni': CHUNK_SIZE,
'nj': CHUNK_SIZE})
self.nc = self.nc.rename({'ni': 'x', 'nj': 'y'})
self.filename_info['start_time'] = datetime.strptime(
self.nc.start_time, '%Y%m%dT%H%M%SZ')
self.filename_info['end_time'] = datetime.strptime(
self.nc.stop_time, '%Y%m%dT%H%M%SZ')
def get_dataset(self, key, info):
"""Get any available dataset."""
stdname = info.get('standard_name')
return self.nc[stdname].squeeze()
@property
def start_time(self):
"""Get start time."""
return self.filename_info['start_time']
@property
def end_time(self):
"""Get end time."""
return self.filename_info['end_time']
@property
def sensor(self):
"""Get the sensor name."""
return self.nc.attrs['sensor'].lower()