Skip to content

Commit

Permalink
Add delitem, and code 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 29, 2014
1 parent 107b52d commit 67086f0
Showing 1 changed file with 32 additions and 39 deletions.
71 changes: 32 additions & 39 deletions mpop/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
LOG = logging.getLogger(__name__)

try:
# Work around for on demand import of pyresample. pyresample depends
# Work around for on demand import of pyresample. pyresample depends
# on scipy.spatial which memory leaks on multiple imports
is_pyresample_loaded = False
from pyresample.geometry import AreaDefinition, SwathDefinition
Expand All @@ -58,6 +58,7 @@


class Satellite(object):

"""This is the satellite class. It contains information on the satellite.
"""

Expand Down Expand Up @@ -102,6 +103,7 @@ def add_method_to_instance(self, func):


class SatelliteScene(Satellite):

"""This is the satellite scene class. It is a capture of the satellite
(channels) data at given *time_slot* and *area_id*/*area*.
"""
Expand All @@ -117,7 +119,6 @@ def __init__(self, time_slot=None, area_id=None, area=None,

self.time_slot = time_slot


self.area_id = None
self.area_def = None

Expand All @@ -140,13 +141,11 @@ def __init__(self, time_slot=None, area_id=None, area=None,

self.orbit = orbit


self.info = {}

self.lat = None
self.lon = None


def get_area(self):
"""Getter for area.
"""
Expand Down Expand Up @@ -178,12 +177,14 @@ def set_area(self, area):
self.area_id = None
except AttributeError:
raise TypeError(("Malformed area argument. "
"Should be a string or an area object. "
"Not %s") % type(area))
"Should be a string or an area object. "
"Not %s") % type(area))

area = property(get_area, set_area)


class SatelliteInstrumentScene(SatelliteScene):

"""This is the satellite instrument class. It is an abstract channel
container, from which all concrete satellite scenes should be derived.
Expand All @@ -209,7 +210,7 @@ def __init__(self, time_slot=None, area_id=None, area=None,

try:
conf = OrderedConfigParser()
conf.read(os.path.join(CONFIG_PATH, self.fullname+".cfg"))
conf.read(os.path.join(CONFIG_PATH, self.fullname + ".cfg"))

for section in conf.sections():
if(not section[:-1].endswith("level") and
Expand Down Expand Up @@ -268,12 +269,19 @@ def __getitem__(self, key, aslist=False):
raise TypeError("Malformed key: " + str(key))

if len(channels) == 0:
raise KeyError("No channel corresponding to "+str(key)+".")
raise KeyError("No channel corresponding to " + str(key) + ".")
elif aslist:
return channels
else:
return channels[0]

def __delitem__(self, key):
"""Delete a channel.
"""
chan = self[key]
self.channels.remove(chan)
del chan

def __setitem__(self, key, data):
# Add a channel if it is not already in the scene. Works only if key is
# a string.
Expand Down Expand Up @@ -302,8 +310,6 @@ def __setitem__(self, key, data):
except AttributeError:
self[key].data = data



# if isinstance(data, Channel):
# self.channels.append(Channel(name=key,
# wavelength_range=data.wavelength_range,
Expand All @@ -316,15 +322,12 @@ def __setitem__(self, key, data):
# self.channels.append(Channel(name=key))
# self[key].data = data



def __str__(self):
return "\n".join([str(chn) for chn in self.channels])

def __iter__(self):
return self.channels.__iter__()


def _set_reader(self, pformat):
"""Gets the reader for *pformat* format, and puts it in the `reader`
attribute.
Expand All @@ -335,7 +338,7 @@ def _set_reader(self, pformat):
reader_module = pformat
reading_element = 'load'

reader = "mpop.satin."+reader_module
reader = "mpop.satin." + reader_module

# Loading old style plugins
reader_module = pformat
Expand All @@ -352,7 +355,6 @@ def _set_reader(self, pformat):
loader = __import__(reader, globals(),
locals(), [reading_element])


