def create_image(path: tp.Optional[str] = None) -> (Scene, tp.Optional[str]): """ Create image of the given satellite data of a SentinelSat-2 satellite :param path: Path to the raw satellite data :return: (Scene, str), Scene of the satellite image, full path to the .tif create """ files = find_files_and_readers(base_dir=path, reader='msi_safe') _scn = Scene(filenames=files) _scn.load(['true_color']) filename = None if path is not None: filename = os.path.join(path, 'RGB.tif') if not os.path.exists(filename): _scn.save_dataset('true_color', filename, writer='simple_image', fill_value=0) return _scn, filename
def setUp(self): """Create temporary images to test on.""" import tempfile from datetime import datetime from pyresample.geometry import AreaDefinition from satpy.scene import Scene self.date = datetime(2018, 1, 1) # Create area definition pcs_id = 'ETRS89 / LAEA Europe' proj4_dict = {'init': 'epsg:3035'} self.x_size = 100 self.y_size = 100 area_extent = (2426378.0132, 1528101.2618, 6293974.6215, 5446513.5222) self.area_def = AreaDefinition('geotiff_area', pcs_id, pcs_id, proj4_dict, self.x_size, self.y_size, area_extent) # Create datasets for L, LA, RGB and RGBA mode images r__ = da.random.randint(0, 256, size=(self.y_size, self.x_size), chunks=(50, 50)).astype(np.uint8) g__ = da.random.randint(0, 256, size=(self.y_size, self.x_size), chunks=(50, 50)).astype(np.uint8) b__ = da.random.randint(0, 256, size=(self.y_size, self.x_size), chunks=(50, 50)).astype(np.uint8) a__ = 255 * np.ones((self.y_size, self.x_size), dtype=np.uint8) a__[:10, :10] = 0 a__ = da.from_array(a__, chunks=(50, 50)) ds_l = xr.DataArray(da.stack([r__]), dims=('bands', 'y', 'x'), attrs={'name': 'test_l', 'start_time': self.date}) ds_l['bands'] = ['L'] ds_la = xr.DataArray(da.stack([r__, a__]), dims=('bands', 'y', 'x'), attrs={'name': 'test_la', 'start_time': self.date}) ds_la['bands'] = ['L', 'A'] ds_rgb = xr.DataArray(da.stack([r__, g__, b__]), dims=('bands', 'y', 'x'), attrs={'name': 'test_rgb', 'start_time': self.date}) ds_rgb['bands'] = ['R', 'G', 'B'] ds_rgba = xr.DataArray(da.stack([r__, g__, b__, a__]), dims=('bands', 'y', 'x'), attrs={'name': 'test_rgba', 'start_time': self.date}) ds_rgba['bands'] = ['R', 'G', 'B', 'A'] # Temp dir for the saved images self.base_dir = tempfile.mkdtemp() # Put the datasets to Scene for easy saving scn = Scene() scn['l'] = ds_l scn['l'].attrs['area'] = self.area_def scn['la'] = ds_la scn['la'].attrs['area'] = self.area_def scn['rgb'] = ds_rgb scn['rgb'].attrs['area'] = self.area_def scn['rgba'] = ds_rgba scn['rgba'].attrs['area'] = self.area_def # Save the images. Two images in PNG and two in GeoTIFF scn.save_dataset('l', os.path.join(self.base_dir, 'test_l.png'), writer='simple_image') scn.save_dataset('la', os.path.join(self.base_dir, '20180101_0000_test_la.png'), writer='simple_image') scn.save_dataset('rgb', os.path.join(self.base_dir, '20180101_0000_test_rgb.tif'), writer='geotiff') scn.save_dataset('rgba', os.path.join(self.base_dir, 'test_rgba.tif'), writer='geotiff') self.scn = scn
print(filename) ##!#if(composite): ##!# day_filenames = glob(filename[:50]+'*') ##!# cmpst_add = '_composite' ##!#else: day_filenames = glob(filename) cmpst_add = '' # Extract the modis true-color plot limits # ---------------------------------------- ##lat_lims = plot_limits_dict[dt_date_str.strftime('%Y-%m-%d')][dt_date_str.strftime('%H%M')]['modis_Lat'] ##lon_lims = plot_limits_dict[dt_date_str.strftime('%Y-%m-%d')][dt_date_str.strftime('%H%M')]['modis_Lon'] # Use satpy (Scene) to open the file # ---------------------------------- scn = Scene(reader='modis_l1b', filenames=day_filenames) # Load true-color data scn.load(['true_color']) scn.save_dataset('true_color', 'test_image_true2.png') scn.show('true_color') plt.show() ##!#if(save): ##!# outname = 'modis_true_color_' + date_str + zoom_add + cmpst_add + '_satpy.png' ##!# plt.savefig(outname,dpi=300) ##!# print("Saved image",outname) ##!#else: ##!# plt.show()
dayno = dt.timetuple().tm_yday lat = float(fields[8]) lon = float(fields[9]) # make up the grid to resample into lats = np.arange(lat - 1, lat + 1, 0.01) # approx 1km resolution, 200km extent lons = np.arange(lon - 1, lon + 1, 0.01) # approx 1km resolution, 200km extent lons, lats = np.meshgrid(lons, lats) grid_def = geometry.GridDefinition(lons=lons, lats=lats) # copy 11-micron band (C14) to local disk # See: https://www.goes-r.gov/education/ABI-bands-quick-info.html gcs_pattern = 'gs://gcp-public-data-goes-16/ABI-L1b-RadF/{0}/{1}/{2}/*C14*_s{0}{1}{2}00*'.format( dt.year, dayno, dt.hour) outfile = '{}/tmp_{}{}{}'.format(tmpdir, dt.year, dayno, dt.hour) os.mkdirs(tmpdir) copy_command = 'gsutil cp -m {} {}'.format(all_files, outfile) subprocess.check_call(copy_command.split()) # create image scene = Scene(filenames=outfile, reader="abi_l1b") scene.resample(grid_def) outfile = '{}/ir_{}{}{}.png'.format(outdir, dt.year, dayno, dt.hour) scene.save_dataset('overview', outfile) # cleanup shutil.rmtree(tmpdir) exit
# -*- coding: utf-8 -*- """ Created on Tue May 28 11:09:38 2019 @author: bav """ from satpy.scene import Scene from satpy import find_files_and_readers from datetime import datetime files = find_files_and_readers(sensor='olci', start_time=datetime(2017, 7, 15, 0, 0, 0), end_time=datetime(2017, 7, 16, 0, 0, 0), base_dir=".\OLCI scenes", reader='olci_l1b') scn = Scene(filenames=files) scn.load(['true_color']) scn.save_dataset('true_color', filename='scene' + '.png')