def set_scale(scale):
     selected_scale =min(self._scales_sorted, key=lambda x:abs(x-scale)) 
     tile_map = self.get_single_scale_map(selected_scale)
     
     indicator = np.zeros(image.shape[:2]+(1,),np.int32)
     indicator_map = tile_map.copy_map_for_image(indicator)
     
     mean_color = np.zeros(image.shape,np.float32)
     mean_color_map = tile_map.copy_map_for_image(mean_color)
     
     existing_color_map = tile_map.copy_map_for_image(image)
     
     for loc in xrange(len(tile_map)):
         key = tile_map.key_from_index(loc)
         indicator_map[key] = loc
         
         tile = existing_color_map[key]
         tile = ma.reshape(tile,(-1,image.shape[-1]))
         mean_color_map[key] = ma.mean(tile,axis=0)
         
     import skimage.segmentation
     seg = skimage.segmentation.mark_boundaries(image,indicator[:,:,0])            
     im_plot.set_data(seg)
     
     im_mean_image.set_data(mean_color)
        def set_scale(scale):
            selected_scale = min(self._scales_sorted,
                                 key=lambda x: abs(x - scale))
            tile_map = self.get_single_scale_map(selected_scale)

            indicator = np.zeros(image.shape[:2] + (1, ), np.int32)
            indicator_map = tile_map.copy_map_for_image(indicator)

            mean_color = np.zeros(image.shape, np.float32)
            mean_color_map = tile_map.copy_map_for_image(mean_color)

            existing_color_map = tile_map.copy_map_for_image(image)

            for loc in xrange(len(tile_map)):
                key = tile_map.key_from_index(loc)
                indicator_map[key] = loc

                tile = existing_color_map[key]
                tile = ma.reshape(tile, (-1, image.shape[-1]))
                mean_color_map[key] = ma.mean(tile, axis=0)

            import skimage.segmentation
            seg = skimage.segmentation.mark_boundaries(image, indicator[:, :,
                                                                        0])
            im_plot.set_data(seg)

            im_mean_image.set_data(mean_color)
    def finger_print_characteristic_collector(self):
        collected_characteristics_vector = self.collected_burst
        collected_labels = self.collected_labels

        for key in self.converted_characteristic.keys():

            self.converted_characteristic[key] = array(
                self.converted_characteristic[key])
            if size(self.converted_characteristic[key]) > 1:
                self.converted_characteristic[key] = reshape(
                    self.converted_characteristic[key],
                    [size(self.converted_characteristic[key], 0), 1])

            collected_characteristics_vector = row_stack(
                (collected_characteristics_vector,
                 self.converted_characteristic[key]))

            if size(collected_labels) == 0:
                collected_labels = {
                    self.output_label_index: self.output_labels[key]
                }
                self.output_label_index += 1

            else:
                collected_labels[
                    self.output_label_index] = self.output_labels[key]
                self.output_label_index += 1

        return collected_characteristics_vector, collected_labels
Exemplo n.º 4
0
def _pfromz_MA(z, lapse_rate, P_bott, T_bott, z_bott):
    """Pressure given altitude in a constant lapse rate layer.

    The dry gas constant is used in calculations requiring the gas
    constant.  See the docstring for press2alt for references.

    Input Arguments:
    * z:  Geopotential altitude [m].
    * lapse_rate:  -dT/dz [K/m] over the layer.
    * P_bott:  Pressure [hPa] at the base of the layer.
    * T_bott:  Temperature [K] at the base of the layer.
    * z_bott:  Geopotential altitude [m] of the base of the layer.

    Output:
    * Pressure [hPa] for each element given in the input arguments.

    All input arguments can be either a scalar or an MA array.  All 
    arguments that are MA arrays, however, are of the same size and 
    shape.  If every input argument is a scalar, the output is a scalar.  
    If any of the input arguments is an MA array, the output is an MA 
    array of the same size and shape.
    """
    #jfp was import Numeric as N
    import numpy as N
    #jfp was import MA
    import numpy.ma as MA
    from atmconst import AtmConst

    const = AtmConst()

    if MA.size(lapse_rate) == 1:
        #jfp was if MA.array(lapse_rate)[0] == 0.0:
        if MA.array(lapse_rate) == 0.0:
            return P_bott * \
                   MA.exp( -const.g / (const.R_d*T_bott) * (z-z_bott) )
        else:
            exponent = const.g / (const.R_d * lapse_rate)
            return P_bott * \
                   ( (1.0 - (lapse_rate * (z-z_bott) / T_bott))**exponent )
    else:
        exponent = const.g / (const.R_d * lapse_rate)
        P = P_bott * \
            ( (1.0 - (lapse_rate * (z-z_bott) / T_bott))**exponent )
        P_at_0 = P_bott * \
                 MA.exp( -const.g / (const.R_d*T_bott) * (z-z_bott) )

        zero_lapse_mask = MA.filled(MA.where(lapse_rate == 0., 1, 0), 0)
        zero_lapse_mask_indices_flat = N.nonzero(N.ravel(zero_lapse_mask))
        P_flat = MA.ravel(P)
        MA.put( P_flat, zero_lapse_mask_indices_flat \
              , MA.take(MA.ravel(P_at_0), zero_lapse_mask_indices_flat) )
        return MA.reshape(P_flat, P.shape)
