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

VGAC decode the time variable #2595

Merged
merged 18 commits into from
Mar 8, 2024
9 changes: 7 additions & 2 deletions satpy/etc/readers/viirs_vgac_l1c_nc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,13 @@ datasets:
units: degrees_east
nc_key: lon

time:
name: time
scanline_timestamps:
name: scanline_timestamps
resolution: 5000
file_type: vgac_nc
nc_key: time

proj_time0:
name: proj_time0
file_type: vgac_nc
nc_key: proj_time0
30 changes: 29 additions & 1 deletion satpy/readers/viirs_vgac_l1c_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def convert_to_bt(self, data, data_lut, scale_factor):

def fix_radiances_not_in_percent(self, data):
"""Scale radiances to percent. This was not done in first version of data."""
return 100*data
return 100 * data

def set_time_attrs(self, data):
"""Set time from attributes."""
Expand All @@ -73,6 +73,32 @@ def set_time_attrs(self, data):
self._end_time = data.attrs["end_time"]
self._start_time = data.attrs["start_time"]

def dt64_to_datetime(self, dt64):
"""Conversion of numpy.datetime64 to datetime objects."""
# https://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64/46921593#46921593
if type(dt64) == np.datetime64:
unix_epoch = np.datetime64(0, 's')
one_second = np.timedelta64(1, 's')
seconds_since_epoch = (dt64 - unix_epoch) / one_second
dt = datetime.utcfromtimestamp(seconds_since_epoch)
return dt
return dt64
ninahakansson marked this conversation as resolved.
Show resolved Hide resolved

def decode_time_variable(self, data, nc):
"""Decode time variable."""
if data.attrs["units"] == "hours since proj_time0":
reference_time = np.datetime64(datetime.strptime(nc['proj_time0'].attrs["units"],
'days since %d/%m/%YT%H:%M:%S'))
ninahakansson marked this conversation as resolved.
Show resolved Hide resolved
delta_days = float(nc['proj_time0'].values) * np.timedelta64(1, 'D').astype('timedelta64[ms]')
ninahakansson marked this conversation as resolved.
Show resolved Hide resolved
delta_hours = data.values * np.timedelta64(1, 'h').astype('timedelta64[ms]')
time_data = xr.DataArray(reference_time + delta_days + delta_hours,
coords=data.coords, attrs={"long_name": "Scanline time"})
mraspaud marked this conversation as resolved.
Show resolved Hide resolved
self._start_time = self.dt64_to_datetime(time_data[0].values)
self._end_time = self.dt64_to_datetime(time_data[-1].values)
return time_data
else:
return data
ninahakansson marked this conversation as resolved.
Show resolved Hide resolved

def get_dataset(self, key, yaml_info):
"""Get dataset."""
logger.debug("Getting data for: %s", yaml_info['name'])
Expand All @@ -82,6 +108,8 @@ def get_dataset(self, key, yaml_info):
file_key = yaml_info.get('nc_key', name)
data = nc[file_key]
data = self.calibrate(data, yaml_info, file_key, nc)
if file_key == "time":
data = self.decode_time_variable(data, nc)
data.attrs.update(nc.attrs) # For now add global attributes to all datasets
data.attrs.update(yaml_info)
self.set_time_attrs(data)
Expand Down
Loading