Exemplo n.º 1
0
    def __getitem__(self, key):
        if isinstance(key, basestring):
            data = self._dataframe.getColumnData(key)
            if data is None:
                return data
            idx = self._index[:]
            r = series.Series(MIArray(data), idx, key)
            return r

        hascolkey = True
        if isinstance(key, tuple):
            ridx = key[0]
            cidx = key[1]
            if isinstance(ridx, int) and isinstance(cidx, int):
                if ridx < 0:
                    ridx = self.shape[0] + ridx
                if cidx < 0:
                    cidx = self.shape[1] + cidx
                return self._dataframe.getValue(ridx, cidx)
            elif isinstance(ridx, int) and isinstance(cidx, basestring):
                if ridx < 0:
                    ridx = self.shape[0] + ridx
                return self._dataframe.getValue(ridx, cidx)
        else:
            key = (key, slice(None))
            hascolkey = False

        k = key[0]
        if isinstance(k, Index):
            k = k.data
        if isinstance(k, int):
            if k < 0:
                k = self.shape[0] + k
            rowkey = k
        elif isinstance(k, basestring):
            sidx = self._index.index(k)
            if sidx < 0:
                return None
            eidx = sidx
            step = 1
            rowkey = Range(sidx, eidx, step)
        elif isinstance(k, slice):
            if isinstance(k.start, basestring):
                sidx = self._index.index(k.start)
                if sidx < 0:
                    sidx = 0
            else:
                sidx = 0 if k.start is None else k.start
                if sidx < 0:
                    sidx = self.shape[0] + sidx
            if isinstance(k.stop, basestring):
                eidx = self._index.index(k.stop)
                if eidx < 0:
                    eidx = self.shape[0] + eidx
            else:
                eidx = self.shape[0] - 1 if k.stop is None else k.stop - 1
                if eidx < 0:
                    eidx = self.shape[0] + eidx
            step = 1 if k.step is None else k.step
            rowkey = Range(sidx, eidx, step)
        elif isinstance(k, (list, tuple, MIArray)):
            if isinstance(k[0], int):
                rowkey = k
            else:
                tlist = []
                for tstr in k:
                    idx = self._index.index(tstr)
                    if idx >= 0:
                        tlist.append(idx)
                rowkey = tlist
        else:
            rowkey = self._index.get_loc(k)

        if not hascolkey:
            colkey = Range(0, self.shape[1] - 1, 1)
        else:
            k = key[1]
            if isinstance(k, int):
                sidx = k
                if sidx < 0:
                    sidx = self.shape[1] + sidx
                eidx = sidx
                step = 1
                colkey = Range(sidx, eidx, step)
            elif isinstance(k, slice):
                sidx = 0 if k.start is None else k.start
                if sidx < 0:
                    sidx = self.shape[1] + sidx
                eidx = self.shape[1] - 1 if k.stop is None else k.stop - 1
                if eidx < 0:
                    eidx = self.shape[1] + eidx
                step = 1 if k.step is None else k.step
                colkey = Range(sidx, eidx, step)
            elif isinstance(k, list):
                if isinstance(k[0], int):
                    colkey = k
                else:
                    colkey = self.columns.indexOfName(k)
            elif isinstance(k, basestring):
                col = self.columns.indexOf(k)
                colkey = Range(col, col + 1, 1)
            else:
                return None

        r = self._dataframe.select(rowkey, colkey)
        if r is None:
            return None
        if isinstance(r, MISeries):
            r = series.Series(series=r)
        else:
            r = DataFrame(dataframe=r)
        return r
