Skip to content
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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions satpy/etc/readers/satpy_cf_nc.yaml
@@ -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'
112 changes: 112 additions & 0 deletions satpy/readers/satpy_cf_nc.py
@@ -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:
Copy link
Member

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?

Copy link
Member Author

@mraspaud mraspaud May 13, 2020

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...

pass
return data