# Build a custom Reader plugin on the fly...
from mpop.plugin_base import Reader
reader_class = type(elements[-1].capitalize() + "Reader",
Expand All @@ -368,12 +370,11 @@ def _set_reader(self, pformat):

setattr(self, elements[-1] + "_reader", reader_instance)


else:
reader_module = ".".join(elements[:-1])
reading_element = elements[-1]

reader = "mpop.satin."+reader_module
reader = "mpop.satin." + reader_module
try:
# Look for builtin reader
imp.find_module(reader_module, mpop.satin.__path__)
Expand All @@ -391,10 +392,6 @@ def _set_reader(self, pformat):

return reader_instance





def load(self, channels=None, load_again=False, area_extent=None, **kwargs):
"""Load instrument data into the *channels*. *Channels* is a list or a
tuple containing channels we will load data into, designated by there
Expand Down Expand Up @@ -446,38 +443,38 @@ def load(self, channels=None, load_again=False, area_extent=None, **kwargs):
conf.read(os.path.join(CONFIG_PATH, self.fullname + ".cfg"))
if len(conf.sections()) == 0:
raise ConfigParser.NoSectionError(("Config file did "
"not make sense"))
"not make sense"))
levels = [section for section in conf.sections()
if section.startswith(self.instrument_name+"-level")]
if section.startswith(self.instrument_name + "-level")]
except ConfigParser.NoSectionError:
LOG.warning("Can't load data, no config file for " + self.fullname)
self.channels_to_load = set()
return

levels.sort()

if levels[0] == self.instrument_name+"-level1":
if levels[0] == self.instrument_name + "-level1":
levels = levels[1:]

if len(levels) == 0:
raise ConfigParser.NoSectionError(
self.instrument_name+"-levelN (N>1) to tell me how to"+
self.instrument_name + "-levelN (N>1) to tell me how to" +
" read data... Not reading anything.")

for level in levels:
if len(self.channels_to_load) == 0:
return

LOG.debug("Looking for sources in section "+level)
LOG.debug("Looking for sources in section " + level)
reader_name = conf.get(level, 'format')
try:
reader_name = eval(reader_name)
except NameError:
reader_name = str(reader_name)
LOG.debug("Using plugin mpop.satin."+reader_name)
LOG.debug("Using plugin mpop.satin." + reader_name)

# read the data
reader = "mpop.satin."+reader_name
reader = "mpop.satin." + reader_name
try:
reader_instance = self._set_reader(reader_name)
if area_extent is not None:
Expand All @@ -490,15 +487,15 @@ def load(self, channels=None, load_again=False, area_extent=None, **kwargs):

reader_instance.load(self, **kwargs)
except ImportError, err:
LOG.exception("ImportError while loading "+reader_name+": "
LOG.exception("ImportError while loading " + reader_name + ": "
+ str(err))
continue
loaded_channels = set([chn.name for chn in self.loaded_channels()])
just_loaded = loaded_channels & self.channels_to_load
if len(just_loaded) == 0:
LOG.info("No channels loaded with " + reader + ".")
self.channels_to_load -= loaded_channels
LOG.debug("Successfully loaded: "+str(just_loaded))
LOG.debug("Successfully loaded: " + str(just_loaded))

if len(self.channels_to_load) > 0:
LOG.warning("Unable to import channels "
Expand All @@ -517,7 +514,7 @@ def save(self, filename, to_format="netcdf4", **options):
try:
writer_module = __import__(writer, globals(), locals(), ["save"])
except ImportError, err:
raise ImportError("Cannot load "+writer+" writer: "+str(err))
raise ImportError("Cannot load " + writer + " writer: " + str(err))

return writer_module.save(self, filename, **options)

Expand All @@ -532,7 +529,6 @@ def unload(self, *channels):
except AttributeError:
LOG.warning("Can't unload channel" + str(chn))


def add_to_history(self, message):
"""Adds a message to history info.
"""
Expand All @@ -543,14 +539,13 @@ def add_to_history(self, message):
else:
self.info["history"] += "\n" + timed_message


def check_channels(self, *channels):
"""Check if the *channels* are loaded, raise an error otherwise.
"""
for chan in channels:
if not self[chan].is_loaded():
raise NotLoadedError("Required channel %s not loaded,"
" aborting."%chan)
" aborting." % chan)

return True

Expand Down Expand Up @@ -598,19 +593,17 @@ def project(self, dest_area, channels=None, precompute=False, mode=None,
try:
_channels |= set([self[chn]])
except KeyError:
LOG.warning("Channel "+str(chn)+" not found,"
LOG.warning("Channel " + str(chn) + " not found,"
"thus not projected.")
else:
raise TypeError("Channels must be a list/"
"tuple/set of channel keys!")


res = copy.copy(self)

if isinstance(dest_area, str):
dest_area = mpop.projector.get_area_def(dest_area)


res.area = dest_area
res.channels = []

Expand Down Expand Up @@ -655,7 +648,7 @@ def project(self, dest_area, channels=None, precompute=False, mode=None,
chn.area = self.area + str(chn.shape)
else:
chn.area = self.area + str(chn.shape)
else: #chn.area is not None
else: # chn.area is not None
if (is_pyresample_loaded and
(not hasattr(chn.area, "area_id") or
not chn.area.area_id)):
Expand Down Expand Up @@ -690,7 +683,7 @@ def project(self, dest_area, channels=None, precompute=False, mode=None,
try:
res.channels.append(chn.project(cov[area_id]))
except NotLoadedError:
LOG.warning("Channel "+str(chn.name)+" not loaded, "
LOG.warning("Channel " + str(chn.name) + " not loaded, "
"thus not projected.")

# Compose with image object
Expand Down

0 comments on commit 67086f0

Please sign in to comment.