Exemplo n.º 2
0
 def __getitem__(self, indices):
     if indices is None:
         inds = []
         for i in range(self.ndim):
             inds.append(slice(None))
         indices = tuple(inds)
             
     if isinstance(indices, str):    #metadata
         rr = self.dataset.read(self.name)
         m = rr.findMember(indices)
         data = rr.getArray(0, m)
         return MIArray(data)
     
     if not isinstance(indices, tuple):
         inds = []
         inds.append(indices)
         indices = inds
     
     if len(indices) != self.ndim:
         print 'indices must be ' + str(self.ndim) + ' dimensions!'
         return None
         
     if not self.proj is None and not self.proj.isLonLat():
         xlim = None
         ylim = None
         xidx = -1
         yidx = -1
         for i in range(0, self.ndim):
             dim = self.dims[i]
             if dim.getDimType() == DimensionType.X:                    
                 k = indices[i]
                 if isinstance(k, basestring):
                     xlims = k.split(':')
                     if len(xlims) == 1:
                         xlim = [float(xlims[0])]
                     else:
                         xlim = [float(xlims[0]), float(xlims[1])]
                     xidx = i
             elif dim.getDimType() == DimensionType.Y:
                 k = indices[i]
                 if isinstance(k, basestring):
                     ylims = k.split(':')
                     if len(ylims) == 1:
                         ylim = [float(ylims[0])]
                     else:
                         ylim = [float(ylims[0]), float(ylims[1])]
                     yidx = i
         if not xlim is None and not ylim is None:                
             fromproj=KnownCoordinateSystems.geographic.world.WGS1984
             inpt = PointD(xlim[0], ylim[0])
             outpt1 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
             if len(xlim) == 1:
                 xlim = [outpt1.X]
                 ylim = [outpt1.Y]
             else:
                 inpt = PointD(xlim[1], ylim[1])
                 outpt2 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
                 xlim = [outpt1.X, outpt2.X]
                 ylim = [outpt1.Y, outpt2.Y]
             indices1 = []
             for i in range(0, self.ndim):
                 if i == xidx:
                     if len(xlim) == 1:
                         indices1.append(str(xlim[0]))
                     else:
                         indices1.append(str(xlim[0]) + ':' + str(xlim[1]))
                 elif i == yidx:
                     if len(ylim) == 1:
                         indices1.append(str(ylim[0]))
                     else:
                         indices1.append(str(ylim[0]) + ':' + str(ylim[1]))
                 else:
                     indices1.append(indices[i])
             indices = indices1
     
     origin = []
     size = []
     stride = []
     ranges = []
     dims = []
     flips = []
     onlyrange = True
     for i in range(0, self.ndim):  
         isrange = True
         dimlen = self.dimlen(i)
         k = indices[i]
         if isinstance(k, int):
             if k < 0:
                 k = self.dims[i].getLength() + k
             sidx = k
             eidx = k
             step = 1
         elif isinstance(k, slice):
             if isinstance(k.start, basestring):
                 sv = float(k.start)
                 sidx = self.dims[i].getValueIndex(sv)
             elif isinstance(k.start, datetime.datetime):
                 sv = miutil.date2num(k.start)
                 sidx = self.dims[i].getValueIndex(sv)
             else:
                 sidx = 0 if k.start is None else k.start
             if sidx < 0:
                 sidx = self.dimlen(i) + sidx
                 
             if isinstance(k.stop, basestring):
                 ev = float(k.stop)
                 eidx = self.dims[i].getValueIndex(ev)
             elif isinstance(k.stop, datetime.datetime):
                 ev = miutil.date2num(k.stop)
                 eidx = self.dims[i].getValueIndex(ev)
             else:
                 eidx = self.dimlen(i) if k.stop is None else k.stop
                 if eidx < 0:
                     eidx = self.dimlen(i) + eidx
                 eidx -= 1
                 
             if isinstance(k.step, basestring):
                 nv = float(k.step) + self.dims[i].getDimValue()[0]
                 nidx = self.dims[i].getValueIndex(nv)
                 step = nidx - sidx
             elif isinstance(k.step, datetime.timedelta):
                 nv = miutil.date2num(k.start + k.step)
                 nidx = self.dims[i].getValueIndex(nv)
                 step = nidx - sidx
             else:
                 step = 1 if k.step is None else k.step
             if sidx > eidx:
                 iidx = eidx
                 eidx = sidx
                 sidx = iidx
         elif isinstance(k, list):
             onlyrange = False
             isrange = False
             if not isinstance(k[0], datetime.datetime):
                 ranges.append(k)
             else:
                 tlist = []
                 for tt in k:
                     sv = miutil.date2num(tt)
                     idx = self.dims[i].getValueIndex(sv)
                     tlist.append(idx)
                 ranges.append(tlist)
                 k = tlist
         elif isinstance(k, basestring):
             dim = self.variable.getDimension(i)
             kvalues = k.split(':')
             sv = float(kvalues[0])
             sidx = dim.getValueIndex(sv)
             if len(kvalues) == 1:
                 eidx = sidx
                 step = 1
             else:                    
                 ev = float(kvalues[1])
                 eidx = dim.getValueIndex(ev)
                 if len(kvalues) == 2:
                     step = 1
                 else:
                     step = int(float(kvalues[2]) / dim.getDeltaValue())
                 if sidx > eidx:
                     iidx = eidx
                     eidx = sidx
                     sidx = iidx
         else:
             print k
             return None
         if isrange:
             if eidx >= dimlen:
                 print 'Index out of range!'
                 return None
             origin.append(sidx)
             n = eidx - sidx + 1
             size.append(n)                   
             if n > 1:
                 dim = self.variable.getDimension(i)
                 if dim.isReverse():
                     step = -step      
                 dim = dim.extract(sidx, eidx, step)
                 dim.setReverse(False)
                 dims.append(dim)
             stride.append(step) 
             if step < 0:
                 step = abs(step)
                 flips.append(i)
             rr = Range(sidx, eidx, step)
             ranges.append(rr)
         else:
             if len(k) > 1:
                 dim = self.variable.getDimension(i)
                 dim = dim.extract(k)
                 dim.setReverse(False)
                 dims.append(dim)
     #rr = self.dataset.read(self.name, origin, size, stride).reduce()
     if onlyrange:
         rr = self.dataset.dataset.read(self.name, ranges)
     else:
         rr = self.dataset.dataset.take(self.name, ranges)
     if rr.getSize() == 1:
         return rr.getObject(0)
     else:
         for i in flips:
             rr = rr.flip(i)
         rr = rr.reduce()
         ArrayMath.missingToNaN(rr, self.fill_value)
         if len(flips) > 0:
             rrr = Array.factory(rr.getDataType(), rr.getShape())
             MAMath.copy(rrr, rr)
             array = MIArray(rrr)
         else:
             array = MIArray(rr)
         data = DimArray(array, dims, self.fill_value, self.dataset.proj)
         return data
Exemplo n.º 3
0
 def __getitem__(self, indices):
     #print type(indices)
     if not isinstance(indices, tuple):
         inds = []
         inds.append(indices)
         indices = inds
     
     if len(indices) != self.ndim:
         print 'indices must be ' + str(self.ndim) + ' dimensions!'
         raise IndexError()
         
     if not self.proj is None and not self.proj.isLonLat():
         xlim = None
         ylim = None
         xidx = -1
         yidx = -1
         for i in range(0, self.ndim):
             dim = self.dims[i]
             if dim.getDimType() == DimensionType.X:                    
                 k = indices[i]
                 #if isinstance(k, (tuple, list)):
                 if isinstance(k, basestring):
                     xlims = k.split(':')
                     xlim = [float(xlims[0]), float(xlims[1])]
                     xidx = i
             elif dim.getDimType() == DimensionType.Y:
                 k = indices[i]
                 #if isinstance(k, (tuple, list)):
                 if isinstance(k, basestring):
                     ylims = k.split(':')
                     ylim = [float(ylims[0]), float(ylims[1])]
                     yidx = i
         if not xlim is None and not ylim is None:                
             fromproj=KnownCoordinateSystems.geographic.world.WGS1984
             inpt = PointD(xlim[0], ylim[0])
             outpt1 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
             inpt = PointD(xlim[1], ylim[1])
             outpt2 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
             xlim = [outpt1.X, outpt2.X]
             ylim = [outpt1.Y, outpt2.Y]
             indices1 = []
             for i in range(0, self.ndim):
                 if i == xidx:
                     #indices1.append(xlim
                     indices1.append(str(xlim[0]) + ':' + str(xlim[1]))
                 elif i == yidx:
                     #indices1.append(ylim)
                     indices1.append(str(ylim[0]) + ':' + str(ylim[1]))
                 else:
                     indices1.append(indices[i])
             indices = indices1
         
     #origin = []
     #size = []
     #stride = []
     dims = []
     ranges = []
     flips = []
     iszerodim = True
     onlyrange = True
     for i in range(0, self.ndim):  
         isrange = True
         k = indices[i]
         if isinstance(k, int):
             if k < 0:
                 k = self.dims[i].getLength() + k
             sidx = k
             eidx = k
             step = 1                
         elif isinstance(k, slice):
             sidx = 0 if k.start is None else k.start
             if sidx < 0:
                 sidx = self.dims[i].getLength() + sidx
             eidx = self.dims[i].getLength()-1 if k.stop is None else k.stop-1
             if eidx < 0:
                 eidx = self.dims[i].getLength() + eidx
             step = 1 if k.step is None else k.step
         elif isinstance(k, list):
             if not isinstance(k[0], datetime.datetime):
                 onlyrange = False
                 isrange = False
                 ranges.append(k)
             else:
                 sv = k[0]
                 sv = miutil.date2num(sv)
                 dim = self.dims[i]
                 sidx = dim.getValueIndex(sv)
                 if len(k) == 1:
                     eidx = sidx
                     step = 1
                 else:
                     ev = k[1]
                     ev = miutil.date2num(ev)
                     eidx = dim.getValueIndex(ev)
                     if len(k) == 2:
                         step = 1
                     else:
                         nv = k[2]
                         nv = miutil.date2num(k[0] + k[2]) - sv
                         step = int(nv / dim.getDeltaValue())
                     if sidx > eidx:
                         iidx = eidx
                         eidx = sidx
                         sidx = iidx
         elif isinstance(k, basestring):
             dim = self.dims[i]
             kvalues = k.split(':')
             sidx = dim.getValueIndex(float(kvalues[0]))
             if len(kvalues) == 1:
                 eidx = sidx
                 step = 1
             else:                    
                 eidx = dim.getValueIndex(float(kvalues[1]))
                 if len(kvalues) == 2:
                     step = 1
                 else:
                     step = int(float(kvalues[2]) / dim.getDeltaValue())
                 if sidx > eidx:
                     iidx = eidx
                     eidx = sidx
                     sidx = iidx
         else:                
             print k
             raise IndexError()
             
         if isrange:
             if sidx >= self.shape[i]:
                 raise IndexError()
                 
             if sidx != eidx:
                 iszerodim = False
             if step < 0:
                 step = abs(step)
                 flips.append(i)
             rr = Range(sidx, eidx, step)
             ranges.append(rr)
             #origin.append(sidx)
             n = eidx - sidx + 1
             #size.append(n)
             #stride.append(step)
             if n > 1:
                 dim = self.dims[i]
                 dims.append(dim.extract(sidx, eidx, step))
         else:
             if len(k) > 1:
                 dim = self.dims[i]
                 dims.append(dim.extract(k))
                 
     #r = ArrayMath.section(self.array.array, origin, size, stride)
     if onlyrange:
         r = ArrayMath.section(self.array.array, ranges)
     else:
         r = ArrayMath.take(self.array.array, ranges)
     if r.getSize() == 1:
         return r.getObject(0)
     else:
         for i in flips:
             r = r.flip(i)
         rr = Array.factory(r.getDataType(), r.getShape());
         MAMath.copy(rr, r);
         array = MIArray(rr)
         data = DimArray(array, dims, self.fill_value, self.proj)
         return data
Exemplo n.º 4
0
 def _getitem_loc(self, key):   
     if not isinstance(key, tuple): 
         key = (key, None)
   
     k = key[0]
     rkeys = key[0]
     if isinstance(k, slice):
         sidx = 0 if k.start is None else self._index.index(k.start)
         if sidx < 0:
             raise KeyError(key)
         eidx = self.shape[0] - 1 if k.stop is None else self._index.index(k.stop)
         if eidx < 0:
             raise KeyError(key)                   
         step = 1 if k.step is None else k.step
         rowkey = Range(sidx, eidx, step)
     else:
         if isinstance(k, (list, tuple, MIArray)):
             rowkey, rkeys = self._index.get_loc(k, outkeys=True)
         else:
             rowkey = self._index.index(k)
             if rowkey < 0:
                 raise KeyError(key)
                
     k = key[1]
     if k is None:
         colkey = Range(0, self.shape[1] - 1, 1)
     else:
         if isinstance(k, slice):
             sidx = 0 if k.start is None else self.columns.indexOfName(k.start)
             if sidx < 0:
                 raise KeyError(key)
             eidx = self.shape[1] - 1 if k.stop is None else self.columns.indexOfName(k.stop)
             if eidx < 0:
                 raise KeyError(key)                  
             step = 1 if k.step is None else k.step
             colkey = Range(sidx, eidx, step)        
         elif isinstance(k, list):
             colkey = self.columns.indexOfName(k)               
         elif isinstance(k, basestring):
             col = self.columns.indexOfName(k)
             if col < 0:
                 raise KeyError(key)
             colkey = [col]
         else:
             return None
             
     if isinstance(rowkey, (int, Range)):
         r = self._dataframe.select(rowkey, colkey)
     else:
         if isinstance(colkey, Range):
             ncol = colkey.length()
         else:
             ncol = len(colkey)
         if len(rowkey) == 1 and ncol == 1:
             return self._dataframe.getValue(rowkey[0], colkey[0])
         if not isinstance(rkeys, list):
             rkeys = [rkeys]
         r = self._dataframe.select(rkeys, rowkey, colkey)
     if r is None:
         return None
     if isinstance(r, MISeries):
         r = series.Series(series=r)
     else:
         r = DataFrame(dataframe=r)
     return r
Exemplo n.º 5
0
 def value(self, indices):
     #print type(indices)
     if not isinstance(indices, tuple):
         inds = []
         inds.append(indices)
         indices = inds
     
     if len(indices) != self.ndim:
         print 'indices must be ' + str(self.ndim) + ' dimensions!'
         return None
         
     #origin = []
     #size = []
     #stride = []
     dims = []
     ranges = []
     flips = []
     for i in range(0, self.ndim):  
         k = indices[i]
         if isinstance(indices[i], int):
             sidx = k
             eidx = k
             step = 1                
         elif isinstance(k, slice):
             sidx = 0 if k.start is None else k.start
             eidx = self.dims[i].getLength()-1 if k.stop is None else k.stop
             step = 1 if k.step is None else k.step
         elif isinstance(k, tuple) or isinstance(k, list):
             dim = self.dims[i]
             sidx = dim.getValueIndex(k[0])
             if len(k) == 1:
                 eidx = sidx
                 step = 1
             else:                    
                 eidx = dim.getValueIndex(k[1])
                 if len(k) == 2:
                     step = 1
                 else:
                     step = int(k[2] / dim.getDeltaValue)
         else:
             print k
             return None
             
         if step < 0:
             step = abs(step)
             flips.append(i)
         rr = Range(sidx, eidx, step)
         ranges.append(rr)
         #origin.append(sidx)
         n = eidx - sidx + 1
         #size.append(n)
         #stride.append(step)
         if n > 1:
             dim = self.dims[i]
             dims.append(dim.extract(sidx, eidx, step))
                 
     #r = ArrayMath.section(self.array.array, origin, size, stride)
     r = ArrayMath.section(self.array.array, ranges)
     for i in flips:
         r = r.flip(i)
     rr = Array.factory(r.getDataType(), r.getShape());
     MAMath.copy(rr, r);
     array = MIArray(rr)
     data = DimArray(array, dims, self.fill_value, self.proj)
     return data
