Commit d9a9dba5 authored by Jeff Piollé's avatar Jeff Piollé

fields returned as ordered dictionary

parent e7d813dc
......@@ -13,7 +13,7 @@ Abstract class for all data feature objects
import logging
import datetime
import copy
import collections
from collections import OrderedDict
from abc import ABCMeta, abstractmethod
import netCDF4
......@@ -34,8 +34,8 @@ class AbstractFeature(object):
Abstract class for all feature objects
Args:
fields (dict): a dictionary of :class:`Field` objects to be added
to the feature content, where the key is a field identifier
fields (OrderedDict): a dictionary of :class:`Field` objects to be
added to the feature content, where the key is a field identifier
bbox (tuple): describes the limites of the covered geographical
area as a tuple (lonmin,latmin,lonmax,latmax)
......@@ -55,16 +55,30 @@ class AbstractFeature(object):
"""
"""
object.__init__(self)
if fields is not None and not isinstance(fields, dict):
raise Exception('fields must be a dictionary (use an OrderedDict '
'if you want to write fields in a specific order).')
# check type of fields
if fields is not None:
if isinstance(fields, dict):
logging.warning("Usage of dict for 'fields' is deprecated")
self._fields = OrderedDict(fields)
elif not isinstance(fields, OrderedDict):
raise TypeError(
'fields must be a dictionary (use an OrderedDict '
'if you want to write fields in a specific order)'
)
else:
self._fields = fields
else:
self._fields = OrderedDict()
self._datastorage = None
# geolocation fields (time,lat,lon)
self._geolocation_fields = {'time': None,
'lat': None,
'lon': None,
'z': None
}
self._geolocation_fields = OrderedDict([
('time', None),
('lat', None),
('lon', None),
('z', None)
])
self.spatial_resolution = spatial_resolution
if longitudes is not None:
self.set_lon(longitudes)
......@@ -109,10 +123,6 @@ class AbstractFeature(object):
self.metadata = metadata
if not metadata:
self.metadata = {}
self._fields = fields
if fields is None:
self._fields = collections.OrderedDict()
return
def __str__(self):
result = 'FEATURE : %s\n' % self.__class__.__name__
......@@ -154,19 +164,19 @@ class AbstractFeature(object):
def get_wkt_bbox(self):
"""Return the bounding box in WKT format."""
#Generate a list of sensible names for the string format
# Generate a list of sensible names for the string format
if self._wkt_bbox is None:
cardinal_names = ['lon_min', 'lat_min', 'lon_max', 'lat_max']
#Create empty WKT string
# Create empty WKT string
polygon = 'POLYGON (({lon_min} {lat_min}, {lon_max} '\
'{lat_min}, {lon_max} {lat_max}, {lon_min} {lat_max}, '\
'{lon_min} {lat_min}))'
#Generate a dictionary with these names and the bbox info
# Generate a dictionary with these names and the bbox info
bbox = dict(zip(cardinal_names, self.get_bbox()))
#Return the formated string as WKT.
# Return the formated string as WKT.
self._wkt_bbox = polygon.format(**bbox)
return self._wkt_bbox
......@@ -208,7 +218,7 @@ class AbstractFeature(object):
dict: an ordered dictionary where key/values are the dimension
names and sizes
"""
dims = collections.OrderedDict()
dims = OrderedDict()
for dim in list(self.get_geolocation_field_dimnames(fieldname)):
dims[dim] = self.get_geolocation_dimsizes()[dim]
return dims
......@@ -413,11 +423,11 @@ class AbstractFeature(object):
OrderedDict<dimension name, dimension size>: dimensions of time
field for the feature class
"""
if isinstance(values, numpy.ndarray) and \
isinstance(values[0], datetime.datetime):
return collections.OrderedDict([('time', 1)])
if (isinstance(values, numpy.ndarray) and
isinstance(values[0], datetime.datetime)):
return OrderedDict([('time', 1)])
else:
dims = collections.OrderedDict(
dims = OrderedDict(
zip(list(self.get_geolocation_field_dimnames('time')),
list(values.shape))
)
......@@ -502,7 +512,7 @@ class AbstractFeature(object):
authority=CF_AUTHORITY,
standardname='longitude'
)
dims = collections.OrderedDict(
dims = OrderedDict(
zip(list(self.get_geolocation_field_dimnames('lon')),
list(values.shape)
)
......@@ -563,7 +573,7 @@ class AbstractFeature(object):
authority=CF_AUTHORITY,
standardname='latitude'
)
dims = collections.OrderedDict(
dims = OrderedDict(
zip(list(self.get_geolocation_field_dimnames('lat')),
list(values.shape))
)
......@@ -602,7 +612,7 @@ class AbstractFeature(object):
authority=CF_AUTHORITY,
standardname=ztype
)
dims = collections.OrderedDict(
dims = OrderedDict(
zip(list(self.get_geolocation_field_dimnames('depth')),
list(values.shape))
)
......@@ -610,8 +620,8 @@ class AbstractFeature(object):
var,
dims,
values=values,
attributes={'depth':{'positive':'down'},
'height':{'positive':'up'}}[ztype]
attributes={'depth': {'positive': 'down'},
'height': {'positive': 'up'}}[ztype]
)
field.units = 'm'
self._geolocation_fields['z'] = field
......@@ -775,7 +785,7 @@ class AbstractFeature(object):
if self._datastorage is None:
metadata = self.get_metadata()
if 'time_coverage_start' in metadata:
if isinstance(metadata['time_coverage_start'],
if isinstance(metadata['time_coverage_start'],
datetime.datetime):
return metadata['time_coverage_start']
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment