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

Cdat web plot subsetting #1951

Merged
merged 16 commits into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Packages/cdms2/Lib/CDMLParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import CDML
import re
import cdmsNode
import string

# Error constants
InvalidAttribute = "Invalid attribute"
Expand Down Expand Up @@ -52,7 +51,7 @@ def handle_data(self, data):
if not matchObj:
if self.verbose: print 'data:',data
if self.root:
self.getCurrentNode().setContentFromString(string.strip(data))
self.getCurrentNode().setContentFromString(data.strip())

def handle_cdata(self, data):
if self.verbose: print 'cdata:', `data`
Expand Down Expand Up @@ -125,7 +124,7 @@ def start_axis(self,attrs):
datatype = attrs.get('datatype')
if _Integer.match(length_s) is None:
raise InvalidAttribute, 'length='+length_s
length = string.atoi(length_s)
length = int(length_s)
axis = cdmsNode.AxisNode(id,length,datatype)
partstring = attrs.get('partition')
if partstring is not None:
Expand Down Expand Up @@ -202,11 +201,11 @@ def start_domElem(self, attrs):
start_s = attrs.get('start')
length_s = attrs.get('length')
if start_s is not None:
start = string.atoi(start_s)
start = int(start_s)
else:
start = None
if length_s is not None:
length = string.atoi(length_s)
length = int(length_s)
else:
length = None
domElem = cdmsNode.DomElemNode(name,start,length)
Expand Down Expand Up @@ -250,15 +249,15 @@ def start_linear(self, attrs):
delta_s = attrs['delta']
length_s = attrs['length']
try:
start=string.atof(start_s)
start=float(start_s)
except ValueError:
raise InvalidAttribute, 'start='+start_s
try:
delta=string.atof(delta_s)
delta=float(delta_s)
except ValueError:
raise InvalidAttribute, 'delta='+delta_s
try:
length=string.atoi(length_s)
length=int(length_s)
except ValueError:
raise InvalidAttribute, 'length='+length_s
linear = cdmsNode.LinearDataNode(start,delta,length)
Expand Down
55 changes: 23 additions & 32 deletions Packages/cdms2/Lib/avariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

"CDMS Variable objects, abstract interface"
import numpy
import types
import string
import re
import warnings
import cdmsNode
Expand Down Expand Up @@ -404,7 +402,7 @@ def getMissing(self, asarray=0):

if asarray==0 and isinstance(mv, numpy.ndarray):
mv = mv[0]
if type(mv) is types.StringType and self.dtype.char not in ['?','c','O','S']:
if isinstance(mv, basestring) and self.dtype.char not in ['?','c','O','S']:
mv = float(mv)
return mv

Expand All @@ -423,17 +421,16 @@ def setMissing(self, value):
return

selftype = self.typecode()
valuetype = type(value)
if valuetype is numpy.ndarray:
if isnstance(value, numpy.ndarray):
value = value.astype(selftype).item()
elif isinstance(value, numpy.floating) or isinstance(value, numpy.integer):
elif isinstance(value, (numpy.floating, numpy.integer)):
value = numpy.array([value], selftype)
elif valuetype in [types.FloatType, types.IntType, types.LongType, types.ComplexType]:
elif isinstance(value, (float, int, long, complex)):
try:
value = numpy.array([value], selftype)
except: # Set fill value when ar[i:j] returns a masked value
value = numpy.array([numpy.ma.default_fill_value(self)], selftype)
elif isinstance(value,(str,numpy.string_,numpy.str,numpy.string0,numpy.str_)) and selftype in ['?','c','O','S']: # '?' for Boolean and object
elif isinstance(value,(basestring,numpy.string_,numpy.str,numpy.string0,numpy.str_)) and selftype in ['?','c','O','S']: # '?' for Boolean and object
pass
else:
raise CDMSError, 'Invalid missing value %s'%`value`
Expand Down Expand Up @@ -1089,7 +1086,7 @@ def regrid (self, togrid, missing=None, order=None, mask=None, **keywords):

if re.search('^regrid', regridTool, re.I):
if keywords.has_key('diag') and \
type(keywords['diag']) == types.DictType:
isinstance(keywords['diag'], dict):
keywords['diag']['regridTool'] = 'regrid'

