Example #1
0
    def _scalar2raster(self, d, var, tndx):
        """
        Convert a single variable into a raster and colorbar.

        :param d: the netcdf file
        :param var: the variable name
        :param tndx: the time index
        :return: two StringIO objects, first is the PNG raster, second is PNG colorbar
        """
        # gather wisdom about the variable
        wisdom = get_wisdom(var).copy()
        wisdom.update(self.wisdom_update.get(var, {}))
        native_unit = wisdom['native_unit']
        cmap_name = wisdom['colormap']
        cmap = mpl.cm.get_cmap(cmap_name)

        # extract variable
        fa = wisdom['retrieve_as'](
            d,
            tndx)  # this calls a lambda defined to read the required 2d field
        lat, lon = wisdom['grid'](d)

        if lat.shape != fa.shape:
            raise PostprocError(
                "Variable %s size does not correspond to grid size." % var)

        # look at mins and maxes
        fa_min, fa_max = np.nanmin(fa), np.nanmax(fa)

        # determine if we will use the range in the variable or a fixed range
        scale = wisdom['scale']
        if scale != 'original':
            fa_min, fa_max = scale[0], scale[1]
            fa[fa < fa_min] = fa_min
            fa[fa > fa_max] = fa_max

        # only create the colorbar if requested
        cb_png_data = None
        if wisdom['colorbar'] is not None:
            cb_unit = wisdom['colorbar']
            cbu_min, cbu_max = convert_value(native_unit, cb_unit,
                                             fa_min), convert_value(
                                                 native_unit, cb_unit, fa_max)
            #  colorbar + add it to the KMZ as a screen overlay
            cb_png_data = make_colorbar([cbu_min, cbu_max], 'vertical', 2,
                                        cmap, wisdom['name'] + ' ' + cb_unit)

        # check for 'transparent' color value and replace with nans
        if 'transparent_values' in wisdom:
            rng = wisdom['transparent_values']
            fa = np.ma.masked_array(fa,
                                    np.logical_and(fa >= rng[0], fa <= rng[1]))

        # create the raster & get coordinate bounds
        raster_png_data, corner_coords = basemap_raster_mercator(
            lon, lat, fa, fa_min, fa_max, cmap)

        return raster_png_data, corner_coords, cb_png_data
Example #2
0
def scalar_field_to_raster(fa, lats, lons, wisdom):
    """
    Render a scalar variable into a geolocated raster and colorbar.

    :param fa: the field to render
    :param lats: the latitudes
    :param lons: the longitudes
    :param wisdom: a configuration dictionary controlling the visualization
    :return: a tuple with the raster as StringIO, its geolocation and the PNG colorbar StringIO (None if not requested)
    """
    # gather wisdom about the variable
    native_unit = wisdom['native_unit']
    cmap_name = wisdom['colormap']
    cmap = mpl.cm.get_cmap(cmap_name)

    if lats.shape != fa.shape:
        raise PostprocError(
            "Variable size %s does not correspond to grid size %s." %
            (fa.shape, lats.shape))

    # look at mins and maxes
    fa_min, fa_max = np.nanmin(fa), np.nanmax(fa)

    # determine if we will use the range in the variable or a fixed range
    scale = wisdom['scale']
    if scale != 'original':
        fa_min, fa_max = scale[0], scale[1]
        fa[fa < fa_min] = fa_min
        fa[fa > fa_max] = fa_max

    # only create the colorbar if requested
    cb_png_data = None
    if wisdom['colorbar'] is not None:
        cb_unit = wisdom['colorbar']
        cbu_min, cbu_max = convert_value(native_unit, cb_unit,
                                         fa_min), convert_value(
                                             native_unit, cb_unit, fa_max)
        #  colorbar + add it to the KMZ as a screen overlay
        cb_png_data = make_colorbar([cbu_min, cbu_max], 'vertical', 2, cmap,
                                    wisdom['name'] + ' ' + cb_unit)

    # check for 'transparent' color value and replace with nans
    if 'transparent_values' in wisdom:
        rng = wisdom['transparent_values']
        fa = np.ma.masked_array(fa, np.logical_and(fa >= rng[0], fa <= rng[1]))

    # create the raster & get coordinate bounds
    raster_png_data, corner_coords = basemap_raster_mercator(
        lons, lats, fa, fa_min, fa_max, cmap)

    return raster_png_data, corner_coords, cb_png_data
