Esempio n. 1
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 type(fileIndices) is not 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
Esempio n. 2
0
File: MV2.py Progetto: Xunius/cdms
def concatenate(arrays, axis=0, axisid=None, axisattributes=None):
    """Concatenate the arrays along the given axis. Give the extended axis the id and
    attributes provided - by default, those of the first array."""

    tarrays = [_makeMaskedArg(a) for a in arrays]
    maresult = numpy.ma.concatenate(arrays, axis=axis)
    if len(arrays) > 1:
        varattributes = None
        varid = None
        axes = commonDomain(tarrays[0], tarrays[1], omit=axis)
        grid = commonGrid(tarrays[0], tarrays[1], axes)
        for i in range(len(arrays) - 2):
            if axes is None:
                break
            axes = commonAxes(tarrays[i + 2], axes, omit=axis)
            grid = commonGrid1(tarrays[i + 2], grid, axes)
    else:
        axes = tarrays[0].getAxisList()
        varattributes = tarrays[0].attributes
        varid = tarrays[0].id
        if (isinstance(tarrays[0], TransientVariable)):
            grid = tarrays[0].getGrid()
        else:
            grid = None
    if axes is not None:
        if axisid is None:
            axisid = tarrays[0].getAxis(axis).id
        allunitsequal = True
        try:
            allunits = tarrays[0].getAxis(axis).units
        except BaseException:
            allunits = None
        for t in tarrays[1:]:
            try:
                tunits = t.getAxis(axis).units
            except BaseException:
                tunits = None
            if tunits != allunits:
                allunitsequal = False
        if allunitsequal:
            if axisattributes is None:
                axisattributes = tarrays[0].getAxis(axis).attributes
            axes[axis] = axisConcatenate([t.getAxis(axis) for t in tarrays],
                                         axisid, axisattributes)

    # If the grid doesn't match the axislist (e.g., catenation was on
    # latitude) then omit it.
    if grid is not None:
        for item in grid.getAxisList():
            if item not in axes:
                grid = None
    F = getattr(arrays[0], "fill_value", 1.e20)
    return TransientVariable(maresult,
                             axes=axes,
                             attributes=varattributes,
                             id=varid,
                             grid=grid,
                             fill_value=F)
Esempio 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