示例#1
0
def plotWinterMaps(dataset, dataVar, winter, minval, maxval, cbarTicks = None, title = "", cmap = 'viridis'):
    """Plot maps of the arctic on North Pole Stereo projection with several months of data overlayed, along with the sea ice edge for each month. 
   
    Args:
        dataset (xr Dataset): dataset from google bucket
        dataVar (str): variable of interest
        winter (list): list of pandas Timestamp objects generated by getWinterDateRange(startYear, endYear)
        minval, maxval (int): minimum and maximum values for the data variable 
        cbarTicks (list or np array of length 2): ticks to use on colorbar (default to [minval + 1, maxval +1])
        title (str, optional): title of subplots (default to empty string)
        cmap (str, optional): color map (default to viridis)

    Returns:
        Figure displayed in notebook 

    """
    #format time for plotting 
    timeFormatted = list(pd.to_datetime(winter).strftime('%B %Y'))
    
    #define projection and transform
    proj = ccrs.NorthPolarStereo(central_longitude = -45)
    transform = ccrs.PlateCarree()
    
    #define arguments if not inputted 
    cbarTicks = np.arange(minval, maxval + 1, 1) if cbarTicks is None else cbarTicks

    #plot the data
    im = dataset[dataVar].where(dataset['seaice_conc_monthly_cdr'] > 0.5).sel(time = winter).plot(x = 'longitude', y = 'latitude', vmin = minval, vmax = maxval, cmap = cmap,
        extend='both', levels=20, transform = transform, col='time', add_colorbar = True, zorder = 2, figsize = (8,8), col_wrap = 3,
        cbar_kwargs = {'ticks': cbarTicks, 'label': "\n".join(wrap(dataset[dataVar].attrs['long_name'] + ' (' + dataset[dataVar].attrs['units'] + ')', 50)), 'orientation': 'horizontal', 'shrink': 0.4, 'pad': 0.025}, 
        subplot_kws = {'projection': proj})
    
    #add a title
    plt.suptitle(title + ': ' + dataset[dataVar].attrs['long_name'], fontsize = 20, y = 0.99, fontweight = 'medium')

    i = 0 #indexer to go through timeFormatted and winter arrays and assign the correct data to each month
    for ax in im.axes.flat:
        ax.coastlines(linewidth=0.25, color = 'black', zorder = 10) #add coastlines 
        ax.add_feature(cfeature.LAND, color ='0.95', zorder = 5) #add land 
        ax.add_feature(cfeature.LAKES, color = 'grey', zorder = 5) #add lakes 
        ax.gridlines(draw_labels = False, linewidth = 0.25, color = 'gray', alpha = 0.75, linestyle='--', zorder = 6) #add gridlines
        ax.set_extent([-179, 179, 50, 90], crs = transform) #zoom in so map only displays the Arctic
        ax.set_title(timeFormatted[i])
        
        #plot sea ice concentration 
        SICarray = dataset['seaice_conc_monthly_cdr'].sel(time = winter[i]).where(dataset['region_mask']!=21) #dont plot contour along coastlines
        lonGreater = ma.masked_greater(SICarray.longitude.values, -0.01)
        lonLesser = ma.masked_less(SICarray.longitude.values, 0)
        latGreater = ma.MaskedArray(SICarray.latitude.values, mask = lonGreater.mask)
        latLesser = ma.MaskedArray(SICarray.latitude.values, mask = lonLesser.mask)
        dataGreater = ma.MaskedArray(SICarray.values, mask = lonGreater.mask)
        dataLesser = ma.MaskedArray(SICarray.values, mask = lonLesser.mask)
        im2a = ax.contour(lonGreater, latGreater, dataGreater, levels = [0.5], transform = transform, colors = 'magenta', linewidths = 0.8, zorder = 5, alpha = 1)
        im2b = ax.contour(lonLesser, latLesser, dataLesser, levels = [0.5], transform = transform, colors = 'magenta', linewidths = 0.8, zorder = 5, alpha = 1)
        
        #update indexer 
        i += 1
        
    #display figure in notebook  
    plt.show()
示例#2
0
def bin_masked_image(image, mask, bin_fac):
    """bins the image using masked array, taking care of the fact that
    some pixels are masked
    image - 2d array, image you want to downsize
    mask - 2d array, pixels to mask
    bin_fac - factor by which to down size the image
    """
    # check if shape of image are integer multiples of bin_fac
    if image.shape[0] % bin_fac or image.shape[1] % bin_fac:
        x = int(self.Y * bin_fac)
        y = int(self.X * bin_fac)
        new_img = np.zeros((x, y), dtype=np.float64)
        new_mask = np.zeros((x, y), dtype=np.bool)

        new_img[:image.shape[0], :image.shape[1]] = image
        new_mask[:image.shape[0], :image.shape[1]] = mask

        img = ma.MaskedArray(new_img, mask=~new_mask.astype(bool))

    else:
        img = ma.MaskedArray(image, mask=~mask.astype(bool))

        Nsmallx = int(img.shape[0] / bin_fac)
        Nsmally = int(img.shape[1] / bin_fac)

    binned_img = img.reshape([Nsmallx,
                              int(bin_fac), Nsmally,
                              int(bin_fac)]).mean(3).mean(1)

    return binned_img.data
示例#3
0
 def setUp(self):
     rng = np.random.RandomState(713244122)
     self.nside = 16
     self.radius = 1.8
     self.testslicer = HealpixSlicer(nside=self.nside,
                                     verbose=False,
                                     latLonDeg=False,
                                     lonCol='ra',
                                     latCol='dec',
                                     radius=self.radius)
     nvalues = 10000
     self.dv = makeDataValues(size=nvalues,
                              minval=0.,
                              maxval=1.,
                              ramin=0,
                              ramax=2 * np.pi,
                              decmin=-np.pi,
                              decmax=0,
                              random=66)
     self.testslicer.setupSlicer(self.dv)
     self.metricdata = ma.MaskedArray(data=np.zeros(len(self.testslicer),
                                                    dtype='float'),
                                      mask=np.zeros(len(self.testslicer),
                                                    'bool'),
                                      fill_value=self.testslicer.badval)
     for i, b in enumerate(self.testslicer):
         idxs = b['idxs']
         if len(idxs) > 0:
             self.metricdata.data[i] = np.mean(self.dv['testdata'][idxs])
         else:
             self.metricdata.mask[i] = True
     self.metricdata2 = ma.MaskedArray(data=rng.rand(len(self.testslicer)),
                                       mask=np.zeros(
                                           len(self.testslicer), 'bool'),
                                       fill_value=self.testslicer.badval)
示例#4
0
    def readData(self, infilename):
        """
        Read metric data from disk, along with the info to rebuild the slicer (minus new slicing capability).

        infilename: the filename containing the metric data.
        """
        import lsst.sims.maf.slicers as slicers
        restored = np.load(infilename)
        # Get metric data set
        if restored['mask'][()] is None:
            metricValues = ma.MaskedArray(data=restored['metricValues'])
        else:
            metricValues = ma.MaskedArray(data=restored['metricValues'],
                                          mask=restored['mask'],
                                          fill_value=restored['fill'])
        # Get Metadata & other simData info.
        header = restored['header'][()]  # extra brackets restore dictionary to dictionary status.
        # Get slicer instantiated.
        slicer_init = restored['slicer_init'][()]
        # Backwards compatibility issue - map 'spatialkey1/spatialkey2' to 'lonCol/latCol'.
        if 'spatialkey1' in slicer_init:
            slicer_init['lonCol'] = slicer_init['spatialkey1']
            del(slicer_init['spatialkey1'])
        if 'spatialkey2' in slicer_init:
            slicer_init['latCol'] = slicer_init['spatialkey2']
            del(slicer_init['spatialkey2'])
        slicer = getattr(slicers, str(restored['slicerName']))(**slicer_init)
        # Restore slicePoint metadata.
        slicer.nslice = restored['slicerNSlice']
        slicer.slicePoints = restored['slicePoints'][()]
        slicer.shape = restored['slicerShape']
        return metricValues, slicer, header
示例#5
0
    def _bin_masked_image(self, image, mask, bin_fac):

        # check if shape of image are integer multiples of bin_fac
        if image.shape[0] % bin_fac or image.shape[1] % bin_fac:
            x = int(self.Y * bin_fac)
            y = int(self.X * bin_fac)
            new_img = np.zeros((x, y), dtype=np.float64)
            new_mask = np.zeros((x, y), dtype=np.bool)

            new_img[:image.shape[0], :image.shape[1]] = image
            new_mask[:image.shape[0], :image.shape[1]] = mask

            img = ma.MaskedArray(new_img, mask=~new_mask.astype(bool))

        else:
            img = ma.MaskedArray(image, mask=~mask.astype(bool))

        Nsmallx = int(img.shape[0] / bin_fac)
        Nsmally = int(img.shape[1] / bin_fac)

        binned_img = img.reshape(
            [Nsmallx, int(bin_fac), Nsmally,
             int(bin_fac)]).mean(3).mean(1)

        return binned_img.data
示例#6
0
 def test_increase_cube_dimensionality(self):
     with self.assertRaises(ValueError):
         # This would increase the dimensionality of the cube due to auto broadcasting
         cubex = iris.cube.Cube(ma.MaskedArray([[
             9,
         ]], mask=[[0]]))
         cubex + ma.MaskedArray([[3, 3, 3, 3]], mask=[[0, 1, 0, 1]])
