Skip to content

Commit

Permalink
move collection of segment info out of the init, to be called only la…
Browse files Browse the repository at this point in the history
…ter during get_dataset

uses the caching trick as discussed in pytroll#2237 (comment)
  • Loading branch information
ameraner committed Nov 25, 2022
1 parent b2d4d8e commit 132f9a3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 104 deletions.
8 changes: 7 additions & 1 deletion satpy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __get__(self, instance, owner=None): # noqa
raise TypeError(
"Cannot use cached_property instance without calling __set_name__ on it.")
try:
cache = instance.__dict__
cache = instance.__dict__ # noqa
except AttributeError: # not all objects have __dict__ (e.g. class defines slots)
msg = (
f"No '__dict__' attribute on {type(instance).__name__!r} "
Expand Down Expand Up @@ -88,3 +88,9 @@ def __get__(self, instance, owner=None): # noqa
# numpy <1.20
from numpy import dtype as DTypeLike # noqa
from numpy import ndarray as ArrayLike # noqa


try:
from functools import cache # type: ignore
except ImportError:
from functools import lru_cache as cache # noqa
12 changes: 6 additions & 6 deletions satpy/readers/fci_l1c_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@
}

HIGH_RES_GRID_INFO = {'fci_l1c_hrfi': {'grid_type': '500m',
'width': 22272},
'grid_width': 22272},
'fci_l1c_fdhsi': {'grid_type': '1km',
'width': 11136}}
'grid_width': 11136}}
LOW_RES_GRID_INFO = {'fci_l1c_hrfi': {'grid_type': '1km',
'width': 11136},
'grid_width': 11136},
'fci_l1c_fdhsi': {'grid_type': '2km',
'width': 5568}}
'grid_width': 5568}}


def _get_aux_data_name_from_dsname(dsname):
Expand Down Expand Up @@ -239,14 +239,14 @@ def get_segment_position_info(self):
'end_position_row': self[vis_06_measured_path + '/end_position_row'].item(),
'segment_height': self[vis_06_measured_path + '/end_position_row'].item() -
self[vis_06_measured_path + '/start_position_row'].item() + 1,
'segment_width': HIGH_RES_GRID_INFO[file_type]['width']
'grid_width': HIGH_RES_GRID_INFO[file_type]['grid_width']
},
LOW_RES_GRID_INFO[file_type]['grid_type']: {
'start_position_row': self[ir_105_measured_path + '/start_position_row'].item(),
'end_position_row': self[ir_105_measured_path + '/end_position_row'].item(),
'segment_height': self[ir_105_measured_path + '/end_position_row'].item() -
self[ir_105_measured_path + '/start_position_row'].item() + 1,
'segment_width': LOW_RES_GRID_INFO[file_type]['width']
'grid_width': LOW_RES_GRID_INFO[file_type]['grid_width']
}
}

Expand Down
52 changes: 24 additions & 28 deletions satpy/readers/yaml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from collections import OrderedDict, deque
from contextlib import suppress
from fnmatch import fnmatch
from functools import lru_cache
from weakref import WeakValueDictionary

import numpy as np
Expand All @@ -38,6 +37,7 @@
from yaml import UnsafeLoader

from satpy import DatasetDict
from satpy._compat import cache
from satpy.aux_download import DataDownloadMixin
from satpy.dataset import DataID, DataQuery, get_key
from satpy.dataset.dataid import default_co_keys_config, default_id_keys_config, get_keys_from_config
Expand Down Expand Up @@ -1380,63 +1380,59 @@ def __init__(self,
**kwargs):
"""Initialise the GEOVariableSegmentYAMLReader object."""
super().__init__(config_dict, filter_parameters, filter_filenames, **kwargs)
self.segment_heights = lru_cache(self._segment_heights)

def create_filehandlers(self, filenames, fh_kwargs=None):
"""Create file handler objects and collect the location information."""
created_fhs = super().create_filehandlers(filenames, fh_kwargs=fh_kwargs)
self._extract_segment_location_dicts(created_fhs)
return created_fhs