Exemplo n.º 5
0
def _pfromz_MA(z, lapse_rate, P_bott, T_bott, z_bott):
    """Pressure given altitude in a constant lapse rate layer.

    The dry gas constant is used in calculations requiring the gas
    constant.  See the docstring for press2alt for references.

    Input Arguments:
    * z:  Geopotential altitude [m].
    * lapse_rate:  -dT/dz [K/m] over the layer.
    * P_bott:  Pressure [hPa] at the base of the layer.
    * T_bott:  Temperature [K] at the base of the layer.
    * z_bott:  Geopotential altitude [m] of the base of the layer.

    Output:
    * Pressure [hPa] for each element given in the input arguments.

    All input arguments can be either a scalar or an MA array.  All 
    arguments that are MA arrays, however, are of the same size and 
    shape.  If every input argument is a scalar, the output is a scalar.  
    If any of the input arguments is an MA array, the output is an MA 
    array of the same size and shape.
    """
    #jfp was import Numeric as N
    import numpy as N
    #jfp was import MA
    import numpy.ma as MA
    from atmconst import AtmConst

    const = AtmConst()

    if MA.size(lapse_rate) == 1:
        #jfp was if MA.array(lapse_rate)[0] == 0.0:
        if MA.array(lapse_rate) == 0.0:
            return P_bott * \
                   MA.exp( -const.g / (const.R_d*T_bott) * (z-z_bott) )
        else:
            exponent = const.g / (const.R_d * lapse_rate)
            return P_bott * \
                   ( (1.0 - (lapse_rate * (z-z_bott) / T_bott))**exponent )
    else:
        exponent = const.g / (const.R_d * lapse_rate)
        P = P_bott * \
            ( (1.0 - (lapse_rate * (z-z_bott) / T_bott))**exponent )
        P_at_0 = P_bott * \
                 MA.exp( -const.g / (const.R_d*T_bott) * (z-z_bott) )

        zero_lapse_mask = MA.filled(MA.where(lapse_rate == 0., 1, 0), 0)
        zero_lapse_mask_indices_flat = N.nonzero(N.ravel(zero_lapse_mask))
        P_flat = MA.ravel(P)
        MA.put( P_flat, zero_lapse_mask_indices_flat \
              , MA.take(MA.ravel(P_at_0), zero_lapse_mask_indices_flat) )
        return MA.reshape(P_flat, P.shape)
Exemplo n.º 6
0
def mean_annual_cycle(data):
    """
    Compute the mean annual cycle of variable.
    Assumes data is masked array with shape [nmonth,nlat,nlon].

    Output: array
    """
    ntime, nlat, nlon = data.shape
    # reshape from [nmonth,nlat,nlon] to [nyear,12,nlat,nlon]
    work = MA.reshape(data,(-1,12,nlat,nlon))
    # compute mean annual cycle
    mean_data = MA.average(work,0)
    return mean_data
Exemplo n.º 7
0
def _zfromp_MA(P, lapse_rate, P_bott, T_bott, z_bott):
    """Altitude given pressure in a constant lapse rate layer.

    The dry gas constant is used in calculations requiring the gas
    constant.  See the docstring for press2alt for references.

    Input Arguments:
    * P:  Pressure [hPa].
    * lapse_rate:  -dT/dz [K/m] over the layer.
    * P_bott:  Pressure [hPa] at the base of the layer.
    * T_bott:  Temperature [K] at the base of the layer.
    * z_bott:  Geopotential altitude [m] of the base of the layer.

    Output:
    * Altitude [m] for each element given in the input arguments.

    All input arguments can be either a scalar or an MA array.  All 
    arguments that are MA arrays, however, are of the same size and 
    shape.  If every input argument is a scalar, the output is a scalar.
    If any of the input arguments is an MA array, the output is an MA 
    array of the same size and shape.
    """
    import numpy as N
    #jfp was import Numeric as N
    import numpy.ma as MA
    #jfp was import MA
    from atmconst import AtmConst

    const = AtmConst()

    if MA.size(lapse_rate) == 1:
        if MA.array(lapse_rate)[0] == 0.0:
            return ( (-const.R_d * T_bott / const.g) * MA.log(P/P_bott) ) + \
                   z_bott
        else:
            exponent = (const.R_d * lapse_rate) / const.g
            return ((T_bott / lapse_rate) * (1. - (P/P_bott)**exponent)) + \
                   z_bott
    else:
        exponent = (const.R_d * lapse_rate) / const.g
        z = ((T_bott / lapse_rate) * (1. - (P / P_bott)**exponent)) + z_bott
        z_at_0 = ( (-const.R_d * T_bott / const.g) * MA.log(P/P_bott) ) + \
                 z_bott

        zero_lapse_mask = MA.filled(MA.where(lapse_rate == 0., 1, 0), 0)
        zero_lapse_mask_indices_flat = N.nonzero(N.ravel(zero_lapse_mask))
        z_flat = MA.ravel(z)
        MA.put( z_flat, zero_lapse_mask_indices_flat \
              , MA.take(MA.ravel(z_at_0), zero_lapse_mask_indices_flat) )
        return MA.reshape(z_flat, z.shape)