Example #3
0
    def _scalar2raster(self, d, var, tndx):
        """
        Convert a single variable into a raster and colorbar.

        :param d: the netcdf file
        :param var: the variable name
        :param tndx: the time index
        :return: two StringIO objects, first is the PNG raster, second is PNG colorbar
        """
        # gather wisdom about the variable
        wisdom = get_wisdom(var).copy()
        wisdom.update(self.wisdom_update.get(var, {}))
        native_unit = wisdom['native_unit']
        cmap_name = wisdom['colormap']
        cmap = mpl.cm.get_cmap(cmap_name)

        # extract variable
        fa = wisdom['retrieve_as'](d,tndx) # this calls a lambda defined to read the required 2d field
        lat, lon = wisdom['grid'](d)

        if lat.shape != fa.shape:
            raise PostprocError("Variable %s size does not correspond to grid size." % var)

        # look at mins and maxes
        fa_min,fa_max = np.nanmin(fa),np.nanmax(fa)

        # determine if we will use the range in the variable or a fixed range
        scale = wisdom['scale']
        if scale != 'original':
            fa_min, fa_max = scale[0], scale[1]
            fa[fa < fa_min] = fa_min
            fa[fa > fa_max] = fa_max

        # only create the colorbar if requested
        cb_png_data = None
        if wisdom['colorbar'] is not None:
            cb_unit = wisdom['colorbar']
            cbu_min,cbu_max = convert_value(native_unit, cb_unit, fa_min), convert_value(native_unit, cb_unit, fa_max)
            #  colorbar + add it to the KMZ as a screen overlay
            cb_png_data = make_colorbar([cbu_min, cbu_max],'vertical',2,cmap,wisdom['name'] + ' ' + cb_unit)

        # check for 'transparent' color value and replace with nans
        if 'transparent_values' in wisdom:
            rng = wisdom['transparent_values']
            fa = np.ma.masked_array(fa, np.logical_and(fa >= rng[0], fa <= rng[1]))

        # create the raster & get coordinate bounds
        raster_png_data,corner_coords = basemap_raster_mercator(lon,lat,fa,fa_min,fa_max,cmap)

        return raster_png_data, corner_coords, cb_png_data
Example #4
0
def scalar_field_to_raster(fa, lats, lons, wisdom):
    """
    Render a scalar variable into a geolocated raster and colorbar.

    :param fa: the field to render
    :param lats: the latitudes
    :param lons: the longitudes
    :param wisdom: a configuration dictionary controlling the visualization
    :return: a tuple with the raster as StringIO, its geolocation and the PNG colorbar StringIO (None if not requested)
    """
    # gather wisdom about the variable
    native_unit = wisdom['native_unit']
    cmap_name = wisdom['colormap']
    cmap = mpl.cm.get_cmap(cmap_name)

    if lats.shape != fa.shape:
        raise PostprocError("Variable size %s does not correspond to grid size %s." % (fa.shape, lats.shape))

    # look at mins and maxes
    fa_min,fa_max = np.nanmin(fa),np.nanmax(fa)

    # determine if we will use the range in the variable or a fixed range
    scale = wisdom['scale']
    if scale != 'original':
        fa_min, fa_max = scale[0], scale[1]
        fa[fa < fa_min] = fa_min
        fa[fa > fa_max] = fa_max

    # only create the colorbar if requested
    cb_png_data = None
    if wisdom['colorbar'] is not None:
        cb_unit = wisdom['colorbar']
        cbu_min,cbu_max = convert_value(native_unit, cb_unit, fa_min), convert_value(native_unit, cb_unit, fa_max)
        #  colorbar + add it to the KMZ as a screen overlay
        cb_png_data = make_colorbar([cbu_min, cbu_max],'vertical',2,cmap,wisdom['name'] + ' ' + cb_unit)

    # check for 'transparent' color value and replace with nans
    if 'transparent_values' in wisdom:
        rng = wisdom['transparent_values']
        fa = np.ma.masked_array(fa, np.logical_and(fa >= rng[0], fa <= rng[1]))

    # create the raster & get coordinate bounds
    raster_png_data,corner_coords = basemap_raster_mercator(lons,lats,fa,fa_min,fa_max,cmap)

    return raster_png_data, corner_coords, cb_png_data
