New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add satpy cf reader #1205
Closed
Closed
Add satpy cf reader #1205
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except (AttributError, KeyError):
or is this doing something special?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the most indented try shouldn't be needed if we fix the cf writer, so it's not supposed to be there. But yeah, I wanted to distinguish the two errors for now. The KeyError is ok, the AttributeError shouldn't happen at all...