Exemplo n.º 8
0
def _zfromp_MA(P, lapse_rate, P_bott, T_bott, z_bott):
    """Altitude given pressure in a constant lapse rate layer.

    The dry gas constant is used in calculations requiring the gas
    constant.  See the docstring for press2alt for references.

    Input Arguments:
    * P:  Pressure [hPa].
    * lapse_rate:  -dT/dz [K/m] over the layer.
    * P_bott:  Pressure [hPa] at the base of the layer.
    * T_bott:  Temperature [K] at the base of the layer.
    * z_bott:  Geopotential altitude [m] of the base of the layer.

    Output:
    * Altitude [m] for each element given in the input arguments.

    All input arguments can be either a scalar or an MA array.  All 
    arguments that are MA arrays, however, are of the same size and 
    shape.  If every input argument is a scalar, the output is a scalar.
    If any of the input arguments is an MA array, the output is an MA 
    array of the same size and shape.
    """
    import numpy as N
    #jfp was import Numeric as N
    import numpy.ma as MA
    #jfp was import MA
    from atmconst import AtmConst

    const = AtmConst()

    if MA.size(lapse_rate) == 1:
        if MA.array(lapse_rate)[0] == 0.0:
            return ( (-const.R_d * T_bott / const.g) * MA.log(P/P_bott) ) + \
                   z_bott
        else:
            exponent = (const.R_d * lapse_rate) / const.g
            return ((T_bott / lapse_rate) * (1. - (P/P_bott)**exponent)) + \
                   z_bott
    else:
        exponent = (const.R_d * lapse_rate) / const.g
        z = ((T_bott / lapse_rate) * (1. - (P/P_bott)**exponent)) + z_bott
        z_at_0 = ( (-const.R_d * T_bott / const.g) * MA.log(P/P_bott) ) + \
                 z_bott

        zero_lapse_mask = MA.filled(MA.where(lapse_rate == 0., 1, 0), 0)
        zero_lapse_mask_indices_flat = N.nonzero(N.ravel(zero_lapse_mask))
        z_flat = MA.ravel(z)
        MA.put( z_flat, zero_lapse_mask_indices_flat \
              , MA.take(MA.ravel(z_at_0), zero_lapse_mask_indices_flat) )
        return MA.reshape(z_flat, z.shape)
    def postProcessor_burst_collector(self):
        # saving the whole burst in the data-bank
        self.collected_burst = self.collected_burst[1:]
        self.collected_burst = reshape(self.collected_burst,
                                       size(self.collected_burst), 1)
        if size(self.data_collection) == 0:
            self.data_collection = self.collected_burst
        else:
            self.data_collection = column_stack(
                (self.data_collection, self.collected_burst))

        self.label_vector = column_stack(
            (self.label_vector, self.device_label))
        self.collected_burst = 0
    def finger_print_burst_collector(self):
        # saving the whole burst in the data-bank
        self.collected_burst = self.collected_burst[1:]
        self.collected_burst = reshape(self.collected_burst,
                                       [size(self.collected_burst, 0), 1])
        if size(self.data_collection) == 0:
            self.data_collection = self.collected_burst
        else:
            self.data_collection = column_stack(
                (self.data_collection, self.collected_burst))

        self.label_vector = column_stack(
            (self.label_vector, self.device_label))
        self.collected_burst = 0
Exemplo n.º 11
0
def make_P(ps, A, B, P0):
    '''
    # Author Charles Doutriaux
    # Version 1.0
    # email: [email protected]
    # Step 1 of conversion of a field from sigma levels to pressure levels
    # Create the Pressure field on sigma levels, from the surface pressure
    
    # Input
    # Ps   : Surface pressure
    # A,B,Po: Coefficients, such as: p=B.ps+A.Po
    # Ps is 2D (lonxlat)
    # B,A are 1D (vertical sigma levels)

    # Output
    # Pressure field from TOP (level 0) to BOTTOM (last level)
    # 3D field (lon/lat/sigma)

    # External : Numeric
    
    # Compute the pressure for the sigma levels'''
    import numpy.ma as MA
    p = MA.outerproduct(B, ps)
    dim = B.shape[0], ps.shape[0], ps.shape[1]
    p = MA.reshape(p, dim)
    ##     p=ps.filled()[Numeric.NewAxis,...]*B.filled()[:,Numeric.NewAxis,Numeric.NewAxis]
    ##     Po=P0*MA.ones(p.shape,Numeric.Float)
    A = MA.outerproduct(A, P0 * MA.ones(p.shape[1:]))
    A = MA.reshape(A, p.shape)
    p = p + A
    # Now checking to make sure we return P[0] as the top
    a = MA.average(MA.average(p[0] - p[-1], axis=0))
    if a > 0:
        # We got the wrong order !
        p = p[::-1]
    return p
Exemplo n.º 12
0
def make_P(ps,A,B,P0):
    '''
    # Author Charles Doutriaux
    # Version 1.0
    # email: [email protected]
    # Step 1 of conversion of a field from sigma levels to pressure levels
    # Create the Pressure field on sigma levels, from the surface pressure
    
    # Input
    # Ps   : Surface pressure
    # A,B,Po: Coefficients, such as: p=B.ps+A.Po
    # Ps is 2D (lonxlat)
    # B,A are 1D (vertical sigma levels)

    # Output
    # Pressure field from TOP (level 0) to BOTTOM (last level)
    # 3D field (lon/lat/sigma)

    # External : Numeric
    
    # Compute the pressure for the sigma levels'''
    import numpy.ma as MA
    p=MA.outerproduct(B,ps)
    dim=B.shape[0],ps.shape[0],ps.shape[1]
    p=MA.reshape(p,dim)
##     p=ps.filled()[Numeric.NewAxis,...]*B.filled()[:,Numeric.NewAxis,Numeric.NewAxis]
##     Po=P0*MA.ones(p.shape,Numeric.Float)
    A=MA.outerproduct(A,P0*MA.ones(p.shape[1:]))
    A=MA.reshape(A,p.shape)
    p=p+A
    # Now checking to make sure we return P[0] as the top
    a=MA.average(MA.average(p[0]-p[-1], axis=0))
    if a>0:
        # We got the wrong order !
        p=p[::-1]
    return p
Exemplo n.º 13
0
def monthly_anom(data):
    """
    Compute monthly anomalies from mean annual cycle.
    Assumes data is masked array with shape [nmonth,nlat,nlon]

    Output: array
    """
    ntime, nlat, nlon = data.shape
    # reshape from [nmonth,nlat,nlon] to [nyear,nmonth,nlat,nlon]
    work = MA.reshape(data,(-1,12,nlat,nlon))
    # compute mean annual cycle
    mean_work = MA.average(work,0)
    # compute anomalies from mean annual cycle
    #anom = MA.reshape(work-mean_work[N.newaxis,...],(-1,nlat,nlon))
    anom = work-mean_work[N.newaxis,...]
    return anom
Exemplo n.º 14
0
    mpidx       = fref.variables['mp'].long_name.split(', ').index('true')

    var = 'yield_' + crop
    if var in fref.variables:
        yield_ref = fref.variables[var][:, :, dtidx, mpidx]
    else:
        print 'Crop %s unavailable in reference file %s. Exiting . . .' % (crop, reffile)
        sys.exit()

with nc(infile) as fin: # pull input data
    ain       = fin.variables[agglvl][:]
    tin       = fin.variables['time'][:]
    tin_units = fin.variables['time'].units
    sum_idx   = fin.variables['irr'].long_name.split(', ').index('sum')
    yield_in = fin.variables['yield_' + crop + '_' + agglvl][:, :, sum_idx]
    yield_in = reshape(yield_in, (len(tin), len(ain), 1))

