Skip to content

Commit

Permalink
Make the nwcsaf nc readers tests use real files
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Jan 18, 2023
1 parent 41179bd commit 3854afd
Show file tree
Hide file tree
Showing 2 changed files with 305 additions and 229 deletions.
54 changes: 30 additions & 24 deletions satpy/readers/nwcsaf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import functools
import logging
import os
from contextlib import suppress
from datetime import datetime

import dask.array as da
Expand Down Expand Up @@ -157,8 +158,22 @@ def get_dataset(self, dsid, info):
variable = self.remove_timedim(variable)
variable = self.scale_dataset(variable, info)
variable = self.drop_xycoords(variable)

self.get_orbital_parameters(variable)
variable.attrs["start_time"] = self.start_time
variable.attrs["end_time"] = self.end_time

return variable

def get_orbital_parameters(self, variable):
"""Get the orbital parameters from the file if possible (geo)."""
with suppress(KeyError):
gdal_params = dict(elt.strip("+").split("=") for elt in self.nc.attrs["gdal_projection"].split())
variable.attrs["orbital_parameters"] = dict(
satellite_nominal_altitude=float(gdal_params["h"]),
satellite_nominal_longitude=self.nc.attrs["sub-satellite_longitude"],
satellite_nominal_latitude=0)

def _get_varname_in_file(self, info, info_type="file_key"):
if isinstance(info[info_type], list):
for key in info[info_type]:
Expand Down Expand Up @@ -344,34 +359,12 @@ def __del__(self):
@property
def start_time(self):
"""Return the start time of the object."""
try:
# MSG:
try:
return datetime.strptime(self.nc.attrs['time_coverage_start'],
'%Y-%m-%dT%H:%M:%SZ')
except TypeError:
return datetime.strptime(self.nc.attrs['time_coverage_start'].astype(str),
'%Y-%m-%dT%H:%M:%SZ')
except ValueError:
# PPS:
return datetime.strptime(self.nc.attrs['time_coverage_start'],
'%Y%m%dT%H%M%S%fZ')
return read_nwcsaf_time(self.nc.attrs['time_coverage_start'])

@property
def end_time(self):
"""Return the end time of the object."""
try:
# MSG:
try:
return datetime.strptime(self.nc.attrs['time_coverage_end'],
'%Y-%m-%dT%H:%M:%SZ')
except TypeError:
return datetime.strptime(self.nc.attrs['time_coverage_end'].astype(str),
'%Y-%m-%dT%H:%M:%SZ')
except ValueError:
# PPS:
return datetime.strptime(self.nc.attrs['time_coverage_end'],
'%Y%m%dT%H%M%S%fZ')
return read_nwcsaf_time(self.nc.attrs['time_coverage_end'])

@property
def sensor_names(self):
Expand Down Expand Up @@ -414,3 +407,16 @@ def remove_empties(variable):
variable.attrs.pop(key)

return variable


def read_nwcsaf_time(time_value):
"""Read the time, nwcsaf-style."""
try:
# MSG:
try:
return datetime.strptime(time_value, '%Y-%m-%dT%H:%M:%SZ')
except TypeError:
return datetime.strptime(time_value.astype(str), '%Y-%m-%dT%H:%M:%SZ')
except ValueError:
# PPS:
return datetime.strptime(time_value, '%Y%m%dT%H%M%S%fZ')
Loading

0 comments on commit 3854afd

Please sign in to comment.