コード例 #1
0
ファイル: gsHost.py プロジェクト: mauzey1/cdms
    def listvariable(self, gstype=None):
        """
        Get all variables

        Parameters
        ----------
        type Grid, Static, Time Dependent or None

        Returns
        -------
        list of all variables, including static and time dependent, Default = None
        """
        isNone = gstype is None
        isStr = isinstance(gstype, string_types)

        if isNone:
            variables = list(self.statVars.keys()) + list(self.timeVars.keys())
            return variables

        elif isStr:
            if gstype.upper() == "STATIC":
                return list(self.statVars.keys())
            if gstype[0:4].upper() == "TIME":
                return list(self.timeVars.keys())
            return None

        # Raise error
        else:
            text = 'type must be "Static", "Time", None or empty'
            raise CDMSError(text)
コード例 #2
0
ファイル: MV2.py プロジェクト: Xunius/cdms
def resize(a, new_shape, axes=None, attributes=None, id=None, grid=None):
    """resize(a, new_shape)

    Resize

    Returns
    -------
    a new array with the specified shape.

    The original array's total size can be any size.
    """
    ignore, attributes, id, ignore = _extractMetadata(a, axes, attributes, id)
    if axes is not None:
        axesshape = [len(item) for item in axes]
        if axesshape != list(new_shape):
            raise CDMSError('axes must be shaped %s' % repr(new_shape))
    ta = _makeMaskedArg(a)
    maresult = numpy.ma.resize(ta, new_shape)
    F = getattr(a, "fill_value", 1.e20)
    return TransientVariable(maresult,
                             axes=axes,
                             attributes=attributes,
                             id=id,
                             grid=grid,
                             fill_value=F)
コード例 #3
0
def createTransientGrid(gFName, coordinates):
    """
    Return the coordinate data associated with variable.
    @param gName The grid_filename
    @param coordinates The coordinates attribute from the variable to be created
    @return grid a cdms2.hgrid.AbstractCurveGrid object
    """
    import re

    fh = cdms2.open(gFName)
    gridid = None
    if libcf.CF_GRIDNAME in list(fh.attributes.keys()):
        gridid = getattr(fh, libcf.CF_GRIDNAME)
    xn, yn = coordinates.split()

    x = fh(xn)
    y = fh(yn)

    # Get the dimensions
    xdim = x.shape
    ydim = y.shape

    if xdim != ydim:
        raise CDMSError("Dimension of coordinates grids don't match")

    ni = xdim[1]
    nj = xdim[0]

    lonstr = 'lon'
    latstr = 'lat'

    if re.search(lonstr, x.standard_name):
        lon = x
    if re.search(lonstr, y.standard_name):
        lon = y
    if re.search(latstr, x.standard_name):
        lat = x
    if re.search(latstr, y.standard_name):
        lat = y

    # Define the axes, verifying the lon and lat grids
    iaxis = TransientVirtualAxis("i", ni)
    jaxis = TransientVirtualAxis("j", nj)

    lataxis = TransientAxis2D(lat,
                              axes=(iaxis, jaxis),
                              attributes={'units': lat.units},
                              id=lat.standard_name)
    lonaxis = TransientAxis2D(lon,
                              axes=(iaxis, jaxis),
                              attributes={'units': lon.units},
                              id=lon.standard_name)

    # Define the combined grid
    grid = TransientCurveGrid(lataxis, lonaxis, id=gridid)
    return grid
コード例 #4
0
    def createSeamGrid(self, x, y, attrs):
        """
        Return the coordinate data associated with variable.

        Parameters
        ----------

             x longitude coordinate

             y latitude coordinate

        Returns
        -------

             attrs Attributes for eash plus the gridid
        """
        LONSTR = 'lon'
        LATSTR = 'lat'

        # Get the dimensions
        xdim = x.shape
        ydim = y.shape

        if xdim != ydim:
            raise CDMSError("Dimension of coordinates grids don't match")

        nj = xdim[0]
        ni = xdim[1]

        # Define the axes, verifying the lon and lat grids
        jaxis = TransientVirtualAxis("j", nj)
        iaxis = TransientVirtualAxis("i", ni)

        if search(LONSTR, attrs['lon']['standard_name']):
            lon = x
        if search(LONSTR, attrs['lat']['standard_name']):
            lon = y
        if search(LATSTR, attrs['lon']['standard_name']):
            lat = x
        if search(LATSTR, attrs['lat']['standard_name']):
            lat = y

        lataxis = TransientAxis2D(lat,
                                  axes=(jaxis, iaxis),
                                  attributes=attrs['lat'],
                                  id=attrs['lat']['standard_name'])
        lonaxis = TransientAxis2D(lon,
                                  axes=(jaxis, iaxis),
                                  attributes=attrs['lon'],
                                  id=attrs['lon']['standard_name'])

        # Define the combined grid
        grid = TransientCurveGrid(lataxis, lonaxis, id=attrs['gridid'])
        return grid
コード例 #5
0
def reshape(a, newshape, axes=None, attributes=None, id=None, grid=None):
    ignore, attributes, id, ignore = _extractMetadata(a, axes, attributes, id)
    if axes is not None:
        axesshape = [len(item) for item in axes]
        if axesshape != list(newshape):
            raise CDMSError('axes must be shaped %s' % repr(newshape))
    ta = _makeMaskedArg(a)
    maresult = numpy.ma.reshape(ta, newshape)
    F = getattr(a, "fill_value", 1.e20)
    return TransientVariable(maresult, axes=axes, attributes=attributes,
                             id=id, grid=grid, no_update_from=True, fill_value=F)
コード例 #6
0
ファイル: gsMosaic.py プロジェクト: zshaheen/cdms
    def __init__(self, uri, mode='r'):
        """
        Constructor
        @param uri Filename with path
        @param mode read/write. Currently only read is supported
        """

        self.id = uri
        self.mode = mode
        self.uri = uri
        self._status = 'Open'

        self.mosaicId_ct = c_int(-1)
        self.lib = None
        for sosuffix in '.so', '.dylib', '.dll', '.DLL', '.a':
            self.lib = CDLL(LIBCFDIR + sosuffix)
            if self.lib:
                break

        libcfdll = self.lib

        self.file_type = ""
        self.contact_map = {}
        self.tile_contacts = {}
        self.tile_contacts_compl = {}
        self.coordinate_names = []
        self.tile_names = []

        status = libcfdll.nccf_def_mosaic_from_file(uri, "",
                                                    byref(self.mosaicId_ct))

        if status != 0:
            raise CDMSError(
                "ERROR: %s is not a valid mosaic file (status = %d)" %
                (uri, status))

        # Get some sizes
        nGrids = c_int(-1)
        ndims = c_int(-1)
        ncontacts = c_int(-1)
        libcfdll.nccf_inq_mosaic_ndims(self.mosaicId_ct, byref(ndims))
        libcfdll.nccf_inq_mosaic_ngrids(self.mosaicId_ct, byref(nGrids))
        libcfdll.nccf_inq_mosaic_ncontacts(self.mosaicId_ct, byref(ncontacts))

        # Build the character arrays
        separator_ct = libCF.CF_TILE_SEPARATOR
        contact_map_ct = c_char_p(" " * (libCF.NC_MAX_NAME + 1))
        tile_contact_ct = c_char_p(" " * (libCF.NC_MAX_NAME + 1))
        tile_name_ct = c_char_p(" " * (libCF.NC_MAX_NAME + 1))
        coord_ct = (c_char_p * ndims.value)()

        for iDim in range(ndims.value):
            coord_ct[iDim] = " " * (libCF.NC_MAX_NAME + 1)

        # Get the grid names
        for igrid in range(nGrids.value):
            libcfdll.nccf_inq_mosaic_gridname(self.mosaicId_ct, igrid,
                                              tile_name_ct)
            tname = str(tile_name_ct)
            self.tile_names.append(tname)

        # Get the coordinate names for the grids
        libcfdll.nccf_inq_mosaic_coordnames(self.mosaicId_ct, coord_ct)

        for iCrd in range(len(coord_ct)):
            self.coordinate_names.append(coord_ct[iCrd])

        # Get the contact map information
        for iContact in range(ncontacts.value):
            status = libcfdll.nccf_inq_mosaic_contactmap(
                self.mosaicId_ct, iContact, contact_map_ct)
            status = libcfdll.nccf_inq_mosaic_tilecontact(
                self.mosaicId_ct, iContact, tile_contact_ct)

            tN1, tN2 = tile_contact_ct.value.split(separator_ct)
            tileName1, tileName2 = tN1.strip(), tN2.strip()
            s1, s2 = contact_map_ct.value.split(separator_ct)

            # slice objects
            slab1 = getSlab(s1.strip())
            slab2 = getSlab(s2.strip())

            # Create the tile contact dictionary. Non symmetric.
            if tileName1 not in self.tile_contacts:
                self.tile_contacts[tileName1] = {}
            # The complement to tile_contacts
            if tileName2 not in self.tile_contacts_compl:
                self.tile_contacts_compl[tileName2] = {}

            # Attach the contact map (slab) between the tiles
            self.tile_contacts[tileName1][tileName2] = (slab1, slab2)
            self.tile_contacts_compl[tileName2][tileName1] = (slab2, slab1)