tref += int(findall(r'\d+', tref_units)[0]) # get reference time
tin  += int(findall(r'\d+', tin_units)[0])  # get simulation time

aggs = intersect1d(ain, aref) # find common gadm indices
naggs, ntime, nscen = len(aggs), len(tin), len(scen)
if not naggs: raise Exception('No common aggregates')

yield_sim_common = masked_array(zeros((naggs, len(tin), nscen)), mask = ones((naggs, len(tin), nscen)))
yield_ref_common = masked_array(zeros((naggs, len(tref))), mask = ones((naggs, len(tref))))
for i in range(naggs):
    yield_sim_common[i] = yield_in[:, list(ain).index(aggs[i])]
    yield_ref_common[i] = yield_ref[list(aref).index(aggs[i])]

sh = (naggs, ntime, nscen, ndt, nmp, ncr)
Exemplo n.º 15
0
medianarr[0] = median(p1, axis = 0)
maxarr[0]    = p1.max(axis = 0)
minarr[0]    = p1.min(axis = 0)
bp1 = ax.boxplot(p1, positions = range(1, 4 * nd, 4))
bps[0] = bp1

# hadgem co2
p2           = wvarr[:, hadgemidx, :, 0]
medianarr[1] = median(p2, axis = 0)
maxarr[1]    = p2.max(axis = 0)
minarr[1]    = p2.min(axis = 0)
bp2 = ax.boxplot(p2, positions = range(2, 4 * nd, 4))
bps[1] = bp2

# all co2
p3           = reshape(wvarr[:, :, :, 0], (nm * ng, nd))
medianarr[2] = median(p3, axis = 0)
maxarr[2]    = p3.max(axis = 0)
minarr[2]    = p3.min(axis = 0)
bp3 = ax.boxplot(p3, positions = range(3, 4 * nd, 4))
bps[2] = bp3

colors = ['r', 'y', 'b']

# change colors
for i in range(len(bps)):
    for j in range(len(bps[i]['boxes'])):
        box = bps[i]['boxes'][j]
        boxx, boxy = box.get_xdata(), box.get_ydata()
        boxcoords = zip(boxx, boxy)
        boxpolygon = Polygon(boxcoords, facecolor = colors[i])
Exemplo n.º 16
0
dy85m = median(dy85arr[:, hadgemidx, :, 1], axis=0)
negy = dy85m < -0.01
barr[0, negy] = 100 * (1 - dy26m[negy] / dy85m[negy])
posy = dy85m > 0.01
larr[0, posy] = 100 * (dy26m[posy] / dy85m[posy] - 1)

# hadgem co2
dy26m = median(dy26arr[:, hadgemidx, :, 0], axis=0)
dy85m = median(dy85arr[:, hadgemidx, :, 0], axis=0)
negy = dy85m < -0.01
barr[1, negy] = 100 * (1 - dy26m[negy] / dy85m[negy])
posy = dy85m > 0.01
larr[1, posy] = 100 * (dy26m[posy] / dy85m[posy] - 1)

# all co2
dy26m = median(reshape(dy26arr[:, :, :, 0], (nm * ng, nfpu)), axis=0)
dy85m = median(reshape(dy85arr[:, :, :, 0], (nm * ng, nfpu)), axis=0)
negy = dy85m < -0.01
barr[2, negy] = 100 * (1 - dy26m[negy] / dy85m[negy])
posy = dy85m > 0.01
larr[2, posy] = 100 * (dy26m[posy] / dy85m[posy] - 1)

filename, ext = splitext(mapfile)
mapfiles = [filename + ".noco2" + ext, filename + ".co2.hadgem" + ext, filename + ".co2" + ext]
filename, ext = splitext(ncfile)
ncfiles = [filename + ".noco2" + ext, filename + ".co2.hadgem" + ext, filename + ".co2" + ext]

for i in range(len(barr)):
    # rasterize
    bmap = masked_array(zeros((nlats, nlons)), mask=ones((nlats, nlons)))
    lmap = masked_array(zeros((nlats, nlons)), mask=ones((nlats, nlons)))
Exemplo n.º 17
0
weights = masked_where(dyarr.mask, weights)  # mask
areas = masked_where(dyarr.mask, areas)

# sum over crops and average over decades
dyarr = (dyarr * weights * areas).mean(axis=3).sum(axis=2) / 1e6  # Gcal -> Pcal

dymarr = masked_array(zeros((3, nfpu)), mask=ones((3, nfpu)))

# hadgem noco2
dymarr[0] = median(dyarr[:, hadgemidx, :, 1], axis=0)

# hadgem co2
dymarr[1] = median(dyarr[:, hadgemidx, :, 0], axis=0)

# all co2
dymarr[2] = median(reshape(dyarr[:, :, :, 0], (nm * ng, nfpu)), axis=0)

filename, ext = splitext(mapfile)
mapfiles = [filename + ".noco2" + ext, filename + ".co2.hadgem" + ext, filename + ".co2" + ext]
filename, ext = splitext(ncfile)
ncfiles = [filename + ".noco2" + ext, filename + ".co2.hadgem" + ext, filename + ".co2" + ext]

for i in range(len(dymarr)):
    # rasterize
    dymap = masked_array(zeros((nlats, nlons)), mask=ones((nlats, nlons)))
    for j in range(len(validfpus)):
        fpuidx = where(fpu == validfpus[j])[0][0]
        dymap[fpumap == validfpus[j]] = dymarr[i, fpuidx]

    # plot map and fpu boundaries
    plt.figure()
Exemplo n.º 18
0
dy85m = median(dy85arr[:, hadgemidx, :, 1], axis=0)
negy = dy85m < -0.01
barr[0, negy] = 100 * (1 - dy26m[negy] / dy85m[negy])
posy = dy85m > 0.01
larr[0, posy] = 100 * (dy26m[posy] / dy85m[posy] - 1)

