Esempio n. 1
0
    def teestTimes2(self):

        a=MV2.masked_array(MV2.array([0,0,0,0,0,0,0,0,0,0,0,0]),[0,1,1,1,1,1,1,1,1,1,1,0])
        bounds=numpy.ma.array(
            [[0,31],
             [31,59],
             [59,90],
             [90,120],
             [120,151],
             [151,181],
             [181,212],
             [212,243],
             [243,273],
             [273,304],
             [304,334],
             [334,365]]
            )
        ax=a.getAxis(0)
        ax.setBounds(bounds)
        #print cdutil.times.centroid(a,[-10,30]) 
        print(('Centroid Normal:',cdutil.times.centroid(a,[0,365]))) 
        print(('Centroid Cyclic:',cdutil.times.cyclicalcentroid(a,[0,365]))) 


        djf=cdutil.DJF(self.tas_mo)
        djf=cdutil.DJF(self.tas_mo,criteriaarg=[.8,0.0001])
        djf=cdutil.ANNUALCYCLE.climatology(self.tas_mo)
        djf=cdutil.YEAR.departures(self.tas_mo)
Esempio n. 2
0
def corr_proba(r, ndata, ndataset=2, dof=False):
    """Probability of rejecting correlations

    - **r**: Correlation coefficient
    - **ndata**: Number of records use for correlations
    - **ndataset**, optional:  Number of datasets (1 for autocorrelations, else 2) [default: 2]

    .. todo::

        This must be rewritten using :mod:`scipy.stats`
    """
    # TODO: use scipy for betai and _gamma?
    from genutil.salstat import betai,_gammaln

    # Basic tests
    ndata = MA.masked_equal(ndata,0,copy=0)
    r = MV2.masked_where(MA.equal(MA.absolute(r),1.),r,copy=0)

    # Degree of freedom
    if dof:
        df = ndata
    else:
        df = ndata-2-ndataset

    # Advanced test: prevent extreme values by locally decreasing the dof
    reduc = N.ones(r.shape)
    z = None
    while z is None or MA.count(MA.masked_greater(z,-600.)):
        if z is not None:
            imax = MA.argmin(z.ravel())
            reduc.flat[imax] += 1
        dfr = df/reduc
        t = r*MV2.sqrt(dfr/((1.0-r)* (1.0+r)))
        a = 0.5*dfr
        b = 0.5
        x = df/(dfr+t**2)
        z = _gammaln(a+b)-_gammaln(a)-_gammaln(b)+a*MA.log(x)+b*MA.log(1.0-x)

    # Perfom the test and format the variable
    prob = MV2.masked_array(betai(a,b,x),axes=r.getAxisList())*100
    prob.id = 'corr_proba' ; prob.name = prob.id
    prob.long_name = 'Probability of rejection'
    prob.units = '%'

    return prob
Esempio n. 3
0
def corr_proba(r, ndata, ndataset=2, dof=False):
    """Probability of rejecting correlations

    - **r**: Correlation coefficient
    - **ndata**: Number of records use for correlations
    - **ndataset**, optional:  Number of datasets (1 for autocorrelations, else 2) [default: 2]

    .. todo::

        This must be rewritten using :mod:`scipy.stats`
    """

    # Basic tests
    ndata = MA.masked_equal(ndata,0,copy=0)
    r = MV2.masked_where(MA.equal(MA.absolute(r),1.),r,copy=0)

    # Degree of freedom
    if dof:
        df = ndata
    else:
        df = ndata-2-ndataset

    # Advanced test: prevent extreme values by locally decreasing the dof
    reduc = N.ones(r.shape)
    z = None
    while z is None or MA.count(MA.masked_greater(z,-600.)):
        if z is not None:
            imax = MA.argmin(z.ravel())
            reduc.flat[imax] += 1
        dfr = df/reduc
        t = r*MV2.sqrt(dfr/((1.0-r)* (1.0+r)))
        a = 0.5*dfr
        b = 0.5
        x = df/(dfr+t**2)
        z = _gammaln(a+b)-_gammaln(a)-_gammaln(b)+a*MA.log(x)+b*MA.log(1.0-x)

    # Perfom the test and format the variable
    prob = MV2.masked_array(betai(a,b,x),axes=r.getAxisList())*100
    prob.id = 'corr_proba' ; prob.name = prob.id
    prob.long_name = 'Probability of rejection'
    prob.units = '%'

    return prob
Esempio n. 4
0
x13 = MV2.add.reduce(uf)
x14 = MV2.add.reduce(ud)
x15 = x9.astype(numpy.float32)
if not x15.dtype.char == numpy.sctype2char(numpy.float32):
    markError('astype error')

## arrayrange(start, stop=None, step=1, typecode=None, axis=None, attributes=None, id=None)
##   Just like range() except it returns a variable whose type can be specfied
##   by the keyword argument typecode. The axis of the result variable may be specified.
xarange = MV2.arange(16., axis=ulat)

## masked_array(a, mask=None, fill_value=None, axes=None, attributes=None, id=None)
##   masked_array(a, mask=None) =
##   array(a, mask=mask, copy=0, fill_value=fill_value)
##   Use fill_value(a) if None.
xmarray = MV2.masked_array(ud)

## masked_object(data, value, copy=1, savespace=0)
##   Create array masked where exactly data equal to value

## masked_values(data, value, rtol=1.0000000000000001e-05, atol=1e-08, copy=1, savespace=0, axes=None,
## attributes=None, id=None)
##   masked_values(data, value, rtol=1.e-5, atol=1.e-8)
##   Create a masked array; mask is None if possible.
##   May share data values with original array, but not recommended.
##   Masked where abs(data-value)<= atol + rtol * abs(value)

## ones(shape, typecode='l', savespace=0, axes=None, attributes=None, id=None)
##   ones(n, typecode=Int, savespace=0, axes=None, attributes=None, id=None) =
##   an array of all ones of the given length or shape.
xones = MV2.ones(uf.shape,
Esempio n. 5
0
    def plot(self, data, template=None, bg=0, x=None, **kwargs):
        if x is None:
            x = self.x
        if template is None:
            template = self.template
        elif isinstance(template, str):
            template = x.gettemplate(template)
        elif not vcs.istemplate(template):  # pragma: no cover
            raise ValueError("Error did not know what to do with template: %s" % template)  # pragma: no cover
        try:
            data_name = data.title
        except AttributeError:
            try:
                data_name = data.long_name
            except AttributeError:
                try:
                    data_name = data.id + data.units
                except AttributeError:
                    try:
                        data_name = data.id
                    except AttributeError:
                        data_name = "array"

        # We'll just flatten the data... if they want to be more precise, should pass in more precise data
        if isinstance(data, cdms2.avariable.AbstractVariable):
            data = data.asma()
        data = data.flatten()

        # ok now we have a good x and a good data
        if not self.bins:
            self.bins = vcs.utils.mkscale(*vcs.minmax(data))

        # Sort the bins
        self.bins.sort()

        # Prune duplicates
        pruned_bins = []
        for bin in self.bins:
            if pruned_bins and numpy.allclose(bin, pruned_bins[-1]):
                continue
            pruned_bins.append(bin)
        self.bins = pruned_bins
        data_bins = numpy.digitize(data, self.bins) - 1
        binned = [data[data_bins==i] for i in range(len(self.bins))]
        means = []
        stds = []

        max_possible_deviance = 0

        for ind, databin in enumerate(binned):
            if len(databin) > 0:
                means.append(databin.mean())
                stds.append(databin.std())
            else:
                means.append(0)
                stds.append(0)
            if len(self.bins) > ind + 1:
                max_possible_deviance = max(means[ind] - self.bins[ind], self.bins[ind + 1] - means[ind], max_possible_deviance)
            else:
                max_possible_deviance = max(means[ind] - self.bins[ind], max_possible_deviance)
        color_values = [std / max_possible_deviance for std in stds]
        y_values = [len(databin) for databin in binned]
        nbars = len(self.bins) - 1

        # create the primitive
        fill = x.createfillarea()
        line = x.createline()
        fill.viewport = [
            template.data.x1, template.data.x2, template.data.y1, template.data.y2]
        line.viewport = [
            template.data.x1, template.data.x2, template.data.y1, template.data.y2]

        vcs_min_max = vcs.minmax(self.bins)
        if numpy.allclose(self.datawc_x1, 1e20):
            xmn = vcs_min_max[0]
        else:
            xmn = self.datawc_x1

        if numpy.allclose(self.datawc_x2, 1e20):
            xmx = vcs_min_max[1]
        else:
            xmx = self.datawc_x2

        if numpy.allclose(self.datawc_y2, 1e20):
            # Make the y scale be slightly larger than the largest bar
            ymx = max(y_values) * 1.25
        else:
            ymx = self.datawc_y2

        if numpy.allclose(self.datawc_y1, 1e20):
            ymn = 0
        else:
            ymn = self.datawc_y1

        fill.worldcoordinate = [xmn, xmx, ymn, ymx]
        line.worldcoordinate = [xmn, xmx, ymn, ymx]

        styles = []
        cols = []
        indices = []
        lt = []
        lw = []
        lc = []
        xs = []
        ys = []

        levels = [.1 * i for i in range(11)]

        # Extend fillarea and line attrs to levels
        if self.fillareastyles:
            while len(self.fillareastyles) < (len(levels) - 1):
                self.fillareastyles.append(self.fillareastyles[-1])
        else:
            self.fillareastyles = ["solid"] * (len(levels) - 1)

        if self.fillareacolors:
            while len(self.fillareacolors) < (len(levels) - 1):
                self.fillareacolors.append(self.fillareacolors[-1])
        else:
            for lev in levels[:-1]:
                self.fillareacolors.append(int((self.color_2 - self.color_1) * lev) + self.color_1)

        if self.fillareaindices:
            while len(self.fillareaindices) < (len(levels) - 1):
                self.fillareaindices.append(self.fillareaindices[-1])
        else:
            self.fillareaindices = [1] * (len(levels) - 1)

        if self.line:
            while len(self.line) < (len(levels) - 1):
                self.line.append(self.line[-1])
        else:
            self.line = ["solid"] * (len(levels) - 1)

        if self.linewidth:
            while len(self.linewidth) < (len(levels) - 1):
                self.linewidth.append(self.linewidth[-1])
        else:
            self.linewidth = [1] * (len(levels) - 1)

        if self.linecolors:
            while len(self.linecolors) < (len(levels) - 1):
                self.linecolors.append(self.linecolors[-1])
        else:
            self.linecolors = ["black"] * (len(levels) - 1)

        for i in range(nbars):
            # Calculate level for bar
            value = color_values[i]
            for lev_ind in range(len(levels)):
                if levels[lev_ind] > value:
                    if lev_ind > 0:
                        lev_ind -= 1
                        break
                    else:
                        # Shouldn't ever get here since level 0 is 0
                        assert False  # pragma: no cover
            else:
                assert False  # pragma: no cover
            styles.append(self.fillareastyles[lev_ind])
            cols.append(self.fillareacolors[lev_ind])
            indices.append(self.fillareaindices[lev_ind])
            lt.append(self.line[lev_ind])
            lw.append(self.linewidth[lev_ind])
            lc.append(self.linecolors[lev_ind])

            xs.append([self.bins[i], self.bins[i], self.bins[i + 1], self.bins[i + 1]])
            ys.append([0, y_values[i], y_values[i], 0])

        fill.style = styles
        fill.x = xs
        fill.y = ys
        fill.style
        fill.index = indices
        fill.color = cols
        fill.colormap = self.colormap
        line.x = xs
        line.y = ys
        line.type = lt
        line.width = lw
        line.color = lc
        displays = []

        x_axis = cdms2.createAxis(self.bins, id=data_name)
        y_axis = cdms2.createAxis(vcs.mkscale(ymn, ymx), id="bin_size")

        displays.append(x.plot(fill, bg=bg, render=False))
        arr = MV2.masked_array(y_values)
        arr.setAxis(0, x_axis)
        dsp = template.plot(x, arr, self, bg=bg, X=x_axis, Y=y_axis)
        for d in dsp:
            if d is not None:
                displays.append(d)
        legend_labels = {0: "No Variance",
                         .1: "",
                         .2: "",
                         .3: "",
                         .4: "",
                         .5: "",
                         .6: "",
                         .7: "",
                         .8: "",
                         .9: "",
                         1: "High Variance"}
        template.drawColorBar(self.fillareacolors, levels,
                              legend=legend_labels, x=x,
                              style=self.fillareastyles,
                              index=self.fillareaindices)

        displays.append(x.plot(line, bg=bg))

        x.worldcoordinate = fill.worldcoordinate

        self.restore()
        return displays
Esempio n. 6
0
    def plot(self, data, template=None, bg=0, x=None, **kwargs):
        if x is None:
            x = self.x
        if template is None:
            template = self.template
        elif isinstance(template, str):
            template = x.gettemplate(template)
        elif not vcs.istemplate(template):  # pragma: no cover
            raise ValueError(
                "Error did not know what to do with template: %s" %
                template)  # pragma: no cover
        try:
            data_name = data.title
        except AttributeError:
            try:
                data_name = data.long_name
            except AttributeError:
                try:
                    data_name = data.id + data.units
                except AttributeError:
                    try:
                        data_name = data.id
                    except AttributeError:
                        data_name = "array"

        # We'll just flatten the data... if they want to be more precise,
        # should pass in more precise data
        if isinstance(data, cdms2.avariable.AbstractVariable):
            data = data.asma()
        data = data.flatten()

        # ok now we have a good x and a good data
        if not self.bins:
            self.bins = vcs.utils.mkscale(*vcs.minmax(data))

        # Sort the bins
        self.bins.sort()

        # Prune duplicates
        pruned_bins = []
        for bin in self.bins:
            if pruned_bins and numpy.allclose(bin, pruned_bins[-1]):
                continue
            pruned_bins.append(bin)
        self.bins = pruned_bins
        data_bins = numpy.digitize(data, self.bins) - 1
        binned = [data[data_bins == i] for i in range(len(self.bins))]
        means = []
        stds = []

        max_possible_deviance = 0

        for ind, databin in enumerate(binned):
            if len(databin) > 0:
                means.append(databin.mean())
                stds.append(databin.std())
            else:
                means.append(0)
                stds.append(0)
            if len(self.bins) > ind + 1:
                max_possible_deviance = max(means[ind] - self.bins[ind],
                                            self.bins[ind + 1] - means[ind],
                                            max_possible_deviance)
            else:
                max_possible_deviance = max(means[ind] - self.bins[ind],
                                            max_possible_deviance)
        color_values = [std / max_possible_deviance for std in stds]
        y_values = [len(databin) for databin in binned]
        nbars = len(self.bins) - 1

        # create the primitive
        fill = x.createfillarea()
        line = x.createline()
        fill.viewport = [
            template.data.x1, template.data.x2, template.data.y1,
            template.data.y2
        ]
        line.viewport = [
            template.data.x1, template.data.x2, template.data.y1,
            template.data.y2
        ]

        vcs_min_max = vcs.minmax(self.bins)
        if numpy.allclose(self.datawc_x1, 1e20):
            xmn = vcs_min_max[0]
        else:
            xmn = self.datawc_x1

        if numpy.allclose(self.datawc_x2, 1e20):
            xmx = vcs_min_max[1]
        else:
            xmx = self.datawc_x2

        if numpy.allclose(self.datawc_y2, 1e20):
            # Make the y scale be slightly larger than the largest bar
            ymx = max(y_values) * 1.25
        else:
            ymx = self.datawc_y2

        if numpy.allclose(self.datawc_y1, 1e20):
            ymn = 0
        else:
            ymn = self.datawc_y1

        fill.worldcoordinate = [xmn, xmx, ymn, ymx]
        line.worldcoordinate = [xmn, xmx, ymn, ymx]

        styles = []
        cols = []
        indices = []
        lt = []
        lw = []
        lc = []
        xs = []
        ys = []

        levels = [.1 * i for i in range(11)]

        # Extend fillarea and line attrs to levels
        if self.fillareastyles:
            while len(self.fillareastyles) < (len(levels) - 1):
                self.fillareastyles.append(self.fillareastyles[-1])
        else:
            self.fillareastyles = ["solid"] * (len(levels) - 1)

        if self.fillareacolors:
            while len(self.fillareacolors) < (len(levels) - 1):
                self.fillareacolors.append(self.fillareacolors[-1])
        else:
            for lev in levels[:-1]:
                self.fillareacolors.append(
                    int((self.color_2 - self.color_1) * lev) + self.color_1)

        if self.fillareaindices:
            while len(self.fillareaindices) < (len(levels) - 1):
                self.fillareaindices.append(self.fillareaindices[-1])
        else:
            self.fillareaindices = [1] * (len(levels) - 1)

        if self.line:
            while len(self.line) < (len(levels) - 1):
                self.line.append(self.line[-1])
        else:
            self.line = ["solid"] * (len(levels) - 1)

        if self.linewidth:
            while len(self.linewidth) < (len(levels) - 1):
                self.linewidth.append(self.linewidth[-1])
        else:
            self.linewidth = [1] * (len(levels) - 1)

        if self.linecolors:
            while len(self.linecolors) < (len(levels) - 1):
                self.linecolors.append(self.linecolors[-1])
        else:
            self.linecolors = ["black"] * (len(levels) - 1)

        for i in range(nbars):
            # Calculate level for bar
            value = color_values[i]
            for lev_ind in range(len(levels)):
                if levels[lev_ind] > value:
                    if lev_ind > 0:
                        lev_ind -= 1
                        break
                    else:
                        # Shouldn't ever get here since level 0 is 0
                        assert False  # pragma: no cover
            else:
                assert False  # pragma: no cover
            styles.append(self.fillareastyles[lev_ind])
            cols.append(self.fillareacolors[lev_ind])
            indices.append(self.fillareaindices[lev_ind])
            lt.append(self.line[lev_ind])
            lw.append(self.linewidth[lev_ind])
            lc.append(self.linecolors[lev_ind])

            xs.append([
                self.bins[i], self.bins[i], self.bins[i + 1], self.bins[i + 1]
            ])
            ys.append([0, y_values[i], y_values[i], 0])

        fill.style = styles
        fill.x = xs
        fill.y = ys
        fill.style
        fill.index = indices
        fill.color = cols
        fill.colormap = self.colormap
        line.x = xs
        line.y = ys
        line.type = lt
        line.width = lw
        line.color = lc
        displays = []

        x_axis = cdms2.createAxis(self.bins, id=data_name)
        y_axis = cdms2.createAxis(vcs.mkscale(ymn, ymx), id="bin_size")

        displays.append(x.plot(fill, bg=bg, render=False))
        arr = MV2.masked_array(y_values)
        arr.setAxis(0, x_axis)
        dsp = template.plot(x, arr, self, bg=bg, X=x_axis, Y=y_axis)
        for d in dsp:
            if d is not None:
                displays.append(d)
        legend_labels = {
            0: "No Variance",
            .1: "",
            .2: "",
            .3: "",
            .4: "",
            .5: "",
            .6: "",
            .7: "",
            .8: "",
            .9: "",
            1: "High Variance"
        }
        template.drawColorBar(self.fillareacolors,
                              levels,
                              legend=legend_labels,
                              x=x,
                              style=self.fillareastyles,
                              index=self.fillareaindices)

        displays.append(x.plot(line, bg=bg))

        x.worldcoordinate = fill.worldcoordinate

        self.restore()
        return displays
Esempio n. 7
0
# Adapted for numpy/ma/cdms2 by convertcdms.py
import cdms2, cdutil, numpy.ma, MV2, os, sys, cdat_info
cdms2.setAutoBounds('on')

# centroid test

a = MV2.masked_array(MV2.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
                     [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0])
bounds = numpy.ma.array([[0, 31], [31, 59], [59, 90], [90, 120], [120, 151],
                         [151, 181], [181, 212], [212, 243], [243, 273],
                         [273, 304], [304, 334], [334, 365]])
ax = a.getAxis(0)
ax.setBounds(bounds)
#print cdutil.times.centroid(a,[-10,30])
print 'Centroid Normal:', cdutil.times.centroid(a, [0, 365])
print 'Centroid Cyclic:', cdutil.times.cyclicalcentroid(a, [0, 365])

f = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), 'tas_mo.nc'))
s = f('tas')