示例#7
0
文件: raster.py 项目: ichem/mapchete
def resample_from_array(
    in_raster=None, in_affine=None, out_tile=None, resampling="nearest",
    nodataval=0
):
    """
    Extract and resample from array to target tile.

    Parameters
    ----------
    in_raster : array
    in_affine : ``Affine``
    out_tile : ``BufferedTile``
    resampling : string
        one of rasterio's resampling methods (default: nearest)
    nodataval : integer or float
        raster nodata value (default: 0)

    Returns
    -------
    resampled array : array
    """
    if isinstance(in_raster, ma.MaskedArray):
        pass
    if isinstance(in_raster, np.ndarray):
        in_raster = ma.MaskedArray(in_raster, mask=in_raster == nodataval)
    elif isinstance(in_raster, ReferencedRaster):
        in_affine = in_raster.affine
        in_raster = in_raster.data
    elif isinstance(in_raster, tuple):
        in_raster = ma.MaskedArray(
            data=np.stack(in_raster),
            mask=np.stack([
                band.mask
                if isinstance(band, ma.masked_array)
                else np.where(band == nodataval, True, False)
                for band in in_raster
            ]),
            fill_value=nodataval
        )
    else:
        raise TypeError("wrong input data type: %s" % type(in_raster))
    if in_raster.ndim == 2:
        in_raster = ma.expand_dims(in_raster, axis=0)
    elif in_raster.ndim == 3:
        pass
    else:
        raise TypeError("input array must have 2 or 3 dimensions")
    if in_raster.fill_value != nodataval:
        ma.set_fill_value(in_raster, nodataval)
    out_shape = (in_raster.shape[0], ) + out_tile.shape
    dst_data = np.empty(out_shape, in_raster.dtype)
    in_raster = ma.masked_array(
        data=in_raster.filled(), mask=in_raster.mask, fill_value=nodataval)
    reproject(
        in_raster, dst_data, src_transform=in_affine, src_crs=out_tile.crs,
        dst_transform=out_tile.affine, dst_crs=out_tile.crs,
        resampling=Resampling[resampling])
    return ma.MaskedArray(dst_data, mask=dst_data == nodataval)
示例#8
0
def plotOneMonth(dataset, dataVar, month, minval, maxval, cbarTicks = None, cmap = 'viridis'): 
    """Plots map of the arctic on North Pole Stereo projection with one month of data overlayed, along with the sea ice edge for each month.
   
    Args:
        dataset (xr Dataset): dataset from google bucket
        dataVar (str): variable of interest
        month (str): month and year of interest, i.e. 'Dec 2019' (does not need to be in any particular format)
        minval, maxval (int): minimum and maximum values for the data variable 
        cbarTicks (list or np array of length 2): ticks to use on colorbar (default to [minval + 1, maxval +1])
        cmap (str, optional): color map (default to viridis)
        
    Returns:
        Figure displayed in notebook 
    
    """
    
    #define projection and transform
    proj = ccrs.NorthPolarStereo(central_longitude = -45)
    transform = ccrs.PlateCarree()
    
    #initialize the figure and axes 
    plt.figure(figsize=(6, 6))
    ax = plt.axes(projection = proj)
    
    #define arguments if not inputted 
    cbarTicks = np.arange(minval, maxval + 1, 1) if cbarTicks is None else cbarTicks
    
    #plot sea ice concentraion 
    SICarray = dataset['seaice_conc_monthly_cdr'].sel(time = month).where(dataset['region_mask']!=21) #dont plot contour along coastlines
    
    #stackexchange workaround for plotting on a rotated grid
    lonGreater = ma.masked_greater(SICarray.longitude.values, -0.01)
    lonLesser = ma.masked_less(SICarray.longitude.values, 0)
    latGreater = ma.MaskedArray(SICarray.latitude.values, mask = lonGreater.mask)
    latLesser = ma.MaskedArray(SICarray.latitude.values, mask = lonLesser.mask)
    dataGreater = ma.MaskedArray(SICarray.values[0], mask = lonGreater.mask)
    dataLesser = ma.MaskedArray(SICarray.values[0], mask = lonLesser.mask)
    
    #plot contour using each part of the 2 masked data sets
    im2a = ax.contour(lonGreater, latGreater, dataGreater, levels = [0.5], transform = transform, colors = 'magenta', linewidths = 0.9, zorder=5, alpha=1)
    im2b = ax.contour(lonLesser, latLesser, dataLesser, levels = [0.5], transform = transform, colors = 'magenta', linewidths = 0.9, zorder=5, alpha=1)
    #im = ax.contour(SICarray.longitude.values, SICarray.latitude.values, SICarray.values[0], levels = [0.15], transform = transform, colors = 'magenta', linewidths = 0.8, zorder=15, alpha=1)
    
    #plot the data
    dataset[dataVar].where(dataset['seaice_conc_monthly_cdr'] > 0.5).sel(time = month).plot(x = 'longitude', y = 'latitude', vmin = minval, vmax = maxval, extend = 'both', 
                    ax = ax, add_colorbar = True, transform = transform, zorder = 2, cmap = cmap, 
                    cbar_kwargs = {'label': "\n".join(wrap(dataset[dataVar].attrs['long_name'] + ' (' + dataset[dataVar].attrs['units'] + ')', 50)), 'orientation': 'horizontal', 'shrink': 0.75, 'pad': 0.025})
    
    #add features to the map
    ax.coastlines(linewidth=0.15, color = 'black', zorder = 10) #add coastlines 
    ax.add_feature(cfeature.LAND, color ='0.95', zorder = 5) #add land 
    ax.add_feature(cfeature.LAKES, color = 'grey', zorder = 5) #add lakes 
    ax.gridlines(draw_labels = False, linewidth = 0.25, color = 'gray', alpha = 0.7, linestyle = '--', zorder = 6) #add gridlines
    ax.set_extent([-179, 179, 55, 90], crs = transform) #zoom in so map only displays the Arctic
    ax.set_title("\n".join(wrap(month + ": " + dataset[dataVar].attrs['long_name'], 38)), fontsize = 'x-large')
    
    #display figure in notebook 
    plt.show()
示例#9
0
def generation(data):
    neighbors= ndi.convolve(data, KERNEL, mode='constant', cval=0)
    has_bugs = data == 1

    dies = np.logical_and(has_bugs, neighbors != 1)

    spawns = np.logical_and(~has_bugs, np.logical_or(neighbors == 1, neighbors == 2))

    result = ma.MaskedArray(data, mask=dies).filled(0)
    return ma.MaskedArray(result, mask=spawns).filled(1)
示例#10
0
文件: raster.py 项目: gijs/mapchete
def _get_warped_array(
    input_file=None, band_idx=None, dst_bounds=None, dst_shape=None,
    dst_affine=None, dst_crs=None, resampling="nearest"
):
    """Extract a numpy array from a raster file."""
    LOGGER.debug("read array using rasterio")
    with rasterio.open(input_file, "r") as src:
        if dst_crs == src.crs:
            src_left, src_bottom, src_right, src_top = dst_bounds
        else:
            # Return empty array if destination bounds don't intersect with
            # file bounds.
            file_bbox = box(*src.bounds)
            tile_bbox = reproject_geometry(
                box(*dst_bounds), src_crs=dst_crs, dst_crs=src.crs)
            if not file_bbox.intersects(tile_bbox):
                LOGGER.debug("file bounding box does not intersect with tile")
                return ma.MaskedArray(
                    data=ma.zeros(dst_shape, dtype=src.profile["dtype"]),
                    mask=ma.ones(dst_shape), fill_value=src.nodata)
            # Reproject tile bounds to source file SRS.
            src_left, src_bottom, src_right, src_top = transform_bounds(
                dst_crs, src.crs, *dst_bounds, densify_pts=21)

        if float('Inf') in (src_left, src_bottom, src_right, src_top):
            # Maybe not the best way to deal with it, but if bounding box
            # cannot be translated, it is assumed that data is emtpy
            LOGGER.debug("tile seems to be outside of input CRS bounds")
            return ma.MaskedArray(
                data=ma.zeros(dst_shape, dtype=src.profile["dtype"]),
                mask=ma.ones(dst_shape), fill_value=src.nodata)
        # Read data window.
        window = src.window(
            src_left, src_bottom, src_right, src_top, boundless=True)
        start = time.time()
        src_band = src.read(band_idx, window=window, boundless=True)
        LOGGER.debug("window read in %ss" % round(time.time() - start, 3))
        # Quick fix because None nodata is not allowed.
        nodataval = 0 if not src.nodata else src.nodata
        # Prepare reprojected array.
        dst_band = np.empty(dst_shape, src.dtypes[band_idx-1])
        # Run rasterio's reproject().
        start = time.time()
        reproject(
            src_band, dst_band, src_transform=src.window_transform(window),
            src_crs=src.crs, src_nodata=nodataval, dst_transform=dst_affine,
            dst_crs=dst_crs, dst_nodata=nodataval,
            resampling=RESAMPLING_METHODS[resampling])
        LOGGER.debug(
            "window reprojected in %ss" % round(time.time() - start, 3))
        return ma.MaskedArray(dst_band, mask=dst_band == nodataval)
示例#11
0
    def readData(self, infilename):
        """
        Read metric data from disk, along with the info to rebuild the slicer (minus new slicing capability).

        Parameters
        -----------
        infilename: str
            The filename containing the metric data.

        Returns
        -------
        np.ma.MaskedArray, lsst.sims.maf.slicer, dict
            MetricValues stored in data file, the slicer basis for those metric values, and a dictionary
            containing header information (runName, metadata, etc.).
        """
        import lsst.sims.maf.slicers as slicers
        # Allowing pickles here is required, because otherwise we cannot restore data saved as objects.
        restored = np.load(infilename, allow_pickle=True)
        # Get metadata and other simData info.
        header = restored['header'][()]
        slicer_init = restored['slicer_init'][()]
        slicerName = str(restored['slicerName'])
        slicePoints = restored['slicePoints'][()]
        # Backwards compatibility issue - map 'spatialkey1/spatialkey2' to 'lonCol/latCol'.
        if 'spatialkey1' in slicer_init:
            slicer_init['lonCol'] = slicer_init['spatialkey1']
            del (slicer_init['spatialkey1'])
        if 'spatialkey2' in slicer_init:
            slicer_init['latCol'] = slicer_init['spatialkey2']
            del (slicer_init['spatialkey2'])
        try:
            slicer = getattr(slicers, slicerName)(**slicer_init)
        except TypeError:
            warnings.warn(
                'Cannot use saved slicer init values; falling back to defaults'
            )
            slicer = getattr(slicers, slicerName)()
        # Restore slicePoint metadata.
        slicer.nslice = restored['slicerNSlice']
        slicer.slicePoints = slicePoints
        slicer.shape = restored['slicerShape']
        # Get metric data set
        if restored['mask'][()] is None:
            metricValues = ma.MaskedArray(data=restored['metricValues'])
        else:
            metricValues = ma.MaskedArray(data=restored['metricValues'],
                                          mask=restored['mask'],
                                          fill_value=restored['fill'])
        return metricValues, slicer, header
示例#12
0
def colorNormReinhard(image):
    #Takes image as a float64 values in range [0,1]

    mst = np.array(([77.5121, 270.5718], [8.9287, -23.6535], [2.9664, 8.3857]))
    M = image.shape[0]
    N = image.shape[1]

    lab = customRGB2LAB(image)
    bcg = bcg_func(image)
    bcg_inverse = (bcg - 1) * -1

    bcgvect = np.reshape(bcg, (M * N))
    bcgv_i = np.reshape(bcg_inverse, (M * N))
    labvect = np.transpose(np.reshape(lab, (M * N, 3)))

    labL = ma.MaskedArray(labvect[0, :], mask=bcgvect)
    labA = ma.MaskedArray(labvect[1, :], mask=bcgvect)
    labB = ma.MaskedArray(labvect[2, :], mask=bcgvect)

    #     labL_inv = ma.MaskedArray(labvect[0,:], mask=bcgv_i)
    #     labA_inv = ma.MaskedArray(labvect[1,:], mask=bcgv_i)
    #     labB_inv = ma.MaskedArray(labvect[2,:], mask=bcgv_i)

    labLm = labL.mean()
    labAm = labA.mean()
    labBm = labB.mean()

    labLs = labL.std()
    labAs = labA.std()
    labBs = labB.std()

    labLn = ((labvect[0, :] - labLm) / labLs) * mst[0, 0] + mst[0, 1]
    labAn = ((labvect[1, :] - labAm) / labAs) * mst[1, 0] + mst[1, 1]
    labBn = ((labvect[2, :] - labBm) / labBs) * mst[2, 0] + mst[2, 1]

    labL = np.multiply(labLn, bcgv_i) / 255 + np.multiply(labL, bcgvect)
    labA = np.multiply(labAn, bcgv_i) / 255 + np.multiply(labA, bcgvect)
    labB = np.multiply(labBn, bcgv_i) / 255 + np.multiply(labB, bcgvect)

    #     labL = ((labL-labLm)/labLs)*mst[0,0]+mst[0,1]
    #     labA = ((labA-labAm)/labAs)*mst[1,0]+mst[1,1]
    #     labB = ((labB-labBm)/labBs)*mst[2,0]+mst[2,1]

    labnorm = np.array((labL, labA, labB))

    labnorm = np.reshape(np.transpose(labnorm), (M, N, 3))
    rgbnorm = customLAB2RGB(labnorm)
    return rgbnorm
示例#13
0
def aggregate2DTrial(sp,
                     varList,
                     trialNumList,
                     fReduce=np.mean,
                     ignoreNaNs=False):
    '''Aggregate all the data from a 2D Parameter space
    (:class:`~grid_cell_model.parameters.param_space.JobTrialSpace2D`).

    In the process, apply ``fReduce`` on the trials of all the data sets in the
    parameter space.

    Parameters
    ----------
    sp : ParamSpace
        A parameter space to apply the reduction on.
    varList : list of strings
        A variable list, specifying the location of the variable to reduce.
        This will be prefixed with ['analysis']
    trialNumList : list of ints
        A list specifying exactly which trials are to be processed.
    fReduce : a function f(data, axis)
        A reduction function.
    ignoreNaNs : bool, optional
        If True, mask the NaN values.
    output : a 2D numpy array of the reduced values
    '''
    varList = ['analysis'] + varList
    retVar = sp.aggregateData(varList,
                              trialNumList,
                              funReduce=np.mean,
                              saveData=False)
    if (ignoreNaNs):
        nans = np.isnan(retVar)
        retVar = ma.MaskedArray(retVar, mask=nans)
    return fReduce(retVar, 2)
示例#14
0
def denoise_sst(sst, bt11, kernel='boxcar', width=20, show=False):
    """
    """
    if kernel == 'boxcar':
        ker = np.ones((width, width))
    elif kernel == 'gaussian':
        gau = gaussian(3 * width, width / 2.)
        ker = np.outer(gau, gau)
    else:
        raise Exception('Unknown kernel')
    mask = ma.getmaskarray(sst) | ma.getmaskarray(bt11)
    sst.data[mask] = 0.
    bt11.data[mask] = 0.
    corr = convolve((sst - 273.15) * 0.9 + 273.15 - bt11, ker)
    #corr = convolve(sst - bt11, ker)
    #corr = convolve((sst - 273) * 0.9 + 273 - bt11, ker)
    norm = convolve(1. - mask, ker)
    corr[mask] = 0
    corr[~mask] /= norm[~mask]
    denoised_sst = bt11 + corr
    denoised_sst.mask = mask
    if show == True:
        import matplotlib.pyplot as plt
        vmin = min([bt11.min(), sst.min(), denoised_sst.min()])
        vmax = max([bt11.max(), sst.max(), denoised_sst.max()])
        corr = ma.MaskedArray(corr, mask=mask)
        for ivar, var in enumerate([sst, bt11, corr, denoised_sst]):
            plt.figure()
            if ivar == 2:
                plt.imshow(var, interpolation='nearest')
            else:
                plt.imshow(var, vmin=vmin, vmax=vmax, interpolation='nearest')
            plt.colorbar()
        plt.show()
    return denoised_sst
示例#15
0
def makeMetricData(slicer, dtype='float', seed=8800):
    rng = np.random.RandomState(seed)
    metricValues = rng.rand(len(slicer)).astype(dtype)
    metricValues = ma.MaskedArray(data=metricValues,
                                  mask=np.zeros(len(slicer), 'bool'),
                                  fill_value=slicer.badval)
    return metricValues
示例#16
0
 def setUp(self):
     """Prepare tests."""
     self.cube = Cube(
         ma.MaskedArray([1.e36], mask=(False, )),
         var_name='od550aer',
     )
     self.fix = Od550Aer(None)
示例#17
0
 def fancy_plot(self):
     """Make a fancier looking sky map of the footprint.
     """
     if not (fancyplot):
         print(
             'Cannot make this fancy plot; MAF plotting utilities unavailable.'
         )
         return None
     # fieldRA / fieldDec are dictionaries - key=prop
     slicer = slicers.OpsimFieldSlicer()
     fignum = None
     colorlist = [[1, 1, 0], [.5, 0, .5], [0, .25, .5], [0, 1, 0],
                  [0, 0, 0], [1, 0, 0], [.5, .5, 1]]
     ci = 0
     colors = {}
     add_planes = True
     for name in self.regions:
         print(name)
         # Modify slicer so we can use it for plotting.
         slicer.slicePoints['ra'] = np.radians(self.regions[name]['ra'])
         slicer.slicePoints['dec'] = np.radians(self.regions[name]['dec'])
         fieldLocs = ma.MaskedArray(data=np.empty(len(self.regions[name]),
                                                  object),
                                    mask=np.zeros(len(self.regions[name]),
                                                  bool),
                                    fill_value=-99)
         colors[name] = [
             colorlist[ci][0], colorlist[ci][1], colorlist[ci][2], 0.4
         ]
         ci += 1
         if ci == len(colorlist):
             ci = 0
         for i in range(len(self.regions[name])):
             fieldLocs.data[i] = colors[name]
         skymap = plots.BaseSkyMap()
         fignum = skymap(fieldLocs,
                         slicer, {
                             'metricIsColor': True,
                             'bgcolor': 'lightgray',
                             'raCen': 0,
                             'figsize': (10, 8),
                             'ecPlane': add_planes,
                             'mwZone': add_planes
                         },
                         fignum=fignum)
         add_planes = False
     fig = plt.figure(fignum)
     labelcolors = []
     labeltext = []
     for name in self.regions:
         el = Ellipse(
             (0, 0),
             0.03,
             0.03,
             fc=(colors[name][0], colors[name][1], colors[name][2]),
             alpha=colors[name][3])
         labelcolors.append(el)
         labeltext.append(name)
     plt.legend(labelcolors, labeltext, loc=(0.85, 0.9), fontsize='smaller')
     return fig
示例#18
0
文件: raster.py 项目: ichem/mapchete
def _prepare_iterable(data, masked, nodata, dtype):
    out_data = ()
    out_mask = ()
    for band in data:
        if isinstance(band, ma.MaskedArray):
            try:
                out_data += (band.data, )
                if masked:
                    assert band.shape == band.mask.shape
                    out_mask += (band.mask, )
            except AssertionError:
                out_mask += (
                    np.where(band.data == nodata, True, False), )
        elif isinstance(band, np.ndarray):
            out_data += (band, )
            if masked:
                out_mask += (np.where(band == nodata, True, False), )
        else:
            raise ValueError("input data bands must be NumPy arrays")
    if masked:
        assert len(out_data) == len(out_mask)
        return ma.MaskedArray(
            data=np.stack(out_data).astype(dtype),
            mask=np.stack(out_mask))
    else:
        return np.stack(out_data).astype(dtype)
示例#19
0
    def setUp(self):
        self.data1 = ma.MaskedArray([[9, 9, 9], [
            8,
            8,
            8,
        ]],
                                    mask=[[0, 1, 0], [0, 0, 1]])
        self.data2 = ma.MaskedArray([[3, 3, 3], [
            2,
            2,
            2,
        ]],
                                    mask=[[0, 1, 0], [0, 1, 1]])

        self.cube1 = iris.cube.Cube(self.data1)
        self.cube2 = iris.cube.Cube(self.data2)
示例#20
0
def update_background(fn):
    with fits.open(fn, mode='update') as hdu:
        im = hdu[0].data.copy()
        mask = ~np.isfinite(im) + (im < DATA_FLOOR)
        if 'MASK' in hdu:
            mask += hdu['MASK'].data > 0
        im = ma.MaskedArray(im, mask=mask, copy=True)

        scim = sigma_clip(im)

        mean = ma.mean(scim)
        mean = mean if mean is not ma.masked else 0

        median = ma.median(scim)
        median = median if median is not ma.masked else 0

        stdev = ma.std(scim)
        stdev = stdev if stdev is not ma.masked else 0

        hdu['SCI'].header['bgmean'] = (mean, 'background sigma-clipped mean')
        hdu['SCI'].header['bgmedian'] = (median,
                                         'background sigma-clipped median')
        hdu['SCI'].header['bgstdev'] = (
            stdev, 'background sigma-clipped standard dev.')
        hdu['SCI'].header['nbg'] = (ma.sum(~scim.mask),
                                    'area considered in background stats.')
