Exemplo n.º 1
0
def fls(self):
    """Make a High Resolution Overview RGB image composite from Seviri
    channels.
    """
    self.check_channels('IR_087', 'IR_120')

    # threshold for liquid clouds
    th_liquid_cloud = 1.8  # K
    # cloud_confidence_range
    ccr = 1.0  # K
    ch_diff = (th_liquid_cloud -
               (self['IR_120'] - self['IR_087']) - ccr) / (-2. * ccr)

    #print "min/max", self['IR_108'].data.min(), self['IR_108'].data.max()
    ## Filter for cold clouds
    #print type(self['IR_108'].data),type(ch_diff),
    #ch_diff.data[self['IR_108'].data<265.] = float('nan')

    min_data = ch_diff.data.min()
    max_data = ch_diff.data.max()
    print("min/max", min_data, max_data)

    from trollimage.image import Image as trollimage
    img = trollimage(ch_diff.data, mode="L", fill_value=(0, 0, 0))

    colormap = deepcopy(rainbow.reverse())
    #colormap.set_range(min_data, max_data)
    colormap.set_range(0, 1)
    print(colormap.values)
    img.colorize(colormap)

    return img
Exemplo n.º 2
0
def VIS006_minus_IR_016(self):
    """Make a colored image of the difference of two Seviri channels.
    """

    from trollimage.image import Image as trollimage

    self.check_channels('VIS006', 'IR_016')

    ch_diff = self['VIS006'] - self['IR_016']

    min_data = ch_diff.data.min()
    max_data = ch_diff.data.max()
    #print "    min/max", min_data, max_data

    img = trollimage(ch_diff.data, mode="L", fill_value=(0, 0, 0))

    cm = deepcopy(rainbow)
    #cm.set_range(min_data, max_data)
    cm.set_range(-15, 40)
    img.colorize(cm)

    #from trollimage.colormap import rdbu
    #rdbu.set_range(min_data, max_data)
    #rdbu.set_range(-50, +50)
    #rdbu.reverse()
    #img.colorize(rdbu)

    #greys.set_range(-70, -25)
    #greys.reverse()
    #cm2 = deepcopy(rainbow)
    #cm2.set_range(-25, 5)
    #my_cm = greys + cm2
    #img.colorize(my_cm)

    return img
Exemplo n.º 3
0
def IR_087_minus_IR_120(self):
    """Make a colored image of the difference of two Seviri channels.
    """

    from trollimage.image import Image as trollimage

    self.check_channels('IR_087', 'IR_120')

    ch_diff = self['IR_087'] - self['IR_120']

    min_data = ch_diff.data.min()
    max_data = ch_diff.data.max()
    print("min/max", min_data, max_data)

    img = trollimage(ch_diff.data, mode="L", fill_value=(0, 0, 0))

    cm2 = deepcopy(rainbow)
    cm2.set_range(min_data, max_data)
    cm2.set_range(-4, +4)
    cm2.reverse()
    img.colorize(cm2)

    #greys.set_range(-4, -1.5)
    #greys.reverse()
    #cm2 = deepcopy(rainbow)
    #cm2.set_range(-1.5, 1.5)
    #my_cm = greys + cm2
    #rdpu.set_range(1.5, 4)
    #my_cm2 = my_cm + rdpu
    #img.colorize(my_cm2)

    return img
def show_image(data, dataname, save_png, colors="rainbow", min_data=None, max_data=None, title=None, add_colorscale=True):
    if min_data is None:
        min_data=data.min()
    if max_data is None:
        max_data=data.max()
    img = trollimage(data, mode="L", fill_value=[0,0,0])
    colormap = get_colormap(colors, min_data, max_data)
    img.colorize(colormap)
    if title is not None:
        title_color=(255,255,255)
        from PIL import ImageFont
        from PIL import ImageDraw 
        PIL_image=img.pil_image()
        draw = ImageDraw.Draw(PIL_image)
        fontsize=18
        font = ImageFont.truetype("/usr/openv/java/jre/lib/fonts/LucidaTypewriterBold.ttf", fontsize)
        draw.text( (10, 10), title, title_color, font=font)

    if add_colorscale:
        dc = DecoratorAGG(PIL_image)
        colormap_r = colormap.reverse()
        dc.align_right()
        dc.write_vertically()
        dc.add_scale(colormap_r, extend=True, tick_marks=5, minor_tick_marks=1, line_opacity=100) #, tick_marks=tick_marks, minor_tick_marks=minor_tick_marks, unit=units
        
    show_or_save_image(PIL_image, save_png, dataname)
Exemplo n.º 5
0
def WV_062_minus_IR_108(self):
    """Make a colored image of the difference of two Seviri channels.
    """

    from trollimage.image import Image as trollimage

    self.check_channels('WV_062', 'IR_108')

    ch_diff = self['WV_062'] - self['IR_108']

    min_data = ch_diff.data.min()
    max_data = ch_diff.data.max()
    print("min/max", min_data, max_data)

    img = trollimage(ch_diff.data, mode="L", fill_value=(0, 0, 0))

    #from trollimage.colormap import rdbu
    #rdbu.set_range(min_data, max_data)
    #rdbu.set_range(-50, +50)
    #rdbu.reverse()
    #img.colorize(rdbu)

    greys.set_range(-70, -25)
    greys.reverse()
    cm2 = deepcopy(rainbow)
    cm2.set_range(-25, 5)
    my_cm = greys + cm2
    img.colorize(my_cm)

    return img
Exemplo n.º 6
0
def trichannel(self):
    """Make a colored image of the difference of three Seviri channels.
    """

    self.check_channels('IR_087', 'IR_108', 'IR_120')

    from trollimage.image import Image as trollimage

    ch_diff = self['IR_087'] - 2 * self['IR_108'] + self['IR_120']

    min_data = ch_diff.data.min()
    max_data = ch_diff.data.max()
    print("min/max", min_data, max_data)

    img = trollimage(ch_diff.data, mode="L", fill_value=(0, 0, 0))

    #from trollimage.colormap import rdbu
    #rdbu.set_range(min_data, max_data)
    #rdbu.set_range(-5, +5)
    #rdbu.reverse()
    #img.colorize(rdbu)

    greys.set_range(-9, -2.5)  # 2.5 according to Mecikalski, 2.2 maybe better?
    greys.reverse()
    cm2 = deepcopy(rainbow)
    cm2.set_range(-2.5, 1.34)
    my_cm = greys + cm2
    greys.set_range(1.34, 6)
    my_cm2 = my_cm + greys
    img.colorize(my_cm2)

    return img
Exemplo n.º 7
0
def IR_120_minus_IR_108(self):
    """Make a colored image of the difference of two Seviri channels.
    """

    from trollimage.image import Image as trollimage

    self.check_channels('IR_108', 'IR_120')

    ch_diff = self['IR_120'] - self['IR_108']

    min_data = ch_diff.data.min()
    max_data = ch_diff.data.max()
    print("min/max", min_data, max_data)

    img = trollimage(ch_diff.data, mode="L", fill_value=(0, 0, 0))

    #from trollimage.colormap import rdbu
    #rdbu.set_range(min_data, max_data)
    #rdbu.set_range(-5, +5)
    #rdbu.reverse()
    #img.colorize(rdbu)

    colormap = deepcopy(rainbow)
    #colormap.set_range(min_data, max_data)
    colormap.set_range(-5, +2)
    colormap.reverse()
    img.colorize(colormap)

    #greys.set_range(-6, -1.6)
    #greys.reverse()
    #cm2 = deepcopy(rainbow)
    #cm2.set_range(-1.6, 0.6)
    #my_cm = greys + cm2
    #greys.set_range(0.6, 2)
    #my_cm2 = my_cm + greys
    #img.colorize(my_cm2)

    return img
Exemplo n.º 8
0
def clouddepth(self):
    """Six tests for cloud depth according to Mecikalski 2010.
    Mecikalski et al. 2010, J. Appl. Meteo. Climatology
    Cloud Top Properties of Growing Cumulus prior to Convective Initiation
    as Measured by MeteoSat Second Generation, Part I: Infrared Fields 
    """

    from trollimage.image import Image as trollimage
    from my_composites import mask_clouddepth

    mask = mask_clouddepth(self)

    img = trollimage(mask, mode="L")
    min_data = mask.min()
    max_data = mask.max()

    print("    use trollimage.image.image for colorized pictures (min=" +
          str(min_data) + ", max=" + str(max_data) + ")")
    cm2 = deepcopy(rainbow)
    cm2.set_range(min_data, max_data)
    img.colorize(cm2)

    return img
Exemplo n.º 9
0
local_data = global_data.project(area)

channel_image = False
if channel_image:
    img = local_data.image.channel_image(chn)
    filesave = time_slot.strftime(
        "MET9-RSS_COALITION2_germ-NPOL-COAL_%y%m%d%H%M.tif")  # %Y%m%d%H%M