# the original cdms2 regridder
Expand Down Expand Up @@ -1206,7 +1203,7 @@ def _single_specs (self, specs):
if specs[i] is Ellipsis:
j = myrank - (nsupplied - (i+1))
else:
if isinstance(specs[i], types.IntType):
if isinstance(specs[i], int):
singles.append(j)
j = j + 1
i = i + 1
Expand All @@ -1227,15 +1224,15 @@ def specs2slices (self, speclist, force=None):
slicelist = []
for i in range(self.rank()):
key = speclist[i]
if isinstance(key, types.IntType): # x[i]
if isinstance(key, int): # x[i]
slicelist.append (slice(key,key+1))
elif type(key) is types.SliceType: # x[i:j:k]
elif isinstance(key, slice): # x[i:j:k]
slicelist.append(key)
elif key is unspecified or key is None or key == ':':
slicelist.append (slice(0, len(self.getAxis(i))))
elif key is Ellipsis:
raise CDMSError, "Misuse of ellipsis in specification."
elif type(key) is types.TupleType:
elif sinstance(key, tuple):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isinstance is misspeled here. Would flake8 catch this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep i need to run flake8 on this.

slicelist.append(slice(*key))
else:
raise CDMSError, 'invalid index: %s'% str(key)
Expand Down Expand Up @@ -1287,13 +1284,13 @@ def reg_specs2slices(self, initspeclist,force=None):

for i in range(self.rank()):
item = speclist[i]
if isinstance(item, types.SliceType):
if isinstance(item, slice):
newitem = item
elif item==':' or item is None or item is unspecified:
axis = self.getAxis(i)
newitem = slice(0,len(axis))
elif isinstance(item, types.ListType) or \
isinstance(item, types.TupleType):
elif isinstance(item, list) or \
isinstance(item, tuple):
axis = self.getAxis(i)
if len(item)==2: # (start,end)
indexInterval = axis.mapIntervalExt(item)
Expand All @@ -1314,13 +1311,7 @@ def reg_specs2slices(self, initspeclist,force=None):
if indexInterval is None:
raise CDMSError, OutOfRange + str(item)
newitem = slice(indexInterval[0],indexInterval[1],indexInterval[2])
elif isinstance(item, numpy.floating) or \
isinstance(item, types.FloatType) or \
isinstance(item, numpy.integer) or \
isinstance(item, types.IntType) or \
isinstance(item, types.LongType) or \
isinstance(item, types.StringType) or \
type(item) in CdtimeTypes:
elif isinstance(item, (numpy.floating, float, numpy.integer, int, long, basestring)) or type(item) in CdtimeTypes:
axis = self.getAxis(i)
#
# default is 'ccn' in axis.mapIntervalExt
Expand Down Expand Up @@ -1397,10 +1388,10 @@ def getGridIndices(self):
# numpy.ma overrides

def __getitem__(self, key):
if type(key) is types.TupleType:
if isinstance(key, tuple):
speclist = self._process_specs(key, {})
else:
if isinstance(key, types.IntType) and key>=len(self):
if isinstance(key, int) and key>=len(self):
raise IndexError, "Index too large: %d"%key
speclist = self._process_specs([key], {})

Expand Down Expand Up @@ -1509,7 +1500,7 @@ def orderparse (order):
remaining axes.
(name) meaning an axis whose id is name
"""
if not isinstance(order, types.StringType):
if not isinstance(order, basestring):
raise CDMSError, 'order arguments must be strings.'
pos = 0
result=[]
Expand All @@ -1523,8 +1514,8 @@ def orderparse (order):
elif r == '...':
r = Ellipsis
elif len(r) == 1:
if r in string.digits:
r = string.atoi(r)
if r in '0123456789':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use r.isdigit()

r = int(r)
result.append(r)
pos = m.end(0)

Expand All @@ -1544,9 +1535,9 @@ def order2index (axes, order):
remaining axes.
(name) meaning an axis whose id is name
"""
if isinstance(order, types.StringType):
if isinstance(order, basestring):
result = orderparse(order)
elif isinstance(order, types.ListType):
elif isinstance(order, list):
result = order
else:
raise CDMSError, 'order2index, order specified of bad type:' + str(type(order))
Expand All @@ -1557,7 +1548,7 @@ def order2index (axes, order):
pos = 0
while j < len(result):
item = result[j]
if isinstance(item, types.StringType):
if isinstance(item, basestring):
if item == 't':
spec = 'time'
elif item == 'x':
Expand All @@ -1581,7 +1572,7 @@ def order2index (axes, order):
break
else:
raise CDMSError, 'No axis matching order spec %s' %str(item)
elif isinstance(item, types.IntType):
elif isinstance(item, int):
if item in permutation:
raise CDMSError, 'Duplicate item in order %s' % order
if item >= n:
Expand Down
Loading