示例#21
0
def plotACTrial(sp, varList, iterList, noise_sigma, trialNumList=[0], **kw):
    '''Plot parameter sweep of gamma autocorrelation peaks.'''
    #kw arguments
    r           = kw.pop('r', 0)
    c           = kw.pop('c', 0)
    cbar        = kw.pop('cbar', True)
    sigmaTitle  = kw.pop('sigmaTitle', True)
    annotations = kw.pop('annotations', None)

    if isinstance(sp, aggr.AggregateData):
        C, X, Y  = sp.getData()
    else:
        C = aggr.aggregate2DTrial(sp, varList, trialNumList)
        Y, X = aggr.computeYX(sp, iterList, r=r, c=c)
    C = ma.MaskedArray(C, mask=np.isnan(C))
    C, ax, cax = plot2DTrial(X, Y, C, colorBar=cbar, **kw)

    print("    max(C): {0}".format(np.max(C)))
    print("    min(C): {0}".format(np.min(C)))

    if (sigmaTitle):
        ax.set_title('$\sigma$ = {0} pA'.format(int(noise_sigma)))

    if (annotations is not None):
        for a in annotations:
            plotSweepAnnotation(X=X, Y=Y, ax=ax, **a)

    return C, ax, cax
示例#22
0
def plotVelTrial(sp, varList, iterList, noise_sigma, **kw):
    '''Plot a parameter sweep of velocity data.

    These are either bump slope or line fit error.
    '''
    # process kwargs
    r           = kw.pop('r', 0)
    c           = kw.pop('c', 0)
    sigmaTitle  = kw.pop('sigmaTitle', True)
    cbar        = kw.pop('cbar', True)
    annotations = kw.pop('annotations', None)

    C    = aggr.aggregate2D(sp, varList, funReduce = np.sum)
    C    = ma.MaskedArray(C, mask = np.isnan(C))
    Y, X = aggr.computeVelYX(sp, iterList, r=r, c=c)
    C, ax, cax = plot2DTrial(X, Y, C, colorBar=cbar, **kw)

    print("plotVelTrial: max(C): {0}".format(np.max(C.ravel())))
    print("plotVelTrial: min(C): {0}".format(np.min(C.ravel())))

    if (sigmaTitle):
        ax.set_title('$\sigma$ = {0} pA'.format(int(noise_sigma)))

    if (annotations is not None):
        for a in annotations:
            plotSweepAnnotation(X=X, Y=Y, ax=ax, **a)

    return C, ax, cax
示例#23
0
    def addVar(self, url, Vars=None, Verbose=True, Levels=None):
        """
        Sample variable along DIAL track.
        """
        from grads import GrADS
        ga = GrADS(Window=False, Echo=False)
        fh = ga.open(url)
        if Levels is not None:
            ga('set lev %s' % Levels)
        qh = ga.query('dims')

        if Vars is None:
            Vars = ga.query('file').vars
        elif type(Vars) is StringType:
            Vars = [
                Vars,
            ]
        for var in Vars:
            if Verbose:
                print ' Working on <%s>' % var
            if fh.Vars[var.lower()].levs == 0:
                ga('set lev 1')  # 2D variable
            else:
                ga.setdim(qh)
            q = ga.sampleXYT(var,
                             self.lon,
                             self.lat,
                             self.tyme,
                             Verbose=Verbose).data
            self.__dict__[var] = ma.MaskedArray(q, mask=q >= MAPL_UNDEF)
示例#24
0
def plotFRTrial(sp, varList, iterList, noise_sigma, trialNumList=[0],
        thr=np.infty, **kw):
    r           = kw.pop('r', 0)
    c           = kw.pop('c', 0)
    ignoreNaNs  = kw.pop('ignoreNaNs', False)
    sigmaTitle  = kw.pop('sigmaTitle', True)
    cbar        = kw.pop('cbar', True)
    annotations = kw.pop('annotations', None)

    C = aggr.aggregate2DTrial(sp, varList, trialNumList,
            ignoreNaNs=ignoreNaNs)
    C = ma.MaskedArray(C, mask=np.logical_or(np.isnan(C), C > thr))
    Y, X = aggr.computeYX(sp, iterList, r=r, c=c)
    C, ax, cax = plot2DTrial(X, Y, C, colorBar=cbar, **kw)

    print("    max(C): {0}".format(np.max(C)))
    print("    min(C): {0}".format(np.min(C)))

    if (sigmaTitle):
        ax.set_title('$\sigma$ = {0} pA'.format(int(noise_sigma)))

    if (annotations is not None):
        for a in annotations:
            plotSweepAnnotation(X=X, Y=Y, ax=ax, **a)

    return C, ax, cax
