def add_to_array(arr, area, time, bufr, savedir='/tmp', name=None, mode='L', resize=None, ptsize=None, save=False): """Add synoptical reports from stations to provided geolocated image array """ # Create array image arrshp = arr.shape[:2] print(np.nanmin(arr), np.nanmax(arr)) arr_img = Image(arr, mode=mode) # arr_img = Image(channels=[arr[:, :, 0], arr[:, :, 1], arr[:, :, 2]], # mode='RGB') arr_img.stretch('crude') arr_img.invert() arr_img.colorize(maskcol) arr_img.invert() # Import bufr stations = read_synop(bufr, 'visibility') currentstations = stations[time.strftime("%Y%m%d%H0000")] lats = [i[2] for i in currentstations] lons = [i[3] for i in currentstations] vis = [i[4] for i in currentstations] # Create array for synop parameter visarr = np.empty(arrshp) visarr.fill(np.nan) x, y = (area.get_xy_from_lonlat(lons, lats)) vis_ma = np.ma.array(vis, mask=x.mask) if ptsize: xpt = np.array([]) ypt = np.array([]) for i, j in zip(x, y): xmesh, ymesh = np.meshgrid(np.linspace(i - ptsize, i + ptsize, ptsize * 2 + 1), np.linspace(j - ptsize, j + ptsize, ptsize * 2 + 1)) xpt = np.append(xpt, xmesh.ravel()) ypt = np.append(ypt, ymesh.ravel()) vispt = np.ma.array([np.full(((ptsize * 2 + 1, ptsize * 2 + 1)), p) for p in vis_ma]) visarr[ypt.astype(int), xpt.astype(int)] = vispt.ravel() else: visarr[y.compressed(), x.compressed()] = vis_ma.compressed() visarr_ma = np.ma.masked_invalid(visarr) station_img = Image(visarr_ma, mode='L') station_img.colorize(vis_colset) station_img.merge(arr_img) if resize is not None: station_img.resize((arrshp[0] * resize, arrshp[1] * resize)) if name is None: timestr = time.strftime("%Y%m%d%H%M") name = "fog_filter_example_stations_{}.png".format(timestr) if save: savepath = os.path.join(savedir, name) station_img.save(savepath) return(station_img)
def to_image(dataset, copy=True, **kwargs): # Only add keywords if they are present for key in ["mode", "fill_value", "palette"]: if key in dataset.info: kwargs.setdefault(key, dataset.info[key]) if dataset.ndim == 2: return Image([dataset], copy=copy, **kwargs) elif dataset.ndim == 3: return Image([band for band in dataset], copy=copy, **kwargs) else: raise ValueError( "Don't know how to convert array with ndim %d to image" % dataset.ndim)
def pilimage2trollimage(pimage): (r, g, b) = _image2array(pimage) return Image((r, g, b), mode="RGB")
def add_to_image(image, area, time, bufr, savedir='/tmp', name=None, bgimg=None, resize=None, ptsize=None, save=False): """Add synoptical visibility reports from station data to provided geolocated image array """ arrshp = image.shape[:2] # Add optional background image if bgimg is not None: # Get background image bg_img = Image(bgimg.squeeze(), mode='L', fill_value=None) bg_img.stretch("crude") bg_img.convert("RGB") # bg_img.invert() image.merge(bg_img) # Import bufr stations = read_synop(bufr, 'visibility') currentstations = stations[time.strftime("%Y%m%d%H0000")] lats = [i[2] for i in currentstations] lons = [i[3] for i in currentstations] vis = [i[4] for i in currentstations] # Create array for synop parameter visarr = np.empty(arrshp) visarr.fill(np.nan) # Red - Violet - Blue - Green vis_colset = Colormap((0, (228 / 255.0, 26 / 255.0, 28 / 255.0)), (1000, (152 / 255.0, 78 / 255.0, 163 / 255.0)), (5000, (55 / 255.0, 126 / 255.0, 184 / 255.0)), (10000, (77 / 255.0, 175 / 255.0, 74 / 255.0))) x, y = (area.get_xy_from_lonlat(lons, lats)) vis_ma = np.ma.array(vis, mask=x.mask) if ptsize: xpt = np.array([]) ypt = np.array([]) for i, j in zip(x, y): xmesh, ymesh = np.meshgrid( np.linspace(i - ptsize, i + ptsize, ptsize * 2 + 1), np.linspace(j - ptsize, j + ptsize, ptsize * 2 + 1)) xpt = np.append(xpt, xmesh.ravel()) ypt = np.append(ypt, ymesh.ravel()) vispt = np.ma.array( [np.full(((ptsize * 2 + 1, ptsize * 2 + 1)), p) for p in vis_ma]) visarr[ypt.astype(int), xpt.astype(int)] = vispt.ravel() else: visarr[y.compressed(), x.compressed()] = vis_ma.compressed() visarr_ma = np.ma.masked_invalid(visarr) station_img = Image(visarr_ma, mode='L') station_img.colorize(vis_colset) image.convert("RGB") station_img.merge(image) if resize is not None: station_img.resize((arrshp[0] * resize, arrshp[1] * resize)) if name is None: timestr = time.strftime("%Y%m%d%H%M") name = "fog_filter_example_stations_{}.png".format(timestr) if save: savepath = os.path.join(savedir, name) station_img.save(savepath) return (station_img)
print(arr.shape) print(np.min(arr)) # Get bufr file base = os.path.split(fogpy.__file__) inbufr = os.path.join(base[0], '..', 'etc', 'result_{}.bufr'.format(time.strftime("%Y%m%d"))) # bufr_dir = '/data/tleppelt/skydata/' # bufr_file = "result_{}".format(time.strftime("%Y%m%d")) # inbufr = os.path.join(bufr_dir, bufr_file) area_id = "geos_germ" name = "geos_germ" proj_id = "geos" proj_dict = { 'a': '6378169.00', 'lon_0': '0.00', 'h': '35785831.00', 'b': '6356583.80', 'proj': 'geos', 'lat_0': '0.00' } x_size = 298 * resize y_size = 141 * resize area_extent = (214528.82635591552, 4370087.2110124603, 1108648.9697693815, 4793144.0573926577) area_def = geometry.AreaDefinition(area_id, name, proj_id, proj_dict, x_size, y_size, area_extent) print(area_def) img = Image(arr[:, :, :3], mode='RGB') add_to_image(img, area_def, time, inbufr)
cot_img.enhance(stretch="crude") #bgimg = germ_scene[10.8].as_image() bgimg = germ_scene.image.ir108() fls_img, fogmask = germ_scene.image.fls_day(elevation_ger.image_data, cot_ger.image_data, reff_ger.image_data, cwp_ger.image_data) snow_rgb = germ_scene.image.snow() daymicro_rgb = germ_scene.image.day_microphysics() overview = germ_scene.image.overview() # Merge masked and colorized fog clouds with backgrund infrared image bgimg.convert("RGB") fls_img = Image(fls_img.channels[0], mode='L') fls_img.colorize(fogcol) fls_img.merge(bgimg) ele_img = Image(ele_img.channels[0], mode='L') #cwp_img = Image(cwp_img.channels[0], mode='L') #cwp_masked = np.ma.array(cwp_ger.image_data, mask=fogmask) #print(np.histogram(cwp_masked.compressed())) #ele_img.show() #cwp_img.show() #overview.show() #fls_img.show() #snow_rgb.show() #daymicro_rgb.show() ele_img.save("/tmp/fog_example_msg_ger_elevation_{}.png".format( time.strftime("%Y%m%d%H%M")))
scn.load(['sea_surface_temperature']) lcd = scn.resample('euro4', radius_of_influence=2000) sstdata = lcd['sea_surface_temperature'][:] import numpy as np arr = np.ma.where(np.less_equal(sstdata, 0), 0, sstdata - 273.15) # Convert sst to numbers between 0 and 28, corresponding to the lut: data = np.ma.where(np.less(arr, 0), 28, 28.0 - arr) data = np.ma.where(np.greater(arr, 23.0), 4, data).round().astype('uint8') from trollimage.image import Image from satpy.imageo import palettes palette = palettes.sstlut_osisaf_metno() img = Image(data, mode='P', palette=palette) img.show() img.save('osisaf_sst_viirs_satpy.png') from pycoast import ContourWriter cw_ = ContourWriter('/home/a000680/data/shapes') pilim = img.pil_image() area_def = lcd['sea_surface_temperature'].info['area'] cw_.add_coastlines(pilim, area_def, resolution='i', level=1, outline=(220, 220, 220)) pilim.show() pilim.save('./osisaf_sst_viirs_satpy_withovl.png')