cdutil.setTimeBoundsMonthly(s)
djf = cdutil.DJF(s)
djf = cdutil.DJF(s, criteriaarg=[.8, 0.0001])
djf = cdutil.ANNUALCYCLE.climatology(s)
djf = cdutil.YEAR.departures(s)
Esempio n. 8
0
    def spatial_mean(self):
        """ Moyenne spatiale en chaque pas de temps pour les champs modelises et observes """
        import cdutil, cdms2, MV2, numpy
        from vacumm.misc.grid import meshweights

        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=-1), axis=-1)
        # --> equivalent a nanmean(nanmean(sst')) sous matlab
        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=1), axis=1)
        # --> equivalent a nanmean(nanmean(sst)) sous matlab
        #print self.modelspatialmean

        self.model.spa_mean = list()  #Initialise une liste
        self.obs.spa_mean = list()  #Initialise une liste

        # Estimation de la moyenne a chaque pas de temps
        tps = self.obs.getTime()
        for i, t in enumerate(tps):
            if self.obs[i, :, :].count() == 0:
                #self.obs.spa_mean.append(self.obs.fill_value) # voir pour mettre un masque dans la variable a tracer ... ou pour choisir une autre valeur
                self.obs.spa_mean.append(0)
                self.model.spa_mean.append(0)
            else:
                #modif J.Gatti- pour que moyenne spatiale fontionne avec une grille curvilineaire et poids soient en metres

                lonmod = self.model.getLongitude()
                latmod = self.model.getLatitude()
                # lon et lat doivent etre 2D pour que meshweigths calcule des poids en m2
                if lonmod.rank() == 1:
                    lonmod, latmod = numpy.meshgrid(lonmod, latmod)

                wtsmod = meshweights(lonmod, latmod, proj=True)
                # la matrice de poids doit avoir le meme type que la variable moyennee (MV dans notre cas)
                wtsmod = MV2.masked_array(
                    wtsmod,
                    MV2.getmask(self.model[i, :, :]),
                    axes=self.model[i, :, :].getAxisList())

                self.model.spa_mean.append(
                    cdutil.averager(self.model[i, :, :],
                                    axis='yx',
                                    weights=wtsmod))

                lonobs = self.obs.getLongitude()
                latobs = self.obs.getLatitude()

                if lonobs.rank() == 1:
                    lonobs, latobs = numpy.meshgrid(lonobs, latobs)

                wtsobs = meshweights(lonobs, latobs, proj=True)
                wtsobs = MV2.masked_array(wtsobs,
                                          MV2.getmask(self.obs[i, :, :]),
                                          axes=self.obs[i, :, :].getAxisList())

                self.obs.spa_mean.append(
                    cdutil.averager(self.obs[i, :, :],
                                    axis='yx',
                                    weights=wtsobs))

                #self.model.spa_mean.append(cdutil.averager(self.model[i, :, :],axis='yx'))
                #self.obs.spa_mean.append(cdutil.averager(self.obs[i, :, :],axis='yx'))
                #fin modif J.Gatti

        # Transformation en variable cdms2
        self.model.spa_mean = cdms2.createVariable(
            self.model.spa_mean,
            typecode='f',
            id='model_spa_mean',
            attributes=dict(units=self.model.units))
        self.obs.spa_mean = cdms2.createVariable(
            self.obs.spa_mean,
            typecode='f',
            id='obs_spa_mean',
            attributes=dict(units=self.obs.units))

        # Ajout de l'axe des temps correspondant a self...
        self.model.spa_mean.setAxis(0, tps)
        self.obs.spa_mean.setAxis(0, tps)
