Ejemplo n.º 1
0
    def createTransientVariableFromList(self, tvList):
        """
        Aggregate a sliced/subseted list of transient variables.
        @param tvlist List of subset/sliced transient variables.
        @return aggregated transient variable
        """

        outvar = tvList[0]
        if len(tvList) > 1:
            for varIndex in range(1, len(tvList)):
                new = MV2concatenate((outvar, tvList[varIndex]))
                outvar = new

        return outvar
Ejemplo n.º 2
0
    def __init__(self, hostObj, varName, **slicekwargs):
        """
        Constructor
        @param hostObj host object
        @param varName variable name
        @param slicekwargs eg lon=(-180,180), lat=(-90,90), time=5
                           cf Packages/cdms2/Lib/cudsinterface.py for
                           a list of keywords
        """

#        TimeVariable(self, hostObj, varName)
        self.id = varName
        self.vars = []

        gridFilenames = hostObj.getGridFilenames()

        kwargs = {}
        for k in list(slicekwargs.keys()):
            kwargs[k.lower()] = slicekwargs[k]

        # time dependent variable. Create a list of list. One list for each
        # grid populated by a list for each time file.
        if ('time' in list(kwargs.keys()) and len(slicekwargs) <= 1) or \
                len(slicekwargs) == 0:
            for gridIndex in range(hostObj.nGrids):

                gFName = gridFilenames[gridIndex]

                for timeFileIndex in range(hostObj.nTimeDataFiles):

                    fName = hostObj.timeDepVars[varName][gridIndex][timeFileIndex]
                    fh = cdms2.open(fName, hostObj=hostObj)

                    # TransientVariable
                    var = fh(varName, **slicekwargs)

                    # Attach the grid to the variable
                    grid = cdms2.gsStaticVariable.createTransientGrid(gFName,
                                                                      var.attributes['coordinates'])
                    axis0 = var.getAxis(0)
                    gridaxes = grid.getAxisList()
                    axes = [axis0] + list(gridaxes)
                    atts = dict(var.attributes)
                    atts.update(fh.attributes)

                    # Create cdms2 transient variable
                    if timeFileIndex == 0:
                        new = cdms2.createVariable(var,
                                                   axes=axes,
                                                   grid=grid,
                                                   attributes=atts,
                                                   id=var.standard_name)
                    else:
                        tmp = MV2concatenate((new, var))
                        axis0 = tmp.getAxis(0)
                        gridaxes = grid.getAxisList()
                        axes = [axis0, gridaxes[0], gridaxes[1]]
#                        new.append(tmp)
                        new = cdms2.createVariable(tmp,
                                                   axes=axes,
                                                   grid=grid,
                                                   attributes=atts,
                                                   id=var.standard_name)
                    fh.close()

                # Add the variable to the index
                self.vars.append(new)

        self._repr_string = "TimeTransientVariable"
Ejemplo n.º 3
0
    def createTransientVariableFromIndices(self, fileIndices, timeIndices):
        """
        Aggregate a time file variable. Start and End Indices use slice notation.
        @param fileIndices the file indices to aggregate across
        @param timeIndices which time steps with in each file
        @return aggregated time dep. variable. Has shape of full grid.
                Subset the grid after exiting.
        """
        from numpy import reshape
        firsttime = True
        nTSF = self.nTimeStepsPerFile
        if not isinstance(fileIndices, int):
            for files, times in zip(fileIndices, timeIndices):
                for indx, file in enumerate(files):
                    # Should make these slices.
                    cvar = self.fvs[file][times[indx]]

                    grid = self.fvs[file].getGrid()
                    atts = cvar.attributes

                    # Insert the new time axis.
                    axisTime = self.fvs[file].getTime()
                    timeAxis = TransientAxis([file * nTSF + times[indx]],
                                             attributes=axisTime.attributes,
                                             id=axisTime.id)
                    axes = self.buildAxes(
                        timeAxis, self.fvs[file].getAxisList())

                    # shape --> tm1.shape = (1, :, :)
                    tm1 = reshape(cvar, tuple([1] + list(cvar.shape)))

                    # Attach needed items
                    var = cdms2.createVariable(tm1,
                                               axes=axes,
                                               grid=grid,
                                               attributes=atts,
                                               id=cvar.standard_name)

                    # Create cdms2 transient variable
                    if firsttime:
                        new = var
                        firsttime = False
                    else:
                        # insert the new time axis.
                        taA = new.getTime()
                        newTime = axisConcatenate((taA, timeAxis),
                                                  attributes=axisTime.attributes,
                                                  id=axisTime.id)
                        axes = self.buildAxes(
                            newTime, self.fvs[file].getAxisList())

                        tmp = MV2concatenate((new, var))
                        new = cdms2.createVariable(tmp,
                                                   axes=axes,
                                                   grid=grid,
                                                   attributes=atts,
                                                   id=cvar.standard_name)

        else:
            new = self.fvs[fileIndices][timeIndices]

        return new