コード例 #7
0
ファイル: gsHost.py プロジェクト: mauzey1/cdms
    def __init__(self, hostfile, mode='r'):
        """
        Constructor

        Parameters
        ----------

        hostfile : path to the host
        mode : read only at the moment
        """

        self.__initialize()
        self.uri = hostfile
        self.mode = mode

        # Data dir based on location of hostfile
        if mode != 'r':
            raise CDMSError('Only read mode is supported for host file')

        for sosuffix in '.so', '.dylib', '.dll', '.a':
            self.libcfdll = CDLL(LIBCF + sosuffix)
            if self.libcfdll:
                break

        if self.libcfdll is None:
            raise CDMSError('libcf not installed or incorrect path\n  ')

        libcfdll = self.libcfdll

        status = libcfdll.nccf_def_host_from_file(hostfile,
                                                  byref(self.hostId_ct))
        if status != 0:
            raise CDMSError("ERROR: not a valid host file %s (status=%d)" %
                            (hostfile, status))

        # Attach global attrs
        libcfdll.nccf_def_global_from_file(hostfile, byref(self.globalId_ct))

        # get the global attributes from the file
        natts = c_int(-1)
        attName_ct = c_char_p(" " * (libCFConfig.NC_MAX_NAME + 1))
        attValu_ct = c_char_p(" " * (libCFConfig.NC_MAX_NAME + 1))
        self.libcfdll.nccf_inq_global_natts(self.globalId_ct, byref(natts))
        for i in range(natts.value):
            self.libcfdll.nccf_inq_global_attval(self.globalId_ct, i,
                                                 attName_ct, attValu_ct)
            if attName_ct.value not in self.attributes:
                self.attributes[attName_ct.value] = attValu_ct.value

        self.id = hostfile

        i_ct = c_int()
        status = libcfdll.nccf_inq_host_ngrids(self.hostId_ct, byref(i_ct))
        self.nGrids = i_ct.value
        status = libcfdll.nccf_inq_host_nstatdatafiles(self.hostId_ct,
                                                       byref(i_ct))
        self.nStatDataFiles = i_ct.value
        status = libcfdll.nccf_inq_host_ntimedatafiles(self.hostId_ct,
                                                       byref(i_ct))

        self.nTimeDataFiles = i_ct.value
        status = libcfdll.nccf_inq_host_ntimeslices(self.hostId_ct,
                                                    byref(i_ct))
        self.nTimeSliceFiles = i_ct.value

        fName_ct = c_char_p(" " * (libCFConfig.NC_MAX_NAME + 1))
        gName_ct = c_char_p(" " * (libCFConfig.NC_MAX_NAME + 1))

        self.dimensions = {
            "nGrids": self.nGrids,
            "nStatDataFiles": self.nStatDataFiles,
            "nTimeDataFiles": self.nTimeDataFiles,
            "nTimeSliceFiles": self.nTimeSliceFiles
        }

        # Mosaic filename (use getMosaic to return the connectivity)
        mosaicFilename = c_char_p(" " * (libCFConfig.NC_MAX_NAME + 1))
        status = libcfdll.nccf_inq_host_mosaicfilename(self.hostId_ct,
                                                       mosaicFilename)
        self.mosaicFilename = mosaicFilename.value

        # Filenames
        timeFilenames = []
        statFilenames = []

        coordinates = []

        # static data
        for vfindx in range(self.nStatDataFiles):
            for gfindx in range(self.nGrids):
                status = libcfdll.nccf_inq_host_statfilename(
                    self.hostId_ct, vfindx, gfindx, fName_ct)
                statFilenames.append(fName_ct.value)
                f = cdms2.open(fName_ct.value, 'r')
                varNames = f.listvariable()

                for vn in varNames:
                    # Add coordinate names a local list of coordinates
                    if 'coordinates' in dir(f[vn]):
                        for coord in f[vn].coordinates.split():
                            if coord not in coordinates:
                                coordinates.append(coord)
                    if vn not in self.statVars:
                        # allocate
                        self.statVars[vn] = ["" for ig in range(self.nGrids)]

                    # set file name
                    self.statVars[vn][gfindx] = fName_ct.value
                f.close()

        # time dependent data
        for vfindx in range(self.nTimeDataFiles):
            for tfindx in range(self.nTimeSliceFiles):
                for gfindx in range(self.nGrids):
                    status = \
                        libcfdll.nccf_inq_host_timefilename(self.hostId_ct,
                                                            vfindx,
                                                            tfindx,
                                                            gfindx,
                                                            fName_ct)
                    timeFilenames.append(fName_ct.value)
                    f = cdms2.open(fName_ct.value, 'r')
                    varNames = f.listvariable()
                    for vn in varNames:
                        # Add coordinate names a local list of coordinates
                        if 'coordinates' in dir(f[vn]):
                            for coord in f[vn].coordinates.split():
                                if coord not in coordinates:
                                    coordinates.append(coord)
                        if vn not in self.timeVars:
                            # allocate
                            self.timeVars[vn] = \
                                [["" for it in range(self.nTimeSliceFiles)]
                                 for ig in range(self.nGrids)]
                        # set file name
                        self.timeVars[vn][gfindx][tfindx] = fName_ct.value
                    f.close()

        # Grid names and data. Must come after time and static file dictionaries
        # because they define the coordinates.
        for gfindx in range(self.nGrids):
            status = libcfdll.nccf_inq_host_gridfilename(
                self.hostId_ct, gfindx, fName_ct)
            status = libcfdll.nccf_inq_host_gridname(self.hostId_ct, gfindx,
                                                     gName_ct)

            varNames = cdms2.open(fName_ct.value, 'r').listvariable()
            for vn in varNames:
                if vn in coordinates:
                    if vn not in list(self.gridVars.keys()):
                        self.gridVars[vn] = []
                        self.gridName[vn] = []

                    self.gridVars[vn].append(fName_ct.value)
                    self.gridName[vn].append(gName_ct.value)

        # Populate the variables dictionary, avoid the grids
        self.variables = {}
        for item in list(self.statVars.keys()):
            self.variables[item] = StaticFileVariable(self, item)
        for item in list(self.timeVars.keys()):
            self.variables[item] = TimeFileVariable(self, item)