Esempio n. 9
0
    def spatial_mean(self):
        """ Moyenne spatiale en chaque pas de temps pour les champs modelises et observes """
        import cdutil,  cdms2,  MV2,  numpy
        from vacumm.misc.grid import meshweights

        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=-1), axis=-1)
        # --> equivalent a nanmean(nanmean(sst')) sous matlab
        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=1), axis=1)
        # --> equivalent a nanmean(nanmean(sst)) sous matlab
        #print self.modelspatialmean


        self.model.spa_mean = list() #Initialise une liste
        self.obs.spa_mean = list() #Initialise une liste

        # Estimation de la moyenne a chaque pas de temps
        tps = self.obs.getTime()
        for i,  t in enumerate(tps):
            if self.obs[i, :, :].count()==0:
                #self.obs.spa_mean.append(self.obs.fill_value) # voir pour mettre un masque dans la variable a tracer ... ou pour choisir une autre valeur
                self.obs.spa_mean.append(0)
                self.model.spa_mean.append(0)
            else:
                #modif J.Gatti- pour que moyenne spatiale fontionne avec une grille curvilineaire et poids soient en metres

                lonmod=self.model.getLongitude()
                latmod=self.model.getLatitude()
                # lon et lat doivent etre 2D pour que meshweigths calcule des poids en m2
                if lonmod.rank()==1:
                    lonmod,latmod=numpy.meshgrid(lonmod,latmod)

                wtsmod=meshweights(lonmod, latmod, proj=True)
                # la matrice de poids doit avoir le meme type que la variable moyennee (MV dans notre cas)
                wtsmod = MV2.masked_array(wtsmod, MV2.getmask(self.model[i, :, :]), axes=self.model[i, :, :].getAxisList())

                self.model.spa_mean.append(cdutil.averager(self.model[i, :, :], axis='yx', weights=wtsmod))

                lonobs=self.obs.getLongitude()
                latobs=self.obs.getLatitude()

                if lonobs.rank()==1:
                    lonobs,latobs=numpy.meshgrid(lonobs,latobs)

                wtsobs=meshweights(lonobs, latobs, proj=True)
                wtsobs = MV2.masked_array(wtsobs, MV2.getmask(self.obs[i, :, :]), axes=self.obs[i, :, :].getAxisList())

                self.obs.spa_mean.append(cdutil.averager(self.obs[i, :, :], axis='yx', weights=wtsobs))

                #self.model.spa_mean.append(cdutil.averager(self.model[i, :, :],axis='yx'))
                #self.obs.spa_mean.append(cdutil.averager(self.obs[i, :, :],axis='yx'))
                #fin modif J.Gatti


        # Transformation en variable cdms2
        self.model.spa_mean = cdms2.createVariable(self.model.spa_mean,typecode='f',id='model_spa_mean', attributes=dict(units=self.model.units))
        self.obs.spa_mean = cdms2.createVariable(self.obs.spa_mean,typecode='f',id='obs_spa_mean', attributes=dict(units=self.obs.units))


        # Ajout de l'axe des temps correspondant a self...
        self.model.spa_mean.setAxis(0, tps)
        self.obs.spa_mean.setAxis(0, tps)