示例#25
0
 def pixeldata(self, img):
     if isinstance(img, str):
         self.__pixeldata = ma.asarray(fits.getdata(img)).astype('<f4')
     elif isinstance(img, np.ndarray):
         self.__pixeldata = ma.MaskedArray(img, mask=False).astype('<f4')
     elif isinstance(img, fits.HDUList):
         if img[0].is_image:
             self.__pixeldata = ma.asarray(img[0].data).astype('<f4')
     elif isinstance(img, fits.PrimaryHDU):
         if img.is_image:
             self.__pixeldata = ma.asarray(img.data).astype('<f4')
     if self.borders:
         sx, sy = self.__pixeldata.shape
         line = self.__pixeldata.data[sx // 2, :]
         pxsum = 0
         for x, px in enumerate(line):
             pxsum += px
             if pxsum > 0.:
                 ldx = x
                 break
         for dx in range(ldx):
             if not np.sum(self.__pixeldata.data[:dx, :]) == 0:
                 ldx = dx
                 break
         pxsum = 0
         for x, px in enumerate(np.flip(line, axis=0)):
             pxsum += px
             if pxsum > 0.:
                 rdx = sx - x
                 break
         for dx in range(x):
             if not np.sum(self.__pixeldata.data[-dx - 1:, :]) == 0:
                 rdx = sx - dx
                 break
         col = self.__pixeldata.data[:, sy // 2]
         pxsum = 0
         for y, px in enumerate(col):
             pxsum += px
             if pxsum > 0.:
                 ldy = y
                 break
         for dy in range(ldy):
             if not np.sum(self.__pixeldata.data[:, :dy]) == 0:
                 ldy = dy
                 break
         pxsum = 0
         for y, px in enumerate(np.flip(line, axis=0)):
             pxsum += px
             if pxsum > 0.:
                 rdy = sy - y
                 break
         for dy in range(y):
             if not np.sum(self.__pixeldata.data[:, -dy - 1:]) == 0:
                 rdy = sy - dy
                 break
         self.__pixeldata = self.__pixeldata[ldx:rdx, ldy:rdy]
     if not np.sum(self.crop) == 0.:
         dx, dy = self.crop
         self.__pixeldata = self.__pixeldata[dx[0]:-dx[1], dy[0]:-dy[1]]
     self.__pixeldata = self.__pixeldata * self.__gain
示例#26
0
 def setUp(self):
     self.data = ma.MaskedArray(
         [[9, 9, 9], [8, 8, 8]],
         mask=[[0, 1, 0], [0, 0, 1]],
         dtype=np.float64,
     )
     self.cube = iris.cube.Cube(self.data)
示例#27
0
 def test_incompatible_dimensions(self):
     data3 = ma.MaskedArray(
         [[3, 3, 3, 4], [2, 2, 2]], mask=[[0, 1, 0, 0], [0, 1, 1]]
     )
     with self.assertRaises(ValueError):
         # Incompatible dimensions.
         self.cube + data3
示例#28
0
def maskedEqual(array, missingValue):
    """ Mask an array where equal to a given (missing)value.

        Unfortunately ma.masked_equal does not work with structured arrays. See:
        https://mail.scipy.org/pipermail/numpy-discussion/2011-July/057669.html

        If the data is a structured array the mask is applied for every field (i.e. forming a
        logical-and). Otherwise ma.masked_equal is called.
    """
    if array_is_structured(array):
        # Enforce the array to be masked
        if not isinstance(array, ma.MaskedArray):
            array = ma.MaskedArray(array)

        # Set the mask separately per field
        for nr, field in enumerate(array.dtype.names):
            if hasattr(missingValue, '__len__'):
                fieldMissingValue = missingValue[nr]
            else:
                fieldMissingValue = missingValue

            array[field] = ma.masked_equal(array[field], fieldMissingValue)

        check_class(array, ma.MaskedArray) # post-condition check
        return array
    else:
        # masked_equal works with missing is None
        result = ma.masked_equal(array, missingValue, copy=False)
        check_class(result, ma.MaskedArray) # post-condition check
        return result
示例#29
0
def plotGridTrial(sp, varList, iterList, noise_sigma, trialNumList=[0], **kw):
    '''Plot a parameter sweep of gridness score.'''
    #kw arguments
    r           = kw.pop('r', 0)
    c           = kw.pop('c', 0)
    nansAs0     = kw.pop('nansAs0', False)
    ignoreNaNs  = kw.pop('ignoreNaNs', False)
    sigmaTitle  = kw.pop('sigmaTitle', True)
    cbar        = kw.pop('cbar', True)
    annotations = kw.pop('annotations', None)

    if isinstance(sp, aggr.AggregateData):
        G, X, Y = sp.getData()
    else:
        G = aggr.aggregate2DTrial(sp, varList, trialNumList, ignoreNaNs=ignoreNaNs)
        Y, X = aggr.computeYX(sp, iterList, r=r, c=c)

    nans = np.isnan(G)
    if (nansAs0):
        G[nans] = 0
    else:
        G = ma.MaskedArray(G, mask=nans)
    G, ax, cax = plot2DTrial(X, Y, G, colorBar=cbar, **kw)

    print("    max(G): {0}".format(np.max(G)))
    print("    min(G): {0}".format(np.min(G)))

    if (sigmaTitle):
        ax.set_title('$\sigma$ = {0} pA'.format(int(noise_sigma)))

    if (annotations is not None):
        for a in annotations:
            plotSweepAnnotation(X=X, Y=Y, ax=ax, **a)

    return G, ax, cax
示例#30
0
 def test_add_masked(self, default):
     data = np.random.rand(5 * 6 * 7, 3)
     masked = ma.MaskedArray(data, mask=data < .4, fill_value=42.)
     default.add(masked, 'D')
     result_masked = str(default)
     default.add(np.where(masked.mask, masked.fill_value, masked), 'D')
     assert result_masked == str(default)