# hadgem co2
dy26m = median(dy26arr[:, hadgemidx, :, 0], axis=0)
dy85m = median(dy85arr[:, hadgemidx, :, 0], axis=0)
negy = dy85m < -0.01
barr[1, negy] = 100 * (1 - dy26m[negy] / dy85m[negy])
posy = dy85m > 0.01
larr[1, posy] = 100 * (dy26m[posy] / dy85m[posy] - 1)

# all co2
dy26m = median(reshape(dy26arr[:, :, :, 0], (nm * ng, nfpu)), axis=0)
dy85m = median(reshape(dy85arr[:, :, :, 0], (nm * ng, nfpu)), axis=0)
negy = dy85m < -0.01
barr[2, negy] = 100 * (1 - dy26m[negy] / dy85m[negy])
posy = dy85m > 0.01
larr[2, posy] = 100 * (dy26m[posy] / dy85m[posy] - 1)

filename, ext = splitext(mapfile)
mapfiles = [
    filename + '.noco2' + ext, filename + '.co2.hadgem' + ext,
    filename + '.co2' + ext
]
filename, ext = splitext(ncfile)
ncfiles = [
    filename + '.noco2' + ext, filename + '.co2.hadgem' + ext,
    filename + '.co2' + ext
Exemplo n.º 19
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 as N
    import numpy.ma as MA
    #jfp was import MA
    #jfp was import Numeric 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:
        #jfp was P0_use = MA.zeros(P_or_z.shape, typecode=MA.Float) \
        P0_use = MA.zeros(P_or_z.shape) \
               + (const.sea_level_press / 100.)
    else:
        #jfp was P0_use = MA.zeros(P_or_z.shape, typecode=MA.Float) \
        P0_use = MA.zeros(P_or_z.shape) \
               + MA.masked_array(P0)

    if T0 == None:
        #jfp was T0_use = MA.zeros(P_or_z.shape, typecode=MA.Float) \
        T0_use = MA.zeros(P_or_z.shape) \
               + const.sea_level_temp
    else:
        #jfp was T0_use = MA.zeros(P_or_z.shape, typecode=MA.Float) \
        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 = 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)
    P_or_z_flat_mask = P_or_z_flat.mask
    if P_or_z_flat.mask==False:
        P_or_z_flat_mask = MA.make_mask_none(P_or_z_flat.shape)
    #jfp was:
    #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)
    #jfp was dTdh_actual       = MA.zeros(P_or_z_flat.shape, typecode=MA.Float)
    #jfp was P_bott_actual     = MA.zeros(P_or_z_flat.shape, typecode=MA.Float)
    #jfp was T_bott_actual     = MA.zeros(P_or_z_flat.shape, typecode=MA.Float)
    #jfp was z_bott_actual     = MA.zeros(P_or_z_flat.shape, typecode=MA.Float)
    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 )
Exemplo n.º 20
0
                           '_global'][:, :, 0, 0, 0,
                                      0]  # global, time, scen, dt, mp, cr

with nc(rcp85file) as f:
    fpu85 = f.variables[variable + '_fpu'][:, :, 0, 0, 0, 0]
    global85 = f.variables[variable + '_global'][:, :, 0, 0, 0, 0]

nt, nf, ng = len(time), len(afpu), len(aglobal)

tidx1, tidx2 = where(time == 1980)[0][0], where(time == 2009)[0][0] + 1

# number of decades
nd = nt / 10

# delta yield
dyfpu26 = reshape(fpu26, (nf, nd, 10)).mean(axis=2) - resize(
    fpu26[:, tidx1:tidx2].mean(axis=1), (nd, nf)).T
dyfpu85 = reshape(fpu85, (nf, nd, 10)).mean(axis=2) - resize(
    fpu85[:, tidx1:tidx2].mean(axis=1), (nd, nf)).T

# absolute global yield
global26 = reshape(global26, (ng, nd, 10)).mean(axis=2)
global85 = reshape(global85, (ng, nd, 10)).mean(axis=2)

with nc(outfile, 'w') as f:
    f.createDimension('fpu', nf)
    fpuvar = f.createVariable('fpu', 'i4', 'fpu')
    fpuvar[:] = afpu
    fpuvar.units = funits
    fpuvar.long_name = flname
Exemplo n.º 21
0
def extend_for_map_server(lons, data, fill_value):
    '''
    (out_lons, out_data_ = extend_for_map_server(lons,data,fill_value)

    This function checks for data grid cells that go over the 180 meridian. For
    example, suppose the first longitude is -180 and the longitude resolution is
    1 degree. This means the data for the first longitude extends from
    -180.5 -- -179.5. This code will take that data and put it in the result
    array at +180 degrees as well.
    '''

    # figure out if we actually need to do anything.
    resolution = get_resolution(lons)

    west_edge = get_canonical_longitude(lons - resolution / 2)
    east_edge = get_canonical_longitude(lons + resolution / 2)

    # see which longitude grid cells cross the 180 meridian
    inds = [
        i for i in range(len(lons)) if west_edge[i] > 0 and east_edge[i] < 0
    ]

    # See if this is just a rounding error by calculating what fraction
    # of the grid cell is on either side of the 180 meridian. For example,
    # suppose the grid cell extends from 179.999 to -179.001. This would mean
    # that the cell is 0.001 of the cell is west of the 180 meridian and
    # 0.999 of the cell is east of the 180 meridian. This is clearly a
    # rounding error.
    smallest_fraction = []
    for ind in inds:
        fraction_west = (180.0 - west_edge[ind]) / resolution
        fraction_east = (east_edge[ind] + 180.0) / resolution
        smallest_fraction.append(
            fraction_west if fraction_west < fraction_east else fraction_east)

    # Only keep indexes where at least 10% of the cell goes over the 180
    # meridian. Why 10%? Well, we currently only have cells that should align
    # perfectly with the 180 meridian (0% overlap) and cells for MERRA that
    # are exactly 50% on either side of the 180 meridian. So 10% seems like a
    # reasonable threshold.
    inds = [inds[i] for i in range(len(inds)) if smallest_fraction[i] > 0.1]

    if len(inds) == 0:
        # Yay! Nothing to do
        return (get_canonical_longitude(lons), data)
    elif len(inds) != 1:
        # This does not make sense...
        raise LonError(
            "Found more than one longitude grid cell extending over the 180 meridian."
        )

    # put the data center points on a [-180,180) grid.
    (new_lons, new_data) = normalize(lons, data, fill_value)

    # now the data spanning the 180 meridian should be the first or the last
    # longitude.
    num_lat = new_data.shape[0]
    if new_lons[0] - resolution / 2 <= -180:
        # It's the first longitude. So copy the data over to the end.

        # Create a column matrix of the first column of data
        column = ma.reshape(new_data[:, 0], (num_lat, 1))
        # concatenate them together
        new_data = ma.concatenate([new_data, column], 1)

        # Update the longitudes
        new_lons = np.append(new_lons, new_lons[0] + 360)

    else:
        # It's the last longitude. So copy the data from the end over to the
        # beginning.

        # Create a column matrix of the last column of data
        column = ma.reshape(new_data[:, -1], (num_lat, 1))
        # concatenate them together
        new_data = ma.concatenate([column, new_data], 1)

        # Update the longitudes
        new_lons = np.append(new_lons[-1] - 360, new_lons)

    return (new_lons, new_data)