Esempio n. 10
0
 def testMaskedArray(self):
     xmarray = MV2.masked_array(self.u_file, mask=self.u_file > .01)
     self.assertEqual(len(xmarray.getAxisList()),
                      len(self.u_file.getAxisList()))
     self.assertTrue(MV2.allequal(xmarray.mask, self.u_file > .01))
Esempio n. 11
0
x10=uf**3
x13 = MV2.add.reduce(uf)
x14 = MV2.add.reduce(ud)
x15 = x9.astype(numpy.float32)
if not x15.dtype.char==numpy.sctype2char(numpy.float32): markError('astype error')

## arrayrange(start, stop=None, step=1, typecode=None, axis=None, attributes=None, id=None) 
##   Just like range() except it returns a variable whose type can be specfied
##   by the keyword argument typecode. The axis of the result variable may be specified.
xarange = MV2.arange(16., axis=ulat)

## masked_array(a, mask=None, fill_value=None, axes=None, attributes=None, id=None) 
##   masked_array(a, mask=None) = 
##   array(a, mask=mask, copy=0, fill_value=fill_value)
##   Use fill_value(a) if None.
xmarray = MV2.masked_array(ud)

## masked_object(data, value, copy=1, savespace=0) 
##   Create array masked where exactly data equal to value

## masked_values(data, value, rtol=1.0000000000000001e-05, atol=1e-08, copy=1, savespace=0, axes=None,
## attributes=None, id=None) 
##   masked_values(data, value, rtol=1.e-5, atol=1.e-8)
##   Create a masked array; mask is None if possible.
##   May share data values with original array, but not recommended.
##   Masked where abs(data-value)<= atol + rtol * abs(value)