Example #5
0
def scalar_field_to_raster(fa, lats, lons, wisdom):
    """
    Render a scalar variable into a geolocated raster and colorbar.

    :param fa: the field to render
    :param lats: the latitudes
    :param lons: the longitudes
    :param wisdom: a configuration dictionary controlling the visualization
    :return: a tuple with the raster as StringIO, its geolocation and the PNG colorbar StringIO (None if not requested)
    """
    # gather wisdom about the variable
    native_unit = wisdom['native_unit']
    cmap_name = wisdom['colormap']
    cmap = mpl.cm.get_cmap(cmap_name)

    if lats.shape != fa.shape:
        raise PostprocError(
            "Variable size %s does not correspond to grid size %s." %
            (fa.shape, lats.shape))

    logging.info('scalar_field_to_raster: variable %s min %s max %s' %
                 (var, np.nanmin(fa), np.nanmax(fa)))

    # mask 'transparent' color value
    if 'transparent_values' in wisdom:
        rng = wisdom['transparent_values']
        fa = np.ma.masked_array(fa, np.logical_and(fa >= rng[0], fa <= rng[1]))
        logging.info(
            'scalar_field_to_raster: transparent from %s to %, elements %s n))ot masked %s'
            % (rng[0], rng[1], fa.size, fa.count()))
        logging.info(
            'scalar_field_to_raster: masked variable %s min %s max %s' %
            (var, np.nanmin(fa), np.nanmax(fa)))

    # look at mins and maxes, transparent don't count
    if fa.count():
        fa_min, fa_max = np.nanmin(fa), np.nanmax(fa)
    else:
        fa_min, fa_max = 0.0, 0.0

    # determine if we will use the range in the variable or a fixed range
    scale = wisdom['scale']
    if scale != 'original':
        fa_min, fa_max = scale[0], scale[1]
        fa[fa < fa_min] = fa_min
        fa[fa > fa_max] = fa_max

    # only create the colorbar if requested
    cb_png_data = None
    if wisdom['colorbar'] is not None:
        if scale == 'original':
            logging.warning(
                'postprocessor: Colorbar %s %s specified with scaling original'
                % (wisdom['name'], wisdom['colorbar']))
            logging.warning(
                'postprocessor: Colorbar is not updated by the online visualization system correctly, use only with explicit scaling'
            )
        cb_unit = wisdom['colorbar']
        cbu_min, cbu_max = convert_value(native_unit, cb_unit,
                                         fa_min), convert_value(
                                             native_unit, cb_unit, fa_max)
        #  colorbar + add it to the KMZ as a screen overlay
        cb_png_data = make_colorbar([cbu_min, cbu_max], 'vertical', 2, cmap,
                                    wisdom['name'] + ' ' + cb_unit)

    # create the raster & get coordinate bounds
    raster_png_data, corner_coords = basemap_raster_mercator(
        lons, lats, fa, fa_min, fa_max, cmap)

    return raster_png_data, corner_coords, cb_png_data
Example #6
0
    def _scalar2raster(self, d, var, tndx):
        """
        Convert a single variable into a raster and colorbar.

        :param d: the netcdf file
        :param var: the variable name
        :param tndx: the time index
        :return: two StringIO objects, first is the PNG raster, second is PNG colorbar
        """
        # gather wisdom about the variable
        wisdom = get_wisdom(var).copy()
        wisdom.update(self.wisdom_update.get(var, {}))
        native_unit = wisdom['native_unit']
        cmap_name = wisdom['colormap']
        cmap = mpl.cm.get_cmap(cmap_name)

        # extract variable
        fa = wisdom['retrieve_as'](
            d,
            tndx)  # this calls a lambda defined to read the required 2d field
        lat, lon = wisdom['grid'](d)

        if lat.shape != fa.shape:
            raise PostprocError(
                "Variable %s size does not correspond to grid size." % var)

        # check for 'transparent' color value and mask
        if 'transparent_values' in wisdom:
            rng = wisdom['transparent_values']
            logging.info(
                '_scalar2raster: variable %s min %s max %s masking transparent from %s to %s'
                % (var, np.nanmin(fa), np.nanmax(fa), rng[0], rng[1]))
            fa = np.ma.masked_array(fa,
                                    np.logical_and(fa >= rng[0], fa <= rng[1]))
        else:
            fa = np.ma.masked_array(fa)

        # create the raster & get coordinate bounds

        # look at mins and maxes
        # fa_min,fa_max = np.nanmin(fa),np.nanmax(fa)
        # look at mins and maxes, transparent don't count
        fa_min, fa_max = np.nanmin(fa), np.nanmax(fa)

        # determine if we will use the range in the variable or a fixed range
        scale = wisdom['scale']
        if scale != 'original':
            m = fa.mask.copy(
            )  # save mask, resetting values below will destroy the mask
            fa_min, fa_max = scale[0], scale[1]
            fa[fa < fa_min] = fa_min
            fa[fa > fa_max] = fa_max
            fa.mask = m  # restore the mask

        # only create the colorbar if requested
        cb_png_data = None
        if wisdom['colorbar'] is not None:
            cb_unit = wisdom['colorbar']
            cbu_min, cbu_max = convert_value(native_unit, cb_unit,
                                             fa_min), convert_value(
                                                 native_unit, cb_unit, fa_max)
            #  colorbar + add it to the KMZ as a screen overlay
            legend = wisdom['name'] + ' ' + cb_unit
            logging.info(
                '_scalar2raster: variable %s colorbar from %s to %s %s' %
                (var, cbu_min, cbu_max, legend))
            cb_png_data = make_colorbar([cbu_min, cbu_max], 'vertical', 2,
                                        cmap, legend)

        # replace masked values by nans just in case
        fa.data[fa.mask] = np.nan
        fa.fill_value = np.nan

        logging.info(
            '_scalar2raster: variable %s elements %s count %s not masked %s min %s max %s'
            % (var, fa.size, fa.count(), np.count_nonzero(fa.mask == False),
               np.nanmin(fa), np.nanmax(fa)))

        raster_png_data, corner_coords = basemap_raster_mercator(
            lon, lat, fa, fa_min, fa_max, cmap)

        return raster_png_data, corner_coords, cb_png_data