Exemplo n.º 22
0
for tm in tim[:]:  # loop over time
    T = v(time=(tm))
    T = MA.average(T, axis=0)
    Ps = fps.getslab(varps, tm, tm)
    Ps = MA.average(Ps, axis=0)
    # create the pressure field
    #   print 'Creating Pressure field'
    P = make_P(Ps, A, B, Po)
    #   print 'Shapes,T,Ps,P',T.shape,Ps.shape,P.shape

    # interpolate
    #   print 'Interpolating now !'
    out = log_linear_vinterp(T, P, levels)
    sh = list(out.shape)
    sh.insert(0, 1)
    out = MA.reshape(out, tuple(sh))
    #tmp=MA.asarray(tmp,Float16)
    t = tim.subAxis(itim, itim + 1)
    xx = reltime(tim[itim], tim.units)
    t_new = xx.torel('days since 1800').value
    t[0] = t_new
    t.units = 'days since 1800'
    meta[0][0] = t
    #   print meta[0][0][:]
    levelsax = cdms.createAxis(levels / 100.)
    levelsax.id = 'plev'
    levelsax.units = 'hPa'
    levelsax.designateLevel()
    meta[0][1] = levelsax
    out = putMetadata(meta, out)
    out.id = varout
Exemplo n.º 23
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)
Exemplo n.º 24
0
for tm in tim[:]: # loop over time
    T=v(time=(tm))
    T=MA.average(T, axis=0)
    Ps=fps.getslab(varps,tm,tm)
    Ps=MA.average(Ps, axis=0)
    # create the pressure field
#   print 'Creating Pressure field'
    P=make_P(Ps,A,B,Po)
#   print 'Shapes,T,Ps,P',T.shape,Ps.shape,P.shape
    
    # interpolate
#   print 'Interpolating now !'
    out=log_linear_vinterp(T,P,levels)
    sh=list(out.shape)
    sh.insert(0,1)
    out=MA.reshape(out,tuple(sh))
    #tmp=MA.asarray(tmp,Float16)
    t=tim.subAxis(itim,itim+1)
    xx=reltime(tim[itim],tim.units)
    t_new=xx.torel('days since 1800').value
    t[0]=t_new
    t.units='days since 1800'
    meta[0][0]=t
#   print meta[0][0][:]
    levelsax=cdms.createAxis(levels/100.)
    levelsax.id='plev'
    levelsax.units='hPa'
    levelsax.designateLevel()
    meta[0][1]=levelsax
    out=putMetadata(meta,out)
    out.id=varout
Exemplo n.º 25
0
marr=MA.masked_array(data=np.arange(10),fill_value=-999)

marr[3]=MA.masked

marr

#%% Check another masekd array

narr=MA.masked_less(marr,7)

narr

narr.fill_value

type(narr.filled())


#%% Another masked array

m1=MA.masked_array(range(1,9))

m2=MA.reshape(m1,(2,4))

m3=MA.masked_greater(m2,6)

m3=m3*100

#A masked array opperated with a nparray continues to be a masked array
type(m3-np.ones((2,4)))

Exemplo n.º 26
0
 x_grid=np.swapaxes(np.swapaxes(x_grid,0,2),0,1)
 #
 #adjust length of time series
 block_num_samp = x_grid.shape[-1]
 #
 if model_data and False:
    x_grid2=np.ones(x_grid.shape)
    dum=x_grid[:,:,0];
    mask=np.zeros(dum.shape); mask[np.where(np.isfinite(dum))]=0
    dum=ma.masked_array(dum,mask)
    Lon_vector2=Lon_vector.copy()
    Lon_vector2[np.where(Lon_vector>180)]=Lon_vector2[np.where(Lon_vector>180)]-360
    lon2,lat2=np.meshgrid(Lon_vector2,Lat_vector)
    dum,weights_out=mutils.smooth2D_parallel(lon2,lat2,dum,n=4,num_cores=15,use_weights=True,weights_only=True,use_median=False,save_weights=False,save_path='')
    jind,iind=ma.where(mask);
    x_grid2=ma.sum(ma.reshape(x_grid,(x_grid.shape[0]*x_grid.shape[1],x_grid[3]))[list(t_inds2),:]*weights_out[:,:,0],0) #ma.sum(x_grid.ravel()[list(t_inds2)]*weights_out[:,:,0],-1)
 #
 if time_ave and dt!=1:
    x_grid=utils.timeMean(x_grid.T,year0=year0,xtype=xtype,dt=dt)
    x_grid=x_grid.T
    jinds,iinds=ma.where(~np.isnan(np.sum(x_grid,-1)))
    x_grid[jinds,iinds,:]=detrend(x_grid[jinds,iinds,:],axis=-1,type='linear')
    block_num_samp = x_grid.shape[-1] #this needs to be calculated again
 #INVERT!
 if timeseries_sensitivity:
   #tts=365*7
   #for tt,tts in enumerate(np.array([365,365*2,365*3,365*5,365*10,365*20])):
   for tt in range(tt0,tt0+U_global.shape[0]): #if done in multiple steps i.e 2 and 1 years
   #for tt in range(x_grid.shape[-1]/tts):
       print(tt*tts/365,(tt*tts+tts)/365)
       if tt*tts+tts>x_grid.shape[-1]:
