-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Raspaud <martin.raspaud@smhi.se>
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
reader: | ||
name: satpy_cf_nc | ||
description: Reader for Satpy's NC/CF files | ||
reader: !!python/name:satpy.readers.yaml_reader.FileYAMLReader | ||
sensors: [many] | ||
default_channels: [] | ||
|
||
#datasets: | ||
|
||
file_types: | ||
graphic: | ||
file_reader: !!python/name:satpy.readers.satpy_cf_nc.SatpyCFFileHandler | ||
file_patterns: | ||
- '{platform_name}-{sensor}-{start_time:%Y%m%d%H%M%S}-{end_time:%Y%m%d%H%M%S}.nc' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2019 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 Satpy-produced netcdf files (CF writer).""" | ||
|
||
from satpy.readers.file_handlers import BaseFileHandler | ||
import logging | ||
import xarray as xr | ||
from satpy import CHUNK_SIZE | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SatpyCFFileHandler(BaseFileHandler): | ||
"""File handler for Satpy's CF netCDF files.""" | ||
|
||
def __init__(self, filename, filename_info, filetype_info): | ||
"""Initialize file handler.""" | ||
super().__init__(filename, filename_info, filetype_info) | ||
self.engine = None | ||
|
||
@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.get('end_time', self.start_time) | ||
|
||
@property | ||
def sensor(self): | ||
"""Get sensor.""" | ||
nc = xr.open_dataset(self.filename, engine=self.engine) | ||
|
||
return nc.attrs['instrument'].replace('/', '-').lower() | ||
|
||
@property | ||
def sensor_names(self): | ||
"""Get sensor set.""" | ||
return {self.sensor} | ||
|
||
def available_datasets(self, configured_datasets=None): | ||
"""Add information to configured datasets.""" | ||
# pass along existing datasets | ||
for is_avail, ds_info in (configured_datasets or []): | ||
yield is_avail, ds_info | ||
nc = xr.open_dataset(self.filename, engine=self.engine) | ||
# get dynamic variables known to this file (that we created) | ||
for var_name, val in nc.data_vars.items(): | ||
ds_info = dict(val.attrs) | ||
ds_info['file_type'] = self.filetype_info['file_type'] | ||
ds_info['name'] = var_name | ||
try: | ||
ds_info['wavelength'] = tuple(ds_info['wavelength']) | ||
except KeyError: | ||
pass | ||
try: | ||
try: | ||
ds_info['modifiers'] = tuple(ds_info['modifiers'].split(' ')) | ||
except AttributeError: | ||
pass | ||
except KeyError: | ||
pass | ||
if var_name not in ['longitude', 'latitude']: | ||
ds_info['coordinates'] = ['longitude', 'latitude'] | ||
yield True, ds_info | ||
for var_name, val in nc.coords.items(): | ||
ds_info = dict(val.attrs) | ||
ds_info['file_type'] = self.filetype_info['file_type'] | ||
ds_info['name'] = var_name | ||
ds_info['resolution'] = 742 | ||
yield True, ds_info | ||
|
||
def get_dataset(self, ds_id, ds_info): | ||
"""Get dataset.""" | ||
logger.debug("Getting data for: %s", ds_id.name) | ||
nc = xr.open_dataset(self.filename, engine=self.engine, | ||
chunks={'y': CHUNK_SIZE, 'x': CHUNK_SIZE}) | ||
file_key = ds_info.get('file_key', ds_id.name) | ||
data = nc[file_key] | ||
if file_key in nc.coords.keys(): | ||
data = data.drop_vars(list(nc.coords.keys())) | ||
try: | ||
data.attrs['wavelength'] = tuple(data.attrs['wavelength']) | ||
except KeyError: | ||
pass | ||
try: | ||
# FIXME in cf writer: this is not consitent: no modifier is (), modifiers is a string | ||
try: | ||
ds_info['modifiers'] = tuple(ds_info['modifiers'].split(' ')) | ||
except AttributeError: | ||
pass | ||
except KeyError: | ||
pass | ||
return data |