def _create_global_mosaic(self, fnames, slot, composite): """Create and save global mosaic.""" self.logger.info("Building composite %s for slot %s", composite, str(slot)) scn = Scene() file_parts = self._get_fname_parts(slot, composite) fname_out = file_parts["uri"] img = self._get_existing_image(fname_out) self.logger.info("Creating composite") scn['img'] = create_world_composite(fnames, self.adef, self.config["lon_limits"], img=img, logger=self.logger) self.logger.info("Saving %s", fname_out) scn.save_dataset('img', filename=fname_out, **self.config["save_settings"]) self._send_message(file_parts) del self.slots[slot][composite]
def simple_export(hrit_files, time, dpi, photo_path, load_photo=['VIS006']): from satpy import Scene from satpy import find_files_and_readers from datetime import datetime import matplotlib as mpl mpl.rcParams['figure.dpi'] = dpi # load_photo = 'VIS006' first = 0 last = len(time) - 1 # print(len(time),last) yearF, monthF, dayF, hourF, minuteF, secondF = time[first].tt_calendar() yearL, monthL, dayL, hourL, minuteL, secondL = time[last].tt_calendar() # IT IS NOT WORKING FIND SOLUTION - Adding a minute in case there is only one point on map if len(time) == 1: time[last].tt = time[last].tt + 1 / 3600 yearL, monthL, dayL, hourL, minuteL, secondL = time[last].tt_calendar() # print("It works") # print(yearF, monthF, dayF, hourF, minuteF, secondF ) # print(yearL, monthL, dayL, hourL, minuteL, secondL) # time[0].tt_calendar()[0] files = find_files_and_readers(base_dir=hrit_files, start_time=datetime(yearF, monthF, dayF, hourF, minuteF), end_time=datetime(yearL, monthL, dayL, hourL, minuteL), reader='seviri_l1b_hrit') scn = Scene(filenames=files) scn.load(load_photo) file = photo_path + 'globe_' + load_photo[ 0] + '_{date:%Y-%m-%d_%H_%M_%S}.png'.format(date=scn.start_time) scn.save_dataset(load_photo[0], writer='simple_image', filename=file, num_threads=8) return ()
def test_write_and_read_via_scene(test_image_small_mid_atlantic_L, tmp_path): """Test that all attributes are written also when writing from scene. It appears that :func:`Satpy.Scene.save_dataset` does not pass the filename to the writer. Test that filename is still written to header when saving this way (the regular way). """ import rasterio sc = Scene() fn = os.fspath(tmp_path / "test-{name}.tif") sc["montanha-do-pico"] = test_image_small_mid_atlantic_L.data sc.save_dataset("montanha-do-pico", writer="ninjogeotiff", filename=fn, fill_value=0, PhysicUnit="C", PhysicValue="Temperature", SatelliteNameID=6400014, ChannelID=900015, DataType="GORN") src = rasterio.open(tmp_path / "test-montanha-do-pico.tif") tgs = src.tags() assert tgs["ninjo_FileName"] == os.fspath(tmp_path / "test-montanha-do-pico.tif")
def prepare_input_data(modis_files): """ Prepares validation data for the MODIS CTP retrieval. Args: modis_file: List of filenames containing the MODIS input data to use as input. Returns: Dictionary containing the different files required to run the retrieval on the CALIOP data. """ from datetime import datetime from satpy import Scene import scipy as sp from scipy.interpolate import RegularGridInterpolator from scipy.ndimage import maximum_filter, minimum_filter from scipy.signal import convolve import numpy as np import xarray from pansat.products.reanalysis.era5 import ERA5Product from pansat.products.satellite.calipso import clay01km from pykdtree.kdtree import KDTree from PIL import Image # # Prepare MODIS data. # scene = Scene(filenames=modis_files, reader="modis_l1b") scene.load(["true_color", "31", "32", "latitude", "longitude"], resolution=1000) scene["true_color_small"] = scene["true_color"][:, ::4, ::4] scene["bt_11_small"] = scene["31"][::4, ::4] scene["bt_12_small"] = scene["32"][::4, ::4] scene["latitude_small"] = scene["latitude"][::4, ::4] scene["longitude_small"] = scene["longitude"][::4, ::4] scene.save_dataset("true_color_small", "modis_true_color.png") image = Image.open("modis_true_color.png") modis_rgb = np.array(image) bt_11_rgb = scene["bt_11_small"].compute() bt_12_rgb = scene["bt_12_small"].compute() lats_rgb = scene["latitude_small"].compute() lons_rgb = scene["longitude_small"].compute() # MODIS data input features. lats_r = scene["latitude"].compute() lons_r = scene["longitude"].compute() bt_11 = scene["31"].compute() bt_12 = scene["32"].compute() def mean_filter(img): k = np.ones((5, 5)) / 25.0 return convolve(img, k, mode="same") def std_filter(img): mu = mean_filter(img ** 2) mu2 = mean_filter(img) ** 2 return np.sqrt(mu - mu2) bt_11_w = maximum_filter(bt_11, [5, 5]) bt_11_c = minimum_filter(bt_11, [5, 5]) bt_12_w = maximum_filter(bt_12, [5, 5]) bt_12_c = minimum_filter(bt_12, [5, 5]) bt_11_s = std_filter(bt_11) bt_1112_s = std_filter(bt_11 - bt_12) # # Calipso data # t_0 = datetime(2016, 10, 12, 17, 00) t_1 = datetime(2016, 10, 12, 17, 50) calipso_files = clay01km.download(t_0, t_1) lat_min = lats_r.data.min() lat_max = lats_r.data.max() lon_min = lons_r.data.min() lon_max = lons_r.data.max() dataset = clay01km.open(calipso_files[0]) lats_c = dataset["latitude"].data lons_c = dataset["longitude"].data ctp_c = dataset["layer_top_pressure"] cth_c = dataset["layer_top_altitude"] indices = np.where( (lats_c > lat_min) * (lats_c <= lat_max) * (lons_c > lon_min) * (lons_c <= lon_max) ) points = np.hstack([lats_r.data.reshape(-1, 1), lons_r.data.reshape(-1, 1)]) kd_tree = KDTree(points) points_c = np.hstack([lats_c.reshape(-1, 1), lons_c.reshape(-1, 1)]) d, indices = kd_tree.query(points_c) valid = d < 0.01 indices = indices[valid] lats_c = lats_c[valid] lons_c = lons_c[valid] ctp_c = ctp_c[valid] cth_c = cth_c[valid] bt_11 = bt_11.data.ravel()[indices] bt_12 = bt_12.data.ravel()[indices] bt_11_w = bt_11_w.ravel()[indices] bt_12_w = bt_12_w.ravel()[indices] bt_11_c = bt_11_c.ravel()[indices] bt_12_c = bt_12_c.ravel()[indices] bt_11_s = bt_11_s.ravel()[indices] bt_1112_s = bt_1112_s.ravel()[indices] lats_r = lats_r.data.ravel()[indices] lons_r = lons_r.data.ravel()[indices] # # ERA 5 data. # t_0 = datetime(2016, 10, 12, 17, 45) t_1 = datetime(2016, 10, 12, 17, 50) surface_variables = ["surface_pressure", "2m_temperature", "tcwv"] domain = [lat_min - 2, lat_max + 2, lon_min - 2, lon_max + 2] surface_product = ERA5Product("hourly", "surface", surface_variables, domain) era_surface_files = surface_product.download(t_0, t_1) pressure_variables = ["temperature"] pressure_product = ERA5Product("hourly", "pressure", pressure_variables, domain) era_pressure_files = pressure_product.download(t_0, t_1) # interpolate pressure data. era5_data = xarray.open_dataset(era_pressure_files[0]) lats_era = era5_data["latitude"][::-1] lons_era = era5_data["longitude"] p_era = era5_data["level"] p_inds = [np.where(p_era == p)[0] for p in [950, 850, 700, 500, 250]] pressures = [] for ind in p_inds: p_interp = RegularGridInterpolator( [lats_era, lons_era], era5_data["t"].data[0, ind[0], ::-1, :] ) pressures.append(p_interp((lats_r, lons_r))) era5_data = xarray.open_dataset(era_surface_files[0]) lats_era = era5_data["latitude"][::-1] lons_era = era5_data["longitude"] t_interp = RegularGridInterpolator( [lats_era, lons_era], era5_data["t2m"].data[0, ::-1, :] ) t_surf = t_interp((lats_r, lons_r)) p_interp = RegularGridInterpolator( [lats_era, lons_era], era5_data["sp"].data[0, ::-1, :] ) p_surf = p_interp((lats_r, lons_r)) tcwv_interp = RegularGridInterpolator( [lats_era, lons_era], era5_data["tcwv"].data[0, ::-1, :] ) tcwv = tcwv_interp((lats_r, lons_r)) # # Assemble input data # x = np.zeros((lats_r.size, 16)) x[:, 0] = p_surf x[:, 1] = t_surf for i, p in enumerate(pressures): x[:, 2 + i] = p x[:, 7] = tcwv x[:, 8] = bt_12 x[:, 9] = bt_11 - bt_12 x[:, 10] = bt_11_w - bt_12_w x[:, 11] = bt_11_c - bt_12_c x[:, 12] = bt_12_w - bt_12 x[:, 13] = bt_12_c - bt_12 x[:, 14] = bt_11_s x[:, 15] = bt_1112_s output_data = { "input_data": x, "ctp": ctp_c, "latitude": lats_r, "longitude": lons_r, "latitude_rgb": lats_rgb, "longitude_rgb": lons_rgb, "modis_rgb": modis_rgb, "bt_11_rgb": bt_11_rgb, "bt_12_rgb": bt_12_rgb, } return output_data
parser.add_argument('--sat_id', dest='sat_id', action="store", help="Satellite ID", default="8888") parser.add_argument('--data_cat', dest='data_cat', action="store", help="Category of data (one of GORN, GPRN, P**N)", default="GORN") parser.add_argument('--area', dest='areadef', action="store", help="Area name, the definition must exist in your areas configuration file", default="nrEURO1km_NPOL_COALeqc") parser.add_argument('--ph_unit', dest='ph_unit', action="store", help="Physical unit", default="CELSIUS") parser.add_argument('--data_src', dest='data_src', action="store", help="Data source", default="EUMETCAST") args = parser.parse_args() if (args.input_dir != None): os.chdir(args.input_dir) cfg = vars(args) if (args.cfg != None): with open(args.cfg, 'r') as ymlfile: cfg = yaml.load(ymlfile) narea = get_area_def(args.areadef) global_data = Scene(sensor="images", reader="generic_image", area=narea) global_data.load(['image']) global_data['image'].info['area'] = narea fname = global_data['image'].info['filename'] ofname = fname[:-3] + "tif" #global_data.save_dataset('image', filename="out.png", writer="simple_image") global_data.save_dataset('image', filename=ofname, writer="ninjotiff", sat_id=cfg['sat_id'], chan_id=cfg['chan_id'], data_cat=cfg['data_cat'], data_source=cfg['data_src'], physic_unit=cfg['ph_unit'])
parser.add_argument('--area', dest='areadef', action="store", help="Area name, the definition must exist in your areas configuration file", default="nrEURO1km_NPOL_COALeqc") parser.add_argument('--ph_unit', dest='ph_unit', action="store", help="Physical unit", default="CELSIUS") parser.add_argument('--data_src', dest='data_src', action="store", help="Data source", default="EUMETCAST") args = parser.parse_args() if (args.input_dir is not None): os.chdir(args.input_dir) cfg = vars(args) if (args.cfg is not None): with open(args.cfg, 'r') as ymlfile: cfg = yaml.load(ymlfile, Loader=UnsafeLoader) narea = get_area_def(args.areadef) global_data = Scene(reader="generic_image") global_data.load(['image']) global_data['image'].info['area'] = narea fname = global_data['image'].info['filename'] ofname = fname[:-3] + "tif" # global_data.save_dataset('image', filename="out.png", writer="simple_image") global_data.save_dataset('image', filename=ofname, writer="ninjotiff", sat_id=cfg['sat_id'], chan_id=cfg['chan_id'], data_cat=cfg['data_cat'], data_source=cfg['data_src'], physic_unit=cfg['ph_unit'])
call(["bunzip2","/scratch/hamann/"+bz2file]) testfile=bz2file[:-4] else: raise ValueError("Unknown computer"+hostname+": no example file is provided") filenames=[data_dir+testfile] print(filenames) #global_scene = Scene(platform_name="Meteosat-9", sensor="seviri", reader=reader, filenames=filenames) global_scene = Scene(sensor="seviri", reader=reader, filenames=filenames) #global_scene.load([0.6, 0.8, 10.8]) global_scene.load(['overview']) #global_scene.load(["VIS006", "VIS008", "IR_108"]) global_scene.save_dataset('overview', data_dir+'/overview_global.png') area="ccs4" #area="SeviriDisk00" local_scene = global_scene.resample("ccs4") local_scene.save_dataset('overview', data_dir+'/overview_'+area+'.png') #print (global_scene) #print (global_scene[0.6]) #local_scene = global_scene.project("ccs4", precompute=True) # print (global_scene.available_datasets()) ## does not work #global_scene.show(0.6)
print("") print("=======================") print("resample to "+area) local_scene = global_scene.resample(area) print("=======================") print("") print("global_scene.available_composite_names()") print(global_scene.available_composite_names()) print("") print("=======================") print("global_scene.save_dataset('overview', pngfile)") pngfile='./'+sat+'_overview_global.png' global_scene.save_dataset('overview', pngfile) print('display '+pngfile+' &') print("") print("=======================") #local_scene.show('overview') pngfile='./'+sat+'_overview_'+area+'.png' local_scene.save_dataset('overview', pngfile) print('display '+pngfile+' &') new=False if new==False: local_scene["ndvi"] = (local_scene[0.8] - local_scene[0.6]) / (local_scene[0.8] + local_scene[0.6]) #from satpy.enhancements import colorize #colorize(img, **kwargs)
import cartopy.feature as cfeat import pdb import pyproj FILENAMES = glob('/Users/dhueholt/Documents/Data/Sips/20181214/VNP*.nc') print(len(FILENAMES)) SCN = Scene(reader='viirs_l1b', filenames=FILENAMES) SCN.load(['DNB']) SCN.load(['dnb_lon']) SCN.load(['dnb_lat']) # MY_AREA = SCN['DNB'].attrs['area'].compute_optimal_bb_area({'proj': 'lcc', 'lon_0': -96., # 'lat_0': 39., 'lat_1': 25., # 'lat_2': 25.}) # NEW_SCN = SCN.resample(MY_AREA) SCN.save_dataset( 'DNB', '/Users/dhueholt/Documents/Hollings_2019/aurora_images/20181214_06480718_08360906_dnb.tif' ) # CRS = NEW_SCN['DNB'].attrs['area'].to_cartopy_crs() # lambert_proj = ccrs.LambertConformal() PlateCarree = ccrs.PlateCarree() fig = plt.figure(frameon=False) fig.set_size_inches(16, 10) AX = plt.axes(projection=PlateCarree) AX.coastlines() AX.gridlines() AX.set_global() # pdb.set_trace() data_plot = plt.pcolormesh(SCN['dnb_lon'].data, SCN['dnb_lat'].data, SCN['DNB'].data,
## get information about loaded channels #print("========================") #print(global_scene.datasets) ## show satellite pictures directly on the screen #global_scene.show(0.6) #global_scene.show('VIS006') #global_scene.show('overview') ## save an RGB as png file outputfile = time_start.strftime(cwd + '/MSG_' + RGB + '-global' + '_%Y%m%d%H%M.png') if False: print("... save " + outputfile) global_scene.save_dataset(RGB, outputfile) print("========================") ## choose area area = "ccs4" #area="SeviriDisk00" #area="cosmo7" #area="cosmo1" print("... resample data to another projection") local_scene = global_scene.resample(area) outputfile = time_start.strftime(cwd + '/MSG_' + RGB + '-' + area + '_%Y%m%d%H%M.png') print("... save " + outputfile) add_border = True if add_border: