Exemplo n.º 1
0
Arquivo: perf.py Projeto: xyshi25/MibS
    def __init__(self, MetricTable):

        # Create empty ratio table
        nprobs = MetricTable.nprobs
        nsolvs = MetricTable.nsolvs
        self.ratios = ma.masked_array(1.0 * ma.zeros((nprobs + 1, nsolvs)))

        # Compute best relative performance ratios across
        # solvers for each problem
        for prob in range(nprobs):
            metrics = MetricTable.prob_mets(prob)
            best_met = ma.minimum(metrics)
            if (ma.count(metrics) == nsolvs
                    and ma.maximum(metrics) <= opts.minlimit):
                self.ratios[prob + 1, :] = 1.0
            else:
                self.ratios[prob + 1, :] = metrics * (1.0 / best_met)

        # Sort each solvers performance ratios
        for solv in range(nsolvs):
            self.ratios[:, solv] = ma.sort(self.ratios[:, solv])

        # Compute largest ratio and use to replace failures entries
        self.maxrat = ma.maximum(self.ratios)
        self.ratios = ma.filled(self.ratios, 1.01 * self.maxrat)
Exemplo n.º 2
0
    def load_prism(self, d):
        """
        load prism data for this day

        :param d:
         :type d: datetime
        :return:
        """

        year = d.year
        month = d.month
        day = d.day

        shape = self._shape

        prism_base = 'PRISMD2_NMHW2mi_{}{:02n}{:02n}'
        name = prism_base.format(year, month, day)
        ppt = tif_to_array(self._prism_root, name)
        tom = d + timedelta(days=1)
        name = prism_base.format(year, tom.month, tom.day)
        ppt_tom = tif_to_array(self._prsim_root, name)
        ppt = maximum(ppt, zeros(shape))
        ppt_tom = maximum(ppt_tom, zeros(shape))
        # PRISM to mintemp, maxtemp, temp
        if year in YEARS:
            name = 'cai_tmin_us_us_30s_{}{:02n}{:02n}'.format(year, month, day)
            min_temp = tif_to_array(self._prism_min_temp_root, name)
        else:
            name = 'TempMin_NMHW2Buff_{}{:02n}{:02n}'.format(year, month, day)
            min_temp = tif_to_array(self._prism_min_temp_root, name)
        name = 'TempMax_NMHW2Buff_{}{:02n}{:02n}'.format(year, month, day)
        max_temp = tif_to_array(self._prism_max_temp_root, name)
        mid_temp = (min_temp + max_temp) / 2

        return ppt, ppt_tom, max_temp, min_temp, mid_temp
Exemplo n.º 3
0
    def load_prism(self, d):
        """
        load prism data for this day

        :param d:
         :type d: datetime
        :return:
        """

        year = d.year
        month = d.month
        day = d.day

        shape = self._shape

        prism_base = 'PRISMD2_NMHW2mi_{}{:02n}{:02n}'
        name = prism_base.format(year, month, day)
        ppt = tif_to_array(self._prism_root, name)
        tom = d + timedelta(days=1)
        name = prism_base.format(year, tom.month, tom.day)
        ppt_tom = tif_to_array(self._prsim_root, name)
        ppt = maximum(ppt, zeros(shape))
        ppt_tom = maximum(ppt_tom, zeros(shape))
        # PRISM to mintemp, maxtemp, temp
        if year in YEARS:
            name = 'cai_tmin_us_us_30s_{}{:02n}{:02n}'.format(year, month, day)
            min_temp = tif_to_array(self._prism_min_temp_root, name)
        else:
            name = 'TempMin_NMHW2Buff_{}{:02n}{:02n}'.format(year, month, day)
            min_temp = tif_to_array(self._prism_min_temp_root, name)
        name = 'TempMax_NMHW2Buff_{}{:02n}{:02n}'.format(year, month, day)
        max_temp = tif_to_array(self._prism_max_temp_root, name)
        mid_temp = (min_temp + max_temp) / 2

        return ppt, ppt_tom, max_temp, min_temp, mid_temp
Exemplo n.º 4
0
    def __init__(self, MetricTable):

        # Create empty ratio table
        nprobs = MetricTable.nprobs
        nsolvs = MetricTable.nsolvs
        self.ratios = ma.masked_array(1.0 * ma.zeros((nprobs+1, nsolvs)))

        # Compute best relative performance ratios across
        # solvers for each problem
        for prob in range(nprobs):
            metrics  = MetricTable.prob_mets(prob)
            best_met = ma.minimum(metrics)
	    if (ma.count(metrics)==nsolvs and
                ma.maximum(metrics)<=opts.minlimit):
                self.ratios[prob+1,:] = 1.0;
	    else:
                self.ratios[prob+1,:] = metrics * (1.0 / best_met)

        # Sort each solvers performance ratios
        for solv in range(nsolvs):
            self.ratios[:,solv] = ma.sort(self.ratios[:,solv])

        # Compute largest ratio and use to replace failures entries
        self.maxrat = ma.maximum(self.ratios)
        self.ratios = ma.filled(self.ratios, 1.01 * self.maxrat)
Exemplo n.º 5
0
 def myfunction(d,mx,mn):
   from numpy.ma import maximum,minimum,absolute,greater,count
   try:
     if count(d)==0 : return mx,mn
     mx=float(maximum(mx,float(maximum(d))))
     mn=float(minimum(mn,float(minimum(d))))
   except:
     for i in d:
       mx,mn=myfunction(i,mx,mn)
   return mx,mn
Exemplo n.º 6
0
 def myfunction(d, mx, mn):
     from numpy.ma import maximum, minimum, absolute, greater, count
     try:
         if count(d) == 0: return mx, mn
         mx = float(maximum(mx, float(maximum(d))))
         mn = float(minimum(mn, float(minimum(d))))
     except:
         for i in d:
             mx, mn = myfunction(i, mx, mn)
     return mx, mn
Exemplo n.º 7
0
Arquivo: utils.py Projeto: AZed/uvcdat
 def myfunction(d,mx,mn):
   from numpy.ma import maximum,minimum,masked_where,absolute,greater,count
   try:
     d=masked_where(greater(absolute(d),9.9E19),d)
     if count(d)==0 : return mx,mn
     mx=float(maximum(mx,float(maximum(d))))
     mn=float(minimum(mn,float(minimum(d))))
   except:
     for i in d:
       mx,mn=myfunction(i,mx,mn)
   return mx,mn
Exemplo n.º 8
0
 def myfunction(d, mx, mn):
     from numpy.ma import maximum, minimum, masked_where, absolute, greater, count
     try:
         d = masked_where(greater(absolute(d), 9.9E19), d)
         if count(d) == 0: return mx, mn
         mx = float(maximum(mx, float(maximum(d))))
         mn = float(minimum(mn, float(minimum(d))))
     except:
         for i in d:
             mx, mn = myfunction(i, mx, mn)
     return mx, mn
Exemplo n.º 9
0
 def test_testMinMax2(self):
     # Test of minimum, maximum.
     assert_(eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]))
     assert_(eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]))
     x = arange(5)
     y = arange(5) - 2
     x[3] = masked
     y[0] = masked
     assert_(eq(minimum(x, y), where(less(x, y), x, y)))
     assert_(eq(maximum(x, y), where(greater(x, y), x, y)))
     assert_(minimum.reduce(x) == 0)
     assert_(maximum.reduce(x) == 4)
def flux_limiter(
        stage,
        f_old,
        Uf,
        z):
    """Applies the flux (numerical and positivity) limiter to the
    ith nominal flux matrix Uf, shape = (z1.N, z2.N)

    inputs:
    f_old -- (ndarray, dim=2) density from previous time step
    CFL -- (instance) Courant numbers dictating phase space advection
    Uf -- (ndarray, dim=2) the normalized flux (high order or not) used in the CS update

    outputs:
    Uf -- (ndarray, dim=2) final normalized flux for MC originating at prepoints[i,j]
            after numerical and positivity limiter has been applied

    Note: for a 768 x 1536 grid

          the masked implementation here takes about 285 ms
          the looped implementation over all i,j takes 3.85 s for the whole grid

          for a 1536 x 3072 grid

          the masked implementation here takes about 1.44 s
          the looped implementation over all i,j takes 17.9 s for the whole grid

          i.e. the computational savings is at least a factor of 10
    """
    # local masked array (ma) copy of CFL.frac used to leave CFL.frac unchanged
    Uf_ma = ma.array(z.CFL.frac[stage,:,:])

    # mask negative values, keep positives
    Uf_ma[z.CFL.numbers[stage,:,:] < 0] = ma.masked

    # assign the mask to a zero matrix of same size for pairwise ma.minimum/maximum operations below
    zeros = ma.zeros(Uf.shape)
    zeros.mask = Uf_ma.mask

    # operating only on positive values (negatives masked out)
    Uf_pos = ma.minimum(ma.maximum(zeros, Uf), 1.0*f_old)

    # mask positives, keep negatives
    Uf_ma.mask = np.logical_not(Uf_ma.mask)
    zeros.mask = Uf_ma.mask

    #operating only on negative values
    Uf_neg = ma.minimum(ma.maximum(-1.0*f_old, Uf), zeros)

    # combine masked values in a single matrix Uf_final
    Uf = np.where(Uf_neg.mask == False, Uf_neg.data, Uf_pos.data)

    return Uf
Exemplo n.º 11
0
 def test_testMinMax2(self):
     # Test of minimum, maximum.
     assert_(eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]))
     assert_(eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]))
     x = arange(5)
     y = arange(5) - 2
     x[3] = masked
     y[0] = masked
     assert_(eq(minimum(x, y), where(less(x, y), x, y)))
     assert_(eq(maximum(x, y), where(greater(x, y), x, y)))
     assert_(minimum.reduce(x) == 0)
     assert_(maximum.reduce(x) == 4)
Exemplo n.º 12
0
    def __init__(self, MetricTable, opts):

        epsilon = 0.0
        if opts.cpu:
            epsilon = 0.01

        # Create empty ratio table
        nprobs = MetricTable.nprobs
        nsolvs = MetricTable.nsolvs
        self.ratios = ma.zeros((nprobs, nsolvs), dtype=numpy.float)

        # Compute best relative performance ratios across
        # solvers for each problem
        for prob in range(nprobs):
            metrics  = MetricTable.prob_mets(prob) + epsilon
            best_met = ma.minimum(metrics)
            self.ratios[prob,:] = metrics * (1.0 / best_met)

        # Sort each solvers performance ratios
        for solv in range(nsolvs):
            self.ratios[:,solv] = ma.sort(self.ratios[:,solv])

        # Compute largest ratio and use to replace failure entries
        self.maxrat = ma.maximum(self.ratios)
        self.ratios = ma.filled(self.ratios, 10 * self.maxrat)
Exemplo n.º 13
0
    def _breakList(self, inList, index, parameter):
        par = float(parameter)

        array = N.empty(shape=[len(inList),],dtype=N.float64)
        i = 0
        for parameters in inList:
            array[i] = parameters[index]
            i = i + 1 

        greater = MA.masked_less(array, par)
        less = MA.masked_greater(array, par)

        upper = MA.minimum(greater)
        lower = MA.maximum(less)

        upperArray = MA.masked_inside(array, par, upper)
        lowerArray = MA.masked_inside(array, lower, par)

        upperList = []
        lowerList = []
        i = 0
        for parameters in inList:
            if upperArray.mask[i]:
                upperList.append(parameters)
            if lowerArray.mask[i]:
                lowerList.append(parameters)
            i = i + 1

        return upperList, lowerList
Exemplo n.º 14
0
def has_close(data, value, rtol=1.e-5, atol=1.e-8):
    """Test if data has any values "equal" to value.

    Returns 1 if any of the elements of argument data has a value 
    "equal" to argument value; returns 0 otherwise.  If data or value
    is floating point, "equal" means where abs(data-value) <= atol + 
    rtol * abs(value).  This is essentially the same algorithm used 
    in the Numeric function allclose.  If data and value are integer,
    "equal" means strict equality.

    Positional Input Arguments:
    * data:   Data.  Scalar or Numeric array, Python list/tuple of
              any size and shape.  Floating or integer type.
    * value:  Test value.  Scalar or 1 element Numeric array, list,
              or tuple.  Floating or integer type.

    Keyword Input Arguments:
    * rtol:   "Relative" tolerance.  Default is 1.e-5.  Used in the
              comparison between data and value only if the two are 
              floating point.  Floating or integer type.
    * atol:   "Absolute" tolerance.  Default is 1.e-8.  Used in the
              comparison between data and value only if the two are 
              floating point.  Floating or integer type.

    Examples:
    >>> from has_close import has_close
    >>> data = [20., -32., -1., 2., 5., 29.]
    >>> has_close(data, -1.)
    1
    >>> has_close(data, 10.)
    0
    """

    #- Make sure data is Numeric type and value is a scalar within the
    #  function:

    dataN = np.array(data)
    valueS = np.array(value)

    #- Safe compare if floating.  Strict compare if integer.  Any other
    #  type returns an error:

    if (dataN.dtype == float) or (valueS.dtype == type(1.)):
        closemask = np.less_equal(np.abs(dataN - valueS),
                                  atol + rtol * np.abs(valueS))
    elif (dataN.dtype == np.int32) and (valueS.dtype == type(1)):
        closemask = np.where(dataN == valueS, 1, 0)
    else:
        print((dataN.dtype))
        print((type(valueS)))
        raise ValueError("has_close:  Inputs must be float or integer")

    #- Return true if any elements of data has value:

    if ma.maximum(closemask) == 1:
        return 1
    else:
        return 0
Exemplo n.º 15
0
    def test_testMinMax(self):
        # Test minimum and maximum.
        (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
        xr = np.ravel(x)  # max doesn't work if shaped
        xmr = ravel(xm)

        # true because of careful selection of data
        self.assertTrue(eq(max(xr), maximum(xmr)))
        self.assertTrue(eq(min(xr), minimum(xmr)))
 def _train(self, blob_generator):
     for blob in blob_generator:
         if self.min is None or self.max is None:
             self.min = blob.data
             self.max = blob.data
         else:
             self.min = minimum(self.min, blob.data)
             self.max = maximum(self.max, blob.data)
         yield blob
Exemplo n.º 17
0
    def test_testMinMax(self):
        # Test minimum and maximum.
        (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
        xr = np.ravel(x)  # max doesn't work if shaped
        xmr = ravel(xm)

        # true because of careful selection of data
        self.assertTrue(eq(max(xr), maximum(xmr)))
        self.assertTrue(eq(min(xr), minimum(xmr)))
Exemplo n.º 18
0
 def _contour_args(self, *args):
     if self.filled: fn = 'contourf'
     else:           fn = 'contour'
     Nargs = len(args)
     if Nargs <= 2:
         z = ma.asarray(args[0], dtype=np.float64)
         x, y = self._initialize_x_y(z)
     elif Nargs <=4:
         x,y,z = self._check_xyz(args[:3])
     else:
         raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn))
     z = ma.masked_invalid(z, copy=False)
     self.zmax = ma.maximum(z)
     self.zmin = ma.minimum(z)
     if self.logscale and self.zmin <= 0:
         z = ma.masked_where(z <= 0, z)
         warnings.warn('Log scale: values of z <=0 have been masked')
         self.zmin = z.min()
     self._auto = False
     if self.levels is None:
         if Nargs == 1 or Nargs == 3:
             lev = self._autolev(z, 7)
         else:   # 2 or 4 args
             level_arg = args[-1]
             try:
                 if type(level_arg) == int:
                     lev = self._autolev(z, level_arg)
                 else:
                     lev = np.asarray(level_arg).astype(np.float64)
             except:
                 raise TypeError(
                     "Last %s arg must give levels; see help(%s)" % (fn,fn))
         if self.filled and len(lev) < 2:
             raise ValueError("Filled contours require at least 2 levels.")
         self.levels = lev
     self._levels = list(self.levels)
     if self.extend in ('both', 'min'):
         self._levels.insert(0, min(self.levels[0],self.zmin) - 1)
     if self.extend in ('both', 'max'):
         self._levels.append(max(self.levels[-1],self.zmax) + 1)
     self._levels = np.asarray(self._levels)
     self.vmin = np.amin(self.levels)  # alternative would be self.layers
     self.vmax = np.amax(self.levels)
     if self.extend in ('both', 'min'):
         self.vmin = 2 * self.levels[0] - self.levels[1]
     if self.extend in ('both', 'max'):
         self.vmax = 2 * self.levels[-1] - self.levels[-2]
     self.layers = self._levels # contour: a line is a thin layer
     if self.filled:
         self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
         if self.extend in ('both', 'min'):
             self.layers[0] = 0.5 * (self.vmin + self._levels[1])
         if self.extend in ('both', 'max'):
             self.layers[-1] = 0.5 * (self.vmax + self._levels[-2])
     return (x, y, z)
Exemplo n.º 19
0
 def test_testScalarArithmetic(self):
     xm = array(0, mask=1)
     #TODO FIXME: Find out what the following raises a warning in r8247
     with np.errstate(divide='ignore'):
         assert_((1 / array(0)).mask)
     assert_((1 + xm).mask)
     assert_((-xm).mask)
     assert_((-xm).mask)
     assert_(maximum(xm, xm).mask)
     assert_(minimum(xm, xm).mask)
     assert_(xm.filled().dtype is xm._data.dtype)
     x = array(0, mask=0)
     assert_(x.filled() == x._data)
     assert_equal(str(xm), str(masked_print_option))
Exemplo n.º 20
0
 def test_testScalarArithmetic(self):
     xm = array(0, mask=1)
     #TODO FIXME: Find out what the following raises a warning in r8247
     with np.errstate(divide='ignore'):
         assert_((1 / array(0)).mask)
     assert_((1 + xm).mask)
     assert_((-xm).mask)
     assert_((-xm).mask)
     assert_(maximum(xm, xm).mask)
     assert_(minimum(xm, xm).mask)
     assert_(xm.filled().dtype is xm._data.dtype)
     x = array(0, mask=0)
     assert_(x.filled() == x._data)
     assert_equal(str(xm), str(masked_print_option))
Exemplo n.º 21
0
 def _process_args(self, *args, **kwargs):
     """
     Process args and kwargs.
     """
     if isinstance(args[0], QuadContourSet):
         C = args[0].Cntr
         if self.levels is None:
             self.levels = args[0].levels
         self.zmin = args[0].zmin
         self.zmax = args[0].zmax
     else:
         x, y, z = self._contour_args(args, kwargs)
         x0 = ma.minimum(x)
         x1 = ma.maximum(x)
         y0 = ma.minimum(y)
         y1 = ma.maximum(y)
         self.ax.update_datalim([(x0,y0), (x1,y1)])
         self.ax.autoscale_view(tight=True)
         _mask = ma.getmask(z)
         if _mask is ma.nomask:
             _mask = None
         C = _cntr.Cntr(x, y, z.filled(), _mask)
     self.Cntr = C
Exemplo n.º 22
0
 def _cast_values(self, values, arguments):
     """Change the return values to be of type self._return_type.
     If "should_check" is defined, first check for 
     values that are too large for the destination type or
     integer wrap-around."""
     type = values.dtype.str
     if self._return_type == type:
         return values
     if self.should_check(arguments):
         max_value = ma.maximum(values)
         if max_value > self._max_storable_value[self._return_type]:
             max_value_str = str(max_value)
             logger.log_error("Variable '%s' is being cast to type '%s', but contains a value (%s) too large to fit into that type."
                              % (self.name(), self._return_type, max_value_str))
     return values.astype(self._return_type)
Exemplo n.º 23
0
def make_solution(case):
    N, K = case
    increasing = np.array(list(range(N)))
    decreasing = np.array(increasing[::-1])

    # status variables
    free = np.ones(N, dtype=bool)
    left = np.zeros(N, dtype=int)
    right = np.zeros(N, dtype=int)

    # init with initial distances
    left[0:N] = increasing
    right[0:N] = decreasing

    for k in range(K):
        # print("customer {}".format(k))
        # mask with current occupancy status
        left = ma.array(left, mask=~free)
        right = ma.array(right, mask=~free)
        #print("left",left)
        #print("right", right)

        mins = ma.minimum(left, right)
        maxs = ma.maximum(left, right)
        #print("mins", mins)
        #print("maxs", maxs)

        # candidates are all stalls where mins are maximal
        max_mins = mins.max()
        candidates = ma.where(mins == max_mins)[0]
        #print(max_mins, candidates)

        # from those candidates select the one where max is also maximal
        selected = ma.argmax(maxs[candidates])
        selected = candidates[selected]
        #print(selected)

        # occupy stall and update left and right
        free[selected] = False
        if selected > 0:
            # left of selected, right distance has to be updated
            right[0:selected] = ma.minimum(decreasing[-selected:], right[0:selected])
        if selected < N -1:
            # right of selected, left distance has to be updated
            left[selected+1:] = ma.minimum(increasing[:N-selected -1], left[selected+1:])

        if k == K -1:
            return maxs[selected], mins[selected]
Exemplo n.º 24
0
 def _cast_values(self, values, arguments):
     """Change the return values to be of type self._return_type.
     If "should_check" is defined, first check for 
     values that are too large for the destination type or
     integer wrap-around."""
     type = values.dtype.str
     if self._return_type == type:
         return values
     if self.should_check(arguments):
         max_value = ma.maximum(values)
         if max_value > self._max_storable_value[self._return_type]:
             max_value_str = str(max_value)
             logger.log_error(
                 "Variable '%s' is being cast to type '%s', but contains a value (%s) too large to fit into that type."
                 % (self.name(), self._return_type, max_value_str))
     return values.astype(self._return_type)
Exemplo n.º 25
0
 def _contour_args(self, args, kwargs):
     if self.filled: fn = 'contourf'
     else:           fn = 'contour'
     Nargs = len(args)
     if Nargs <= 2:
         z = ma.asarray(args[0], dtype=np.float64)
         x, y = self._initialize_x_y(z)
         args = args[1:]
     elif Nargs <=4:
         x,y,z = self._check_xyz(args[:3], kwargs)
         args = args[3:]
     else:
         raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn))
     z = ma.masked_invalid(z, copy=False)
     self.zmax = ma.maximum(z)
     self.zmin = ma.minimum(z)
     if self.logscale and self.zmin <= 0:
         z = ma.masked_where(z <= 0, z)
         warnings.warn('Log scale: values of z <= 0 have been masked')
         self.zmin = z.min()
     self._contour_level_args(z, args)
     return (x, y, z)
Exemplo n.º 26
0
def denoise(im, U_init, tolerance=0.1, tau=0.125, tv_weight=100):

    """
    
    An implementation of the Rudin Osher Fatemi(ROF) denoising model using
    the numerical procedure presented in eq(11)

    """
    m,n = im.shape

    U = U_init
    Px = im
    Py = im
    error =1

    while(error > tolerance):
        Uold = U

        # gradient of primal variable
        GradUx = roll(U, -1, axis = 1)-U
        GradUy = roll(U, -1, axis = 0)-U

        PxNew = Px + (tau/tv_weight)*GradUx
        PyNew = Py + (tau/tv_weight)*GradUy
        NormNew = maximum(1, sqrt(PxNew**2 + PyNew**2))

        Px = PxNew/NormNew
        Py = PyNew/NormNew

        RxPx = roll(Px,1,axis=1)
        RyPy = roll(Py,1,axis=0)

        DivP = (Px-RxPx) + (Py-RyPy)
        U = im + tv_weight*DivP

        error = linalg.norm(U-Uold)/sqrt(n*m)

    return U,im-U
Exemplo n.º 27
0
 def _contour_args(self, *args):
     if self.filled: fn = 'contourf'
     else: fn = 'contour'
     Nargs = len(args)
     if Nargs <= 2:
         z = ma.asarray(args[0], dtype=np.float64)
         x, y = self._initialize_x_y(z)
     elif Nargs <= 4:
         x, y, z = self._check_xyz(args[:3])
     else:
         raise TypeError("Too many arguments to %s; see help(%s)" %
                         (fn, fn))
     z = ma.masked_invalid(z, copy=False)
     self.zmax = ma.maximum(z)
     self.zmin = ma.minimum(z)
     if self.logscale and self.zmin <= 0:
         z = ma.masked_where(z <= 0, z)
         warnings.warn('Log scale: values of z <=0 have been masked')
         self.zmin = z.min()
     self._auto = False
     if self.levels is None:
         if Nargs == 1 or Nargs == 3:
             lev = self._autolev(z, 7)
         else:  # 2 or 4 args
             level_arg = args[-1]
             try:
                 if type(level_arg) == int:
                     lev = self._autolev(z, level_arg)
                 else:
                     lev = np.asarray(level_arg).astype(np.float64)
             except:
                 raise TypeError(
                     "Last %s arg must give levels; see help(%s)" %
                     (fn, fn))
         if self.filled and len(lev) < 2:
             raise ValueError("Filled contours require at least 2 levels.")
         self.levels = lev
     return (x, y, z)
Exemplo n.º 28
0
 def _contour_args(self, *args):
     if self.filled: fn = 'contourf'
     else:           fn = 'contour'
     Nargs = len(args)
     if Nargs <= 2:
         z = ma.asarray(args[0], dtype=np.float64)
         x, y = self._initialize_x_y(z)
     elif Nargs <=4:
         x,y,z = self._check_xyz(args[:3])
     else:
         raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn))
     z = ma.masked_invalid(z, copy=False)
     self.zmax = ma.maximum(z)
     self.zmin = ma.minimum(z)
     if self.logscale and self.zmin <= 0:
         z = ma.masked_where(z <= 0, z)
         warnings.warn('Log scale: values of z <=0 have been masked')
         self.zmin = z.min()
     self._auto = False
     if self.levels is None:
         if Nargs == 1 or Nargs == 3:
             lev = self._autolev(z, 7)
         else:   # 2 or 4 args
             level_arg = args[-1]
             try:
                 if type(level_arg) == int:
                     lev = self._autolev(z, level_arg)
                 else:
                     lev = np.asarray(level_arg).astype(np.float64)
             except:
                 raise TypeError(
                     "Last %s arg must give levels; see help(%s)" % (fn,fn))
         if self.filled and len(lev) < 2:
             raise ValueError("Filled contours require at least 2 levels.")
         self.levels = lev
     return (x, y, z)
Exemplo n.º 29
0
    def load_current_use(self):
        """

        :return:
        """
        root = self._current_use_root
        qDeps = tif_to_array(root, 'Q_deps_std')

        min_val = ones(qDeps.shape) * 0.001
        qDeps = maximum(qDeps, min_val)

        aws = tif_to_array(root, 'aws_mod_4_21_10_0')
        aws = maximum(aws, min_val)
        self._dataset_params = tif_params(root, 'aws_mod_4_21_10_0')

        nlcd_rt_z = tif_to_array(root, 'nlcd_root_dpth_15apr')
        nlcd_rt_z = maximum(nlcd_rt_z, min_val)

        nlcd_plt_hgt = tif_to_array(root, 'nlcd_plnt_hgt1_250_m_degraded1')
        nlcd_plt_hgt = maximum(nlcd_plt_hgt, min_val)

        ksat = tif_to_array(root, 'Soil_Ksat_15apr')
        ksat = maximum(ksat, min_val)

        tew = tif_to_array(root, 'tew_250_15apr')
        tew = maximum(tew, min_val)

        self._qDeps = qDeps
        self._taw = aws
        self._nlcd_rt_z = nlcd_rt_z
        self._nlcd_plt_hgt = nlcd_plt_hgt
        self._ksat = ksat
        self._tew = tew

        self._min_val = min_val
        self._shape = aws.shape
Exemplo n.º 30
0
    def load_current_use(self):
        """

        :return:
        """
        root = self._current_use_root
        qDeps = tif_to_array(root, 'Q_deps_std')

        min_val = ones(qDeps.shape) * 0.001
        qDeps = maximum(qDeps, min_val)

        aws = tif_to_array(root, 'aws_mod_4_21_10_0')
        aws = maximum(aws, min_val)
        self._dataset_params = tif_params(root, 'aws_mod_4_21_10_0')

        nlcd_rt_z = tif_to_array(root, 'nlcd_root_dpth_15apr')
        nlcd_rt_z = maximum(nlcd_rt_z, min_val)

        nlcd_plt_hgt = tif_to_array(root, 'nlcd_plnt_hgt1_250_m_degraded1')
        nlcd_plt_hgt = maximum(nlcd_plt_hgt, min_val)

        ksat = tif_to_array(root, 'Soil_Ksat_15apr')
        ksat = maximum(ksat, min_val)

        tew = tif_to_array(root, 'tew_250_15apr')
        tew = maximum(tew, min_val)

        self._qDeps = qDeps
        self._taw = aws
        self._nlcd_rt_z = nlcd_rt_z
        self._nlcd_plt_hgt = nlcd_plt_hgt
        self._ksat = ksat
        self._tew = tew

        self._min_val = min_val
        self._shape = aws.shape
Exemplo n.º 31
0
    def plot_map(self,
                 dataset,
                 attribute_data,
                 min_value=None,
                 max_value=None,
                 file=None,
                 my_title="",
                 filter=None,
                 background=None):
        """    Plots a 2D image of attribute given by 'name'. matplotlib required.
               The dataset must have a method 'get_2d_attribute' defined that returns
               a 2D array that is to be plotted. If min_value/max_value are given, all values
               that are smaller/larger than these values are set to min_value/max_value.
               Argument background is a value to be used for background. If it is not given,
               it is considered as a 1/100 under the minimum value of the array.
               Filter is a 2D array. Points where filter is > 0 are masked out (put into background).
        """
        import matplotlib
        matplotlib.use('Qt4Agg')

        from matplotlib.pylab import jet, imshow, colorbar, show, axis, savefig, close, figure, title, normalize
        from matplotlib.pylab import rot90

        attribute_data = attribute_data[filter]
        coord_2d_data = dataset.get_2d_attribute(attribute_data=attribute_data)
        data_mask = coord_2d_data.mask
        #        if filter is not None:
        #            if isinstance(filter, ndarray):
        #                if not ma.allclose(filter.shape, coord_2d_data.shape):
        #                    raise StandardError, "Argument filter must have the same shape as the 2d attribute."
        #                filter_data = filter
        #            else:
        #                raise TypeError, "The filter type is invalid. A character string or a 2D numpy array allowed."
        #            filter_data = where(ma.filled(filter_data,1) > 0, 1,0)
        #            data_mask = ma.mask_or(data_mask, filter_data)
        nonmaskedmin = ma.minimum(coord_2d_data) - .2 * (
            ma.maximum(coord_2d_data) - ma.minimum(coord_2d_data))
        if max_value == None:
            max_value = ma.maximum(coord_2d_data)
        if min_value == None:
            min_value = nonmaskedmin

        coord_2d_data = ma.filled(coord_2d_data, min_value)
        if background is None:
            value_range = max_value - min_value
            background = min_value - value_range / 100
        coord_2d_data = ma.filled(
            ma.masked_array(coord_2d_data, mask=data_mask), background)

        # Our data uses NW as 0,0, while matplotlib uses SW for 0,0.
        # Rotate the data so the map is oriented correctly.
        coord_2d_data = rot90(coord_2d_data, 1)

        jet()
        figure()
        norm = normalize(min_value, max_value)
        im = imshow(
            coord_2d_data,
            origin='lower',
            aspect='equal',
            interpolation=None,
            norm=norm,
        )

        tickfmt = '%4d'
        if isinstance(min_value, float) or isinstance(max_value, float):
            tickfmt = '%1.4f'
        colorbar(format=tickfmt)

        title(my_title)
        axis('off')
        if file:
            savefig(file)
            close()
        else:
            show()
Exemplo n.º 32
0
    def create(self,
               parent=None,
               min=None,
               max=None,
               save_file=None,
               thread_it=1,
               rate=None,
               bitrate=None,
               ffmpegoptions=''):
        from vcs import minmax
        from numpy.ma import maximum, minimum
        ##from tkMessageBox import showerror

        # Cannot "Run" or "Create" an animation while already creating an animation
        if self.run_flg == 1: return
        if self.vcs_self.canvas.creating_animation() == 1: return

        if self.vcs_self.animate_info == []:
            str = "No data found!"
            showerror("Error Message to User", str)
            return
        finish_queued_X_server_requests(self.vcs_self)
        self.vcs_self.canvas.BLOCK_X_SERVER()

        # Stop the (thread) execution of the X main loop (if it is running).
        self.vcs_self.canvas.stopxmainloop()

        # Force VCS to update its orientation, needed when the user changes the
        # VCS Canvas size.
        self.vcs_self.canvas.updateorientation()

        # Make sure the animate information is up-to-date for creating images
        if ((self.gui_popup == 1) and (self.create_flg == 0)):
            self.update_animate_display_list()

        # Save the min and max values for the graphics methods.
        # Will need to restore values back when animation is done.
        self.save_original_min_max()

        # Set up the animation min and max values by changing the graphics method
        # Note: cannot set the min and max values if the default graphics method is set.
        do_min_max = 'yes'
        try:
            if (parent is not None) and (parent.iso_spacing == 'Log'):
                do_min_max = 'no'
        except:
            pass

        # Draw specified continental outlines if needed.
        self.continents_hold_value = self.vcs_self.canvas.getcontinentstype()
        self.vcs_self.canvas.setcontinentstype(self.continents_value)

        if (do_min_max == 'yes'):
            minv = []
            maxv = []
            if (min is None) or (max is None):
                for i in range(len(self.vcs_self.animate_info)):
                    minv.append(1.0e77)
                    maxv.append(-1.0e77)
                for i in range(len(self.vcs_self.animate_info)):
                    dpy, slab = self.vcs_self.animate_info[i]
                    mins, maxs = minmax(slab)
                    minv[i] = float(minimum(float(minv[i]), float(mins)))
                    maxv[i] = float(maximum(float(maxv[i]), float(maxs)))
            if isinstance(min, list) or isinstance(max, list):
                for i in range(len(self.vcs_self.animate_info)):
                    try:
                        minv.append(min[i])
                    except:
                        minv.append(min[-1])
                    try:
                        maxv.append(max[i])
                    except:
                        maxv.append(max[-1])
            else:
                for i in range(len(self.vcs_self.animate_info)):
                    minv.append(min)
                    maxv.append(max)

            # Set the min an max for each plot in the page. If the same graphics method is used
            # to display the plots, then the last min and max setting of the data set will be used.
            for i in range(len(self.vcs_self.animate_info)):
                try:
                    self.set_animation_min_max(minv[i], maxv[i], i)
                except Exception, err:
                    pass  # if it is default, then you cannot set the min and max, so pass.