Exemplo n.º 6
0
    def __getitem__(self, indices):
        #print type(indices)
        if not isinstance(indices, tuple):
            inds = []
            inds.append(indices)
            indices = inds
            
        allint = True
        aindex = self.array.getIndex()
        i = 0
        for ii in indices:
            if isinstance(ii, int):
                if ii < 0:
                    ii = self.shape[i] + ii
                aindex.setDim(i, ii)
            else:
                allint = False
                break;
            i += 1
        if allint:
            return self.array.getObject(aindex)
        
        if len(indices) != self.ndim:
            print 'indices must be ' + str(self.ndim) + ' dimensions!'
            raise IndexError()
            
        if not self.proj is None and not self.proj.isLonLat():
            xlim = None
            ylim = None
            xidx = -1
            yidx = -1
            for i in range(0, self.ndim):
                dim = self.dims[i]
                if dim.getDimType() == DimensionType.X:                    
                    k = indices[i]
                    #if isinstance(k, (tuple, list)):
                    if isinstance(k, basestring):
                        xlims = k.split(':')
                        xlim = [float(xlims[0]), float(xlims[1])]
                        xidx = i
                elif dim.getDimType() == DimensionType.Y:
                    k = indices[i]
                    #if isinstance(k, (tuple, list)):
                    if isinstance(k, basestring):
                        ylims = k.split(':')
                        ylim = [float(ylims[0]), float(ylims[1])]
                        yidx = i
            if not xlim is None and not ylim is None:                
                fromproj=KnownCoordinateSystems.geographic.world.WGS1984
                inpt = PointD(xlim[0], ylim[0])
                outpt1 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
                inpt = PointD(xlim[1], ylim[1])
                outpt2 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
                xlim = [outpt1.X, outpt2.X]
                ylim = [outpt1.Y, outpt2.Y]
                indices1 = []
                for i in range(0, self.ndim):
                    if i == xidx:
                        #indices1.append(xlim
                        indices1.append(str(xlim[0]) + ':' + str(xlim[1]))
                    elif i == yidx:
                        #indices1.append(ylim)
                        indices1.append(str(ylim[0]) + ':' + str(ylim[1]))
                    else:
                        indices1.append(indices[i])
                indices = indices1

        ndims = []
        ranges = []
        flips = []
        iszerodim = True
        onlyrange = True
        alllist = True
        isempty = False
        nshape = []
        for i in range(0, self.ndim):  
            isrange = True
            k = indices[i]
            if isinstance(k, int):
                if k < 0:
                    k = self.dims[i].getLength() + k
                sidx = k
                eidx = k
                step = 1       
                alllist = False
            elif isinstance(k, slice):
                if isinstance(k.start, basestring):
                    sv = float(k.start)
                    sidx = self.dims[i].getValueIndex(sv)
                elif isinstance(k.start, datetime.datetime):
                    sv = miutil.date2num(k.start)
                    sidx = self.dims[i].getValueIndex(sv)
                else:
                    sidx = 0 if k.start is None else k.start
                if sidx < 0:
                    sidx = self.dims[i].getLength() + sidx
                    
                if isinstance(k.stop, basestring):
                    ev = float(k.stop)
                    eidx = self.dims[i].getValueIndex(ev)
                elif isinstance(k.stop, datetime.datetime):
                    ev = miutil.date2num(k.stop)
                    eidx = self.dims[i].getValueIndex(ev)
                else:
                    eidx = self.dims[i].getLength() if k.stop is None else k.stop
                    if eidx < 0:
                        eidx = self.dims[i].getLength() + eidx
                    eidx -= 1
                    
                if isinstance(k.step, basestring):
                    nv = float(k.step) + self.dims[i].getDimValue()[0]
                    nidx = self.dims[i].getValueIndex(nv)
                    step = nidx - sidx
                elif isinstance(k.step, datetime.timedelta):
                    nv = miutil.date2num(k.start + k.step)
                    nidx = self.dims[i].getValueIndex(nv)
                    step = nidx - sidx
                else:
                    step = 1 if k.step is None else k.step
                alllist = False
            elif isinstance(k, list):
                onlyrange = False
                isrange = False
                if not isinstance(k[0], datetime.datetime):                    
                    ranges.append(k)
                else:
                    tlist = []
                    for tt in k:
                        sv = miutil.date2num(tt)
                        idx = self.dims[i].getValueIndex(sv)
                        tlist.append(idx)
                    ranges.append(tlist)
                    k = tlist
            elif isinstance(k, basestring):
                dim = self.dims[i]
                kvalues = k.split(':')
                sidx = dim.getValueIndex(float(kvalues[0]))
                if len(kvalues) == 1:
                    eidx = sidx
                    step = 1
                else:                    
                    eidx = dim.getValueIndex(float(kvalues[1]))
                    if len(kvalues) == 2:
                        step = 1
                    else:
                        step = int(float(kvalues[2]) / dim.getDeltaValue())
                    if sidx > eidx:
                        iidx = eidx
                        eidx = sidx
                        sidx = iidx
                alllist = False
            else:                
                print k
                raise IndexError()
                
            if isrange:
                if sidx >= self.shape[i]:
                    raise IndexError()
                    
                if sidx != eidx:
                    iszerodim = False
                if step < 0:
                    step = abs(step)
                    flips.append(i)
                    if eidx < sidx:
                        tempidx = sidx
                        sidx = eidx + 2
                        eidx = tempidx
                if eidx < sidx:
                    isempty = True
                else:
                    rr = Range(sidx, eidx, step)
                    ranges.append(rr)
                    n = eidx - sidx + 1
                    if n > 1:
                        dim = self.dims[i]
                        ndims.append(dim.extract(sidx, eidx, step))
                nshape.append(eidx - sidx + 1 if eidx - sidx >= 0 else 0)
            else:
                if len(k) > 1:
                    dim = self.dims[i]
                    ndims.append(dim.extract(k))
        
        if isempty:
            r = ArrayUtil.zeros(nshape, 'int')
            return MIArray(r)
        
        if onlyrange:
            r = ArrayMath.section(self.array, ranges)
        else:
            if alllist:
                r = ArrayMath.takeValues(self.array, ranges)
                return MIArray(r)
            else:
                r = ArrayMath.take(self.array, ranges)
        if r.getSize() == 1:
            return r.getObject(0)
        else:
            for i in flips:
                r = r.flip(i)
            rr = Array.factory(r.getDataType(), r.getShape());
            MAMath.copy(rr, r);
            array = MIArray(rr)
            data = DimArray(array, ndims, self.fill_value, self.proj)
            return data        