else:
    from trollimage.image import Image as trollimage
    from trollimage.colormap import rainbow
    colormap = rainbow
    min_data = local_data[chn].data.min()
    max_data = local_data[chn].data.max()
    colormap.set_range(min_data, max_data)
    img = trollimage(local_data[chn].data, mode="L",
                     fill_value=[1, 1, 1])  # fill_value=[0,0,0]
    img.colorize(colormap)
    #PIL_image=img.pil_image()
    filesave = time_slot.strftime(
        "MET9-RSS_COAL2TROLL_germ-NPOL-COAL_%y%m%d%H%M.tif")

print("... type(img)", type(img))
print("... save file ", filesave)
img.save(filesave,
         fformat='mpop.imageo.formats.ninjotiff',
         ninjo_product_name="IR_108",
         chan_id=chan_id,
         data_source="MeteoSwiss COALITION2 algorithm",
         data_cat="GPRN",
         image_dt=time_slot)
"""
Exemplo n.º 10
0
        tick_marks = 100
        minor_tick_marks = 50
    elif prop_str == 'curr_per_lightning':
        units = 'I/kA'
        prop_str2 = 'current per lightning'
        tick_marks = 5
        minor_tick_marks = 1
    else:
        print("*** Error, unknown property", prop_str)

    if plot_type == 'trollimage':
        print('... use trollimage to ', method, ' plot data (min,max)=',
              min_data, max_data)
        if 'fill_value' in locals():
            img = trollimage(
                prop, mode="L",
                fill_value=fill_value)  # fillvalue    -> opaque background
        else:
            img = trollimage(prop,
                             mode="L")  # no fillvalue -> transparent backgroud

        #colorbar=prgn        # colorbars in trollimage-v0.3.0-py2.7.egg/trollimage/colormap.py
        #colorbar=rainbow
        #colorbar=pubugn
        #colorbar=brbg
        #colorbar=piyg   # too purple at the beginning
        #colorbar=spectral
        colorbar = greens2.reverse()
        colorbar.set_range(min_data, max_data)
        img.colorize(colorbar)
Exemplo n.º 11
0
# convert structure from data["CTTH"].height.data etc to data["CTH"].data
from my_msg_module import convert_NWCSAF_to_radiance_format
IS_PYRESAMPLE_LOADED = True
convert_NWCSAF_to_radiance_format(global_data, None, prop, nwcsaf_calibrate,
                                  IS_PYRESAMPLE_LOADED)

# project data to anther projection defined in etc/area.def
#area = get_area_def("euro")
data = global_data.project(area, precompute=True)

from trollimage.colormap import rainbow
colormap = rainbow

## normal convection is something like data[pge].height
## but after convert_NWCSAF_to_radiance_format we can use
#min_data = data[pge].height.data.min()
#max_data = data[pge].height.data.max()
#colormap.set_range(min_data, max_data)
#from trollimage.image import Image as trollimage
#img = trollimage(data[prop].height.data, mode="L", fill_value=[0,0,0])

min_data = data[prop].data.min()
max_data = data[prop].data.max()
colormap.set_range(min_data, max_data)
from trollimage.image import Image as trollimage
img = trollimage(data[prop].data, mode="L", fill_value=[0, 0, 0])

img.colorize(colormap)
img.show()
#img.save("nwcsaf_demo.png")
Exemplo n.º 12
0
print(global_data)

#area="EuropeCanaryS95"
area = "ccs4"
data = global_data.project(area, precompute=True)

from trollimage.colormap import rainbow
colormap = rainbow
#chn = 'VIS006'
chn = 'IR_108'
from trollimage.image import Image as trollimage

min_data = data[chn].data.min()
max_data = data[chn].data.max()
colormap.set_range(min_data, max_data)

img = trollimage(data[chn].data, mode="L", fill_value=[0, 0, 0])
img.colorize(colormap)
img.show()

from plot_coalition2 import downscale_array

data[chn].data = downscale_array(data[chn].data,
                                 mode='gaussian_225_125',
                                 mask=data[chn].data.mask)
img2 = trollimage(data[chn].data, mode="L", fill_value=[0, 0, 0])
img2.colorize(colormap)
img2.show()

#img.save("tmp.png")
Exemplo n.º 13
0
def create_PIL_image(rgb, data, in_msg):

    if in_msg.verbose:
        print("*** make image for: ", rgb)

    # get the data array that you want to plot
    if rgb in products.MSG:
        prop = data[rgb].data
        plot_type = 'channel_image'
    elif rgb in products.MSG_color:
        prop = data[rgb.replace("c", "")].data
        plot_type = 'trollimage'
    elif rgb in products.CTTH:
        prop = data[rgb].data
        prop.mask = (prop == 0)
        if rgb == 'CTH':
            prop /= 1000.  # 1000. == m -> km
        plot_type = 'trollimage'
    elif rgb in products.CT:
        prop = data[rgb].data
        plot_type = 'palette'
        if rgb == 'CT_QUALITY':
            plot_type = 'trollimage'
    elif rgb in products.CMa or rgb in products.SPhR:
        prop = data[rgb].data
        if hasattr(data[rgb], 'palette') and in_msg.nwcsaf_calibrate == False:
            plot_type = 'palette'
        else:
            plot_type = 'trollimage'
    elif rgb in products.HSAF:
        prop = data[rgb].data
        plot_type = 'trollimage'
    else:
        # includes products.RGBs_buildin
        prop = ma.asarray([-999., -999.])
        plot_type = 'user_defined'

    #from numpy import log10
    #prop=log10(prop)

    # search minimum and maximum
    # (default)
    min_data = prop.min()
    max_data = prop.max()
    # replace default with fixed min/max if specified

    if in_msg.fixed_minmax:
        if rgb in list(in_msg.rad_min.keys()):
            min_data = in_msg.rad_min[rgb]
        else:
            if rgb not in products.RGBs_buildin:
                print(
                    "*** Warning, no specified minimum for plotting in get_input_msg.py or input file"
                )
        if rgb in list(in_msg.rad_max.keys()):
            max_data = in_msg.rad_max[rgb]
        else:
            if rgb not in products.RGBs_buildin:
                print(
                    "*** Warning, no specified maximum for plotting in get_input_msg.py or input file"
                )
    if in_msg.verbose and rgb not in products.RGBs_buildin:
        print('... set value range from min_data (', min_data,
              ') to max_data (', max_data, ')')

    # specifies if a colorbar does make sense at all
    in_msg.colormap = {}

    # make the image
    if plot_type == 'channel_image':
        if in_msg.verbose:
            print(
                "    use data.image.channel_image for black and white pictures"
            )
        img = data.image.channel_image(rgb)
        in_msg.colormap[rgb] = None
    elif plot_type == 'trollimage':
        if in_msg.verbose:
            print(
                "    use trollimage.image.image for colorized pictures (min=" +
                str(min_data) + ", max=" + str(max_data) + ")")
        img = trollimage(prop, mode="L", fill_value=in_msg.fill_value)
        rainbow.set_range(min_data, max_data)
        img.colorize(rainbow)
        rainbow_r.set_range(
            min_data, max_data
        )  # attention set_range does modify the colormap, but does not have a return values !
        in_msg.colormap[rgb] = rainbow.reverse()
        # print "in_msg.colormap[rgb]", rgb, in_msg.colormap[rgb]
    elif plot_type == 'palette':
        min_data = 0.
        max_data = float(len(data[rgb].palette) - 1)
        if in_msg.verbose:
            print("    use GeoImage and colorize with a palette (min=" +
                  str(min_data) + ", max=" + str(max_data) + ")")
        img = GeoImage(prop,
                       data.area,
                       data.time_slot,
                       mode="P",
                       palette=data[rgb].palette,
                       fill_value=in_msg.fill_value)
        colormap = convert_palette2colormap(data[rgb].palette)
        colormap.set_range(min_data, max_data)  # no return value!
        in_msg.colormap[rgb] = colormap
    elif plot_type == 'user_defined':
        obj_image = get_image(data, rgb)
        if in_msg.verbose:
            print("    use image function defined by my_msg_module.py")
        img = obj_image()
        in_msg.colormap[rgb] = None
        #if rgb == 'ndvi':
        #   in_msg.colormap[rgb] = rdylgn_r
    else:
        print("*** Error in create_PIL_image (" +
              inspect.getfile(inspect.currentframe()) + ")")
        print("    unknown plot_type ", plot_type)
        quit()

    if in_msg.HRV_enhancement:
        if in_msg.verbose:
            print("enhance the image with the HRV channel")
        luminance = GeoImage((data["HRV"].data),
                             data.area,
                             data.time_slot,
                             crange=(0, 100),
                             mode="L")
        luminance.enhance(gamma=2.0)
        img.replace_luminance(luminance.channels[0])
        rgb = 'HR' + rgb

    ## alternative: for geoimages is possible to add coasts and borders, but not for trollimage
    #if hasattr(img, 'add_overlay'):
    #   if in_msg.verbose:
    #      print "    add coastlines to image by add_averlay"
    #   img.add_overlay(color=(0, 0, 0), width=0.5, resolution=None)

    # convert image to PIL image
    if hasattr(img, 'pil_image'):
        if in_msg.verbose:
            print("    convert to PIL_image by pil_image function")
        PIL_image = img.pil_image()
    else:
        if in_msg.verbose:
            print("    convert to PIL_image by saving and reading")
        tmp_file = outputDir + satS + '_' + dateS + '_' + timeS + '__' + area + '_' + rgb.replace(
            "_", "-") + '_tmp.png'  # in_msg.
        img.save(tmp_file)
        PIL_image = Image.open(tmp_file)
        subprocess.call("rm " + tmp_file, shell=True)

    return PIL_image
Exemplo n.º 14
0
def scatter_rad_rcz(in_msg):

    # get date of the last SEVIRI observation
    if in_msg.datetime is None:
        in_msg.get_last_SEVIRI_date()

    yearS = str(in_msg.datetime.year)
    #yearS = yearS[2:]
    monthS = "%02d" % in_msg.datetime.month
    dayS = "%02d" % in_msg.datetime.day
    hourS = "%02d" % in_msg.datetime.hour
    minS = "%02d" % in_msg.datetime.minute

    dateS = yearS + '-' + monthS + '-' + dayS
    timeS = hourS + '-' + minS

    if in_msg.sat_nr is None:
        in_msg.sat_nr = choose_msg(in_msg.datetime, in_msg.RSS)

    # check if PyResample is loaded
    try:
        # Work around for on demand import of pyresample. pyresample depends
        # on scipy.spatial which memory leaks on multiple imports
        IS_PYRESAMPLE_LOADED = False
        from pyresample import geometry
        from mpop.projector import get_area_def
        IS_PYRESAMPLE_LOADED = True
    except ImportError:
        LOGGER.warning(
            "pyresample missing. Can only work in satellite projection")

    if in_msg.datetime.year > 2012:
        if in_msg.sat_nr == 8:
            area_loaded = get_area_def("EuropeCanary35")
        elif in_msg.sat_nr == 9:  # rapid scan service satellite
            area_loaded = get_area_def("EuropeCanary95")
        elif in_msg.sat_nr == 10:  # default satellite
            area_loaded = get_area_def(
                "met09globeFull"
            )  # full disk service, like EUMETSATs NWC-SAF products
        elif in_msg.sat_nr == 0:  # fake satellite for reprojected ccs4 data in netCDF
            area_loaded = get_area_def("ccs4")  #
            #area_loaded = get_area_def("EuropeCanary")
            #area_loaded = get_area_def("alps")  # new projection of SAM
        else:
            print("*** Error, unknown satellite number ", in_msg.sat_nr)
            area_loaded = get_area_def("hsaf")  #
    else:
        if in_msg.sat_nr == 8:
            area_loaded = get_area_def("EuropeCanary95")
        elif in_msg.sat_nr == 9:  # default satellite
            area_loaded = get_area_def("EuropeCanary")

    # define contour write for coasts, borders, rivers
    cw = ContourWriterAGG(in_msg.mapDir)

    if type(in_msg.sat_nr) is int:
        sat_nr_str = str(in_msg.sat_nr).zfill(2)
    elif type(in_msg.sat_nr) is str:
        sat_nr_str = in_msg.sat_nr
    else:
        print("*** Waring, unknown type of sat_nr", type(in_msg.sat_nr))
        sat_nr_str = in_msg.sat_nr

    if in_msg.verbose:
        print('*** Create plots for ')
        print('    Satellite/Sensor: ' + in_msg.sat + '  ' + sat_nr_str)
        print('    Date/Time:        ' + dateS + ' ' + hourS + ':' + minS +
              'UTC')
        print('    RGBs:            ', in_msg.RGBs)
        print('    Area:            ', in_msg.areas)

    # check if input data is complete
    if in_msg.verbose:
        print("*** check input data")
    RGBs = check_input(in_msg, in_msg.sat + sat_nr_str, in_msg.datetime)
    if len(RGBs) != len(in_msg.RGBs):
        print("*** Warning, input not complete.")
        print("*** Warning, process only: ", RGBs)

    # define time and data object
    global_data = GeostationaryFactory.create_scene(in_msg.sat, sat_nr_str,
                                                    "seviri", in_msg.datetime)
    # print "type(global_data) ", type(global_data)   # <class 'mpop.scene.SatelliteInstrumentScene'>
    # print "dir(global_data)", dir(global_data)  [..., '__init__', ... 'area', 'area_def', 'area_id', 'channel_list', 'channels',
    #      'channels_to_load', 'check_channels', 'fullname', 'get_area', 'image', 'info', 'instrument_name', 'lat', 'load', 'loaded_channels',
    #      'lon', 'number', 'orbit', 'project', 'remove_attribute', 'satname', 'save', 'set_area', 'time_slot', 'unload', 'variant']

    global_data_radar = GeostationaryFactory.create_scene(
        "swissradar", "", "radar", in_msg.datetime)
    global_data_radar.load(['precip'])

    if len(RGBs) == 0:
        return RGBs

    if in_msg.verbose:
        print(
            "*** load satellite channels for " + in_msg.sat + sat_nr_str + " ",
            global_data.fullname)

    # initialize processed RGBs
    RGBs_done = []

    # load all channels / information
    for rgb in RGBs:
        if in_msg.verbose:
            print("    load prerequisites for: ", rgb)

        if rgb in products.MSG or rgb in products.MSG_color:
            for channel in products.MSG:
                if rgb.find(
                        channel
                ) != -1:  # if a channel name (IR_108) is in the rgb name (IR_108c)
                    if in_msg.verbose:
                        print("    load prerequisites by name: ", channel)
                    if in_msg.reader_level is None:
                        global_data.load(
                            [channel], area_extent=area_loaded.area_extent
                        )  # try all reader levels  load the corresponding data
                    else:
                        global_data.load([channel],
                                         area_extent=area_loaded.area_extent,
                                         reader_level=in_msg.reader_level
                                         )  # load the corresponding data

        if rgb in products.RGBs_buildin or rgb in products.RGBs_user:
            obj_image = get_image(global_data,
                                  rgb)  # find corresponding RGB image object
            if in_msg.verbose:
                print("    load prerequisites by function: ",
                      obj_image.prerequisites)
            global_data.load(
                obj_image.prerequisites,
                area_extent=area_loaded.area_extent)  # load prerequisites

        if rgb in products.CMa or rgb in products.CT or rgb in products.CTTH or rgb in products.SPhR:
            if rgb in products.CMa:
                pge = "CloudMask"
            elif rgb in products.CT:
                pge = "CloudType"
            elif rgb in products.CTTH:
                pge = "CTTH"
            elif rgb in products.SPhR:
                pge = "SPhR"
            else:
                print("*** Error in scatter_rad_rcz (" +
                      inspect.getfile(inspect.currentframe()) + ")")
                print("    unknown NWC-SAF PGE ", rgb)
                quit()
            if in_msg.verbose:
                print("    load NWC-SAF product: " + pge)
            global_data.load(
                [pge],
                calibrate=in_msg.nwcsaf_calibrate,
                reader_level="seviri-level3"
            )  # False, area_extent=area_loaded.area_extent (difficulties to find correct h5 input file)
            #print global_data.loaded_channels()
            #loaded_channels = [chn.name for chn in global_data.loaded_channels()]
            #if pge not in loaded_channels:
            #   return []
            if area_loaded != global_data[pge].area:
                print("*** Warning: NWC-SAF input file on a differnt grid (" +
                      global_data[pge].area.name +
                      ") than suggested input area (" + area_loaded.name + ")")
                print("    use " + global_data[pge].area.name +
                      " as standard grid")
                area_loaded = global_data[pge].area
            convert_NWCSAF_to_radiance_format(global_data, area_loaded, rgb,
                                              IS_PYRESAMPLE_LOADED)

        if rgb in products.HSAF:
            if in_msg.verbose:
                print("    load hsaf product by name: ", rgb)
            global_data.load(
                [rgb]
            )  # , area_extent=area_loaded.area_extent load the corresponding data

        if in_msg.HRV_enhancement:
            # load also the HRV channel (there is a check inside in the load function, if the channel is already loaded)
            if in_msg.verbose:
                print(
                    "    load additionally the HRV channel for HR enhancement")
            global_data.load(["HRV"], area_extent=area_loaded.area_extent)

    # loaded_channels = [chn.name for chn in global_data.loaded_channels()]
    # print loaded_channels

    # check if all prerequisites are loaded
    #rgb_complete = []
    #for rgb in RGBs:
    #   all_loaded = True
    #   if rgb in products.RGBs_buildin or rgb in products.RGB_user:
    #      obj_image = get_image(global_data, rgb)
    #      for pre in obj_image.prerequisites:
    #         if pre not in loaded_channels:
    #            all_loaded = False
    #   elif rgb in products.MSG_color:
    #      if rgb.replace("c","") not in loaded_channels:
    #         all_loaded = False
    #   else:
    #      if rgb not in loaded_channels:
    #         all_loaded = False
    #   if all_loaded:
    #      rgb_complete.append(rgb)
    #print "rgb_complete", rgb_complete

    # preprojecting the data to another area
    # --------------------------------------
    for area in in_msg.areas:
        print("")
        obj_area = get_area_def(area)
        if obj_area == area_loaded:
            if in_msg.verbose:
                print("*** Use data for the area loaded: ", area)
            #obj_area = area_loaded
            data = global_data
            resolution = 'l'
        else:
            if in_msg.verbose:
                print("*** Reproject data to area: ", area,
                      "(org projection: ", area_loaded.name, ")")
            obj_area = get_area_def(area)
            # PROJECT data to new area
            data = global_data.project(area)
            resolution = 'i'

        if in_msg.mapResolution is None:
            if area.find("EuropeCanary") != -1:
                resolution = 'l'
            if area.find("ccs4") != -1:
                resolution = 'i'
            if area.find("ticino") != -1:
                resolution = 'h'
        else:
            resolution = in_msg.mapResolution

        # define area
        proj4_string = obj_area.proj4_string
        # e.g. proj4_string = '+proj=geos +lon_0=0.0 +a=6378169.00 +b=6356583.80 +h=35785831.0'
        area_extent = obj_area.area_extent
        # e.g. area_extent = (-5570248.4773392612, -5567248.074173444, 5567248.074173444, 5570248.4773392612)
        area_tuple = (proj4_string, area_extent)

        # save reprojected data
        if area in in_msg.save_reprojected_data:  # and area != area_loaded
            _sat_nr = int(data.number) - 7 if int(data.number) - 7 > 0 else 0
            nc_dir = (
                global_data.time_slot.strftime(in_msg.reprojected_data_dir) % {
                    "area": area,
                    "msg": "MSG" + str(_sat_nr)
                })
            nc_file = (global_data.time_slot.strftime(
                in_msg.reprojected_data_filename) % {
                    "area": area,
                    "msg": "MSG" + str(_sat_nr)
                })
            ncOutputFile = nc_dir + nc_file
            # check if output directory exists, if not create it
            path = dirname(ncOutputFile)
            if not exists(path):
                if in_msg.verbose:
                    print('... create output directory: ' + path)
                makedirs(path)
            if in_msg.verbose:
                print("... save reprojected data: ncview " + ncOutputFile +
                      " &")
            #data.save(ncOutputFile, to_format="netcdf4", compression=False)
            data.save(ncOutputFile, band_axis=0, concatenate_bands=False)

        # mask for the cloud depths tests (masked data)
        #if area == 'ccs4':
        if area == False:
            print('... apply convective mask')
            mask_depth = data.image.mask_clouddepth()
            #print type(mask_depth.max)
            #print dir(mask_depth.max)
            index = where(
                mask_depth <
                5)  # less than 5 (of 6) tests successfull -> mask out
            for rgb in RGBs:
                if rgb in products.MSG_color:
                    rgb2 = rgb.replace("c", "")
                    data[rgb2].data.mask[index] = True
                    fill_value = data[rgb2].data.fill_value
                    #data["IR_108"].data[index] = fill_value

        #print "data[IR_108].data.min/max ", data["IR_108"].data.min(), data["IR_108"].data.max()
        #if rgb == "IR_108c":
        #   print type(data["IR_108"].data)
        #   print dir(data["IR_108"].data)
        #print data["IR_108"].data.mask

        # save average values
        if in_msg.save_statistics:
            mean_array = zeros(len(RGBs))
            #statisticFile = '/data/COALITION2/database/meteosat/ccs4/'+yearS+'/'+monthS+'/'+dayS+'/MSG_'+area+'_'+yearS[2:]+monthS+dayS+'.txt'
            statisticFile = './' + yearS + '-' + monthS + '-' + dayS + '/MSG_' + area + '_' + yearS[
                2:] + monthS + dayS + '.txt'
            if in_msg.verbose:
                print("*** write statistics (average values) to " +
                      statisticFile)
            f1 = open(statisticFile, 'a')  # mode append
            i_rgb = 0
            for rgb in RGBs:
                if rgb in products.MSG_color:
                    mean_array[i_rgb] = data[rgb.replace("c", "")].data.mean()
                    i_rgb = i_rgb + 1

            # create string to write
            str2write = dateS + ' ' + hourS + ' : ' + minS + ' UTC  '
            for mm in mean_array:
                str2write = str2write + ' ' + "%7.2f" % mm
            str2write = str2write + "\n"
            f1.write(str2write)
            f1.close()

        print("y.shape ", global_data_radar['precip'].data.shape)
        from numpy import copy
        y = copy(global_data_radar['precip'].data)
        y = y.ravel()
        print("y.shape ", y.shape)

        if 1 == 0:
            if 'X' in locals():
                del X
            from numpy import column_stack, append, concatenate
            for rgb in RGBs:
                # poor mans parallax correction
                if rgb in products.MSG_color:
                    rgb2 = rgb.replace("c", "")
                else:
                    rgb2 = rgb
                x1 = data[rgb2].data.ravel()
                if 'X' not in locals():
                    X = x1
                    X = [X]
                else:
                    concatenate((X, [x1]), axis=0)
                print("X.shape ", X.shape)
            X = append(X, [[1] * len(x1)], axis=1)

            print("y.shape ", y.shape)
            #theta = np.linalg.lstsq(X,y)[0]
            return

            ind_gt_1 = y > 1
            x = x[ind_gt_1]
            y = y[ind_gt_1]
            ind_lt_200 = y < 200
            x = x[ind_lt_200]
            y = y[ind_lt_200]

            #ind_gt_0 = x>0
            #x = x[ind_gt_0]
            #y = y[ind_gt_0]

            #X = np.column_stack(x+[[1]*len(x[0])])
            #beta_hat = np.linalg.lstsq(X,y)[0]
            #print beta_hat
            #X_hat= np.dot(X,theta)
            #y_hat = X_hat.reshape((640, 710))

        # creating plots/images
        if in_msg.make_plots:

            ind_cloudy = data['CTH'].data > 0
            ind_clear = data['CTH'].data <= 0
            ind_cloudy = ind_cloudy.ravel()

            for rgb in RGBs:

                if rgb in products.MSG_color:
                    rgb2 = rgb.replace("c", "")
                else:
                    rgb2 = rgb
                if rgb == 'ir108':
                    rgb2 = 'IR_108'

                # poor mans parallax correction
                if 1 == 0:
                    print("... poor mans parallax correction")
                    data[rgb2].data[25:640, :] = data[rgb2].data[0:615, :]
                    #data[rgb2].data[15:640,:] = data[rgb2].data[0:625,:]
                    data[rgb2].data[:, 0:700] = data[rgb2].data[:, 10:710]

                # create output filename
                outputDir = format_name(in_msg.outputDir,
                                        data.time_slot,
                                        area=area,
                                        rgb=rgb,
                                        sat_nr=data.number)
                outputFile = outputDir + format_name(in_msg.outputFile,
                                                     data.time_slot,
                                                     area=area,
                                                     rgb=rgb,
                                                     sat_nr=data.number)

                PIL_image = create_PIL_image(
                    rgb, data, in_msg
                )  # !!! in_msg.colorbar[rgb] is initialized inside (give attention to rgbs) !!!

                if 1 == 1:
                    y = copy(global_data_radar['precip'].data)
                    ind_gt_300 = y > 300  # replace no rain marker with 0mm/h
                    y[ind_gt_300] = 0

                    y = y.ravel()
                    print("y.shape ", y.shape)

                    x = copy(data[rgb2].data)
                    x = x.ravel()

                    ## get rid of clear sky
                    x = x[ind_cloudy]
                    y = y[ind_cloudy]
                    #ind_gt_01 = x>0.1
                    #x = x[ind_gt_01]
                    #y = y[ind_gt_01]

                    # get rid of no rain limits for rainfall
                    ind_gt_01 = y > 0.1
                    x = x[ind_gt_01]
                    y = y[ind_gt_01]
                    ind_lt_300 = y < 300
                    x = x[ind_lt_300]
                    y = y[ind_lt_300]

                    plt.figure()
                    plt.title('Scatterplot precipitation - radiance')
                    plt.xlabel(rgb)
                    plt.ylabel('precipitation in mm/h')
                    plt.scatter(x, y)  #, s=area, c=colors, alpha=0.5
                    outputDir = format_name(in_msg.outputDir,
                                            data.time_slot,
                                            area=area,
                                            rgb=rgb,
                                            sat_nr=data.number)
                    outputFileScatter = outputDir + format_name(
                        'scatterplot_%(area)s_%Y%m%d%H%M_%(rgb)s_precip_pc.png',
                        data.time_slot,
                        area=area,
                        rgb=rgb,
                        sat_nr=data.number)
                    #plt.show()
                    from numpy import arange
                    x_line = arange(x.min(), x.max(), 1)
                    print("*** display " + outputFileScatter + " &")
                    from numpy import ones, linalg, array
                    print(x.min(), x.max(), y.min(), y.max())
                    A = array([x, ones(x.size)])
                    w = linalg.lstsq(A.T, y)[0]  # obtaining the parameters
                    y_line = w[0] * x_line + w[1]  # regression line
                    #---
                    #from scipy import stats
                    #slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
                    #print "slope, intercept, r_value, p_value, std_err"
                    #print slope, intercept, r_value, p_value, std_err
                    #y_line = slope*x_line + intercept
                    from pylab import plot
                    plot(x_line, y_line, 'r-')
                    plt.savefig(outputFileScatter)
                    y_hat = w[0] * data[rgb2].data + w[1]
                    print("y_hat.shape: ", y_hat.shape)

                    # set clear sky to 0
                    y_hat[ind_clear] = 0
                    y_hat = ma.asarray(y_hat)
                    y_hat.mask = (y_hat == 9999.9) | (y_hat <= 0.0001)

                    from trollimage.colormap import RainRate
                    colormap = rainbow
                    min_data = 0.0
                    #max_data=y_hat.max()
                    max_data = 8
                    colormap.set_range(min_data, max_data)
                    #colormap = RainRate
                    in_msg.colormap[rgb] = colormap
                    units = 'mm/h'
                    img = trollimage(y_hat, mode="L")
                    img.colorize(in_msg.colormap[rgb])
                    in_msg.colormap[rgb] = colormap.reverse()
                    PIL_image = img.pil_image()
                    outputFile = outputDir + format_name(
                        'fit_%(area)s_%Y%m%d%H%M_%(rgb)s_precip.png',
                        data.time_slot,
                        area=area,
                        rgb=rgb,
                        sat_nr=data.number)
                    #PIL_image.save(outputFile)

                ## add coasts, borders, and rivers, database is heree
                ## http://www.soest.hawaii.edu/pwessel/gshhs/index.html
                ## possible resolutions
                ## f  full resolution: Original (full) data resolution.
                ## h  high resolution: About 80 % reduction in size and quality.
                ## i  intermediate resolution: Another ~80 % reduction.
                ## l  low resolution: Another ~80 % reduction.
                ## c  crude resolution: Another ~80 % reduction.
                if in_msg.add_rivers:
                    if in_msg.verbose:
                        print("    add rivers to image (resolution=" +
                              resolution + ")")
                    cw.add_rivers(PIL_image,
                                  area_tuple,
                                  outline='blue',
                                  resolution=resolution,
                                  outline_opacity=127,
                                  width=0.5,
                                  level=5)  #
                    if in_msg.verbose:
                        print("    add lakes to image (resolution=" +
                              resolution + ")")
                    cw.add_coastlines(PIL_image,
                                      area_tuple,
                                      outline='blue',
                                      resolution=resolution,
                                      outline_opacity=127,
                                      width=0.5,
                                      level=2)  #, outline_opacity=0
                if in_msg.add_borders:
                    if in_msg.verbose:
                        print("    add coastlines to image (resolution=" +
                              resolution + ")")
                    cw.add_coastlines(PIL_image,
                                      area_tuple,
                                      outline=(255, 0, 0),
                                      resolution=resolution,
                                      width=1)  #, outline_opacity=0
                    if in_msg.verbose:
                        print("    add borders to image (resolution=" +
                              resolution + ")")
                    cw.add_borders(PIL_image,
                                   area_tuple,
                                   outline=(255, 0, 0),
                                   resolution=resolution,
                                   width=1)  #, outline_opacity=0

                #if area.find("EuropeCanary") != -1 or area.find("ccs4") != -1:
                dc = DecoratorAGG(PIL_image)

                # add title to image
                if in_msg.add_title:
                    PIL_image = add_title(PIL_image, rgb, int(data.number),
                                          dateS, hourS, minS, area, dc,
                                          in_msg.font_file, in_msg.verbose)

                # add MeteoSwiss and Pytroll logo
                if in_msg.add_logos:
                    if in_msg.verbose:
                        print('... add logos')
                    dc.align_right()
                    if in_msg.add_colorscale:
                        dc.write_vertically()
                    dc.add_logo("../logos/meteoSwiss3.jpg", height=60.0)
                    dc.add_logo("../logos/pytroll3.jpg", height=60.0)

                # add colorscale
                if in_msg.add_colorscale and in_msg.colormap[rgb] is not None:

                    dc.align_right()
                    dc.write_vertically()
                    font_scale = aggdraw.Font(
                        "black",
                        "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-Bold.ttf",
                        size=16)

                    # get tick marks
                    tick_marks = 20  # default
                    minor_tick_marks = 5  # default
                    if rgb in list(in_msg.tick_marks.keys()):
                        tick_marks = in_msg.tick_marks[rgb]
                    if rgb in list(in_msg.minor_tick_marks.keys()):
                        minor_tick_marks = in_msg.minor_tick_marks[rgb]
                    if rgb.find(
                            "-"
                    ) != -1:  # for channel differences use tickmarks of 1
                        tick_marks = 1
                        minor_tick_marks = 1

                    tick_marks = 2  # default
                    minor_tick_marks = 1  # default

                    if in_msg.verbose:
                        print('... add colorscale')
                    dc.add_scale(in_msg.colormap[rgb],
                                 extend=True,
                                 tick_marks=tick_marks,
                                 minor_tick_marks=minor_tick_marks,
                                 font=font_scale,
                                 line_opacity=100)  #, unit='T / K'

                ## test to plot a wind barb
                #import matplotlib.pyplot as plt
                #ax = plt.axes(PIL_image)
                #ax.barbs(0, 0, 20, 20, length=8, pivot='middle', barbcolor='red')
                #ax.barbs(8, 46, 20, 20, length=8, pivot='middle', barbcolor='red')

                # check if output directory exists, if not create it
                path = dirname(outputFile)
                if not exists(path):
                    if in_msg.verbose:
                        print('... create output directory: ' + path)
                    makedirs(path)

                # save file
                if in_msg.verbose:
                    print('... save final file :' + outputFile)
                PIL_image.save(outputFile,
                               optimize=True)  # optimize -> minimize file size

                if in_msg.compress_to_8bit:
                    if in_msg.verbose:
                        print('... compress to 8 bit image: display ' +
                              outputFile.replace(".png", "-fs8.png") + ' &')
                    subprocess.call("/usr/bin/pngquant -force 256 " +
                                    outputFile + " 2>&1 &",
                                    shell=True)  # 256 == "number of colors"

                #if in_msg.verbose:
                #   print "    add coastlines to "+outputFile

                ## alternative: reopen image and modify it (takes longer due to additional reading and saving)
                #cw.add_rivers_to_file(img, area_tuple, level=5, outline='blue', width=0.5, outline_opacity=127)
                #cw.add_coastlines_to_file(outputFile, obj_area, resolution=resolution, level=4)
                #cw.add_borders_to_file(outputFile, obj_area, outline=outline, resolution=resolution)

                # copy to another place
                if in_msg.scpOutput:
                    if in_msg.verbose:
                        print("... secure copy " + outputFile + " to " +
                              in_msg.scpOutputDir)
                    subprocess.call("scp " + in_msg.scpID + " " + outputFile +
                                    " " + in_msg.scpOutputDir + " 2>&1 &",
                                    shell=True)
                    if in_msg.compress_to_8bit:
                        if in_msg.verbose:
                            print("... secure copy " +
                                  outputFile.replace(".png", "-fs8.png") +
                                  " to " + in_msg.scpOutputDir)
                            subprocess.call(
                                "scp " + in_msg.scpID + " " +
                                outputFile.replace(".png", "-fs8.png") + " " +
                                in_msg.scpOutputDir + " 2>&1 &",
                                shell=True)

                if rgb not in RGBs_done:
                    RGBs_done.append(rgb)

        ## start postprocessing
        if area in in_msg.postprocessing_areas:
            postprocessing(in_msg, global_data.time_slot, data.number, area)

    if in_msg.verbose:
        print(" ")

    return RGBs_done
Exemplo n.º 15
0
from __future__ import print_function

from mpop.satellites import GeostationaryFactory
from mpop.projector import get_area_def
import datetime

time_slot = datetime.datetime(2014, 07, 16, 13, 30)
global_data = GeostationaryFactory.create_scene("meteosat", "09", "seviri",
                                                time_slot)
europe = get_area_def("EuropeCanary")

# load data
global_data.load(["vaa"], reader_level="seviri-level6")

prop = global_data["vaa"].data
plot_type = 'trollimage'
min_data = prop.min()
max_data = prop.max()
print(prop.shape, min_data, max_data)

from trollimage.colormap import rainbow

colormap = rainbow
colormap.set_range(min_data, max_data)

from trollimage.image import Image as trollimage

img = trollimage(prop, mode="L", fill_value=None)
img.colorize(colormap)
img.save("test_vaa.png")
Exemplo n.º 16
0
def estimate_cth(IR_108, cth_atm="standard"):
    '''
    Estimation of the cloud top height using the 10.8 micron channel
    limitations: this is the most simple approach
                 a simple fit of the ir108 to the temperature profile
                 * no correction for water vapour or any other trace gas
                 * no viewing angle dependency
                 * no correction for semi-transparent clouds

    optional input:
      cth_atm    * "standard", "tropics", "midlatitude summer", "midlatitude winter", "subarctic summer", "subarctic winter"
                  Matching the 10.8 micron temperature with atmosphere profile
                  (s)  AFGL atmospheric constituent profile. U.S. standard atmosphere 1976. (AFGL-TR-86-0110) 
                  (t)  AFGL atmospheric constituent profile. tropical.                      (AFGL-TR-86-0110)
                  (mw) AFGL atmospheric constituent profile. midlatitude summer.            (AFGL-TR-86-0110) 
                  (ms) AFGL atmospheric constituent profile. midlatitude winter.            (AFGL-TR-86-0110)
                  (ss) AFGL atmospheric constituent profile. subarctic summer.              (AFGL-TR-86-0110) 
                  (sw) AFGL atmospheric constituent profile. subarctic winter.              (AFGL-TR-86-0110)
                  Ulrich Hamann (MeteoSwiss)
                * "tropopause"
                  Assuming a fixed tropopause height and a fixed temperature gradient
                  Richard Mueller (DWD)
    output: 
      parallax corrected channel
                 the content of the channel will be parallax corrected.
                 The name of the new channel will be
                 *original_chan.name+'_PC'*, eg. "IR_108_PC". This name is
                 also stored to the info dictionary of the originating channel.

    Versions: 05.07.2016 initial version
              Ulrich Hamann (MeteoSwiss), Richard Mueller (DWD)
    '''

    print "*** estimating CTH using the 10.8 micro meter brightness temperature "

    if cth_atm.lower() != "tropopause":

        # define atmospheric temperature profile
        import os
        from numpy import loadtxt, zeros, where, logical_and
        import mpop

        mpop_dir = os.path.dirname(mpop.__file__)
        afgl_file = mpop_dir + "/afgl.dat"
        print "... assume ", cth_atm, " atmosphere for temperature profile"

        if cth_atm.lower() == "standard" or cth_atm.lower() == "s":
            z, T = loadtxt(afgl_file,
                           usecols=(0, 1),
                           unpack=True,
                           comments="#")
        elif cth_atm.lower() == "tropics" or cth_atm.lower() == "t":
            z, T = loadtxt(afgl_file,
                           usecols=(0, 2),
                           unpack=True,
                           comments="#")
        elif cth_atm.lower() == "midlatitude summer" or cth_atm.lower(
        ) == "ms":
            z, T = loadtxt(afgl_file,
                           usecols=(0, 3),
                           unpack=True,
                           comments="#")
        elif cth_atm.lower() == "midlatitude winter" or cth_atm.lower(
        ) == "ws":
            z, T = loadtxt(afgl_file,
                           usecols=(0, 4),
                           unpack=True,
                           comments="#")
        elif cth_atm.lower() == "subarctic summer" or cth_atm.lower() == "ss":
            z, T = loadtxt(afgl_file,
                           usecols=(0, 5),
                           unpack=True,
                           comments="#")
        elif cth_atm.lower() == "subarctic winter" or cth_atm.lower() == "ss":
            z, T = loadtxt(afgl_file,
                           usecols=(0, 6),
                           unpack=True,
                           comments="#")
        else:
            print "*** Error in estimate_cth (mpop/tools.py)"
            print "unknown temperature profiel for CTH estimation: cth_atm = ", cth_atm
            quit()

        height = zeros(IR_108.shape)
        # warmer than lowest level -> clear sky
        height[where(IR_108 > T[-1])] = -1.
        print "     z0(km)   z1(km)   T0(K)   T1(K)  number of pixels"
        print "------------------------------------------------------"
        for i in range(z.size)[::-1]:

            # search for temperatures between layer i-1 and i
            ind = np.where(logical_and(T[i - 1] < IR_108, IR_108 < T[i]))
            # interpolate CTH according to ir108 temperature
            height[ind] = z[i] + (IR_108[ind] -
                                  T[i]) / (T[i - 1] - T[i]) * (z[i - 1] - z[i])
            # verbose output
            print " {0:8.1f} {1:8.1f} {2:8.1f} {3:8.1f} {4:8d}".format(
                z[i], z[i - 1], T[i], T[i - 1], len(ind[0]))

            # if temperature increases above 8km -> tropopause detected
            if z[i] >= 8. and T[i] <= T[i - 1]:
                # no cloud above tropopose
                break
            # no cloud heights above 20km
            if z[i] >= 20.:
                break

        # if height is still 0 -> cloud colder than tropopause -> cth == tropopause height
        height[np.where(height == 0)] = z[i]

    else:

        Htropo = 11.0  # km
        # this is an assumption it should be optimized
        # by making it dependent on region and season.
        # It might be good to include the ITC in the
        # region of interest, that would make a fixed Htropo
        # value more reliable.
        Tmin = np.amin(IR_108)
        # for Tmin it might be better to use the 5th or 10th percentile
        # else overshoting tops induces further uncertainties
        # in the calculation of the cloud height.
        # However numpy provides weird results for 5th percentile.
        # Hence, for the working version the minima is used

        print "... assume tropopause height ", Htropo, ", tropopause temperature ", Tmin, "K (", Tmin - 273.16, "deg C)"
        print "    and constant temperature gradient 6.5 K/km"

        height = -(IR_108 - Tmin) / 6.5 + Htropo
        # calculation of the height, the temperature gradient
        # 6.5 K/km is an assumption
        # derived from USS and MPI standard profiles. It
        # has to be improved as well

    # convert to masked array
    # convert form km to meter
    height = np.ma.masked_where(height <= 0, height, copy=False) * 1000.

    if False:
        from trollimage.image import Image as trollimage
        from trollimage.colormap import rainbow
        from copy import deepcopy
        # cloud top height
        prop = height
        min_data = prop.min()
        max_data = prop.max()
        print " estimated CTH(meter) (min/max): ", min_data, max_data
        min_data = 0
        max_data = 12000
        colormap = deepcopy(rainbow)
        colormap.set_range(min_data, max_data)
        img = trollimage(prop, mode="L")  #, fill_value=[0,0,0]
        img.colorize(colormap)
        img.show()

    # return cloud top height in meter
    return height
Exemplo n.º 17
0
glbd.load(varnames)  #, area_extent=area.area_extent

varname=varnames[0]
print (glbd[varname].data.shape)
print (glbd[varname].data)
max_data = glbd[varname].data.max()
min_data = glbd[varname].data.min()


from trollimage.image import Image as trollimage
from trollimage.colormap import rainbow
colormap=rainbow

if False:
    img = trollimage(glbd[varname].data, mode="L")  # , fill_value=0
    if colormap.values[0] == 0.0 and colormap.values[-1]==1.0:  # scale normalized colormap to range of data 
        colormap.set_range(min_data, max_data)
    img.colorize(colormap)
    img.show()

area="ccs4" #
local_data = glbd.project(area)

print(local_data[varname].data.shape)

if True:
    for varname in varnames:
      img = trollimage(local_data[varname].data, mode="L")  # , fill_value=0
      if  colormap.values[0] == 0.0 and colormap.values[-1]==1.0:  # scale normalized colormap to range of data 
          colormap.set_range(min_data, max_data)
Exemplo n.º 18
0
    #        scale = conf.get(radar_product, "scale")
    #        print '... read color scale from ', scale
    #        break
    ##scale = conf.get(prop_str, "scale")
    #colorscale = np.loadtxt(scale, skiprows=1)
    #print colorscale
    #print colorscale.shape, type(colorscale)
    #print colorscale[1:,4], type(colorscale[0,:])
    #print colorscale[1:,1:4]    
    
else:
    print("*** ERROR, unknown color mode")

print('... use trollimage to ', method,' plot data (min,max)=',min_data, max_data)
if 'fill_value' in locals():
    img = trollimage(prop, mode="L", fill_value=fill_value) 
else:
    img = trollimage(prop, mode="L")
 
img.colorize(colormap)

PIL_image=img.pil_image()
dc = DecoratorAGG(PIL_image)

# define contour write for coasts, borders, rivers
cw = ContourWriterAGG('/data/OWARNA/hau/maps_pytroll/')

resolution='l'
if area.find("EuropeCanary") != -1:
    resolution='l'
if area.find("ccs4") != -1:
Exemplo n.º 19
0
#europe = get_area_def(area)
local_radar = global_radar.project(area, precompute=True)

fill_value = None  # transparent background
#fill_value=(1,1,1) # white background
min_radar = 0.0
max_radar = 150
from trollimage.colormap import RainRate
colormap = RainRate

LOG.debug("... min_radar/max_radar: " + str(min_radar) + " / " +
          str(max_radar))
colormap.set_range(min_radar, max_radar)

from trollimage.image import Image as trollimage
img = trollimage(local_radar[prop_str].data, mode="L", fill_value=fill_value)
img.colorize(colormap)
# img.show()

PIL_image = img.pil_image()
from pycoast import ContourWriterAGG
cw = ContourWriterAGG('/opt/users/common/shapes/')
obj_area = get_area_def(area)
area_def = (obj_area.proj4_string, obj_area.area_extent)
resolution = 'l'
cw.add_coastlines(PIL_image,
                  area_def,
                  outline='white',
                  resolution=resolution,
                  outline_opacity=127,
                  width=1,
Exemplo n.º 20
0
from trollimage.image import Image as trollimage

import datetime

debug_on()

time_slot = datetime.datetime(2015, 7, 8, 12, 00)

global_data = GeostationaryFactory.create_scene("meteosat", "09", "seviri",
                                                time_slot)

global_data.load(["CloudType"], calibrate=False)

print(global_data)

prod = "CloudType"

global_data = global_data.project("ccs4", precompute=True)

img = trollimage(global_data[prod].cloudtype,
                 mode="P",
                 palette=global_data[prod].cloudtype_palette)

img.save('./CTtest.png')

img = trollimage(global_data[prod].cloudphase,
                 mode="P",
                 palette=global_data[prod].cloudphase_palette)

img.save('./CT_PHASEtest.png')
    max_data = 70
    colormap = rainbow
    lower_value = 13

if prop_str == 'ACRR':
    min_data = 0
    max_data = 250
    lower_value = 0.15

if lower_value > -1000:
    prop[prop < lower_value] = np.ma.masked

LOG.debug("min_data/max_data: " + str(min_data) + " / " + str(max_data))
colormap.set_range(min_data, max_data)

img = trollimage(prop, mode="L", fill_value=fill_value)
img.colorize(colormap)
PIL_image = img.pil_image()
dc = DecoratorAGG(PIL_image)

resolution = 'l'

if False:
    cw = ContourWriterAGG('/data/OWARNA/hau/pytroll/shapes/')
    cw.add_coastlines(PIL_image,
                      area_def,
                      outline='white',
                      resolution=resolution,
                      outline_opacity=127,
                      width=1,
                      level=2)  #, outline_opacity=0
Exemplo n.º 22
0
def estimate_cth(IR_108, cth_atm="standard"):

    '''
    Estimation of the cloud top height using the 10.8 micron channel
    limitations: this is the most simple approach
                 a simple fit of the ir108 to the temperature profile
                 * no correction for water vapour or any other trace gas
                 * no viewing angle dependency
                 * no correction for semi-transparent clouds

    optional input:
      cth_atm    * "standard", "tropics", "midlatitude summer", "midlatitude winter", "subarctic summer", "subarctic winter"
                  Matching the 10.8 micron temperature with atmosphere profile
                  (s)  AFGL atmospheric constituent profile. U.S. standard atmosphere 1976. (AFGL-TR-86-0110) 
                  (t)  AFGL atmospheric constituent profile. tropical.                      (AFGL-TR-86-0110)
                  (mw) AFGL atmospheric constituent profile. midlatitude summer.            (AFGL-TR-86-0110) 
                  (ms) AFGL atmospheric constituent profile. midlatitude winter.            (AFGL-TR-86-0110)
                  (ss) AFGL atmospheric constituent profile. subarctic summer.              (AFGL-TR-86-0110) 
                  (sw) AFGL atmospheric constituent profile. subarctic winter.              (AFGL-TR-86-0110)
                  Ulrich Hamann (MeteoSwiss)
                * "tropopause"
                  Assuming a fixed tropopause height and a fixed temperature gradient
                  Richard Mueller (DWD)
    output: 
      parallax corrected channel
                 the content of the channel will be parallax corrected.
                 The name of the new channel will be
                 *original_chan.name+'_PC'*, eg. "IR_108_PC". This name is
                 also stored to the info dictionary of the originating channel.

    Versions: 05.07.2016 initial version
              Ulrich Hamann (MeteoSwiss), Richard Mueller (DWD)
    '''

    print "*** estimating CTH using the 10.8 micro meter brightness temperature "

    if cth_atm.lower() != "tropopause":

        # define atmospheric temperature profile    
        import os
        from numpy import loadtxt, zeros, where, logical_and
        import mpop 

        mpop_dir = os.path.dirname(mpop.__file__)
        afgl_file = mpop_dir+"/afgl.dat"
        print "... assume ", cth_atm, " atmosphere for temperature profile"

        if cth_atm.lower()=="standard" or cth_atm.lower()=="s":
            z, T = loadtxt(afgl_file, usecols=(0, 1), unpack=True, comments="#")
        elif cth_atm.lower()=="tropics" or cth_atm.lower()=="t":
            z, T = loadtxt(afgl_file, usecols=(0, 2), unpack=True, comments="#")
        elif cth_atm.lower()=="midlatitude summer" or cth_atm.lower()=="ms":
            z, T = loadtxt(afgl_file, usecols=(0, 3), unpack=True, comments="#")
        elif cth_atm.lower()=="midlatitude winter" or cth_atm.lower()=="ws":
            z, T = loadtxt(afgl_file, usecols=(0, 4), unpack=True, comments="#")
        elif cth_atm.lower()=="subarctic summer" or cth_atm.lower()=="ss":
            z, T = loadtxt(afgl_file, usecols=(0, 5), unpack=True, comments="#")
        elif cth_atm.lower()=="subarctic winter" or cth_atm.lower()=="ss":
            z, T = loadtxt(afgl_file, usecols=(0, 6), unpack=True, comments="#")
        else:
            print "*** Error in estimate_cth (mpop/tools.py)"
            print "unknown temperature profiel for CTH estimation: cth_atm = ", cth_atm
            quit()

        height = zeros(IR_108.shape)
        # warmer than lowest level -> clear sky 
        height[where(IR_108 > T[-1])] = -1.
        print "     z0(km)   z1(km)   T0(K)   T1(K)  number of pixels"
        print "------------------------------------------------------"
        for i in range(z.size)[::-1]:

            # search for temperatures between layer i-1 and i
            ind =  np.where( logical_and( T[i-1]< IR_108, IR_108 < T[i]) )
            # interpolate CTH according to ir108 temperature
            height[ind] = z[i] + (IR_108[ind]-T[i])/(T[i-1]-T[i]) * (z[i-1]-z[i])
            # verbose output
            print " {0:8.1f} {1:8.1f} {2:8.1f} {3:8.1f} {4:8d}".format(z[i], z[i-1], T[i], T[i-1], len(ind[0]))

            # if temperature increases above 8km -> tropopause detected
            if z[i]>=8. and T[i] <= T[i-1]:
                # no cloud above tropopose
                break
            # no cloud heights above 20km
            if z[i]>=20.:
                break

        # if height is still 0 -> cloud colder than tropopause -> cth == tropopause height
        height[np.where( height == 0 )] = z[i]
        
    else:

        Htropo=11.0 # km
        # this is an assumption it should be optimized 
        # by making it dependent on region and season. 
        # It might be good to include the ITC in the  
        # region of interest, that would make a fixed Htropo 
        # value more reliable. 
        Tmin = np.amin(IR_108) 
        # for Tmin it might be better to use the 5th or 10th percentile 
        # else overshoting tops induces further uncertainties  
        # in the calculation of the cloud height. 
        # However numpy provides weird results for 5th percentile. 
        # Hence, for the working version the minima is used 

        print "... assume tropopause height ", Htropo, ", tropopause temperature ", Tmin, "K (", Tmin-273.16, "deg C)"
        print "    and constant temperature gradient 6.5 K/km"

        height = -(IR_108 - Tmin)/6.5 + Htropo 
        # calculation of the height, the temperature gradient 
        # 6.5 K/km is an assumption  
        # derived from USS and MPI standard profiles. It 
        # has to be improved as well 

    # convert to masked array
    # convert form km to meter
    height = np.ma.masked_where(height <= 0, height, copy=False) * 1000.

    if False:
        from trollimage.image import Image as trollimage
        from trollimage.colormap import rainbow
        from copy import deepcopy 
        # cloud top height
        prop = height
        min_data = prop.min()
        max_data = prop.max()
        print " estimated CTH(meter) (min/max): ", min_data, max_data
        min_data =     0
        max_data = 12000    
        colormap = deepcopy(rainbow)
        colormap.set_range(min_data, max_data)
        img = trollimage(prop, mode="L") #, fill_value=[0,0,0]
        img.colorize(colormap)
        img.show()

    # return cloud top height in meter
    return height
Exemplo n.º 23
0
            pge = get_NWC_pge_name(chn)
            global_data.load([pge], calibrate=True, reader_level="seviri-level3") 
            convert_NWCSAF_to_radiance_format(global_data, None, chn, True, True)
        else:
            global_data.load([chn])
        
        # Resample to Plate Caree.
        local_data = global_data.project(area_out, mode='quick', precompute=True)

        from trollimage.image import Image as trollimage
        from trollimage.colormap import rainbow
        colormap = rainbow
        min_data = local_data[chn].data.min()
        max_data = local_data[chn].data.max()
        colormap.set_range(min_data, max_data)
        TROLL_imgage = trollimage(local_data[chn].data, mode="L", fill_value=None) # fill_value=[0,0,0]
        TROLL_imgage.colorize(colormap)
        PIL_image = TROLL_imgage.pil_image()
        from mpop.projector import get_area_def
        area_def = get_area_def(area_out)
        GEO_image = pilimage2geoimage(PIL_image, area_def, TIMESLOT)
        if True:
            filesave = TIMESLOT.strftime("MET"+SATNO+"_RSS_"+ product_name+"______"+area_out+"_%Y%m%d%H%M.tif")
            LOG.info("Saving %s as Ninjo tif" % filesave)
            GEO_image.save(filesave,
                           fformat='mpop.imageo.formats.ninjotiff',
                           ninjo_product_name=ninjo_product_name,
                           nbits=BITS_PER_SAMPLE)
        else:
            filesave = TIMESLOT.strftime("MET"+SATNO+"_RSS_"+ product_name+"______"+area_out+"_%Y%m%d%H%M.png")
            LOG.info("Saving %s as png" % filesave)
Exemplo n.º 24
0
            TRT_tmp.iloc[0]['TRT_Rank_pred|30'],
            TRT_tmp.iloc[0]['TRT_Rank_pred|35'],
            TRT_tmp.iloc[0]['TRT_Rank_pred|40'],
            TRT_tmp.iloc[0]['TRT_Rank_pred|45']
        ]
        print(Rank_predicted)
    else:
        Rank_predicted = None
else:
    Rank_predicted = None
#print ("Rank_predicted:", Rank_predicted)

if old_style:
    print('... use trollimage to ', method, ' plot data (min,max)=', min_data,
          max_data)
    img = trollimage(
        prop, mode="L")  # , fill_value=(0,0,0)  add black background color
    #colormap   = rainbow
    #colormap_r = rainbow.reverse()
    if True:
        colormap = TRT
        colormap_r = TRT.reverse()
        colorscale = True
        black_vel = False
    else:
        colormap = black
        colormap_r = black
        colorscale = False
        black_vel = True
    colormap.set_range(min_data, max_data)
    img.colorize(colormap)
Exemplo n.º 25
0
        # check if all needed channels are loaded
        #for rgb in rgbs:
        if not check_loaded_channels(rgbs, data):
            print("*** Error in produce_forecast_nrt (" + current_file + ")")
            print("    missing data")
            quit()

        if False:
            from trollimage.image import Image as trollimage
            from trollimage.colormap import rainbow
            prop = data["IR_108"].data
            min_data = prop.min()
            max_data = prop.max()
            colormap = deepcopy(rainbow)
            colormap.set_range(min_data, max_data)
            img = trollimage(prop, mode="L", fill_value=[0, 0, 0])
            img.colorize(colormap)
            img.show()
            quit()

        if downscaling_data == True:
            from plot_coalition2 import downscale
            data = downscale(data, mode=mode_downscaling)

        # read wind field
        if wind_source == "HRW":
            u_d = np.zeros((n_levels, nx, ny))
            v_d = np.zeros((n_levels, nx, ny))
            for level in range(n_levels):
                p_max = p_min
                if level == n_levels - 1:
Exemplo n.º 26
0
time_slot = datetime.datetime(2015, 7, 9, 13, 00)

#area = get_area_def("alps")

global_data = GeostationaryFactory.create_scene("meteosat", "09", "seviri",
                                                time_slot)

prod = "SPhR"

global_data.load([prod], calibrate=False)

global_data = global_data.project("ccs4", precompute=True)

img = trollimage(global_data[prod].sphr_bl,
                 mode="P",
                 palette=global_data[prod].sphr_bl_palette)

img.save('./SPHR_BL_test.png')

img = trollimage(global_data[prod].sphr_hl,
                 mode="P",
                 palette=global_data[prod].sphr_hl_palette)

img.save('./SPHR_HL_test.png')

img = trollimage(global_data[prod].sphr_ki,
                 mode="P",
                 palette=global_data[prod].sphr_ki_palette)

img.save('./SPHR_KI_test.png')
Exemplo n.º 27
0
def create_trollimage(rgb,
                      prop,
                      colormap,
                      cw,
                      filename,
                      time_slot,
                      area,
                      fill_value=None,
                      composite_file=None,
                      background=None,
                      add_borders=True,
                      add_rivers=False,
                      resolution='l',
                      bits_per_pixel=8,
                      mask=None,
                      scpOutput=False):

    from trollimage.image import Image as trollimage

    fill_value = None
    img = trollimage(prop, mode="L", fill_value=fill_value)
    img.colorize(colormap)
    PIL_image = img.pil_image()

    # define area
    from mpop.projector import get_area_def
    obj_area = get_area_def(area)
    proj4_string = obj_area.proj4_string
    # e.g. proj4_string = '+proj=geos +lon_0=0.0 +a=6378169.00 +b=6356583.80 +h=35785831.0'
    area_extent = obj_area.area_extent
    # e.g. area_extent = (-5570248.4773392612, -5567248.074173444, 5567248.074173444, 5570248.4773392612)
    area_tuple = (proj4_string, area_extent)

    from plot_msg import add_borders_and_rivers
    add_borders_and_rivers(PIL_image,
                           cw,
                           area_tuple,
                           add_borders=add_borders,
                           add_rivers=add_rivers,
                           resolution=resolution,
                           verbose=False)

    # indicate mask
    if mask is not None:
        print("    indicate measurement mask")

        #from skimage import feature
        #mask = feature.canny(mask) - mask

        if True:
            # https://stackoverflow.com/questions/37365928/python-get-edges-from-multiple-400-binary-images-fast
            if isinstance(mask, np.ma.MaskedArray):
                image = mask.data
            else:
                image = mask
            from scipy.ndimage import maximum_filter, minimum_filter
            imax = (maximum_filter(image, size=3) != image)
            imin = (minimum_filter(image, size=3) != image)
            icomb = np.logical_or(imax, imin)
            edge = np.where(icomb, image, False)
            #edge = np.where(icomb,mask,np.nan)
            img = trollimage(~edge, mode="L",
                             fill_value=None)  #fill_value,[1,1,1], None
            from trollimage.colormap import greys
            img.colorize(greys)
            img.putalpha(edge)
            PIL_mask = img.pil_image()
            #PIL_mask.save("test_mask.png", optimize=True)
        else:
            # https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.signal.convolve2d.html
            from scipy import signal
            scharr = np.array([[-3 - 3j, 0 - 10j, +3 - 3j],
                               [-10 + 0j, 0 + 0j, +10 + 0j],
                               [-3 + 3j, 0 + 10j, +3 + 3j]])  # Gx + j*Gy
            grad = signal.convolve2d(mask,
                                     scharr,
                                     boundary='symm',
                                     mode='same')
            grad = np.absolute(grad)
            grad /= grad.max()
            edge = 1 - grad
            #print (mask.max(),mask.min())

            img = trollimage(edge, mode="L",
                             fill_value=None)  #fill_value,[1,1,1], None
            from trollimage.colormap import greys
            img.colorize(greys)

            ##img.putalpha(mask*0 + 0.5)
            img.putalpha((edge.max() - edge) * 0.5)
            PIL_mask = img.pil_image()

        from PIL import Image as PILimage
        PIL_image = PILimage.alpha_composite(PIL_mask, PIL_image)
        #PIL_image = PIL_mask

    return PIL_image