Exemplo n.º 33
0
 def autoscale_None(self, A):
     ' autoscale only None-valued vmin or vmax'
     if self.vmin is None: self.vmin = ma.minimum(A)
     if self.vmax is None: self.vmax = ma.maximum(A)
Exemplo n.º 34
0
def press2alt(arg, P0=None, T0=None, missing=1e+20, invert=0):
    """Calculate elevation given pressure (or vice versa).

    Calculations are made assuming that the temperature distribution
    follows the 1976 Standard Atmosphere.  Technically the standard
    atmosphere defines temperature distribution as a function of
    geopotential altitude, and this routine actually calculates geo-
    potential altitude rather than geometric altitude.


    Method Positional Argument:
    * arg:  Numeric floating point vector of any shape and size, or a
      Numeric floating point scalar.  If invert=0 (the default), arg 
      is air pressure [hPa].  If invert=1, arg is elevation [m].


    Method Keyword Arguments:
    * P0:  Pressure [hPa] at the surface (altitude equals 0).  Numeric 
      floating point vector of same size and shape as arg or a scalar.
      Default of keyword is set to None, in which case the routine 
      uses the value of instance attribute sea_level_press (converted
      to hPa) from the AtmConst class.  Keyword value is used if the 
      keyword is set in the function call.  This keyword cannot have 
      any missing values.

    * T0:  Temperature [K] at the surface (altitude equals 0).  Numeric 
      floating point vector of same size and shape as arg or a scalar.  
      Default of keyword is set to None, in which case the routine uses 
      the value of instance attribute sea_level_temp from the AtmConst
      class.  Keyword value is used if the keyword is set in the func-
      tion call.  This keyword cannot have any missing values.

    * missing:  If arg has missing values, this is the missing value 
      value.  Floating point scalar.  Default is 1e+20.

    * invert:  If set to 1, function calculates pressure [hPa] from 
      altitude [m].  In that case, positional input variable arg is 
      altitude [m] and the output is pressure [hPa].  Default value of 
      invert=0, which means the function calculates altitude given 
      pressure.


    Output:
    * If invert=0 (the default), output is elevation [m] at each 
      element of arg, relative to the surface.  If invert=1, output
      is the air pressure [hPa].  Numeric floating point array of 
      the same size and shape as arg.

      If there are any missing values in output, those values are set 
      to the value in argument missing from the input.  If there are 
      missing values in the output due to math errors and missing is 
      set to None, output will fill those missing values with the MA 
      default value of 1e+20.


    References:
    * Carmichael, Ralph (2003):  "Definition of the 1976 Standard Atmo-
      sphere to 86 km," Public Domain Aeronautical Software (PDAS).
      URL:  http://www.pdas.com/coesa.htm.

    * Wallace, J. M., and P. V. Hobbs (1977): Atmospheric Science:
      An Introductory Survey.  San Diego, CA:  Academic Press, ISBN
      0-12-732950-1, pp. 60-61.


    Examples:

    (1) Calculating altitude given pressure:

    >>> from press2alt import press2alt
    >>> import Numeric as N
    >>> press = N.array([200., 350., 850., 1e+20, 50.])
    >>> alt = press2alt(press, missing=1e+20)
    >>> ['%.7g' % alt[i] for i in range(5)]
    ['11783.94', '8117.19', '1457.285', '1e+20', '20575.96']

    (2) Calculating pressure given altitude:

    >>> alt = N.array([0., 10000., 15000., 20000., 50000.])
    >>> press = press2alt(alt, missing=1e+20, invert=1)
    >>> ['%.7g' % press[i] for i in range(5)]
    ['1013.25', '264.3589', '120.443', '54.74718', '0.7593892']

    (3) Input is a Numeric floating point scalar, and using a keyword
        set surface pressure to a different scalar:

    >>> alt = press2alt(N.array(850.), P0=1000.)
    >>> ['%.7g' % alt[0]]
    ['1349.778']
    """
    import numpy.ma as MA
    import numpy as N
    from atmconst import AtmConst
    #from is_numeric_float import is_numeric_float

    #- Check input is of the correct type:

    #if is_numeric_float(arg) != 1:
    #    raise TypeError, "press2alt:  Arg not Numeric floating"

    #- Import general constants and set additional constants.  h1_std
    #  is the lower limit of the Standard Atmosphere layer geopoten-
    #  tial altitude [m], h2_std is the upper limit [m] of the layer,
    #  and dT/dh is the temperature gradient (i.e. negative of the
    #  lapse rate) [K/m]:

    const = AtmConst()

    h1_std = N.array([0., 11., 20., 32., 47., 51., 71.]) * 1000.
    h2_std = N.array(MA.concatenate([h1_std[1:], [84.852 * 1000.]]))
    dTdh_std = N.array([-6.5, 0.0, 1.0, 2.8, 0.0, -2.8, -2.0]) / 1000.

    #- Prep arrays for masked array calculation and set conditions
    #  at sea-level.  Pressures are in hPa and temperatures in K.
    #  Sea-level conditions arrays are same shape/size as P_or_z.
    #  If input argument is a scalar, make the local variable used
    #  for calculations a 1-element vector:

    if missing == None: P_or_z = MA.masked_array(arg)
    else: P_or_z = MA.masked_values(arg, missing, copy=0)

    if P_or_z.shape == ():
        P_or_z = MA.reshape(P_or_z, (1, ))

    if P0 == None:
        P0_use = MA.zeros(P_or_z.shape) \
               + (const.sea_level_press / 100.)
    else:
        P0_use = MA.zeros(P_or_z.shape) \
               + MA.masked_array(P0)

    if T0 == None:
        T0_use = MA.zeros(P_or_z.shape) \
               + const.sea_level_temp
    else:
        T0_use = MA.zeros(P_or_z.shape) \
               + MA.masked_array(T0)

    #- Calculate P and T for the boundaries of the 7 layers of the
    #  Standard Atmosphere for the given P0 and T0 (layer 0 goes from
    #  P0 to P1, layer 1 from P1 to P2, etc.).  These are given as
    #  8 element dictionaries P_std and T_std where the key is the
    #  location (P_std[0] is at the bottom of layer 0, P_std[1] is the
    #  top of layer 0 and bottom of layer 1, ... and P_std[7] is the
    #  top of layer 6).  Remember P_std and T_std are dictionaries but
    #  dTdh_std, h1_std, and h2_std are vectors:

    P_std = {0: P0_use}
    T_std = {0: T0_use}

    for i in range(len(h1_std)):
        P_std[i+1] = _pfromz_MA( h2_std[i], -dTdh_std[i] \
                               , P_std[i], T_std[i], h1_std[i] )
        T_std[i + 1] = T_std[i] + (dTdh_std[i] * (h2_std[i] - h1_std[i]))

    #- Test input is within Standard Atmosphere limits:

    if invert == 0:
        tmp = MA.where(P_or_z < P_std[len(h1_std)], 1, 0)
        if MA.sum(MA.ravel(tmp)) > 0:
            raise ValueError, "press2alt:  Pressure out-of-range"
    else:
        tmp = MA.where(P_or_z > MA.maximum(h2_std), 1, 0)
        if MA.sum(MA.ravel(tmp)) > 0:
            raise ValueError, "press2alt:  Altitude out-of-range"

    #- What layer number is each element of P_or_z in?

    P_or_z_layer = 0  #MA.zeros(P_or_z.shape)

    #if invert == 0:
    #    for i in range(len(h1_std)):
    #        tmp = MA.where( MA.logical_and( (P_or_z <= P_std[i]) \
    #                                      , (P_or_z >  P_std[i+1]) ) \
    #                      , i, 0 )
    #        P_or_z_layer += tmp
    #else:
    #    for i in range(len(h1_std)):
    #        tmp = MA.where( MA.logical_and( (P_or_z >= h1_std[i]) \
    #                                      , (P_or_z <  h2_std[i]) ) \
    #                      , i, 0 )
    #        P_or_z_layer += tmp

    #- Fill in the bottom-of-the-layer variables and the lapse rate
    #  for the layers that the levels are in.  The *_actual variables
    #  are the values of dTdh, P_bott, etc. for each element in the
    #  P_or_z_flat array:

    P_or_z_flat = MA.ravel(P_or_z)
    if P_or_z_flat.mask() == None:
        P_or_z_flat_mask = MA.make_mask_none(P_or_z_flat.shape)
    else:
        P_or_z_flat_mask = P_or_z_flat.mask()

    P_or_z_layer_flat = MA.ravel(P_or_z_layer)
    dTdh_actual = MA.zeros(P_or_z_flat.shape)
    P_bott_actual = MA.zeros(P_or_z_flat.shape)
    T_bott_actual = MA.zeros(P_or_z_flat.shape)
    z_bott_actual = MA.zeros(P_or_z_flat.shape)

    for i in xrange(MA.size(P_or_z_flat)):
        if P_or_z_flat_mask[i] != 1:
            layer_number = P_or_z_layer_flat[i]
            dTdh_actual[i] = dTdh_std[layer_number]
            P_bott_actual[i] = MA.ravel(P_std[layer_number])[i]
            T_bott_actual[i] = MA.ravel(T_std[layer_number])[i]
            z_bott_actual[i] = h1_std[layer_number]
        else:
            dTdh_actual[i] = MA.masked
            P_bott_actual[i] = MA.masked
            T_bott_actual[i] = MA.masked
            z_bott_actual[i] = MA.masked

    #- Calculate pressure/altitude from altitude/pressure (output is
    #  a flat array):

    if invert == 0:
        output = _zfromp_MA( P_or_z_flat, -dTdh_actual \
                           , P_bott_actual, T_bott_actual, z_bott_actual )
    else:
        output = _pfromz_MA( P_or_z_flat, -dTdh_actual \
                           , P_bott_actual, T_bott_actual, z_bott_actual )

    #- Return output as same shape as input positional argument:

    return MA.filled(MA.reshape(output, arg.shape), missing)
def flux_limiter(
        stage,
        f_old,
        Uf,
        z
        ):

    """Applies the flux (numerical and positivity) limiter to the
    ith nominal flux matrix Uf, shape = (z1.N, z2.N)

    inputs:
    f_old -- (ndarray, dim=2) density from previous time step
    CFL -- (instance) Courant numbers dictating phase space advection
    Uf -- (ndarray, dim=2) the normalized flux (high order or not) used in the CS update

    outputs:
    Uf -- (ndarray, dim=2) final normalized flux for MC originating at prepoints[i,j]
            after numerical and positivity limiter has been applied

    Note: for a 768 x 1536 grid

          the masked implementation here takes about 285 ms
          the looped implementation over all i,j takes 3.85 s for the whole grid

          for a 1536 x 3072 grid

          the masked implementation here takes about 1.44 s
          the looped implementation over all i,j takes 17.9 s for the whole grid

          i.e. the computational savings is at least a factor of 10
    """
    # note that before we apply the limiter, there is no guarantee that sign(Uf) == sign(z.CFL.numbers)
    # this is one of the functions of the limiter, to make these signs agree if the unlimited value of Uf
    # has been unphysically flipped in sign. Recall that since f >= 0 by initial condition, sign(Uf) == sign(U)
    # for all points f > 0, and where f = 0 then Uf = 0.0 and its role in remapping is to add zero contribution.
    #
    # here, U is the high order correction to z.CFL.frac, i.e.
    #
    #     U = z.CFL.frac + high order terms
    #
    # there isn't a restriction on the size of each high order term or the sign. It is possible for
    # the correction to cause sign(U) != sign(z.CFL.frac) which is not physical.
    # Put in other words, this is unphysical because "if f[i] -> f[i+k] lies between grid points i+k and i+k+1, then
    # the high order correction df keeps (f[i] + Uf[i]) should also between grid points i+k and i+k+1.
    # if U suffers a sign reversal from the high order term contributions, this maps the density packet to
    # outside the interval we know to zeroeth order it must lie in (z.CFL.frac gives the remap fraction to zereoth order)

    # Finally, note that z.CFL.frac and z.CFL.number by construction have the same sign. We often prefer doing
    # sign checks on z.CFL.numbers, but this is not of consequence.

    # assign the mask to a zero matrix of same size for pairwise ma.minimum/maximum operations below
    zeros = ma.zeros(Uf.shape)
    zeros[z.CFL.numbers[stage,:,:] < 0] = ma.masked

    # operating only on positive values (negatives masked out), assert the limiter criteria
    # note: there is some decision making we let python make here (at the cost of slight efficiency, but at the benefit
    # of less storage for local copies of the same arrays). Uf and f_old are ndarrays (not a masked arrays)
    # however, having one masked object (here, the zeros array) is sufficient so that the ma.minimum and
    # ma.maximum operations will act only on the unmasked entries of zeros and hence will pairwise operate on comparing
    # minima and maximum for the nested objects that are compared.
    Uf_pos = ma.minimum(ma.maximum(zeros, Uf), 1.0*f_old)

    # mask positives, keep negatives
    zeros.mask = np.logical_not(zeros.mask)

    # operating only on negative values; see note just before U_pos calculation regarding how this operation
    # plays out given f_old and Uf are unmasked and zeros is masked.
    Uf_neg = ma.minimum(ma.maximum(-1.0*f_old, Uf), zeros)

    # combine masked data that has been limited by the operations above
    # into a final Uf object to return.
    # Here, we can interpret the np.where call as "where Uf_neg is unmasked [i.e. is negative]
    # insert the negative data at those entries in Uf, wherever there are masked values [i.e.
    # corresponded to masked value], insert the positive data that has been limited at those
    # [i,j] locations in Uf"
    Uf = np.where(Uf_neg.mask == False, Uf_neg.data, Uf_pos.data)

    return Uf
Exemplo n.º 36
0
    def _contour_args(self, *args):
        if self.filled: fn = 'contourf'
        else: fn = 'contour'
        Nargs = len(args)
        if Nargs <= 2:
            z = ma.asarray(args[0], dtype=np.float64)
            x, y = self._initialize_x_y(z)
        elif Nargs <= 4:
            x, y, z = self._check_xyz(args[:3])
        else:
            raise TypeError("Too many arguments to %s; see help(%s)" %
                            (fn, fn))
        self.zmax = ma.maximum(z)
        self.zmin = ma.minimum(z)
        if self.logscale and self.zmin <= 0:
            z = ma.masked_where(z <= 0, z)
            warnings.warn('Log scale: values of z <=0 have been masked')
            self.zmin = z.min()
        self._auto = False
        if self.levels is None:
            if Nargs == 1 or Nargs == 3:
                lev = self._autolev(z, 7)
            else:  # 2 or 4 args
                level_arg = args[-1]
                try:
                    if type(level_arg) == int:
                        lev = self._autolev(z, level_arg)
                    else:
                        lev = np.asarray(level_arg).astype(np.float64)
                except:
                    raise TypeError(
                        "Last %s arg must give levels; see help(%s)" %
                        (fn, fn))
            if self.filled and len(lev) < 2:
                raise ValueError("Filled contours require at least 2 levels.")
            # Workaround for cntr.c bug wrt masked interior regions:
            #if filled:
            #    z = ma.masked_array(z.filled(-1e38))
            # It's not clear this is any better than the original bug.
            self.levels = lev
        #if self._auto and self.extend in ('both', 'min', 'max'):
        #    raise TypeError("Auto level selection is inconsistent "
        #                             + "with use of 'extend' kwarg")
        self._levels = list(self.levels)
        if self.extend in ('both', 'min'):
            self._levels.insert(0, min(self.levels[0], self.zmin) - 1)
        if self.extend in ('both', 'max'):
            self._levels.append(max(self.levels[-1], self.zmax) + 1)
        self._levels = np.asarray(self._levels)
        self.vmin = np.amin(self.levels)  # alternative would be self.layers
        self.vmax = np.amax(self.levels)
        if self.extend in ('both', 'min'):
            self.vmin = 2 * self.levels[0] - self.levels[1]
        if self.extend in ('both', 'max'):
            self.vmax = 2 * self.levels[-1] - self.levels[-2]
        self.layers = self._levels  # contour: a line is a thin layer
        if self.filled:
            self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
            if self.extend in ('both', 'min'):
                self.layers[0] = 0.5 * (self.vmin + self._levels[1])
            if self.extend in ('both', 'max'):
                self.layers[-1] = 0.5 * (self.vmax + self._levels[-2])

        return (x, y, z)
Exemplo n.º 37
0
    def specify(self, slab, axes, specification, confined_by, aux):
        ''' First part: confine the slab within a Domain wide enough to do the exact in post'''
        import string, copy
        from numpy.ma import minimum, maximum
        # myconfined is for later, we can't confine a dimension twice with an argument plus a keyword or 2 keywords
        myconfined = [None] * len(axes)
        self.aux = copy.copy(specification)
        # First look at the arguments (i.e not keywords) and confine the dimensions
        # in the order of the arguments
        for i in range(len(self.args)):
            if confined_by[
                    i] is None:  # Check it hasn't been confined by somebody else
                myconfined[i] = 1  # dim confined by argument list
                confined_by[
                    i] = self  # for cdms I want to confine this dimension
                self.aux[i] = specs = list(
                    self.args[i])  # How do we want to confine this dim ?
                if type(specs) == type(slice(0)):
                    specification[i] = specs  # If it's a slicing nothing to do
                else:  # But if it's not...
                    if specs[0] is None:
                        tmp = axes[i].getBounds()
                        if tmp is None:
                            raise ValueError, 'Region error, axis:' + axes[
                                i].id + ' has no bounds'
                        specs[0] = minimum(minimum(tmp[0], tmp[-1]))
                    if specs[1] is None:
                        tmp = axes[i].getBounds()
                        if tmp is None:
                            raise ValueError, 'Region error, axis:' + axes[
                                i].id + ' has no bounds'
                        specs[1] = maximum(maximum(tmp[0], tmp[-1]))
                    if axes[i].isTime():  # Time is as always "Special"
                        import cdtime
                        tc = type(cdtime.comptime(0))  # component time type
                        tr = type(cdtime.reltime(
                            0, 'months since 0'))  # relative time type
                        t = type(specs[0])  # my first spec type
                        if t == type(
                                ''):  #if my first spec is passed as a string
                            specs[0] = cdtime.s2r(specs[0], axes[i].units)
                        elif t == tc or t == tr:  #if my first spec is passed as a cdtime object
                            specs[0] = cdtime.torel(specs[0], axes[i].units)
                        else:  # If not it has to be that the users knows the time values in the axis
                            pass
                        t = type(specs[1])  # my second spec type
                        if t == type(
                                ''):  #if my second spec is passed as a string
                            specs[1] = cdtime.s2r(specs[1], axes[i].units)
                        elif t == tc or t == tr:  #if my second spec is passed as a cdtime object
                            specs[1] = cdtime.torel(specs[1], axes[i].units)
                    sp = [
                        specs[0], specs[1], 'oob'
                    ]  # Now retrieve the values wide enough for the exact                    specification[i]=sp  # sets the specifications
            else:
                return 1
        for kw in self.kargs.keys():
            axis = None
            for i in range(len(axes)):
                if axes[i].id == kw: axis = i
            if axis is None:
                if kw == 'time':
                    for i in range(len(axes)):
                        if axes[i].isTime(): axis = i
                elif kw == 'level':
                    for i in range(len(axes)):
                        if axes[i].isLevel(): axis = i
                elif kw == 'longitude':
                    for i in range(len(axes)):
                        if axes[i].isLongitude(): axis = i
                elif kw == 'latitude':
                    for i in range(len(axes)):
                        if axes[i].isLatitude(): axis = i
                elif not kw in [
                        'exact', 'atol', 'rtol'
                ]:  # keyword not a recognised keyword or dimension name
                    raise 'Error, keyword: ' + kw + ' not recognized'
            # At this point, if axis is None:
            # we are dealing with a keyword for the selector
            # so we'll skip it
            if not axis is None:
                if confined_by[axis] is None:
                    confined_by[axis] = self
                    myconfined[axis] = 1
                    self.aux[axis] = specs = list(self.kargs[kw])
                    if type(specs) != type(slice(0)):
                        if specs[0] is None:
                            tmp = axes[axis].getBounds()
                            if tmp is None:
                                raise ValueError, 'Region error, axis:' + axes[
                                    axis].id + ' has no bounds'
                            specs[0] = minimum(minimum(tmp[0], tmp[-1]))
                        if specs[1] is None:
                            tmp = axes[axis].getBounds()
                            if tmp is None:
                                raise ValueError, 'Region error, axis:' + axes[
                                    axis].id + ' has no bounds'
                            specs[1] = maximum(maximum(tmp[0], tmp[-1]))
                        if axes[axis].isTime():
                            import cdtime
                            tc = type(cdtime.comptime(0))
                            tr = type(cdtime.reltime(0, 'months since 0'))
                            t = type(specs[0])
                            if t == type(''):
                                specs[0] = cdtime.s2r(specs[0], axes[i].units)
                            elif t == tc or t == tr:
                                specs[0] = cdtime.torel(
                                    specs[0], axes[i].units)
                            t = type(specs[1])
                            if t == type(''):
                                specs[1] = cdtime.s2r(specs[1], axes[i].units)
                            elif t == tc or t == tr:
                                specs[1] = cdtime.torel(
                                    specs[1], axes[i].units)
                        sp = [specs[0], specs[1], 'oob']
                        specification[axis] = sp
                    else:
                        specification[axis] = specs

                else:
                    if myconfined[axis] == 1:
                        raise 'Error you are attempting to set the axis: ' + str(
                            axes[axis].id) + ' more than once'
                    else:
                        return 1
        return 0
Exemplo n.º 38
0
    def specify(self, slab, axes, specification, confined_by, aux):
        """ First part: confine the slab within a Domain wide enough to do the exact in post"""
        import string, copy
        from numpy.ma import minimum, maximum

        # myconfined is for later, we can't confine a dimension twice with an argument plus a keyword or 2 keywords
        myconfined = [None] * len(axes)
        self.aux = copy.copy(specification)
        # First look at the arguments (i.e not keywords) and confine the dimensions
        # in the order of the arguments
        for i in range(len(self.args)):
            if confined_by[i] is None:  # Check it hasn't been confined by somebody else
                myconfined[i] = 1  # dim confined by argument list
                confined_by[i] = self  # for cdms I want to confine this dimension
                self.aux[i] = specs = list(self.args[i])  # How do we want to confine this dim ?
                if not (isinstance(specs, list) or isinstance(specs, tuple)):
                    raise Exception, "Error in Selector, you must specify a list or a tuple, you passed:" + str(specs)
                elif (
                    type(specs[0]) == type(cdtime.comptime(1999))
                    or type(specs[0]) == type(cdtime.reltime(0, "days since 1999"))
                    or type(specs[0]) == type("")
                ):
                    list2 = []
                    for l in specs:
                        if type(l) != type(""):
                            list2.append(l.torel("days since 1900").value)
                        else:
                            list2.append(cdtime.s2r(l, "days since 1900").value)
                    min = minimum(list2)
                    max = maximum(list2)
                    specification[i] = cdtime.reltime(min, "days since 1900"), cdtime.reltime(max, "days since 1900")
                else:  # But if it's not...
                    specification[i] = minimum(specs), maximum(specs)  # sets the specifications
            else:
                return 1
        for kw in self.kargs.keys():
            axis = None
            for i in range(len(axes)):
                if axes[i].id == kw:
                    axis = i
            if axis is None:
                if kw == "time":
                    for i in range(len(axes)):
                        if axes[i].isTime():
                            axis = i
                elif kw == "level":
                    for i in range(len(axes)):
                        if axes[i].isLevel():
                            axis = i
                elif kw == "longitude":
                    for i in range(len(axes)):
                        if axes[i].isLongitude():
                            axis = i
                elif kw == "latitude":
                    for i in range(len(axes)):
                        if axes[i].isLatitude():
                            axis = i
                elif not kw in ["match"]:  # keyword not a recognised keyword or dimension name
                    raise Exception, "Error, keyword: " + kw + " not recognized"
            # At this point, if axis is None:
            # we are dealing with a keyword for the selector
            # so we'll skip it
            if not axis is None:
                if confined_by[axis] is None:
                    confined_by[axis] = self
                    myconfined[axis] = 1
                    self.aux[axis] = specs = list(self.kargs[kw])
                    if (
                        type(specs[0]) == type(cdtime.comptime(1999))
                        or type(specs[0]) == type(cdtime.reltime(0, "days since 1999"))
                        or type(specs[0]) == type("")
                    ):
                        list2 = []
                        for l in specs:
                            if type(l) != type(""):
                                list2.append(l.torel("days since 1900").value)
                            else:
                                list2.append(cdtime.s2r(l, "days since 1900").value)
                        min = minimum(list2)
                        max = maximum(list2)
                        specification[axis] = (
                            cdtime.reltime(min, "days since 1900"),
                            cdtime.reltime(max, "days since 1900"),
                        )
                    else:  # But if it's not...
                        specification[axis] = minimum(specs), maximum(specs)

                else:
                    if myconfined[axis] == 1:
                        raise "Error you are attempting to set the axis: " + str(axes[axis].id) + " more than once"
                    else:
                        return 1
        return 0
Exemplo n.º 39
0
def interp(y, x, xinterp, missing=1e+20):
    """Simple linear interpolation for ordinate with missing values.


    Vectors x and y are the data describing a piecewise linear function.
    Function returns the interpolated values of the ordinate function 
    at abscissa values in xinterp.  Values of xinterp outside the range 
    of x are returned as missing.  Any elements in the output that uses
    missing values in y for the interpolation are set to missing.


    Positional Input Arguments:
    * y:  Ordinate values of data.  Rank 1 numeric vector.  Required.
      Can have missing values.  Floating or integer type.

    * x:  Abscissa values of data.  Rank 1 numeric vector.  Required.
      Can have no missing values.  Must be monotonically ascending.  
      Floating or integer type.

    * xinterp:  Abscissa values to calculate interpolated ordinate 
      values at.  Rank 1 numeric vector or numeric scalar.  Required.  
      Can have no missing values.  Can be in any order.  Floating or 
      integer type.


    Keyword Input Arguments:
    * missing:  If input has missing values, this is the missing value 
      value.  Scalar.  Floating or integer type.  Default is 1e+20.


    Output Result:
    * Interpolated ordinate values at xinterp.  Rank 1 numeric vector 
      of same length as xinterp (if xinterp is a numeric scalar, 
      output is also a numeric scalar).  Missing values are set to the 
      value of argument missing.  Type is Float, even if argument 
      missing and inputs are all integer.


    References:
    * Lin, J. W.-B.:  "Simple Interpolation."
      Python/CDAT for Earth Scientists: Tips and Examples.
      http://www.johnny-lin.com/cdat_tips/tips_math/interp.html


    Example with no missing values (gives same output as function
    arrayfns.interp):

    >>> from interp import interp
    >>> import numpy as N
    >>> x = N.array([1., 2., 3., 4., 5.])
    >>> y = N.array([3., 6., 2.,-5.,-3.])
    >>> xint = N.array([3.4, 2.3])
    >>> yint = interp(y, x, xint, missing=1e+20)
    >>> ['%.7g' % yint[i] for i in range(len(yint))]
    ['-0.8', '4.8']

    Example with missing values:

    >>> x = N.array([1.,    2., 3.,  4.,  5.])
    >>> y = N.array([3., 1e+20, 2., -5., -3.])
    >>> xint = N.array([3.4, 2.3])
    >>> yint = interp(y, x, xint, missing=1e+20)
    >>> ['%.7g' % yint[i] for i in range(len(yint))]
    ['-0.8', '1e+20']

    Example with values out of range of the data:

    >>> x = N.array([1.,   2.1, 3.,  4., 5.1])
    >>> y = N.array([3., 1e+20, 2., -5., -3.])
    >>> xint = N.array([3.4, -2.3, 6.])
    >>> yint = interp(y, x, xint, missing=1e+20)
    >>> ['%.7g' % yint[i] for i in range(len(yint))]
    ['-0.8', '1e+20', '1e+20']
    """
    import arrayfns
    import numpy.ma as MA
    import numpy as N
    from .where_close import where_close

    #- Check inputs for possible errors:

    if (N.rank(y) != 1) or (N.rank(x) != 1):
        raise ValueError("interp:  Input(s) not a vector")
    if N.rank(xinterp) > 1:
        raise ValueError("interp:  xinterp not a vector or scalar")
    if x[-1] <= x[0]:
        raise ValueError("interp:  x not monotonically increasing")

    #- Establish constants and define xint, a rank 1 version of
    #  xinterp to be used for the rest of the function:

    if N.rank(xinterp) == 0:
        xint = N.reshape(xinterp, (1, ))
    else:
        xint = xinterp

    num_xint = N.size(xint)

    #- Mask as missing values of xint that are outside of the range
    #  of x:

    yint_outrange_mask = N.logical_or( N.less(xint, x[0]) \
                                     , N.greater(xint, x[-1]) )

    #- Mask of elements with missing values in y, if there are any
    #  missing values in y.  If xint equals a value in x, missing
    #  values mask for that xint is the same as the corresponding
    #  value in x; and mask elements in xint which fall in an interval
    #  (whose upper bound index is top_idx) where one of the endpoints
    #  is missing:

    y_miss_mask = where_close(y, missing)
    yint_miss_mask = N.zeros(num_xint)

    if MA.maximum(y_miss_mask) == 1:

        for i in range(num_xint):
            if yint_outrange_mask[i] == 0:
                x_eq_xint = where_close(x, xint[i])
                if MA.maximum(x_eq_xint) == 1:
                    yint_miss_mask[i] = y_miss_mask[N.nonzero(x_eq_xint)]
                else:
                    top_idx = N.nonzero(N.greater(x, xint[i]))[0]
                    yint_miss_mask[i] = y_miss_mask[top_idx] or \
                                        y_miss_mask[top_idx-1]

    #- Return interpolated values, set to missing values as
    #  appropriate, and making a scalar if xinterp is a scalar:

    yint = arrayfns.interp(y, x, xint)
    N.putmask( yint, N.logical_or(yint_miss_mask, yint_outrange_mask) \
             , missing)
    if N.rank(xinterp) == 0: yint = yint[0]

    return yint
Exemplo n.º 40
0
 def autoscale(self, A):
     '''
     Set vmin, vmax to min, max of A.
     '''
     self.vmin = ma.minimum(A)
     self.vmax = ma.maximum(A)
Exemplo n.º 41
0
 def __init__(self, ax, *args, **kwargs):
     """
     Draw contour lines or filled regions, depending on
     whether keyword arg 'filled' is False (default) or True.
     The first argument of the initializer must be an axes
     object.  The remaining arguments and keyword arguments
     are described in ContourSet.contour_doc.
     """
     self.ax = ax
     self.levels = kwargs.get('levels', None)
     self.filled = kwargs.get('filled', False)
     self.linewidths = kwargs.get('linewidths', None)
     self.linestyles = kwargs.get('linestyles', None)
     self.alpha = kwargs.get('alpha', 1.0)
     self.origin = kwargs.get('origin', None)
     self.extent = kwargs.get('extent', None)
     cmap = kwargs.get('cmap', None)
     self.colors = kwargs.get('colors', None)
     norm = kwargs.get('norm', None)
     self.extend = kwargs.get('extend', 'neither')
     self.antialiased = kwargs.get('antialiased', True)
     self.nchunk = kwargs.get('nchunk', 0)
     self.locator = kwargs.get('locator', None)
     if (isinstance(norm, colors.LogNorm)
             or isinstance(self.locator, ticker.LogLocator)):
         self.logscale = True
         if norm is None:
             norm = colors.LogNorm()
         if self.extend is not 'neither':
             raise ValueError('extend kwarg does not work yet with log scale')
     else:
         self.logscale = False
     if self.origin is not None: assert(self.origin in
                                         ['lower', 'upper', 'image'])
     if self.extent is not None: assert(len(self.extent) == 4)
     if cmap is not None: assert(isinstance(cmap, colors.Colormap))
     if self.colors is not None and cmap is not None:
         raise ValueError('Either colors or cmap must be None')
     if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
     if isinstance(args[0], ContourSet):
         C = args[0].Cntr
         if self.levels is None:
             self.levels = args[0].levels
     else:
         x, y, z = self._contour_args(*args)
         x0 = ma.minimum(x)
         x1 = ma.maximum(x)
         y0 = ma.minimum(y)
         y1 = ma.maximum(y)
         self.ax.update_datalim([(x0,y0), (x1,y1)])
         self.ax.autoscale_view()
         _mask = ma.getmask(z)
         if _mask is ma.nomask:
             _mask = None
         C = _cntr.Cntr(x, y, z.filled(), _mask)
     self.Cntr = C
     self._process_levels()
     if self.colors is not None:
         cmap = colors.ListedColormap(self.colors, N=len(self.layers))
     if self.filled:
         self.collections = cbook.silent_list('collections.PathCollection')
     else:
         self.collections = cbook.silent_list('collections.LineCollection')
     self.labelTexts = []
     self.labelCValues = []
     kw = {'cmap': cmap}
     if norm is not None:
         kw['norm'] = norm
     cm.ScalarMappable.__init__(self, **kw) # sets self.cmap;
     self._process_colors()
     if self.filled:
         if self.linewidths is not None:
             warnings.warn('linewidths is ignored by contourf')
         lowers = self._levels[:-1]
         uppers = self._levels[1:]
         for level, level_upper in zip(lowers, uppers):
             nlist = C.trace(level, level_upper, nchunk = self.nchunk)
             nseg = len(nlist)//2
             segs = nlist[:nseg]
             kinds = nlist[nseg:]
             paths = self._make_paths(segs, kinds)
             col = collections.PathCollection(paths,
                                  antialiaseds = (self.antialiased,),
                                  edgecolors= 'none',
                                  alpha=self.alpha)
             self.ax.add_collection(col)
             self.collections.append(col)
     else:
         tlinewidths = self._process_linewidths()
         self.tlinewidths = tlinewidths
         tlinestyles = self._process_linestyles()
         for level, width, lstyle in zip(self.levels, tlinewidths, tlinestyles):
             nlist = C.trace(level)
             nseg = len(nlist)//2
             segs = nlist[:nseg]
             col = collections.LineCollection(segs,
                                  linewidths = width,
                                  linestyle = lstyle,
                                  alpha=self.alpha)
             col.set_label('_nolegend_')
             self.ax.add_collection(col, False)
             self.collections.append(col)
     self.changed() # set the colors