Exemplo n.º 7
0
    def _getitem_loc(self, key):
        if not isinstance(key, tuple):
            key = (key, None)

        k = key[0]
        rkeys = key[0]
        if isinstance(k, slice):
            sidx = 0 if k.start is None else self._index.index(k.start)
            if sidx < 0:
                raise KeyError(key)
            eidx = self.shape[0] - 1 if k.stop is None else self._index.index(
                k.stop)
            if eidx < 0:
                raise KeyError(key)
            step = 1 if k.step is None else k.step
            rowkey = Range(sidx, eidx, step)
        else:
            rowkey, rkeys = self._index.get_loc(k, outkeys=True)
            if len(rowkey) == 0:
                raise KeyError(key)
                #rowkey = self._index.index(k)
                #if rowkey < 0:
                #    raise KeyError(key)

        k = key[1]
        if k is None:
            colkey = Range(0, self.shape[1] - 1, 1)
        else:
            if isinstance(k, slice):
                sidx = 0 if k.start is None else self.columns.indexOfName(
                    k.start)
                if sidx < 0:
                    raise KeyError(key)
                eidx = self.shape[
                    1] - 1 if k.stop is None else self.columns.indexOfName(
                        k.stop)
                if eidx < 0:
                    raise KeyError(key)
                step = 1 if k.step is None else k.step
                colkey = Range(sidx, eidx, step)
            elif isinstance(k, list):
                colkey = self.columns.indexOfName(k)
            elif isinstance(k, basestring):
                col = self.columns.indexOfName(k)
                if col < 0:
                    raise KeyError(key)
                colkey = [col]
            else:
                return None

        if isinstance(rowkey, (int, Range)):
            r = self._dataframe.select(rowkey, colkey)
        else:
            if isinstance(colkey, Range):
                ncol = colkey.length()
            else:
                ncol = len(colkey)
            if len(rowkey) == 1 and ncol == 1:
                return self._dataframe.getValue(rowkey[0], colkey[0])
            if not isinstance(rkeys, list):
                rkeys = [rkeys]
            r = self._dataframe.select(rkeys, rowkey, colkey)
        if r is None:
            return None
        if isinstance(r, MISeries):
            r = series.Series(series=r)
        else:
            r = DataFrame(dataframe=r)
        return r
