Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Raspaud <martin.raspaud@smhi.se>
  • Loading branch information
mraspaud committed Oct 20, 2015
1 parent 474ae98 commit 542db00
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
9 changes: 5 additions & 4 deletions mpop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from logging import getLogger
try:
import configparser
except:
except ImportError:
from six.moves import configparser
LOG = getLogger(__name__)

Expand All @@ -47,15 +47,15 @@

CONFIG_PATH = os.environ.get('PPP_CONFIG_DIR', PACKAGE_CONFIG_PATH)

def _runtime_import(object_path):

def runtime_import(object_path):
"""Import at runtime
"""
obj_module, obj_element = object_path.rsplit(".", 1)
loader = __import__(obj_module, globals(), locals(), [str(obj_element)])
return getattr(loader, obj_element)



def get_config(filename):
"""Blends the different configs, from package defaults to .
"""
Expand All @@ -80,6 +80,7 @@ def get_config(filename):

LOG.warning("Couldn't file any config file matching %s", filename)


def get_config_path(filename):
"""Get the appropriate path for a filename, in that order: filename, ., PPP_CONFIG_DIR, package's etc dir.
"""
Expand All @@ -94,4 +95,4 @@ def get_config_path(filename):

for path in paths:
if os.path.exists(path):
return path
return path
27 changes: 16 additions & 11 deletions mpop/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from datetime import datetime, timedelta
try:
import configparser
except:
except ImportError:
from six.moves import configparser
import glob

Expand All @@ -43,7 +43,7 @@

from mpop.plugin_base import Plugin
from mpop.projectable import Projectable
from mpop import _runtime_import
from mpop import runtime_import

LOG = logging.getLogger(__name__)

Expand All @@ -56,12 +56,13 @@ class ReaderFinder(object):
"""

def __init__(self, scene):
# fixme: we could just pass the info and the ppp_config_dir
self.info = scene.info.copy()
self.ppp_config_dir = scene.ppp_config_dir

def __call__(self, filenames=None, sensor=None, reader_name=None):
if reader_name is not None:
return self._find_reader_by_name(reader_name, filenames)
return self._find_reader(reader_name, filenames)
elif sensor is not None:
return self._find_sensors_readers(sensor, filenames)
elif filenames is not None:
Expand Down Expand Up @@ -245,11 +246,12 @@ def _read_reader_config(self, cfg_file):

return reader_info

def _load_reader(self, reader_info):
@staticmethod
def _load_reader(reader_info):
"""Import and setup the reader from *reader_info*
"""
try:
loader = _runtime_import(reader_info["reader"])
loader = runtime_import(reader_info["reader"])
except ImportError:
raise ImportError("Could not import reader class '%s' for reader '%s'" % (reader_info["reader"],
reader_info["name"]))
Expand All @@ -259,6 +261,7 @@ def _load_reader(self, reader_info):
# self.readers[reader_info["name"]] = reader_instance
return reader_instance

@staticmethod
def assign_matching_files(reader_info, *files):
"""Assign *files* to the *reader_info*
"""
Expand Down Expand Up @@ -306,6 +309,8 @@ def __init__(self, name=None,
self.description = self.config_options.get("description", None) if description is None else description
self.sensor = self.config_options.get("sensor", "").split(",") if sensor is None else set(sensor)

self.config_options = None

# These can't be provided by a configuration file
self.start_time = start_time
self.end_time = end_time
Expand Down Expand Up @@ -334,6 +339,7 @@ def sensor_names(self):
return sensors | self.sensor

def load_section_reader(self, section_name, section_options):
del section_name
self.config_options = section_options

def load_section_dataset(self, section_name, section_options):
Expand Down Expand Up @@ -389,7 +395,6 @@ def get_dataset(self, key, aslist=False):
else:
return datasets[0]


# get by name
else:
datasets = []
Expand Down Expand Up @@ -510,6 +515,7 @@ def _interpolate_navigation(self, lon, lat):
def _load_navigation(self, nav_name, dep_file_type, extra_mask=None):
"""Load the `nav_name` navigation.
"""
del dep_file_type
nav_info = self.navigations[nav_name]
lon_key = nav_info["longitude_key"]
lat_key = nav_info["latitude_key"]
Expand Down Expand Up @@ -627,11 +633,10 @@ def _get_dataset_info(self, name, calibration):
# Load metadata and calibration information for this dataset
try:
cal_info = self.calibrations.get(cal_name, None)
for k, info_dict in [
("file_type", self.file_types),
("file_key", self.file_keys),
("navigation", self.navigations),
("calibration", self.calibrations)]:
for k, info_dict in [("file_type", self.file_types),
("file_key", self.file_keys),
("navigation", self.navigations),
("calibration", self.calibrations)]:
val = dataset_info[k][cal_index]
if cal_info is not None:
val = cal_info.get(k, val)
Expand Down
14 changes: 7 additions & 7 deletions mpop/readers/eps_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __getitem__(self, key):
try:
self.cache[key] = int(val) * self.form.scales[altkey][key]
return self.cache[key]
except ValueError: # it's probably a string
except ValueError: # it's probably a string
self.cache[key] = val
return self.cache[key]
except ValueError:
Expand Down Expand Up @@ -245,14 +245,14 @@ def _get_channel(self, chan, calib_type, data_out=None, mask_out=None):
if chan.name == "1":
if calib_type == 1:
data_out[:] = radiance_to_refl(self["SCENE_RADIANCES"][:, 0, :],
self["CH1_SOLAR_FILTERED_IRRADIANCE"])
self["CH1_SOLAR_FILTERED_IRRADIANCE"])
else:
data_out[:] = self["SCENE_RADIANCES"][:, 0, :]
mask_out[:] = False
if chan.name == "2":
if calib_type == 1:
data_out[:] = radiance_to_refl(self["SCENE_RADIANCES"][:, 1, :],
self["CH2_SOLAR_FILTERED_IRRADIANCE"])
self["CH2_SOLAR_FILTERED_IRRADIANCE"])
else:
data_out[:] = self["SCENE_RADIANCES"][:, 1, :]
mask_out[:] = False
Expand All @@ -261,7 +261,7 @@ def _get_channel(self, chan, calib_type, data_out=None, mask_out=None):
frames = (self["FRAME_INDICATOR"] & 2 ** 16) != 0
if calib_type == 1:
data_out[frames, :] = radiance_to_refl(self["SCENE_RADIANCES"][frames, 2, :],
self["CH3A_SOLAR_FILTERED_IRRADIANCE"])
self["CH3A_SOLAR_FILTERED_IRRADIANCE"])
else:
data_out[frames, :] = np.ma.array(self["SCENE_RADIANCES"][frames, 2, :])
mask_out[~frames, :] = True
Expand All @@ -271,9 +271,9 @@ def _get_channel(self, chan, calib_type, data_out=None, mask_out=None):
frames = (self["FRAME_INDICATOR"] & 2 ** 16) == 0
if calib_type == 1:
data_out[:] = radiance_to_bt(self["SCENE_RADIANCES"][:, 2, :],
self["CH3B_CENTRAL_WAVENUMBER"],
self["CH3B_CONSTANT1"],
self["CH3B_CONSTANT2_SLOPE"])
self["CH3B_CENTRAL_WAVENUMBER"],
self["CH3B_CONSTANT1"],
self["CH3B_CONSTANT2_SLOPE"])

else:
data_out[:] = self["SCENE_RADIANCES"][:, 2, :]
Expand Down
8 changes: 4 additions & 4 deletions mpop/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from mpop import get_config, get_config_path, utils
try:
import configparser
except:
except ImportError:
from six.moves import configparser

LOG = getLogger(__name__)
Expand Down Expand Up @@ -99,7 +99,6 @@ def dump(self, filename):
LOG.info("Saving projection to %s", filename)
np.savez(filename, **self.cache)


def resample(self, data, cache_dir=False, **kwargs):
"""Resample the *data*, saving the projection info on disk if *precompute* evaluates to True.
"""
Expand All @@ -111,6 +110,7 @@ def __call__(self, *args, **kwargs):
"""
self.resample(*args, **kwargs)