## ones(shape, typecode='l', savespace=0, axes=None, attributes=None, id=None) 
##   ones(n, typecode=Int, savespace=0, axes=None, attributes=None, id=None) = 
##   an array of all ones of the given length or shape.
xones = MV2.ones(uf.shape, numpy.float32, axes=uf.getAxisList(), attributes=uf.attributes, id=uf.id)
pft_13.long_name = 'Vegetation types'
pft_13.units = '-'

pft13_axislist_pft = pft_13.getAxisList(('veget', 'lat', 'lon'))
timax_slide = cdms2.createAxis(np.asarray([2010], np.float32))
timax_slide.designateTime(calendar=cdtime.MixedCalendar)
timax_slide.id = 'time_counter'
timax_slide.units = 'years since 0-1-1'
#********************************************************************************************************
#********************************************************************************************************
#Get the LUH masking
#mask_file	= cdms2.open('esa_mask.nc')
#esa_mask 	= mask_file('Mask_only',latitude=latrange, longitude=lonrange)
luh_mask = MV2.getmask(primf[0, :, :])
print 'luh_mask', luh_mask.shape
icwtr = MV2.masked_array(icwtr, mask=luh_mask)
#Total land frac in ORCmap  (Sum of all land units in ORCmap is 1)
orc_pft = MV2.masked_array(pft)
for n1 in range(0, 15):
    orc_pft[n1, :, :] = MV2.masked_array(orc_pft[n1, :, :], mask=luh_mask)
    #print n1