def _extract_segment_location_dicts(self, created_fhs):
self.segment_heights = cache(self._segment_heights)
self.segment_infos = dict()
for filetype, filetype_fhs in created_fhs.items():
self._initialise_segment_infos(filetype, filetype_fhs)
self._collect_segment_position_infos(filetype, filetype_fhs)

def _extract_segment_location_dicts(self, filetype):
self._initialise_segment_infos(filetype)
self._collect_segment_position_infos(filetype)
return

def _collect_segment_position_infos(self, filetype, filetype_fhs):
def _collect_segment_position_infos(self, filetype):
# collect the segment positioning infos for all available segments
filetype_fhs = self.file_handlers[filetype]
for fh in filetype_fhs:
chk_infos = fh.get_segment_position_info()
chk_infos.update({'segment_nr': fh.filename_info['segment'] - 1})
self.segment_infos[filetype]['available_segment_infos'].append(chk_infos)

def _initialise_segment_infos(self, filetype, filetype_fhs):
def _initialise_segment_infos(self, filetype):
# initialise the segment info for this filetype
filetype_fhs = self.file_handlers[filetype]
exp_segment_nr = filetype_fhs[0].filetype_info['expected_segments']
width_to_grid_type = _get_width_to_grid_type(filetype_fhs[0].get_segment_position_info())
grid_width_to_grid_type = _get_grid_width_to_grid_type(filetype_fhs[0].get_segment_position_info())
self.segment_infos.update({filetype: {'available_segment_infos': [],
'expected_segments': exp_segment_nr,
'width_to_grid_type': width_to_grid_type}})
'grid_width_to_grid_type': grid_width_to_grid_type}})

def _get_empty_segment(self, dim=None, idx=None, filetype=None):
width = self.empty_segment.shape[1]
segment_height = self.segment_heights(filetype, width)[idx]
grid_width = self.empty_segment.shape[1]
segment_height = self.segment_heights(filetype, grid_width)[idx]
return _get_empty_segment_with_height(self.empty_segment, segment_height, dim=dim)

def _segment_heights(self, filetype, width):
def _segment_heights(self, filetype, grid_width):
"""Compute optimal padded segment heights (in number of pixels) based on the location of available segments."""
grid_type = self.segment_infos[filetype]['width_to_grid_type'][width]
segment_heights = _compute_optimal_missing_segment_heights(self.segment_infos[filetype], grid_type, width)
self._extract_segment_location_dicts(filetype)
grid_type = self.segment_infos[filetype]['grid_width_to_grid_type'][grid_width]
segment_heights = _compute_optimal_missing_segment_heights(self.segment_infos[filetype], grid_type, grid_width)
return segment_heights

def _get_new_areadef_heights(self, previous_area, previous_seg_size, segment_n=None, filetype=None):
# retrieve the segment height in number of pixels
width = previous_seg_size[1]
new_height_px = self.segment_heights(filetype, width)[segment_n - 1]
grid_width = previous_seg_size[1]
new_height_px = self.segment_heights(filetype, grid_width)[segment_n - 1]
# scale the previous vertical area extent using the new pixel height
prev_area_extent = previous_area.area_extent[1] - previous_area.area_extent[3]
new_height_proj_coord = prev_area_extent * new_height_px / previous_seg_size[0]

return new_height_proj_coord, new_height_px


def _get_width_to_grid_type(seg_info):
width_to_grid_type = dict()
def _get_grid_width_to_grid_type(seg_info):
grid_width_to_grid_type = dict()
for grid_type, grid_type_seg_info in seg_info.items():
width_to_grid_type.update({grid_type_seg_info['segment_width']: grid_type})
return width_to_grid_type
grid_width_to_grid_type.update({grid_type_seg_info['grid_width']: grid_type})
return grid_width_to_grid_type


def _compute_optimal_missing_segment_heights(seg_infos, grid_type, expected_vertical_size):
Expand Down

0 comments on commit 132f9a3

Please sign in to comment.