Exemplo n.º 8
0
    def __setitem__(self, key, value):
        if isinstance(value, datetime.datetime):
            value = miutil.jdatetime(value)
        if isinstance(value, (list, tuple)):
            if isinstance(value[0], datetime.datetime):
                value = miutil.jdatetime(value)
            value = minum.array(value)
        if isinstance(value, MIArray):
            value = value.array

        if isinstance(key, basestring):
            if isinstance(value, series.Series):
                value = value.values.array
            self._dataframe.setColumn(key, value)
            return

        hascolkey = True
        if isinstance(key, tuple):
            ridx = key[0]
            cidx = key[1]
            if isinstance(ridx, int) and isinstance(cidx, int):
                if ridx < 0:
                    ridx = self.shape[0] + ridx
                if cidx < 0:
                    cidx = self.shape[1] + cidx
                self._dataframe.setValue(ridx, cidx, value)
                return
            elif isinstance(ridx, int) and isinstance(cidx, basestring):
                if ridx < 0:
                    ridx = self.shape[0] + ridx
                self._dataframe.setValue(ridx, cidx, value)
                return
        else:
            key = (key, slice(None))
            hascolkey = False

        k = key[0]
        if isinstance(k, int):
            if k < 0:
                k = self.shape[0] + k
            rowkey = k
        elif isinstance(k, basestring):
            sidx = self._index.index(k)
            if sidx < 0:
                return None
            eidx = sidx
            step = 1
            rowkey = Range(sidx, eidx, step)
        elif isinstance(k, slice):
            if isinstance(k.start, basestring):
                sidx = self._index.index(k.start)
                if sidx < 0:
                    sidx = 0
            else:
                sidx = 0 if k.start is None else k.start
                if sidx < 0:
                    sidx = self.shape[0] + sidx
            if isinstance(k.stop, basestring):
                eidx = self._index.index(k.stop)
                if eidx < 0:
                    eidx = self.shape[0] + eidx
            else:
                eidx = self.shape[0] - 1 if k.stop is None else k.stop - 1
                if eidx < 0:
                    eidx = self.shape[0] + eidx
            step = 1 if k.step is None else k.step
            rowkey = Range(sidx, eidx, step)
        elif isinstance(k, list):
            if isinstance(k[0], int):
                rowkey = k
            else:
                tlist = []
                for tstr in k:
                    idx = self._index.index(tstr)
                    if idx >= 0:
                        tlist.append(idx)
                rowkey = tlist
        else:
            return

        if not hascolkey:
            colkey = Range(0, self.shape[1] - 1, 1)
        else:
            k = key[1]
            if isinstance(k, int):
                sidx = k
                if sidx < 0:
                    sidx = self.shape[1] + sidx
                eidx = sidx
                step = 1
                colkey = Range(sidx, eidx, step)
            elif isinstance(k, slice):
                sidx = 0 if k.start is None else k.start
                if sidx < 0:
                    sidx = self.shape[1] + sidx
                eidx = self.shape[1] - 1 if k.stop is None else k.stop - 1
                if eidx < 0:
                    eidx = self.shape[1] + eidx
                step = 1 if k.step is None else k.step
                colkey = Range(sidx, eidx, step)
            elif isinstance(k, list):
                if isinstance(k[0], int):
                    colkey = k
                else:
                    colkey = self.columns.indexOfName(k)
            elif isinstance(k, basestring):
                col = self.columns.indexOf(k)
                colkey = Range(col, col + 1, 1)
            else:
                return

        self._dataframe.setValues(rowkey, colkey, value)