Exemplo n.º 27
0
    var = 'yield_' + crop
    if var in fref.variables:
        yield_ref = fref.variables[var][:, :, dtidx, mpidx]
    else:
        print 'Crop %s unavailable in reference file %s. Exiting . . .' % (
            crop, reffile)
        sys.exit()

with nc(infile) as fin:  # pull input data
    ain = fin.variables[agglvl][:]
    tin = fin.variables['time'][:]
    tin_units = fin.variables['time'].units
    sum_idx = fin.variables['irr'].long_name.split(', ').index('sum')
    yield_in = fin.variables['yield_' + crop + '_' + agglvl][:, :, sum_idx]
    yield_in = reshape(yield_in, (len(tin), len(ain), 1))

tref += int(findall(r'\d+', tref_units)[0])  # get reference time
tin += int(findall(r'\d+', tin_units)[0])  # get simulation time

aggs = intersect1d(ain, aref)  # find common gadm indices
naggs, ntime, nscen = len(aggs), len(tin), len(scen)
if not naggs: raise Exception('No common aggregates')

yield_sim_common = masked_array(zeros((naggs, len(tin), nscen)),
                                mask=ones((naggs, len(tin), nscen)))
yield_ref_common = masked_array(zeros((naggs, len(tref))),
                                mask=ones((naggs, len(tref))))
for i in range(naggs):
    yield_sim_common[i] = yield_in[:, list(ain).index(aggs[i])]
    yield_ref_common[i] = yield_ref[list(aref).index(aggs[i])]
if apply_weights:
    for n in n_weights:
        print(n)
        if n < 6:
            n_cores = 12
        elif n < 10:
            n_cores = 8
        elif n < 14:
            n_cores = 4
        else:
            n_cores = 4
        #
        d2 = np.load(wpath + str(n) + '_degree_smoothing_weights_coslat_y' +
                     str(n) + '_x' + str(2 * n) + '.npz')
        t_inds = ma.reshape(np.arange(sst.ravel().shape[0]),
                            (sst.shape[0], sst.shape[1]))
        #
        folder1 = tempfile.mkdtemp()
        path1 = os.path.join(folder1, 'weights_out.mmap')
        path2 = os.path.join(folder1, 't_inds2.mmap')
        #
        weights_out = np.memmap(path1,
                                dtype=float,
                                shape=d2['weights_out'].shape,
                                mode='w+')
        t_inds2 = np.memmap(path2,
                            dtype=int,
                            shape=d2['weights_out'].shape[:2],
                            mode='w+')
        #
        weights_out[:] = d2['weights_out'][:]
Exemplo n.º 29
0
weights = masked_where(dyarr.mask, weights) # mask
areas   = masked_where(dyarr.mask, areas)

# average over crops and decades
dyarr = (dyarr * weights * areas).sum(axis = 3).sum(axis = 2) / areas.sum(axis = 3).sum(axis = 2)

carr = masked_array(zeros((3, nfpu)), mask = ones((3, nfpu)))

# hadgem noco2
carr[0] = (dyarr[:, hadgemidx, :, 1] < 0).sum(axis = 0) * 100. / nm # convert to percent

# hadgem co2
carr[1] = (dyarr[:, hadgemidx, :, 0] < 0).sum(axis = 0) * 100. / nm

# all co2
carr[2] = (reshape(dyarr[:, :, :, 0], (nm * ng, nfpu)) < 0).sum(axis = 0) * 100. / (nm * ng)

filename, ext = splitext(mapfile)
mapfiles = [filename + '.noco2' + ext, filename + '.co2.hadgem' + ext, filename + '.co2' + ext]
filename, ext = splitext(ncfile)
ncfiles = [filename + '.noco2' + ext, filename + '.co2.hadgem' + ext, filename + '.co2' + ext]

for i in range(len(carr)):
    # rasterize
    cmap = masked_array(zeros((nlats, nlons)), mask = ones((nlats, nlons)))
    for j in range(len(validfpus)):
        fpuidx = where(fpu == validfpus[j])[0][0]
        cmap[fpumap == validfpus[j]] = carr[i, fpuidx]

    # plot map and fpu boundaries
    plt.figure()
Exemplo n.º 30
0
        eigen_dim = original.shape[1]
        padding = (-len(original)) % size
        # padding = int(original.shape[0] / (size-1)) - (original.shape[0] % (size-1))

        eigenmodes = ma.zeros((original.shape[0] + padding, eigen_dim))
        eigenmodes[:-padding, :] = original

        print(eigenmodes.shape, padding, original.shape)

        send_buf = ma.empty(
            [size, int(eigenmodes.shape[0] / size), eigen_dim],
            dtype=np.float64)

        send_buf[:, :, :] = ma.reshape(
            eigenmodes,
            [size, int(eigenmodes.shape[0] / size), eigen_dim])

        buf1_size = np.empty(2, dtype=np.int32)
        buf1_size[:] = np.array([int(len(eigenmodes) / size), eigen_dim])
        print('preparing parameters took {}, with size {}'.format(
            time.time() - t0, send_buf.shape))
    else:
        buf1_size = np.empty(2, dtype=np.int32)

    sys.stdout.flush()
    comm.Barrier()
    comm.Bcast(buf1_size, root=0)
    recv_buf = ma.empty([buf1_size[0], buf1_size[1]], dtype=np.float64)

    comm.Scatter(send_buf, recv_buf, root=0)
Exemplo n.º 31
0
medianarr[0] = median(p1, axis=0)
maxarr[0] = p1.max(axis=0)
minarr[0] = p1.min(axis=0)
bp1 = ax.boxplot(p1, positions=range(1, 4 * nd, 4))
bps[0] = bp1

# hadgem co2
p2 = wvarr[:, hadgemidx, :, 0]
medianarr[1] = median(p2, axis=0)
maxarr[1] = p2.max(axis=0)
minarr[1] = p2.min(axis=0)
bp2 = ax.boxplot(p2, positions=range(2, 4 * nd, 4))
bps[1] = bp2

# all co2
p3 = reshape(wvarr[:, :, :, 0], (nm * ng, nd))
medianarr[2] = median(p3, axis=0)
maxarr[2] = p3.max(axis=0)
minarr[2] = p3.min(axis=0)
bp3 = ax.boxplot(p3, positions=range(3, 4 * nd, 4))
bps[2] = bp3

colors = ["r", "y", "b"]

# change colors
for i in range(len(bps)):
    for j in range(len(bps[i]["boxes"])):
        box = bps[i]["boxes"][j]
        boxx, boxy = box.get_xdata(), box.get_ydata()
        boxcoords = zip(boxx, boxy)
        boxpolygon = Polygon(boxcoords, facecolor=colors[i])
Exemplo n.º 32
0
areas = masked_where(dyarr.mask, areas)

# sum over crops and average over decades
dyarr = (dyarr * weights *
         areas).mean(axis=3).sum(axis=2) / 1e6  # Gcal -> Pcal

dymarr = masked_array(zeros((3, nfpu)), mask=ones((3, nfpu)))

# hadgem noco2
dymarr[0] = median(dyarr[:, hadgemidx, :, 1], axis=0)

# hadgem co2
dymarr[1] = median(dyarr[:, hadgemidx, :, 0], axis=0)

# all co2
dymarr[2] = median(reshape(dyarr[:, :, :, 0], (nm * ng, nfpu)), axis=0)

filename, ext = splitext(mapfile)
mapfiles = [
    filename + '.noco2' + ext, filename + '.co2.hadgem' + ext,
    filename + '.co2' + ext
]
filename, ext = splitext(ncfile)
ncfiles = [
    filename + '.noco2' + ext, filename + '.co2.hadgem' + ext,
    filename + '.co2' + ext
]

for i in range(len(dymarr)):
    # rasterize
    dymap = masked_array(zeros((nlats, nlons)), mask=ones((nlats, nlons)))
         str(n) + '.npz',
         weights_out1=weights_out1)
     np.savez(
         '/export/scratch/anummel1/smoothing_weights/newmask_smooth_' +
         str(n) + '.npz',
         weights_out2=weights_out2)
 #
 weight_data1 = np.load(
     '/export/scratch/anummel1/smoothing_weights/KyKx_smooth_' +
     str(n) + '.npz')
 weight_data2 = np.load(
     '/export/scratch/anummel1/smoothing_weights/newmask_smooth_' +
     str(n) + '.npz')
 weights_out1 = weight_data1['weights_out1'][:]
 weights_out2 = weight_data2['weights_out2'][:]
 t_inds = ma.reshape(np.arange(Ky.ravel().shape[0]),
                     (Ky.shape[0], Ky.shape[1]))
 t_inds2 = t_inds[weights_out1[:, :, 1].astype('int'),
                  weights_out1[:, :, 2].astype('int')]
 Ky2_smooth = ma.masked_array(np.zeros(Ky2.shape), mask=newmask2)
 Kx2_smooth = ma.masked_array(np.zeros(Ky2.shape), mask=newmask2)
 mask_smooth = ma.masked_array(np.zeros(Ky2.shape), mask=newmask)
 jind, iind = ma.where(1 - newmask2)
 Ky2_smooth[jind, iind] = ma.sum(
     Ky2.ravel()[list(t_inds2)] * weights_out1[:, :, 0], -1)
 Kx2_smooth[jind, iind] = ma.sum(
     Kx2.ravel()[list(t_inds2)] * weights_out1[:, :, 0], -1)
 jind, iind = ma.where(1 - newmask)
 t_inds2 = t_inds[weights_out2[:, :, 1].astype('int'),
                  weights_out2[:, :, 2].astype('int')]
 mask_smooth[jind, iind] = ma.sum(
     newmask2.ravel()[list(t_inds2)] * weights_out2[:, :, 0], -1)
Exemplo n.º 34
0
    fpu26    = f.variables[variable + '_fpu'][:, :, 0, 0, 0, 0]    # fpu, time, scen, dt, mp, cr
    global26 = f.variables[variable + '_global'][:, :, 0, 0, 0, 0] # global, time, scen, dt, mp, cr

with nc(rcp85file) as f:
    fpu85    = f.variables[variable + '_fpu'][:, :, 0, 0, 0, 0]
    global85 = f.variables[variable + '_global'][:, :, 0, 0, 0, 0]

nt, nf, ng = len(time), len(afpu), len(aglobal)

tidx1, tidx2 = where(time == 1980)[0][0], where(time == 2009)[0][0] + 1

# number of decades
nd = nt / 10

# delta yield
dyfpu26 = reshape(fpu26, (nf, nd, 10)).mean(axis = 2) - resize(fpu26[:, tidx1 : tidx2].mean(axis = 1), (nd, nf)).T
dyfpu85 = reshape(fpu85, (nf, nd, 10)).mean(axis = 2) - resize(fpu85[:, tidx1 : tidx2].mean(axis = 1), (nd, nf)).T

# absolute global yield
global26 = reshape(global26, (ng, nd, 10)).mean(axis = 2)
global85 = reshape(global85, (ng, nd, 10)).mean(axis = 2)

with nc(outfile, 'w') as f:
    f.createDimension('fpu', nf)
    fpuvar = f.createVariable('fpu', 'i4', 'fpu')
    fpuvar[:] = afpu
    fpuvar.units = funits
    fpuvar.long_name = flname

    f.createDimension('global', ng)
    globalvar = f.createVariable('global', 'i4', 'global')