class KDTreeResampler(BaseResampler):
"""
Resample using nearest neighbour.
Expand Down Expand Up @@ -149,7 +149,6 @@ def hash_area(area):
area.kdtree_hash = area_hash
return area_hash


def get_hash(self, **kwargs):
"""Get hash for the current resample with the given *kwargs*.
"""
Expand Down Expand Up @@ -201,7 +200,7 @@ def precompute(self, radius_of_influence=10000, epsilon=0, reduce_data=True, npr
nprocs=nprocs,
segments=segments)

# it's important here not to modify the existing cache dictionnary.
# it's important here not to modify the existing cache dictionary.
self.cache = {"valid_input_index": valid_input_index,
"valid_output_index": valid_output_index,
"index_array": index_array,
Expand Down Expand Up @@ -234,6 +233,7 @@ def compute(self, data, weight_funcs=None, fill_value=None, with_uncert=False, *
RESAMPLERS = {"kd_tree": KDTreeResampler,
"nearest": KDTreeResampler}


def resample(source_area, data, destination_area, resampler=KDTreeResampler, **kwargs):
"""Do the resampling
"""
Expand Down
6 changes: 3 additions & 3 deletions mpop/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import os
import logging

from mpop import _runtime_import
from mpop import runtime_import
from mpop.projectable import Projectable, InfoObject
from mpop import PACKAGE_CONFIG_PATH
from mpop.readers import ReaderFinder
Expand Down Expand Up @@ -113,7 +113,7 @@ def read_composites_config(self, composite_config=None, sensor=None, names=None,
options["name"])

try:
loader = _runtime_import(comp_cls)
loader = runtime_import(comp_cls)
except ImportError:
LOG.warning("Could not import composite class '%s' for"
" compositor '%s'", comp_cls, options["name"])
Expand Down Expand Up @@ -372,7 +372,7 @@ def load_writer_config(self, config_file, **kwargs):
if section_name.startswith("writer:"):
options = dict(conf.items(section_name))
writer_class_name = options["writer"]
writer_class = _runtime_import(writer_class_name)
writer_class = runtime_import(writer_class_name)
writer = writer_class(ppp_config_dir=self.ppp_config_dir, config_file=config_file, **kwargs)
return writer

Expand Down

0 comments on commit 542db00

Please sign in to comment.