lndf_esa = MV2.minimum(MV2.sum(orc_pft, axis=0), 1)
lndf_esa.id = 'lndfrac_esa'
lndf_esa.long_name = 'Sum of all ORCmap PFTs'

#Total land frac in Hurtt data set (sum of all land units in LUH is one after including icwtr)
lndf_hurt = primf[nyears - 1, :, :] + secdf[nyears - 1, :, :] + primn[
    nyears -
    1, :, :] + secdn[nyears - 1, :, :] + c3ann[nyears - 1, :, :] + c4ann[
        nyears -
        1, :, :] + c3per[nyears - 1, :, :] + c4per[nyears - 1, :, :] + c3nfx[
Esempio n. 13
0
# Adapted for numpy/ma/cdms2 by convertcdms.py
import cdms2, cdutil, numpy.ma, MV2, os, sys

cdms2.setAutoBounds("on")

# centroid test

a = MV2.masked_array(MV2.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0])
bounds = numpy.ma.array(
    [
        [0, 31],
        [31, 59],
        [59, 90],
        [90, 120],
        [120, 151],
        [151, 181],
        [181, 212],
        [212, 243],
        [243, 273],
        [273, 304],
        [304, 334],
        [334, 365],
    ]
)
ax = a.getAxis(0)
ax.setBounds(bounds)
# print cdutil.times.centroid(a,[-10,30])
print "Centroid Normal:", cdutil.times.centroid(a, [0, 365])
print "Centroid Cyclic:", cdutil.times.cyclicalcentroid(a, [0, 365])

f = cdms2.open(os.path.join(cdms2.__path__[0], "..", "..", "..", "..", "sample_data", "tas_mo.nc"))