Exemplo n.º 42
0
    def create(self, parent=None, min=None, max=None, save_file=None,
               thread_it=1, rate=None, bitrate=None, ffmpegoptions=''):
        from vcs import minmax
        from numpy.ma import maximum, minimum

        # Cannot "Run" or "Create" an animation while already creating an
        # animation
        if self.run_flg == 1:
            return
        if self.vcs_self.canvas.creating_animation() == 1:
            return

        if self.vcs_self.animate_info == []:
            str = "No data found!"
            showerror("Error Message to User", str)
            return

        # Stop the (thread) execution of the X main loop (if it is running).
        self.vcs_self.canvas.stopxmainloop()

        # Force VCS to update its orientation, needed when the user changes the
        # VCS Canvas size.
        self.vcs_self.canvas.updateorientation()

        # Make sure the animate information is up-to-date for creating images
        if ((self.gui_popup == 1) and (self.create_flg == 0)):
            self.update_animate_display_list()

        # Save the min and max values for the graphics methods.
        # Will need to restore values back when animation is done.
        self.save_original_min_max()

        # Set up the animation min and max values by changing the graphics method
        # Note: cannot set the min and max values if the default graphics
        # method is set.
        do_min_max = 'yes'
        try:
            if (parent is not None) and (parent.iso_spacing == 'Log'):
                do_min_max = 'no'
        except:
            pass

        # Draw specified continental outlines if needed.
        self.continents_hold_value = self.vcs_self.canvas.getcontinentstype()
        self.vcs_self.canvas.setcontinentstype(self.continents_value)

        if (do_min_max == 'yes'):
            minv = []
            maxv = []
            if (min is None) or (max is None):
                for i in range(len(self.vcs_self.animate_info)):
                    minv.append(1.0e77)
                    maxv.append(-1.0e77)
                for i in range(len(self.vcs_self.animate_info)):
                    dpy, slab = self.vcs_self.animate_info[i]
                    mins, maxs = minmax(slab)
                    minv[i] = float(minimum(float(minv[i]), float(mins)))
                    maxv[i] = float(maximum(float(maxv[i]), float(maxs)))
            if isinstance(min, list) or isinstance(max, list):
                for i in range(len(self.vcs_self.animate_info)):
                    try:
                        minv.append(min[i])
                    except:
                        minv.append(min[-1])
                    try:
                        maxv.append(max[i])
                    except:
                        maxv.append(max[-1])
            else:
                for i in range(len(self.vcs_self.animate_info)):
                    minv.append(min)
                    maxv.append(max)

            # Set the min an max for each plot in the page. If the same graphics method is used
            # to display the plots, then the last min and max setting of the
            # data set will be used.
            for i in range(len(self.vcs_self.animate_info)):
                try:
                    self.set_animation_min_max(minv[i], maxv[i], i)
                except:
                    # if it is default, then you cannot set the min and max, so
                    # pass.
                    pass

        if save_file is None or save_file.split('.')[-1].lower() == 'ras':
            if thread_it:
                thread.start_new_thread(
                    self.vcs_self.canvas.animate_init, (save_file,))
            else:
                self.vcs_self.canvas.animate_init(save_file)
        else:  # ffmpeg stuff
            save_info = self.vcs_self.animate_info
            animation_info = self.animate_info_from_python()
            slabs = []
            templates = []
            dpys = []
            for i in range(len(self.vcs_self.animate_info)):
                dpy, slab = self.vcs_self.animate_info[i]
                slabs.append(slab)
                dpys.append(dpy)
                templates.append(dpy.template)
            sh = slabs[0].shape
            if dpy.g_type in ['boxfill', 'isofill', 'isoline', 'meshfill',
                              'outfill', 'outline', 'taylordiagram', 'vector', ]:
                r = len(sh) - 2
            else:
                r = len(sh) - 1
            # now create the list of all previous indices to plot
            indices = []
            for i in range(r):
                this = list(range(sh[i]))
                tmp = []
                if indices == []:
                    for k in this:
                        indices.append([k, ])
                else:
                    for j in range(len(indices)):
                        for k in this:
                            tmp2 = copy.copy(indices[j])
                            tmp2.append(k)
                            tmp.append(tmp2)
                    indices = tmp
            count = 1
            white_square = self.vcs_self.createfillarea()
            white_square.color = 240
            white_square.x = [0, 1, 1, 0]
            white_square.y = [0, 0, 1, 1]
            new_vcs = self.vcs_self
            if self.vcs_self.orientation() == 'portrait':
                new_vcs.portrait()
            # self.vcs_self.close()

            for index in indices:
                new_vcs.clear()
                new_vcs.plot(white_square, bg=1)
                for i in range(len(save_info)):
                    slab = slabs[i]
                    template = templates[i]
                    gtype = animation_info["gtype"][i].lower()
                    gname = animation_info["gname"][i]
                    gm = None  # for flake8 to be happy
                    exec("gm = new_vcs.get%s('%s')" % (gtype, gname))
                    for j in index:
                        slab = slab[j]
                    new_vcs.plot(slab, gm, new_vcs.gettemplate(template), bg=1)
                new_vcs.png("tmp_anim_%i" % count)
                count += 1
            new_vcs.ffmpeg(
                save_file,
                "tmp_anim_%d.png",
                bitrate=bitrate,
                rate=rate,
                options=ffmpegoptions)
            for i in range(count - 1):
                os.remove("tmp_anim_%i.png" % (i + 1))
            del(new_vcs)
        self.create_flg = 1
Exemplo n.º 43
0
    def specify(self,slab,axes,specification,confined_by,aux):
        ''' First part: confine the slab within a Domain wide enough to do the exact in post'''
        import string,copy
        from numpy.ma import minimum,maximum
        # myconfined is for later, we can't confine a dimension twice with an argument plus a keyword or 2 keywords
        myconfined=[None]*len(axes)
        self.aux=copy.copy(specification)
        # First look at the arguments (i.e not keywords) and confine the dimensions
        # in the order of the arguments
        for i in range(len(self.args)):
            if confined_by[i] is None :  # Check it hasn't been confined by somebody else
                myconfined[i]=1  # dim confined by argument list
                confined_by[i]=self # for cdms I want to confine this dimension
                self.aux[i]=specs=list(self.args[i]) # How do we want to confine this dim ?
                if type(specs)==type(slice(0)):
                    specification[i]=specs  # If it's a slicing nothing to do
                else: # But if it's not...
                    if specs[0] is None:
                        tmp=axes[i].getBounds()
                        if tmp is None:
                            raise ValueError, 'Region error, axis:'+axes[i].id+' has no bounds'
                        specs[0]=minimum(minimum(tmp[0],tmp[-1]))
                    if specs[1] is None:
                        tmp=axes[i].getBounds()
                        if tmp is None:
                            raise ValueError, 'Region error, axis:'+axes[i].id+' has no bounds'
                        specs[1]=maximum(maximum(tmp[0],tmp[-1]))
                    if axes[i].isTime(): # Time is as always "Special"
                        import cdtime
                        tc=type(cdtime.comptime(0))  # component time type
                        tr=type(cdtime.reltime(0,'months since 0'))  # relative time type
                        t=type(specs[0]) # my first spec type
                        if t==type(''): #if my first spec is passed as a string
                            specs[0]=cdtime.s2r(specs[0],axes[i].units)
                        elif t==tc or t==tr: #if my first spec is passed as a cdtime object
                            specs[0]=cdtime.torel(specs[0],axes[i].units)
                        else: # If not it has to be that the users knows the time values in the axis
                            pass
                        t=type(specs[1]) # my second spec type
                        if t==type(''): #if my second spec is passed as a string
                            specs[1]=cdtime.s2r(specs[1],axes[i].units)
                        elif t==tc or t==tr: #if my second spec is passed as a cdtime object
                            specs[1]=cdtime.torel(specs[1],axes[i].units)
                    sp=[specs[0],specs[1],'oob']  # Now retrieve the values wide enough for the exact                    specification[i]=sp  # sets the specifications
            else:
                return 1
        for kw in self.kargs.keys():
            axis=None
            for i in range(len(axes)):
                if axes[i].id==kw : axis=i
            if axis is None:
                if kw=='time' :
                    for i in range(len(axes)):
                        if axes[i].isTime() : axis=i
                elif kw=='level' :
                    for i in range(len(axes)):
                        if axes[i].isLevel() : axis=i
                elif kw=='longitude' :
                    for i in range(len(axes)):
                        if axes[i].isLongitude() : axis=i
                elif kw=='latitude' :
                    for i in range(len(axes)):
                        if axes[i].isLatitude() : axis=i
                elif not kw in ['exact','atol','rtol']: # keyword not a recognised keyword or dimension name
                    raise 'Error, keyword: '+kw+' not recognized'
            # At this point, if axis is None:
            # we are dealing with a keyword for the selector
            # so we'll skip it
            if not axis is None : 
                if confined_by[axis] is None:
                    confined_by[axis]=self
                    myconfined[axis]=1
                    self.aux[axis]=specs=list(self.kargs[kw])
                    if type(specs)!=type(slice(0)):
                        if specs[0] is None:
                            tmp=axes[axis].getBounds()
                            if tmp is None:
                                raise ValueError, 'Region error, axis:'+axes[axis].id+' has no bounds'
                            specs[0]=minimum(minimum(tmp[0],tmp[-1]))
                        if specs[1] is None:
                            tmp=axes[axis].getBounds()
                            if tmp is None:
                                raise ValueError, 'Region error, axis:'+axes[axis].id+' has no bounds'
                            specs[1]=maximum(maximum(tmp[0],tmp[-1]))
                        if axes[axis].isTime():
                            import cdtime
                            tc=type(cdtime.comptime(0))
                            tr=type(cdtime.reltime(0,'months since 0'))
                            t=type(specs[0])
                            if t==type(''):
                                specs[0]=cdtime.s2r(specs[0],axes[i].units)
                            elif t==tc or t==tr:
                                specs[0]=cdtime.torel(specs[0],axes[i].units)
                            t=type(specs[1])
                            if t==type(''):
                                specs[1]=cdtime.s2r(specs[1],axes[i].units)
                            elif t==tc or t==tr:
                                specs[1]=cdtime.torel(specs[1],axes[i].units)
                        sp=[specs[0],specs[1],'oob']
                        specification[axis]=sp
                    else:
                        specification[axis]=specs

                else:
                    if myconfined[axis]==1:
                        raise 'Error you are attempting to set the axis: '+str(axes[axis].id)+' more than once'
                    else:
                        return 1
        return 0
Exemplo n.º 44
0
 def autoscale(self, A):
     """
     Set *vmin*, *vmax* to min, max of *A*.
     """
     self.vmin = ma.minimum(A)
     self.vmax = ma.maximum(A)
Exemplo n.º 45
0
def interp(y, x, xinterp, missing=1e+20):
    """Simple linear interpolation for ordinate with missing values.


    Vectors x and y are the data describing a piecewise linear function.
    Function returns the interpolated values of the ordinate function 
    at abscissa values in xinterp.  Values of xinterp outside the range 
    of x are returned as missing.  Any elements in the output that uses
    missing values in y for the interpolation are set to missing.


    Positional Input Arguments:
    * y:  Ordinate values of data.  Rank 1 numeric vector.  Required.
      Can have missing values.  Floating or integer type.

    * x:  Abscissa values of data.  Rank 1 numeric vector.  Required.
      Can have no missing values.  Must be monotonically ascending.  
      Floating or integer type.

    * xinterp:  Abscissa values to calculate interpolated ordinate 
      values at.  Rank 1 numeric vector or numeric scalar.  Required.  
      Can have no missing values.  Can be in any order.  Floating or 
      integer type.


    Keyword Input Arguments:
    * missing:  If input has missing values, this is the missing value 
      value.  Scalar.  Floating or integer type.  Default is 1e+20.


    Output Result:
    * Interpolated ordinate values at xinterp.  Rank 1 numeric vector 
      of same length as xinterp (if xinterp is a numeric scalar, 
      output is also a numeric scalar).  Missing values are set to the 
      value of argument missing.  Type is Float, even if argument 
      missing and inputs are all integer.


    References:
    * Lin, J. W.-B.:  "Simple Interpolation."
      Python/CDAT for Earth Scientists: Tips and Examples.
      http://www.johnny-lin.com/cdat_tips/tips_math/interp.html


    Example with no missing values (gives same output as function
    arrayfns.interp):

    >>> from interp import interp
    >>> import numpy as N
    >>> x = N.array([1., 2., 3., 4., 5.])
    >>> y = N.array([3., 6., 2.,-5.,-3.])
    >>> xint = N.array([3.4, 2.3])
    >>> yint = interp(y, x, xint, missing=1e+20)
    >>> ['%.7g' % yint[i] for i in range(len(yint))]
    ['-0.8', '4.8']

    Example with missing values:

    >>> x = N.array([1.,    2., 3.,  4.,  5.])
    >>> y = N.array([3., 1e+20, 2., -5., -3.])
    >>> xint = N.array([3.4, 2.3])
    >>> yint = interp(y, x, xint, missing=1e+20)
    >>> ['%.7g' % yint[i] for i in range(len(yint))]
    ['-0.8', '1e+20']

    Example with values out of range of the data:

    >>> x = N.array([1.,   2.1, 3.,  4., 5.1])
    >>> y = N.array([3., 1e+20, 2., -5., -3.])
    >>> xint = N.array([3.4, -2.3, 6.])
    >>> yint = interp(y, x, xint, missing=1e+20)
    >>> ['%.7g' % yint[i] for i in range(len(yint))]
    ['-0.8', '1e+20', '1e+20']
    """
    import arrayfns
    import numpy.ma as MA
    import numpy as N
    from where_close import where_close


    #- Check inputs for possible errors:

    if (N.rank(y) != 1) or (N.rank(x) != 1):
        raise ValueError, "interp:  Input(s) not a vector"
    if N.rank(xinterp) > 1:
        raise ValueError, "interp:  xinterp not a vector or scalar"
    if x[-1] <= x[0]:
        raise ValueError, "interp:  x not monotonically increasing"


    #- Establish constants and define xint, a rank 1 version of
    #  xinterp to be used for the rest of the function:

    if N.rank(xinterp) == 0:
        xint = N.reshape(xinterp, (1,))
    else:
        xint = xinterp

    num_xint = N.size(xint)


    #- Mask as missing values of xint that are outside of the range
    #  of x:

    yint_outrange_mask = N.logical_or( N.less(xint, x[0]) \
                                     , N.greater(xint, x[-1]) )


    #- Mask of elements with missing values in y, if there are any
    #  missing values in y.  If xint equals a value in x, missing 
    #  values mask for that xint is the same as the corresponding 
    #  value in x; and mask elements in xint which fall in an interval 
    #  (whose upper bound index is top_idx) where one of the endpoints 
    #  is missing:

    y_miss_mask    = where_close(y, missing)
    yint_miss_mask = N.zeros(num_xint)

    if MA.maximum(y_miss_mask) == 1:

        for i in xrange(num_xint):
            if yint_outrange_mask[i] == 0:
                x_eq_xint = where_close(x, xint[i])
                if MA.maximum(x_eq_xint) == 1:
                    yint_miss_mask[i] = y_miss_mask[N.nonzero(x_eq_xint)]
                else:
                    top_idx = N.nonzero(N.greater(x, xint[i]))[0]
                    yint_miss_mask[i] = y_miss_mask[top_idx] or \
                                        y_miss_mask[top_idx-1]


    #- Return interpolated values, set to missing values as 
    #  appropriate, and making a scalar if xinterp is a scalar:

    yint = arrayfns.interp(y, x, xint)
    N.putmask( yint, N.logical_or(yint_miss_mask, yint_outrange_mask) \
             , missing)
    if N.rank(xinterp) == 0:  yint = yint[0]

    return yint
Exemplo n.º 46
0
   def create( self, parent=None, min=None, max=None, save_file=None, thread_it = 1, rate=None, bitrate=None, ffmpegoptions='' ):
      from vcs import minmax
      from numpy.ma import maximum,minimum
      ##from tkMessageBox import showerror

      # Cannot "Run" or "Create" an animation while already creating an animation
      if self.run_flg == 1: return
      if self.vcs_self.canvas.creating_animation() == 1: return

      if self.vcs_self.animate_info == []:
         str = "No data found!"
         showerror( "Error Message to User", str )
         return
      finish_queued_X_server_requests( self.vcs_self )
      self.vcs_self.canvas.BLOCK_X_SERVER()

      # Stop the (thread) execution of the X main loop (if it is running).
      self.vcs_self.canvas.stopxmainloop( )

      # Force VCS to update its orientation, needed when the user changes the
      # VCS Canvas size.
      self.vcs_self.canvas.updateorientation()

      # Make sure the animate information is up-to-date for creating images
      if ((self.gui_popup == 1) and (self.create_flg == 0)):
         self.update_animate_display_list( )

      # Save the min and max values for the graphics methods.
      # Will need to restore values back when animation is done.
      self.save_original_min_max()

      # Set up the animation min and max values by changing the graphics method
      # Note: cannot set the min and max values if the default graphics method is set.
      do_min_max = 'yes'
      try:
         if (parent is not None) and (parent.iso_spacing == 'Log'):
            do_min_max = 'no'
      except:
         pass

      # Draw specified continental outlines if needed.
      self.continents_hold_value = self.vcs_self.canvas.getcontinentstype( )
      self.vcs_self.canvas.setcontinentstype( self.continents_value )

      if ( do_min_max == 'yes' ):
         minv = []
         maxv=[]
         if (min is None) or (max is None):
            for i in range(len(self.vcs_self.animate_info)):
               minv.append( 1.0e77 )
               maxv.append( -1.0e77 )
            for i in range(len(self.vcs_self.animate_info)):
               dpy, slab = self.vcs_self.animate_info[i]
               mins, maxs = minmax(slab)
               minv[i] = float(minimum(float(minv[i]), float(mins)))
               maxv[i] = float(maximum(float(maxv[i]), float(maxs)))
         if isinstance(min,list) or isinstance(max,list):
            for i in range(len(self.vcs_self.animate_info)):
               try:
                  minv.append( min[i] )
               except:
                  minv.append( min[-1] )
               try:
                  maxv.append( max[i] )
               except:
                  maxv.append( max[-1] )
         else:
            for i in range(len(self.vcs_self.animate_info)):
                minv.append( min )
                maxv.append( max )

         # Set the min an max for each plot in the page. If the same graphics method is used
         # to display the plots, then the last min and max setting of the data set will be used.
         for i in range(len(self.vcs_self.animate_info)):
            try:
               self.set_animation_min_max( minv[i], maxv[i], i )
            except Exception,err:
               pass # if it is default, then you cannot set the min and max, so pass.
Exemplo n.º 47
0
    def _plot_with_flag(self, scan, showflag=False):
        # total number of panles to plot as a whole
        nptot = scan.nrow()
        # remaining panels to plot
        n = nptot - self._ipanel - 1
        ganged = False
        maxpanel = 25

        if n > 1:
            ganged = rcParams['plotter.ganged']
            if self._rows and self._cols:
                n = min(n,self._rows*self._cols)
                self._plotter.set_panels(rows=self._rows,cols=self._cols,
                                         nplots=n,margin=self._margins,ganged=ganged)
            else:
                n = min(n,maxpanel)
                self._plotter.set_panels(rows=n,cols=0,nplots=n,margin=self._margins,ganged=ganged)
        else:
            self._plotter.set_panels(margin=self._margins)
        #r = 0
        r = self._startrow
        # total row number of scantable
        nr = scan.nrow()
        panelcount = 0
        allylim = []
        allxlim = []
        
        while r < nr:
            # always plot to new panel
            self._plotter.subplot(panelcount)
            self._plotter.palette(0)
            # title and axes labels
            xlab = self._abcissa and self._abcissa[panelcount] \
                       or scan._getabcissalabel()
            if self._offset and not self._abcissa:
                xlab += " (relative)"
            ylab = self._ordinate and self._ordinate[panelcount] \
                   or scan._get_ordinate_label()
            self._plotter.set_axes('xlabel', xlab)
            self._plotter.set_axes('ylabel', ylab)
            lbl = self._get_label(scan, r, mode='title', userlabel=self._title)
            if type(lbl) in (list, tuple):
                if 0 <= panelcount < len(lbl):
                    lbl = lbl[panelcount]
                else:
                    # get default label
                    lbl = self._get_label(scan, r, 'title')
            self._plotter.set_axes('title',lbl)
            panelcount += 1
            # Now get data to plot
            y = scan._getspectrum(r)
            # Check for FLAGROW column
            mr = scan._getflagrow(r)
            from numpy import ma, array
            if mr:
                ys = ma.masked_array(y,mask=mr)
                if showflag:
                    yf = ma.masked_array(y, mask=(not mr))
            else:
                m = scan._getmask(r)
                from numpy import logical_not, logical_and
                if self._maskselection and len(self._usermask) == len(m):
                    if d[self._stacking](r) in self._maskselection[self._stacking]:
                        m = logical_and(m, self._usermask)
                ys = ma.masked_array(y,mask=logical_not(array(m,copy=False)))
                if showflag:
                    yf = ma.masked_array(y,mask=m)

            x = array(scan._getabcissa(r))
            if self._offset:
                x += self._offset
            #llbl = self._get_label(scan, r, mode='legend', userlabel=self._lmap)
            #if type(llbl) in (list, tuple):
            #    llbl = llbl[0]
            #self._plotter.set_line(label=llbl)
            self._plotter.set_line(label="data")
            #plotit = self._plotter.plot
            #if self._hist: plotit = self._plotter.hist
            self._plotter.plot(x,ys)
            if showflag:
                self._plotter.set_line(label="flagged")
                self._plotter.plot(x,yf)
                ylim = self._minmaxy or [min(y),max(y)]
                xlim= self._minmaxx or [min(x),max(x)]
            elif mr or ys.mask.all():
                ylim = self._minmaxy or []
                xlim = self._minmaxx or []
            else:
                ylim = self._minmaxy or [ma.minimum(ys),ma.maximum(ys)]
                xlim= self._minmaxx or [min(x),max(x)]
            allylim += ylim
            allxlim += xlim
            if (panelcount == n) or (r == nr-1):
                break
            r+=1 # next row

        # Set x- and y- limts of subplots
        if ganged:
            xlim = None
            ylim = None
            if len(allylim) > 0:
                allylim.sort()
                ylim = allylim[0],allylim[-1]
            if len(allxlim) > 0:
                allxlim.sort()
                xlim = allxlim[0],allxlim[-1]
            self._plotter.set_limits(xlim=xlim,ylim=ylim)

        # save the current counter for multi-page plotting
        self._startrow = r+1
        self._ipanel += panelcount
        if self.casabar_exists():
            if self._ipanel >= nptot-1:
                self._plotter.figmgr.casabar.disable_next()
            else:
                self._plotter.figmgr.casabar.enable_next()
            if self._ipanel + 1 - panelcount > 0:
                self._plotter.figmgr.casabar.enable_prev()
            else:
                self._plotter.figmgr.casabar.disable_prev()
Exemplo n.º 48
0
    def specify(self, slab, axes, specification, confined_by, aux):
        ''' First part: confine the slab within a Domain wide enough to do the exact in post'''
        import string, copy
        from numpy.ma import minimum, maximum
        # myconfined is for later, we can't confine a dimension twice with an argument plus a keyword or 2 keywords
        myconfined = [None] * len(axes)
        self.aux = copy.copy(specification)
        # First look at the arguments (i.e not keywords) and confine the dimensions
        # in the order of the arguments
        for i in range(len(self.args)):
            if confined_by[
                    i] is None:  # Check it hasn't been confined by somebody else
                myconfined[i] = 1  # dim confined by argument list
                confined_by[
                    i] = self  # for cdms I want to confine this dimension
                self.aux[i] = specs = list(
                    self.args[i])  # How do we want to confine this dim ?
                if not (isinstance(specs, list) or isinstance(specs, tuple)):
                    raise Exception, "Error in Selector, you must specify a list or a tuple, you passed:" + str(
                        specs)
                elif type(specs[0]) == type(
                        cdtime.comptime(1999)) or type(specs[0]) == type(
                            cdtime.reltime(0, 'days since 1999')) or type(
                                specs[0]) == type(''):
                    list2 = []
                    for l in specs:
                        if type(l) != type(''):
                            list2.append(l.torel('days since 1900').value)
                        else:
                            list2.append(
                                cdtime.s2r(l, 'days since 1900').value)
                    min = minimum(list2)
                    max = maximum(list2)
                    specification[i] = cdtime.reltime(
                        min, 'days since 1900'), cdtime.reltime(
                            max, 'days since 1900')
                else:  # But if it's not...
                    specification[i] = minimum(specs), maximum(
                        specs)  # sets the specifications
            else:
                return 1
        for kw in self.kargs.keys():
            axis = None
            for i in range(len(axes)):
                if axes[i].id == kw: axis = i
            if axis is None:
                if kw == 'time':
                    for i in range(len(axes)):
                        if axes[i].isTime(): axis = i
                elif kw == 'level':
                    for i in range(len(axes)):
                        if axes[i].isLevel(): axis = i
                elif kw == 'longitude':
                    for i in range(len(axes)):
                        if axes[i].isLongitude(): axis = i
                elif kw == 'latitude':
                    for i in range(len(axes)):
                        if axes[i].isLatitude(): axis = i
                elif not kw in [
                        'match'
                ]:  # keyword not a recognised keyword or dimension name
                    raise Exception, 'Error, keyword: ' + kw + ' not recognized'
            # At this point, if axis is None:
            # we are dealing with a keyword for the selector
            # so we'll skip it
            if not axis is None:
                if confined_by[axis] is None:
                    confined_by[axis] = self
                    myconfined[axis] = 1
                    self.aux[axis] = specs = list(self.kargs[kw])
                    if type(specs[0]) == type(cdtime.comptime(1999)) or type(
                            specs[0]) == type(
                                cdtime.reltime(0, 'days since 1999')) or type(
                                    specs[0]) == type(''):
                        list2 = []
                        for l in specs:
                            if type(l) != type(''):
                                list2.append(l.torel('days since 1900').value)
                            else:
                                list2.append(
                                    cdtime.s2r(l, 'days since 1900').value)
                        min = minimum(list2)
                        max = maximum(list2)
                        specification[axis] = cdtime.reltime(
                            min, 'days since 1900'), cdtime.reltime(
                                max, 'days since 1900')
                    else:  # But if it's not...
                        specification[axis] = minimum(specs), maximum(specs)

                else:
                    if myconfined[axis] == 1:
                        raise 'Error you are attempting to set the axis: ' + str(
                            axes[axis].id) + ' more than once'
                    else:
                        return 1
        return 0
Exemplo n.º 49
0
    def run_model(self):
        """
        Run the model

        :return:
        """

        shape = self._shape
        tew = self._tew
        taw = self._taw

        start_time = datetime.now()

        pkcb = zeros(shape)
        swe = zeros(shape)
        tot_snow = zeros(shape)

        tot_mass = zeros(shape)
        cum_mass = zeros(shape)
        ref_et = zeros(shape)
        a_min = ones(shape) * 0.45
        a_max = ones(shape) * 0.90
        a = a_max
        pA = a_min

        days = list(
            rrule.rrule(rrule.DAILY, dtstart=self._start, until=self._end))
        nsteps = len(days)
        plt_day = zeros(nsteps)
        plt_rain = zeros(nsteps)
        plt_eta = zeros(nsteps)
        plt_snow_fall = zeros(nsteps)
        plt_ro = zeros(nsteps)
        plt_dr = zeros(nsteps)
        plt_de = zeros(nsteps)
        plt_drew = zeros(nsteps)
        plt_temp = zeros(nsteps)
        plt_dp_r = zeros(nsteps)
        plt_ks = zeros(nsteps)
        plt_pdr = zeros(nsteps)
        plt_etrs = zeros(nsteps)
        plt_kcb = zeros(nsteps)
        plt_ppt = zeros(nsteps)
        plt_ke = zeros(nsteps)
        plt_kr = zeros(nsteps)
        plt_mlt = zeros(nsteps)
        plt_swe = zeros(nsteps)
        plt_tempm = zeros(nsteps)
        plt_fs1 = zeros(nsteps)
        plt_mass = zeros(nsteps)

        p_mo_et = zeros(shape)
        p_mo_precip = zeros(shape)
        p_mo_ro = zeros(shape)
        p_mo_deps = self._dr + self._de + self._drew
        p_mo_infil = zeros(shape)
        p_mo_etrs = zeros(shape)

        p_yr_et = zeros(shape)
        p_yr_precip = zeros(shape)
        p_yr_ro = zeros(shape)
        p_yr_deps = self._dr + self._de + self._drew
        p_yr_infil = zeros(shape)
        p_yr_etrs = zeros(shape)

        start_month = self._start_month
        end_month = self._end_month

        for i, dday in enumerate(days):
            if i > 0:
                pkcb = kcb
            doy = dday.timetuple().tm_yday
            year = dday.year
            month = dday.month
            day = dday.day

            msg = 'Time : {} day {}_{}'.format(datetime.now() - start_time,
                                               doy, year)
            logging.debug(msg)

            # --------------  kcb -------------------
            if year == 2000:
                ndvi = self.calculate_ndvi_2000(doy)
            elif year == 2001:
                ndvi = self.calculate_ndvi_2001(doy)
            else:
                ndvi = self.calculate_ndvi(year, doy)

            kcb = ndvi * 1.25
            kcb = maximum(kcb, self._min_val)
            kcb = where(isnan(kcb), pkcb, kcb)

            # -------------- PRISM -------------------
            ppt, ppt_tom, max_temp, min_temp, mid_temp = self.load_prism(dday)

            # -------------- PM -------------------
            # PM data to etrs
            name = os.path.join('PM{}'.format(year),
                                'PM_NM_{}_{:03n}'.format(year, doy))
            etrs = tif_to_array(self._pm_data_root, name)
            etrs = maximum(etrs, self._min_val)

            name = os.path.join('PM{}'.format(year),
                                'RLIN_NM_{}_{:03n}'.format(year, doy))
            rlin = tif_to_array(self._pm_data_root, name)
            rlin = maximum(rlin, zeros(shape))

            name = os.path.join('rad{}'.format(year),
                                'RTOT_{}_{:03n}'.format(year, doy))
            rg = tif_to_array(self._pm_data_root, name)
            rg = maximum(rg, zeros(shape))

            if i == 0:
                #  Total evaporable water is depth of water in the evaporable
                #  soil layer, i.e., the water available to both stage 1 and 2 evaporation
                rew = minimum((2 + (tew / 3.)), 0.8 * tew)
                # del tew1, tew2

                # you should have all these from previous model runs
                pDr = self._dr
                pDe = self._de
                pDrew = self._drew
                dr = self._dr
                de = self._de
                drew = self._drew

            nom = 2 if start_month <= doy <= end_month else 6
            ksat = self._ksat * nom / 24.

            kc_max_1 = kcb + 0.0001
            min_val = ones(shape) * 0.0001
            kc_max = maximum(min_val, kc_max_1)

            self._nlcd_plt_hgt = self._nlcd_plt_hgt * 0.5 + 1
            numr = maximum(kcb - self._kc_min, min_val * 10)
            denom = maximum((kc_max - self._kc_min), min_val * 10)
            fcov_ref = (numr / denom)**self._nlcd_plt_hgt
            fcov_min = minimum(fcov_ref, ones(shape))
            fcov = maximum(fcov_min, min_val * 10)
            few = maximum(1 - fcov, 0.01)  # exposed ground

            pKr = kr
            kr = minimum(((tew - de) / (tew - rew)), ones(shape))
            kr = where(isnan(kr), pKr, kr)

            pKs = ks
            ks_ref = where(((taw - pDr) / (0.6 * taw)) < zeros(shape),
                           ones(shape) * 0.001, ((taw - pDr) / (0.6 * taw)))
            ks_ref = where(isnan(ks), pKs, ks_ref)
            ks = minimum(ks_ref, ones(shape))

            # Ke evaporation reduction coefficient; stage 1 evaporation
            fsa = where(isnan((rew - drew) / (KE_MAX * etrs)), zeros(shape),
                        (rew - drew) / (KE_MAX * etrs))
            fsb = minimum(fsa, ones(shape))
            fs1 = maximum(fsb, zeros(shape))
            ke = where(
                drew < rew,
                minimum((fs1 + (1 - fs1) * kr) * (kc_max - ks * kcb),
                        few * kc_max), zeros(shape))

            transp = (ks * kcb) * etrs
            et_init = (ks * kcb + ke) * etrs
            eta = maximum(et_init, zeros(shape))
            evap_init = ke * etrs
            evap_min = maximum(evap_init, zeros(shape))
            evap = minimum(evap_min, kc_max)

            # Load temp, find swe, melt, and precipitation, load Ksat
            # Use SNOTEL data for precip and temps:
            # df_snow : (stel_date, stel_snow, stel_precip, stel_tobs, stel_tmax, stel_tmin, stel_tavg, stel_snwd)

            snow_fall = where(mid_temp <= 0.0, ppt, zeros(shape))
            rain = where(mid_temp >= 0.0, ppt, zeros(shape))

            pA = a
            a = where(snow_fall > 3.0, ones(shape) * a_max, a)
            a = where(snow_fall <= 3.0, a_min + (pA - a_min) * exp(-0.12), a)
            a = where(snow_fall == 0.0, a_min + (pA - a_min) * exp(-0.05), a)
            a = where(a < a_min, a_min, a)

            swe += snow_fall

            mlt_init = maximum(((1 - a) * rg * 0.2) + (mid_temp - 1.8) * 11.0,
                               zeros(shape))  # use calibrate coefficients
            mlt = minimum(swe, mlt_init)

            swe -= mlt

            # Find depletions
            pDr = dr
            pDe = de
            pDrew = drew
            watr = rain + mlt
            deps = dr + de + drew

            ro = zeros(shape)
            ro = where(watr > ksat + deps, watr - ksat - deps, ro)
            ro = maximum(ro, zeros(shape))

            dp_r = zeros(shape)
            id1 = where(watr > deps, ones(shape), zeros(shape))
            id2 = where(ksat > watr - deps, ones(shape), zeros(shape))
            dp_r = where(id1 + id2 > 1.99, maximum(watr - deps, zeros(shape)),
                         dp_r)

            dp_r = where(watr > ksat + deps, ksat, dp_r)
            dp_r = maximum(dp_r, zeros(shape))

            drew_1 = minimum((pDrew + ro + (evap - (rain + mlt))), rew)
            drew = maximum(drew_1, zeros(shape))
            diff = maximum(pDrew - drew, zeros(shape))

            de_1 = minimum((pDe + (evap - (rain + mlt - diff))), tew)
            de = maximum(de_1, zeros(shape))
            diff = maximum(((pDrew - drew) + (pDe - de)), zeros(shape))

            dr_1 = minimum((pDr + ((transp + dp_r) - (rain + mlt - diff))),
                           taw)
            dr = maximum(dr_1, zeros(shape))

            # Create cumulative rasters to show net over entire run

            infil += dp_r
            infil = maximum(infil, zeros(shape))

            prev_et = et
            ref_et += etrs
            et = et + evap + transp
            et_ind = et / ref_et
            et = where(isnan(et) == True, prev_et, et)
            et = where(et > ref_et, ref_et / 2., et)
            et = maximum(et, ones(shape) * 0.001)

            precip = precip + rain + snow_fall
            precip = maximum(precip, zeros(shape))

            runoff += ro
            runoff = maximum(runoff, zeros(shape))

            snow_ras = swe + snow_fall - mlt
            snow_ras = maximum(snow_ras, zeros(shape))

            tot_snow += snow_fall

            mo_date = calendar.monthrange(year, month)
            if day == mo_date[1]:
                infil_mo = infil - p_mo_infil
                infil_mo = maximum(infil_mo, zeros(shape))

                ref_et_mo = etrs - p_mo_etrs
                et_mo = et - p_mo_et
                et_mo = where(isnan(et_mo) == True, p_mo_et, et_mo)
                et_mo = where(et_mo > ref_et, ref_et / 2., et_mo)
                et_mo = maximum(et_mo, ones(shape) * 0.001)

                precip_mo = precip - p_mo_precip
                precip_mo = maximum(precip_mo, zeros(shape))

                runoff_mo = ro - p_mo_ro
                runoff_mo = maximum(runoff_mo, zeros(shape))

                snow_ras_mo = swe
                snow_ras_mo = maximum(snow_ras_mo, zeros(shape))

                deps_mo = drew + de + dr
                delta_s_mo = p_mo_deps - deps_mo

                outputs = (('infil', infil_mo), ('et', et_mo), ('precip',
                                                                precip_mo),
                           ('runoff', runoff_mo), ('snow_ras', snow_ras_mo),
                           ('delta_s_mo', delta_s_mo), ('deps_mo', deps_mo))

                self.save_month_step(outputs, month, year)

                p_mo_et = et
                p_mo_precip = precip
                p_mo_ro = ro
                p_mo_deps = deps_mo
                p_mo_infil = infil
                p_mo_etrs = etrs

            if day == 31 and month == 12:
                infil_yr = infil - p_yr_infil
                infil_yr = maximum(infil_yr, zeros(shape))

                ref_et_yr = etrs - p_yr_etrs
                et_yr = et - p_yr_et
                et_yr = where(isnan(et_yr) == True, p_yr_et, et_yr)
                et_yr = where(et_yr > ref_et, ref_et / 2., et_yr)
                et_yr = maximum(et_yr, ones(shape) * 0.001)

                precip_yr = precip - p_yr_precip
                precip_yr = maximum(precip_yr, zeros(shape))

                runoff_yr = ro - p_yr_ro
                runoff_yr = maximum(runoff_yr, zeros(shape))

                snow_ras_yr = swe
                snow_ras_yr = maximum(snow_ras_yr, zeros(shape))

                deps_yr = drew + de + dr
                delta_s_yr = p_yr_deps - deps_yr

                outputs = (('infil', infil_yr), ('et', et_yr), ('precip',
                                                                precip_yr),
                           ('runoff', runoff_yr), ('snow_ras', snow_ras_yr),
                           ('delta_s_yr', delta_s_yr), ('deps_yr', deps_yr))

                p_yr_et = et
                p_yr_precip = precip
                p_yr_ro = ro
                p_yr_deps = deps_yr  # this was originally p_mo_deps = deps_yr im assuming this is a typo
                p_yr_infil = infil
                p_yr_etrs = etrs

                self.save_year_step(outputs, month, year)

            # Check MASS BALANCE for the love of WATER!!!
            mass = rain + mlt - (ro + transp + evap + dp_r +
                                 ((pDr - dr) + (pDe - de) + (pDrew - drew)))
            tot_mass += abs(mass)
            cum_mass += mass

            plt_day[i] = dday

            plt_rain[i] = rain[S, E]
            plt_eta[i] = eta[S, E]
            plt_snow_fall[i] = snow_fall[S, E]
            plt_ro[i] = ro[S, E]
            plt_dr[i] = dr[S, E]
            plt_de[i] = de[S, E]
            plt_drew[i] = drew[S, E]
            plt_temp[i] = mid_temp[S, E]
            plt_dp_r[i] = dp_r[S, E]
            plt_ks[i] = ks[S, E]
            plt_pdr[i] = pDr[S, E]
            plt_etrs[i] = etrs[S, E]
            plt_kcb[i] = kcb[S, E]
            plt_ppt[i] = ppt[S, E]
            plt_ke[i] = ke[S, E]
            plt_kr[i] = kr[S, E]
            plt_mlt[i] = mlt[S, E]
            plt_swe[i] = swe[S, E]
            plt_tempm[i] = max_temp[S, E]
            plt_fs1[i] = fs1[S, E]
            plt_mass[i] = mass[S, E]
Exemplo n.º 50
0
    def __init__(self, ax, *args, **kwargs):
        """
        Draw contour lines or filled regions, depending on
        whether keyword arg 'filled' is False (default) or True.

        The first argument of the initializer must be an axes
        object.  The remaining arguments and keyword arguments
        are described in ContourSet.contour_doc.

        """
        self.ax = ax
        self.levels = kwargs.get('levels', None)
        self.filled = kwargs.get('filled', False)
        self.linewidths = kwargs.get('linewidths', None)
        self.linestyles = kwargs.get('linestyles', None)

        self.alpha = kwargs.get('alpha', 1.0)
        self.origin = kwargs.get('origin', None)
        self.extent = kwargs.get('extent', None)
        cmap = kwargs.get('cmap', None)
        self.colors = kwargs.get('colors', None)
        norm = kwargs.get('norm', None)
        self.extend = kwargs.get('extend', 'neither')
        self.antialiased = kwargs.get('antialiased', True)
        self.nchunk = kwargs.get('nchunk', 0)
        self.locator = kwargs.get('locator', None)
        if (isinstance(norm, colors.LogNorm)
                or isinstance(self.locator, ticker.LogLocator)):
            self.logscale = True
            if norm is None:
                norm = colors.LogNorm()
            if self.extend is not 'neither':
                raise ValueError(
                    'extend kwarg does not work yet with log scale')
        else:
            self.logscale = False

        if self.origin is not None:
            assert (self.origin in ['lower', 'upper', 'image'])
        if self.extent is not None: assert (len(self.extent) == 4)
        if cmap is not None: assert (isinstance(cmap, colors.Colormap))
        if self.colors is not None and cmap is not None:
            raise ValueError('Either colors or cmap must be None')
        if self.origin == 'image': self.origin = mpl.rcParams['image.origin']

        if isinstance(args[0], ContourSet):
            C = args[0].Cntr
            if self.levels is None:
                self.levels = args[0].levels
        else:
            x, y, z = self._contour_args(*args)

            x0 = ma.minimum(x)
            x1 = ma.maximum(x)
            y0 = ma.minimum(y)
            y1 = ma.maximum(y)
            self.ax.update_datalim([(x0, y0), (x1, y1)])
            self.ax.autoscale_view()
            _mask = ma.getmask(z)
            if _mask is ma.nomask:
                _mask = None
            C = _cntr.Cntr(x, y, z.filled(), _mask)
        self.Cntr = C
        self._process_levels()

        if self.colors is not None:
            cmap = colors.ListedColormap(self.colors, N=len(self.layers))
        if self.filled:
            self.collections = cbook.silent_list('collections.PathCollection')
        else:
            self.collections = cbook.silent_list('collections.LineCollection')
        # label lists must be initialized here
        self.labelTexts = []
        self.labelCValues = []

        kw = {'cmap': cmap}
        if norm is not None:
            kw['norm'] = norm
        cm.ScalarMappable.__init__(self, **kw)  # sets self.cmap;
        self._process_colors()
        if self.filled:
            if self.linewidths is not None:
                warnings.warn('linewidths is ignored by contourf')
            lowers = self._levels[:-1]
            uppers = self._levels[1:]
            for level, level_upper in zip(lowers, uppers):
                nlist = C.trace(level, level_upper, nchunk=self.nchunk)
                nseg = len(nlist) // 2
                segs = nlist[:nseg]
                kinds = nlist[nseg:]

                paths = self._make_paths(segs, kinds)

                col = collections.PathCollection(
                    paths,
                    antialiaseds=(self.antialiased, ),
                    edgecolors='none',
                    alpha=self.alpha)
                self.ax.add_collection(col)
                self.collections.append(col)
        else:
            tlinewidths = self._process_linewidths()
            self.tlinewidths = tlinewidths
            tlinestyles = self._process_linestyles()
            for level, width, lstyle in zip(self.levels, tlinewidths,
                                            tlinestyles):
                nlist = C.trace(level)
                nseg = len(nlist) // 2
                segs = nlist[:nseg]
                #kinds = nlist[nseg:]
                col = collections.LineCollection(segs,
                                                 linewidths=width,
                                                 linestyle=lstyle,
                                                 alpha=self.alpha)

                col.set_label('_nolegend_')
                self.ax.add_collection(col, False)
                self.collections.append(col)
        self.changed()  # set the colors
Exemplo n.º 51
0
 def autoscale(self, A):
     '''
     Set *vmin*, *vmax* to min, max of *A*.
     '''
     self.vmin = ma.minimum(A)
     self.vmax = ma.maximum(A)