Exemplo n.º 9
0
 def __getitem__(self, indices):
     if indices is None:
         rr = self.dataset.read(self.name)
         if rr.getDataType().isNumeric():
             ArrayMath.missingToNaN(rr, self.fill_value)
             array = MIArray(rr)
             data = DimArray(array, self.dims, self.fill_value, self.dataset.proj)
             return data
         else:
             return rr
             
     if isinstance(indices, str):    #metadata
         rr = self.dataset.read(self.name)
         m = rr.findMember(indices)
         data = rr.getArray(0, m)
         return MIArray(data)
     
     if not isinstance(indices, tuple):
         inds = []
         inds.append(indices)
         indices = inds
     
     if len(indices) != self.ndim:
         print 'indices must be ' + str(self.ndim) + ' dimensions!'
         return None
         
     if not self.proj is None and not self.proj.isLonLat():
         xlim = None
         ylim = None
         xidx = -1
         yidx = -1
         for i in range(0, self.ndim):
             dim = self.dims[i]
             if dim.getDimType() == DimensionType.X:                    
                 k = indices[i]
                 if isinstance(k, basestring):
                     xlims = k.split(':')
                     if len(xlims) == 1:
                         xlim = [float(xlims[0])]
                     else:
                         xlim = [float(xlims[0]), float(xlims[1])]
                     xidx = i
             elif dim.getDimType() == DimensionType.Y:
                 k = indices[i]
                 if isinstance(k, basestring):
                     ylims = k.split(':')
                     if len(ylims) == 1:
                         ylim = [float(ylims[0])]
                     else:
                         ylim = [float(ylims[0]), float(ylims[1])]
                     yidx = i
         if not xlim is None and not ylim is None:                
             fromproj=KnownCoordinateSystems.geographic.world.WGS1984
             inpt = PointD(xlim[0], ylim[0])
             outpt1 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
             if len(xlim) == 1:
                 xlim = [outpt1.X]
                 ylim = [outpt1.Y]
             else:
                 inpt = PointD(xlim[1], ylim[1])
                 outpt2 = Reproject.reprojectPoint(inpt, fromproj, self.proj)
                 xlim = [outpt1.X, outpt2.X]
                 ylim = [outpt1.Y, outpt2.Y]
             indices1 = []
             for i in range(0, self.ndim):
                 if i == xidx:
                     if len(xlim) == 1:
                         indices1.append(str(xlim[0]))
                     else:
                         indices1.append(str(xlim[0]) + ':' + str(xlim[1]))
                 elif i == yidx:
                     if len(ylim) == 1:
                         indices1.append(str(ylim[0]))
                     else:
                         indices1.append(str(ylim[0]) + ':' + str(ylim[1]))
                 else:
                     indices1.append(indices[i])
             indices = indices1
     
     origin = []
     size = []
     stride = []
     ranges = []
     dims = []
     flips = []
     onlyrange = True
     for i in range(0, self.ndim):  
         isrange = True
         dimlen = self.dimlen(i)
         k = indices[i]
         if isinstance(k, int):
             if k < 0:
                 k = self.dims[i].getLength() + k
             sidx = k
             eidx = k
             step = 1
         elif isinstance(k, slice):
             if isinstance(k.start, basestring):
                 sv = float(k.start)
                 sidx = self.dims[i].getValueIndex(sv)
             elif isinstance(k.start, datetime.datetime):
                 sv = miutil.date2num(k.start)
                 sidx = self.dims[i].getValueIndex(sv)
             else:
                 sidx = 0 if k.start is None else k.start
             if sidx < 0:
                 sidx = self.dimlen(i) + sidx
                 
             if isinstance(k.stop, basestring):
                 ev = float(k.stop)
                 eidx = self.dims[i].getValueIndex(ev)
             elif isinstance(k.stop, datetime.datetime):
                 ev = miutil.date2num(k.stop)
                 eidx = self.dims[i].getValueIndex(ev)
             else:
                 eidx = self.dimlen(i) if k.stop is None else k.stop
                 if eidx < 0:
                     eidx = self.dimlen(i) + eidx
                 eidx -= 1
                 
             if isinstance(k.step, basestring):
                 nv = float(k.step) + self.dims[i].getDimValue()[0]
                 nidx = self.dims[i].getValueIndex(nv)
                 step = nidx - sidx
             elif isinstance(k.step, datetime.timedelta):
                 nv = miutil.date2num(k.start + k.step)
                 nidx = self.dims[i].getValueIndex(nv)
                 step = nidx - sidx
             else:
                 step = 1 if k.step is None else k.step
             if sidx > eidx:
                 iidx = eidx
                 eidx = sidx
                 sidx = iidx
         elif isinstance(k, list):
             onlyrange = False
             isrange = False
             if not isinstance(k[0], datetime.datetime):
                 ranges.append(k)
             else:
                 tlist = []
                 for tt in k:
                     sv = miutil.date2num(tt)
                     idx = self.dims[i].getValueIndex(sv)
                     tlist.append(idx)
                 ranges.append(tlist)
                 k = tlist
         elif isinstance(k, basestring):
             dim = self.variable.getDimension(i)
             kvalues = k.split(':')
             sv = float(kvalues[0])
             sidx = dim.getValueIndex(sv)
             if len(kvalues) == 1:
                 eidx = sidx
                 step = 1
             else:                    
                 ev = float(kvalues[1])
                 eidx = dim.getValueIndex(ev)
                 if len(kvalues) == 2:
                     step = 1
                 else:
                     step = int(float(kvalues[2]) / dim.getDeltaValue())
                 if sidx > eidx:
                     iidx = eidx
                     eidx = sidx
                     sidx = iidx
         else:
             print k
             return None
         if isrange:
             if eidx >= dimlen:
                 print 'Index out of range!'
                 return None
             origin.append(sidx)
             n = eidx - sidx + 1
             size.append(n)                   
             if n > 1:
                 dim = self.variable.getDimension(i)
                 if dim.isReverse():
                     step = -step      
                 dim = dim.extract(sidx, eidx, step)
                 dim.setReverse(False)
                 dims.append(dim)
             stride.append(step) 
             if step < 0:
                 step = abs(step)
                 flips.append(i)
             rr = Range(sidx, eidx, step)
             ranges.append(rr)
         else:
             if len(k) > 1:
                 dim = self.variable.getDimension(i)
                 dim = dim.extract(k)
                 dim.setReverse(False)
                 dims.append(dim)
     #rr = self.dataset.read(self.name, origin, size, stride).reduce()
     if onlyrange:
         rr = self.dataset.dataset.read(self.name, ranges)
     else:
         rr = self.dataset.dataset.take(self.name, ranges)
     if rr.getSize() == 1:
         return rr.getObject(0)
     else:
         for i in flips:
             rr = rr.flip(i)
         rr = rr.reduce()
         ArrayMath.missingToNaN(rr, self.fill_value)
         rrr = Array.factory(rr.getDataType(), rr.getShape());
         MAMath.copy(rrr, rr);
         array = MIArray(rrr)
         data = DimArray(array, dims, self.fill_value, self.dataset.proj)
         return data