Ejemplo n.º 1
0
 def __init__(self, axes=None, name=None):
     '''
     Describe a Ferret grid from the given axes
         axes (sequence of FerAxis): axes of this grid
         name (string): Ferret name for the grid or the variable using this grid
     '''
     if name:
         if not isinstance(name, str):
             raise TypeError('grid name is not a string')
         self._gridname = name.strip()
     else:
         self._gridname = ''
     # axes of this grid
     self._axes = [None] * pyferret.MAX_FERRET_NDIM
     if axes:
         try:
             for k in range(len(axes)):
                 ax = axes[k]
                 if ax:
                     if not isinstance(ax, pyferret.FerAxis):
                         raise ValueError('axes[%d] is not a FerAxis' % k)
                     self._axes[k] = ax.copy()
         except TypeError:
             raise TypeError('axes is not a sequence type')
         except IndexError:
             raise ValueError('more than %d axes specified' %
                              pyferret.MAX_FERRET_NDIM)
     for k in range(len(self._axes)):
         if self._axes[k] is None:
             self._axes[k] = pyferret.FerAxis(
                 axtype=pyferret.AXISTYPE_NORMAL)
Ejemplo n.º 2
0
 def load(self):
     '''
     Retrieves the grid and data for this Ferret variable from Ferret.
     This method is automatically called before returning the grid or data 
     for the first time for this variable.  This can be called to update
     the grid or data in this FerVar after any change in the definition 
     of the variable.  Alternatively, cleardata can be called to clear any
     stored grid and data, delaying the update from Ferret until the grid
     or data is requested.
     Raises a ValueEror if problems occur.
     '''
     fername = self.fername()
     datadict = pyferret.getdata(fername, False)
     feraxes = []
     for (axistype, axcoords, axunit,
          axname) in zip(datadict["axis_types"], datadict["axis_coords"],
                         datadict["axis_units"], datadict["axis_names"]):
         feraxes.append(
             pyferret.FerAxis(coords=axcoords,
                              axtype=axistype,
                              unit=axunit,
                              name=axname))
     self._datagrid = pyferret.FerGrid(axes=feraxes, name=fername)
     self._dataarray = datadict["data"]
     self._dataunit = datadict["data_unit"]
     self._missingvalue = datadict["missing_value"]
Ejemplo n.º 3
0
    def copy(self, name=None, ax=None, newax=None):
        '''
        Returns a copy of this FerGrid object, possibly with one axis replaced.
        The FerGrid object returned does not share any mutable values (namely, 
        the axes) with this FerGrid object.

        name (string): new name for the copied grid.
            If name is given, this will be the name of the new grid.
            If name is not given, then
                if ax, and newax are not given, the name of the grid is also copied;
                otherwise, the name of the new grid is not assigned.

        ax (int): index of the axis to modify; one of 
                pyferret.X_AXIS (0)
                pyferret.Y_AXIS (1)
                pyferret.Z_AXIS (2)
                pyferret.T_AXIS (3)
                pyferret.E_AXIS (4)
                pyferret.F_AXIS (5)
            If ax is not given but newax is given, an attempt is made to replace
            an axis of the same type (longitude, latitude, level, time, custom, abstract).

        newax (FerAxis): new axis to use in the copied grid.
            If newax is not given but ax is given, the indicated axis will be replaced
            by an axis normal to the data (pyferret.AXISTYPE_NORMAL).
        '''
        # figure out the new grid name
        if name:
            newgridname = name
        elif (newax is not None) or (ax is not None):
            newgridname = None
        else:
            newgridname = self._gridname
        # check the index of the axis to replace: 0 - 6, or None
        if ax is not None:
            if not ax in _VALID_AXIS_NUMS:
                raise ValueError('ax (%s) is not valid' % str(ax))
        newaxidx = ax
        # check the replacement axis
        if newax is not None:
            if not isinstance(newax, pyferret.FerAxis):
                raise ValueError('newax is not valid (not a FerAxis)')
            if (newaxidx is None) and (newax.getaxtype() !=
                                       pyferret.AXISTYPE_NORMAL):
                for k in range(len(self._axes)):
                    if self._axes[k].getaxtype() == newax.getaxtype():
                        newaxidx = k
                        break
            if newaxidx is None:
                raise ValueError(
                    'Unable to determine new axis index from axis type')
        elif newaxidx is not None:
            newax = pyferret.FerAxis(axtype=pyferret.AXISTYPE_NORMAL)
        # Create and return the new grid
        newaxes = self._axes[:]
        if newax:
            newaxes[newaxidx] = newax
        newgrid = FerGrid(name=newgridname, axes=newaxes)
        return newgrid