Exemplo n.º 52
0
    def run_model(self):
        """
        Run the model

        :return:
        """

        shape = self._shape
        tew = self._tew
        taw = self._taw

        start_time = datetime.now()

        pkcb = zeros(shape)
        swe = zeros(shape)
        tot_snow = zeros(shape)

        tot_mass = zeros(shape)
        cum_mass = zeros(shape)
        ref_et = zeros(shape)
        a_min = ones(shape) * 0.45
        a_max = ones(shape) * 0.90
        a = a_max
        pA = a_min

        days = list(rrule.rrule(rrule.DAILY, dtstart=self._start, until=self._end))
        nsteps = len(days)
        plt_day = zeros(nsteps)
        plt_rain = zeros(nsteps)
        plt_eta = zeros(nsteps)
        plt_snow_fall = zeros(nsteps)
        plt_ro = zeros(nsteps)
        plt_dr = zeros(nsteps)
        plt_de = zeros(nsteps)
        plt_drew = zeros(nsteps)
        plt_temp = zeros(nsteps)
        plt_dp_r = zeros(nsteps)
        plt_ks = zeros(nsteps)
        plt_pdr = zeros(nsteps)
        plt_etrs = zeros(nsteps)
        plt_kcb = zeros(nsteps)
        plt_ppt = zeros(nsteps)
        plt_ke = zeros(nsteps)
        plt_kr = zeros(nsteps)
        plt_mlt = zeros(nsteps)
        plt_swe = zeros(nsteps)
        plt_tempm = zeros(nsteps)
        plt_fs1 = zeros(nsteps)
        plt_mass = zeros(nsteps)

        p_mo_et = zeros(shape)
        p_mo_precip = zeros(shape)
        p_mo_ro = zeros(shape)
        p_mo_deps = self._dr + self._de + self._drew
        p_mo_infil = zeros(shape)
        p_mo_etrs = zeros(shape)

        p_yr_et = zeros(shape)
        p_yr_precip = zeros(shape)
        p_yr_ro = zeros(shape)
        p_yr_deps = self._dr + self._de + self._drew
        p_yr_infil = zeros(shape)
        p_yr_etrs = zeros(shape)

        start_month = self._start_month
        end_month = self._end_month

        for i, dday in enumerate(days):
            if i > 0:
                pkcb = kcb
            doy = dday.timetuple().tm_yday
            year = dday.year
            month = dday.month
            day = dday.day

            msg = 'Time : {} day {}_{}'.format(datetime.now() - start_time, doy, year)
            logging.debug(msg)

            # --------------  kcb -------------------
            if year == 2000:
                ndvi = self.calculate_ndvi_2000(doy)
            elif year == 2001:
                ndvi = self.calculate_ndvi_2001(doy)
            else:
                ndvi = self.calculate_ndvi(year, doy)

            kcb = ndvi * 1.25
            kcb = maximum(kcb, self._min_val)
            kcb = where(isnan(kcb), pkcb, kcb)

            # -------------- PRISM -------------------
            ppt, ppt_tom, max_temp, min_temp, mid_temp = self.load_prism(dday)

            # -------------- PM -------------------
            # PM data to etrs
            name = os.path.join('PM{}'.format(year),
                                'PM_NM_{}_{:03n}'.format(year, doy))
            etrs = tif_to_array(self._pm_data_root, name)
            etrs = maximum(etrs, self._min_val)

            name = os.path.join('PM{}'.format(year),
                                'RLIN_NM_{}_{:03n}'.format(year, doy))
            rlin = tif_to_array(self._pm_data_root, name)
            rlin = maximum(rlin, zeros(shape))

            name = os.path.join('rad{}'.format(year),
                                'RTOT_{}_{:03n}'.format(year, doy))
            rg = tif_to_array(self._pm_data_root, name)
            rg = maximum(rg, zeros(shape))

            if i == 0:
                #  Total evaporable water is depth of water in the evaporable
                #  soil layer, i.e., the water available to both stage 1 and 2 evaporation
                rew = minimum((2 + (tew / 3.)), 0.8 * tew)
                # del tew1, tew2

                # you should have all these from previous model runs
                pDr = self._dr
                pDe = self._de
                pDrew = self._drew
                dr = self._dr
                de = self._de
                drew = self._drew

            nom = 2 if start_month <= doy <= end_month else 6
            ksat = self._ksat * nom / 24.

            kc_max_1 = kcb + 0.0001
            min_val = ones(shape) * 0.0001
            kc_max = maximum(min_val, kc_max_1)

            self._nlcd_plt_hgt = self._nlcd_plt_hgt * 0.5 + 1
            numr = maximum(kcb - self._kc_min, min_val * 10)
            denom = maximum((kc_max - self._kc_min), min_val * 10)
            fcov_ref = (numr / denom) ** self._nlcd_plt_hgt
            fcov_min = minimum(fcov_ref, ones(shape))
            fcov = maximum(fcov_min, min_val * 10)
            few = maximum(1 - fcov, 0.01)  # exposed ground

            pKr = kr
            kr = minimum(((tew - de) / (tew - rew)), ones(shape))
            kr = where(isnan(kr), pKr, kr)

            pKs = ks
            ks_ref = where(((taw - pDr) / (0.6 * taw)) < zeros(shape), ones(shape) * 0.001,
                           ((taw - pDr) / (0.6 * taw)))
            ks_ref = where(isnan(ks), pKs, ks_ref)
            ks = minimum(ks_ref, ones(shape))

            # Ke evaporation reduction coefficient; stage 1 evaporation
            fsa = where(isnan((rew - drew) / (KE_MAX * etrs)), zeros(shape),
                        (rew - drew) / (KE_MAX * etrs))
            fsb = minimum(fsa, ones(shape))
            fs1 = maximum(fsb, zeros(shape))
            ke = where(drew < rew, minimum((fs1 + (1 - fs1) * kr) * (kc_max - ks * kcb), few * kc_max),
                       zeros(shape))

            transp = (ks * kcb) * etrs
            et_init = (ks * kcb + ke) * etrs
            eta = maximum(et_init, zeros(shape))
            evap_init = ke * etrs
            evap_min = maximum(evap_init, zeros(shape))
            evap = minimum(evap_min, kc_max)

            # Load temp, find swe, melt, and precipitation, load Ksat
            # Use SNOTEL data for precip and temps:
            # df_snow : (stel_date, stel_snow, stel_precip, stel_tobs, stel_tmax, stel_tmin, stel_tavg, stel_snwd)

            snow_fall = where(mid_temp <= 0.0, ppt, zeros(shape))
            rain = where(mid_temp >= 0.0, ppt, zeros(shape))

            pA = a
            a = where(snow_fall > 3.0, ones(shape) * a_max, a)
            a = where(snow_fall <= 3.0, a_min + (pA - a_min) * exp(-0.12), a)
            a = where(snow_fall == 0.0, a_min + (pA - a_min) * exp(-0.05), a)
            a = where(a < a_min, a_min, a)

            swe += snow_fall

            mlt_init = maximum(((1 - a) * rg * 0.2) + (mid_temp - 1.8) * 11.0,
                               zeros(shape))  # use calibrate coefficients
            mlt = minimum(swe, mlt_init)

            swe -= mlt

            # Find depletions
            pDr = dr
            pDe = de
            pDrew = drew
            watr = rain + mlt
            deps = dr + de + drew

            ro = zeros(shape)
            ro = where(watr > ksat + deps, watr - ksat - deps, ro)
            ro = maximum(ro, zeros(shape))

            dp_r = zeros(shape)
            id1 = where(watr > deps, ones(shape), zeros(shape))
            id2 = where(ksat > watr - deps, ones(shape), zeros(shape))
            dp_r = where(id1 + id2 > 1.99, maximum(watr - deps, zeros(shape)), dp_r)

            dp_r = where(watr > ksat + deps, ksat, dp_r)
            dp_r = maximum(dp_r, zeros(shape))

            drew_1 = minimum((pDrew + ro + (evap - (rain + mlt))), rew)
            drew = maximum(drew_1, zeros(shape))
            diff = maximum(pDrew - drew, zeros(shape))

            de_1 = minimum((pDe + (evap - (rain + mlt - diff))), tew)
            de = maximum(de_1, zeros(shape))
            diff = maximum(((pDrew - drew) + (pDe - de)), zeros(shape))

            dr_1 = minimum((pDr + ((transp + dp_r) - (rain + mlt - diff))), taw)
            dr = maximum(dr_1, zeros(shape))

            # Create cumulative rasters to show net over entire run

            infil += dp_r
            infil = maximum(infil, zeros(shape))

            prev_et = et
            ref_et += etrs
            et = et + evap + transp
            et_ind = et / ref_et
            et = where(isnan(et) == True, prev_et, et)
            et = where(et > ref_et, ref_et / 2., et)
            et = maximum(et, ones(shape) * 0.001)

            precip = precip + rain + snow_fall
            precip = maximum(precip, zeros(shape))

            runoff += ro
            runoff = maximum(runoff, zeros(shape))

            snow_ras = swe + snow_fall - mlt
            snow_ras = maximum(snow_ras, zeros(shape))

            tot_snow += snow_fall

            mo_date = calendar.monthrange(year, month)
            if day == mo_date[1]:
                infil_mo = infil - p_mo_infil
                infil_mo = maximum(infil_mo, zeros(shape))

                ref_et_mo = etrs - p_mo_etrs
                et_mo = et - p_mo_et
                et_mo = where(isnan(et_mo) == True, p_mo_et, et_mo)
                et_mo = where(et_mo > ref_et, ref_et / 2., et_mo)
                et_mo = maximum(et_mo, ones(shape) * 0.001)

                precip_mo = precip - p_mo_precip
                precip_mo = maximum(precip_mo, zeros(shape))

                runoff_mo = ro - p_mo_ro
                runoff_mo = maximum(runoff_mo, zeros(shape))

                snow_ras_mo = swe
                snow_ras_mo = maximum(snow_ras_mo, zeros(shape))

                deps_mo = drew + de + dr
                delta_s_mo = p_mo_deps - deps_mo

                outputs = (('infil', infil_mo), ('et', et_mo), ('precip', precip_mo), ('runoff', runoff_mo),
                           ('snow_ras', snow_ras_mo), ('delta_s_mo', delta_s_mo), ('deps_mo', deps_mo))

                self.save_month_step(outputs, month, year)

                p_mo_et = et
                p_mo_precip = precip
                p_mo_ro = ro
                p_mo_deps = deps_mo
                p_mo_infil = infil
                p_mo_etrs = etrs

            if day == 31 and month == 12:
                infil_yr = infil - p_yr_infil
                infil_yr = maximum(infil_yr, zeros(shape))

                ref_et_yr = etrs - p_yr_etrs
                et_yr = et - p_yr_et
                et_yr = where(isnan(et_yr) == True, p_yr_et, et_yr)
                et_yr = where(et_yr > ref_et, ref_et / 2., et_yr)
                et_yr = maximum(et_yr, ones(shape) * 0.001)

                precip_yr = precip - p_yr_precip
                precip_yr = maximum(precip_yr, zeros(shape))

                runoff_yr = ro - p_yr_ro
                runoff_yr = maximum(runoff_yr, zeros(shape))

                snow_ras_yr = swe
                snow_ras_yr = maximum(snow_ras_yr, zeros(shape))

                deps_yr = drew + de + dr
                delta_s_yr = p_yr_deps - deps_yr

                outputs = (('infil', infil_yr), ('et', et_yr), ('precip', precip_yr), ('runoff', runoff_yr),
                           ('snow_ras', snow_ras_yr), ('delta_s_yr', delta_s_yr), ('deps_yr', deps_yr))

                p_yr_et = et
                p_yr_precip = precip
                p_yr_ro = ro
                p_yr_deps = deps_yr  # this was originally p_mo_deps = deps_yr im assuming this is a typo
                p_yr_infil = infil
                p_yr_etrs = etrs

                self.save_year_step(outputs, month, year)

            # Check MASS BALANCE for the love of WATER!!!
            mass = rain + mlt - (ro + transp + evap + dp_r + ((pDr - dr) + (pDe - de) + (pDrew - drew)))
            tot_mass += abs(mass)
            cum_mass += mass

            plt_day[i] = dday

            plt_rain[i] = rain[S, E]
            plt_eta[i] = eta[S, E]
            plt_snow_fall[i] = snow_fall[S, E]
            plt_ro[i] = ro[S, E]
            plt_dr[i] = dr[S, E]
            plt_de[i] = de[S, E]
            plt_drew[i] = drew[S, E]
            plt_temp[i] = mid_temp[S, E]
            plt_dp_r[i] = dp_r[S, E]
            plt_ks[i] = ks[S, E]
            plt_pdr[i] = pDr[S, E]
            plt_etrs[i] = etrs[S, E]
            plt_kcb[i] = kcb[S, E]
            plt_ppt[i] = ppt[S, E]
            plt_ke[i] = ke[S, E]
            plt_kr[i] = kr[S, E]
            plt_mlt[i] = mlt[S, E]
            plt_swe[i] = swe[S, E]
            plt_tempm[i] = max_temp[S, E]
            plt_fs1[i] = fs1[S, E]
            plt_mass[i] = mass[S, E]
Exemplo n.º 53
0
    def plot_map(self, dataset, attribute_data, min_value=None, max_value=None, file=None,
                 my_title="", filter=None, background=None):
        """    Plots a 2D image of attribute given by 'name'. matplotlib required.
               The dataset must have a method 'get_2d_attribute' defined that returns
               a 2D array that is to be plotted. If min_value/max_value are given, all values
               that are smaller/larger than these values are set to min_value/max_value.
               Argument background is a value to be used for background. If it is not given,
               it is considered as a 1/100 under the minimum value of the array.
               Filter is a 2D array. Points where filter is > 0 are masked out (put into background).
        """
        import matplotlib
        matplotlib.use('Qt4Agg') 
        
        from matplotlib.pylab import jet,imshow,colorbar,show,axis,savefig,close,figure,title,normalize
        from matplotlib.pylab import rot90
        
        attribute_data = attribute_data[filter]
        coord_2d_data = dataset.get_2d_attribute(attribute_data = attribute_data)
        data_mask = coord_2d_data.mask
#        if filter is not None:
#            if isinstance(filter, ndarray):
#                if not ma.allclose(filter.shape, coord_2d_data.shape):
#                    raise StandardError, "Argument filter must have the same shape as the 2d attribute."
#                filter_data = filter
#            else:
#                raise TypeError, "The filter type is invalid. A character string or a 2D numpy array allowed."
#            filter_data = where(ma.filled(filter_data,1) > 0, 1,0)
#            data_mask = ma.mask_or(data_mask, filter_data)
        nonmaskedmin = ma.minimum(coord_2d_data) - .2 * (ma.maximum(coord_2d_data) - ma.minimum(coord_2d_data))
        if max_value == None:
            max_value = ma.maximum(coord_2d_data)
        if min_value == None:
            min_value = nonmaskedmin

        coord_2d_data = ma.filled(coord_2d_data,min_value)
        if background is None:
            value_range = max_value-min_value
            background = min_value-value_range/100
        coord_2d_data = ma.filled(ma.masked_array(coord_2d_data, mask=data_mask), background)

        # Our data uses NW as 0,0, while matplotlib uses SW for 0,0.
        # Rotate the data so the map is oriented correctly.
        coord_2d_data = rot90(coord_2d_data, 1)

        jet()
        figure()
        norm = normalize(min_value, max_value)
        im = imshow(coord_2d_data,
            origin='lower',
            aspect='equal',
            interpolation=None,
            norm=norm,
            )
        
        tickfmt = '%4d'
        if isinstance(min_value, float) or isinstance(max_value, float):
            tickfmt='%1.4f'
        colorbar(format=tickfmt)

        title(my_title)
        axis('off')
        if file:
            savefig(file)
            close()
        else:
            show()
Exemplo n.º 54
0
 def autoscale(self, A):
     '''
     Set *vmin*, *vmax* to min, max of *A*.
     '''
     self.vmin = ma.minimum(A)
     self.vmax = ma.maximum(A)
Exemplo n.º 55
0
 def autoscale_None(self, A):
     ' autoscale only None-valued vmin or vmax'
     if self.vmin is None: self.vmin = ma.minimum(A)
     if self.vmax is None: self.vmax = ma.maximum(A)
Exemplo n.º 56
0
    def create(self,
               parent=None,
               min=None,
               max=None,
               save_file=None,
               thread_it=1,
               rate=None,
               bitrate=None,
               ffmpegoptions=''):
        from vcs import minmax
        from numpy.ma import maximum, minimum

        # Cannot "Run" or "Create" an animation while already creating an
        # animation
        if self.run_flg == 1:
            return
        if self.vcs_self.canvas.creating_animation() == 1:
            return

        if self.vcs_self.animate_info == []:
            str = "No data found!"
            showerror("Error Message to User", str)
            return

        # Stop the (thread) execution of the X main loop (if it is running).
        self.vcs_self.canvas.stopxmainloop()

        # Force VCS to update its orientation, needed when the user changes the
        # VCS Canvas size.
        self.vcs_self.canvas.updateorientation()

        # Make sure the animate information is up-to-date for creating images
        if ((self.gui_popup == 1) and (self.create_flg == 0)):
            self.update_animate_display_list()

        # Save the min and max values for the graphics methods.
        # Will need to restore values back when animation is done.
        self.save_original_min_max()

        # Set up the animation min and max values by changing the graphics method
        # Note: cannot set the min and max values if the default graphics
        # method is set.
        do_min_max = 'yes'
        try:
            if (parent is not None) and (parent.iso_spacing == 'Log'):
                do_min_max = 'no'
        except:
            pass

        # Draw specified continental outlines if needed.
        self.continents_hold_value = self.vcs_self.canvas.getcontinentstype()
        self.vcs_self.canvas.setcontinentstype(self.continents_value)

        if (do_min_max == 'yes'):
            minv = []
            maxv = []
            if (min is None) or (max is None):
                for i in range(len(self.vcs_self.animate_info)):
                    minv.append(1.0e77)
                    maxv.append(-1.0e77)
                for i in range(len(self.vcs_self.animate_info)):
                    dpy, slab = self.vcs_self.animate_info[i]
                    mins, maxs = minmax(slab)
                    minv[i] = float(minimum(float(minv[i]), float(mins)))
                    maxv[i] = float(maximum(float(maxv[i]), float(maxs)))
            if isinstance(min, list) or isinstance(max, list):
                for i in range(len(self.vcs_self.animate_info)):
                    try:
                        minv.append(min[i])
                    except:
                        minv.append(min[-1])
                    try:
                        maxv.append(max[i])
                    except:
                        maxv.append(max[-1])
            else:
                for i in range(len(self.vcs_self.animate_info)):
                    minv.append(min)
                    maxv.append(max)

            # Set the min an max for each plot in the page. If the same graphics method is used
            # to display the plots, then the last min and max setting of the
            # data set will be used.
            for i in range(len(self.vcs_self.animate_info)):
                try:
                    self.set_animation_min_max(minv[i], maxv[i], i)
                except:
                    # if it is default, then you cannot set the min and max, so
                    # pass.
                    pass

        if save_file is None or save_file.split('.')[-1].lower() == 'ras':
            if thread_it:
                thread.start_new_thread(self.vcs_self.canvas.animate_init,
                                        (save_file, ))
            else:
                self.vcs_self.canvas.animate_init(save_file)
        else:  # ffmpeg stuff
            save_info = self.vcs_self.animate_info
            animation_info = self.animate_info_from_python()
            slabs = []
            templates = []
            dpys = []
            for i in range(len(self.vcs_self.animate_info)):
                dpy, slab = self.vcs_self.animate_info[i]
                slabs.append(slab)
                dpys.append(dpy)
                templates.append(dpy.template)
            sh = slabs[0].shape
            if dpy.g_type in [
                    'boxfill',
                    'isofill',
                    'isoline',
                    'meshfill',
                    'outfill',
                    'outline',
                    'taylordiagram',
                    'vector',
            ]:
                r = len(sh) - 2
            else:
                r = len(sh) - 1
            # now create the list of all previous indices to plot
            indices = []
            for i in range(r):
                this = list(range(sh[i]))
                tmp = []
                if indices == []:
                    for k in this:
                        indices.append([
                            k,
                        ])
                else:
                    for j in range(len(indices)):
                        for k in this:
                            tmp2 = copy.copy(indices[j])
                            tmp2.append(k)
                            tmp.append(tmp2)
                    indices = tmp
            count = 1
            white_square = self.vcs_self.createfillarea()
            white_square.color = 240
            white_square.x = [0, 1, 1, 0]
            white_square.y = [0, 0, 1, 1]
            new_vcs = self.vcs_self
            if self.vcs_self.orientation() == 'portrait':
                new_vcs.portrait()
            # self.vcs_self.close()

            for index in indices:
                new_vcs.clear()
                new_vcs.plot(white_square, bg=1)
                for i in range(len(save_info)):
                    slab = slabs[i]
                    template = templates[i]
                    gtype = animation_info["gtype"][i].lower()
                    gname = animation_info["gname"][i]
                    gm = None  # for flake8 to be happy
                    exec("gm = new_vcs.get%s('%s')" % (gtype, gname))
                    for j in index:
                        slab = slab[j]
                    new_vcs.plot(slab, gm, new_vcs.gettemplate(template), bg=1)
                new_vcs.png("tmp_anim_%i" % count)
                count += 1
            new_vcs.ffmpeg(save_file,
                           "tmp_anim_%d.png",
                           bitrate=bitrate,
                           rate=rate,
                           options=ffmpegoptions)
            for i in range(count - 1):
                os.remove("tmp_anim_%i.png" % (i + 1))
            del (new_vcs)
        self.create_flg = 1