def test_save_array(self): from satpy import Scene import xarray as xr import tempfile scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) scn['test-array'] = xr.DataArray([1, 2, 3], attrs=dict(start_time=start_time, end_time=end_time)) try: handle, filename = tempfile.mkstemp() os.close(handle) scn.save_datasets(filename=filename, writer='cf') import h5netcdf as nc4 with nc4.File(filename) as f: self.assertTrue(all(f['test-array'][:] == [1, 2, 3])) finally: os.remove(filename)
def open_generic_product(inpath): """Open a satellite product. Open a satellite product based on satpy functions without specifying a reader. :param inpath: Path to the folder containing the satellite images :type inpath: pathlib.PosixPath :return: Satpy scene containing the opened satellite product :rtype: satpy.scene.Scene """ # Get files in the satellite folder fpath_nc = inpath.parents[0].joinpath('*') fnames = glob(str(fpath_nc)) # Open product and store prod = Scene(filenames=fnames) return prod
def test_save_with_compression(self): """Test saving an array with compression.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) with mock.patch('satpy.writers.cf_writer.xr.Dataset') as xrdataset,\ mock.patch('satpy.writers.cf_writer.make_time_bounds'): scn['test-array'] = xr.DataArray( [1, 2, 3], attrs=dict(start_time=start_time, end_time=end_time, prerequisites=[DatasetID('hej')])) comp = {'zlib': True, 'complevel': 9} scn.save_datasets(filename='bla', writer='cf', compression=comp) ars, kws = xrdataset.call_args_list[1] self.assertDictEqual(ars[0]['test-array'].encoding, comp)
def test_header_attrs(self): """Check master attributes are set.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) scn['test-array'] = xr.DataArray([1, 2, 3], attrs=dict(start_time=start_time, end_time=end_time)) with TempFile() as filename: header_attrs = {'sensor': 'SEVIRI', 'orbit': None} scn.save_datasets(filename=filename, header_attrs=header_attrs, writer='cf') with xr.open_dataset(filename) as f: self.assertTrue(f.attrs['sensor'] == 'SEVIRI') self.assertTrue('sensor' in f.attrs.keys()) self.assertTrue('orbit' not in f.attrs.keys())
def test_basic_write(self, tmpdir, src_dtype, dst_dtype, enhance): """Test writing data to disk.""" src_data_arr = _create_fake_data_arr(dtype=src_dtype) scn = Scene() scn[src_data_arr.attrs["name"]] = src_data_arr scn.save_datasets(writer="binary", base_dir=str(tmpdir), dtype=dst_dtype, enhance=enhance) exp_fn = tmpdir.join( "noaa-20_viirs_fake_p2g_name_20210101_120000_fake_area.dat") if dst_dtype is None: dst_dtype = src_dtype if src_dtype != np.float64 else np.float32 assert os.path.isfile(exp_fn) data = np.memmap(str(exp_fn), mode="r", dtype=dst_dtype) exp_data = self._generate_expected_output(src_data_arr, dst_dtype, enhance) np.testing.assert_allclose(data, exp_data, atol=2e-7)
def test_bounds(self): """Test setting time bounds.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) test_array = np.array([[1, 2], [3, 4]]).reshape(2, 2, 1) scn['test-array'] = xr.DataArray( test_array, dims=['x', 'y', 'time'], coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, attrs=dict(start_time=start_time, end_time=end_time)) with TempFile() as filename: scn.save_datasets(filename=filename, writer='cf') import h5netcdf as nc4 with nc4.File(filename) as f: self.assertTrue( np.all(f['time_bnds'][:] == np.array([-300., 600.])))
def get_cloud_mask(l1_filename, cloud_mask_dir): """ return a 2d mask, with cloudy pixels marked as 1, non-cloudy pixels marked as 0 """ basename = os.path.split(l1_filename) cloud_mask_filename = glob.glob( os.path.join(cloud_mask_dir, 'MYD35*' + l1_filename.split('.A')[1][:12] + '*'))[0] # satpy returns(0=Cloudy, 1=Uncertain, 2=Probably Clear, 3=Confident Clear) swath = Scene(reader='modis_l2', filenames=[cloud_mask_filename]) swath.load(['cloud_mask'], resolution=1000) cloud_mask = np.array(swath['cloud_mask'].load())[:MAX_HEIGHT, :MAX_WIDTH] cloud_mask = (cloud_mask == 0) cloud_mask = cloud_mask.astype(np.intp) return cloud_mask
def read_image(filepath): """Read the image from *filepath* and return it as PIL Image object.""" scn = Scene(reader='generic_image', filenames=[ filepath, ]) scn.load(['image']) y_size = scn['image']['y'].size x_size = scn['image']['x'].size arr = np.array(scn['image']) alpha = np.isnan(arr[0, :, :]) arr = arr.astype(np.uint8) if np.any(alpha): alpha = 255 * np.invert(alpha).astype(np.uint8) alpha = alpha.reshape(1, y_size, x_size) arr = np.vstack((arr, alpha)) arr = np.squeeze(np.rollaxis(arr, 0, 3)) img = Image.fromarray(arr) return img
def test_load_category_dataset(self, input_files, loadables, request_resolution, exp_resolution, exp_area): """Test loading category products.""" scene = Scene(reader='modis_l2', filenames=input_files) kwargs = { "resolution": request_resolution } if request_resolution is not None else {} scene.load(loadables, **kwargs) for ds_name in loadables: cat_id = make_dataid(name=ds_name, resolution=exp_resolution) assert cat_id in scene cat_data_arr = scene[cat_id] assert cat_data_arr.shape == _shape_for_resolution(exp_resolution) assert cat_data_arr.attrs.get('resolution') == exp_resolution # mask variables should be integers assert np.issubdtype(cat_data_arr.dtype, np.integer) assert cat_data_arr.attrs.get('_FillValue') is not None _check_shared_metadata(cat_data_arr, expect_area=exp_area)
def test_single_time_value(self): """Test setting a single time value.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) test_array = np.array([[1, 2], [3, 4]]) scn['test-array'] = xr.DataArray(test_array, dims=['x', 'y'], coords={'time': np.datetime64('2018-05-30T10:05:00')}, attrs=dict(start_time=start_time, end_time=end_time)) with TempFile() as filename: scn.save_datasets(filename=filename, writer='cf') with xr.open_dataset(filename, decode_cf=True) as f: np.testing.assert_array_equal(f['time'], scn['test-array']['time']) bounds_exp = np.array([[start_time, end_time]], dtype='datetime64[m]') np.testing.assert_array_equal(f['time_bnds'], bounds_exp)
def test_unlimited_dims_kwarg(self): """Test specification of unlimited dimensions.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) test_array = np.array([[1, 2], [3, 4]]) scn['test-array'] = xr.DataArray( test_array, dims=['x', 'y'], coords={'time': np.datetime64('2018-05-30T10:05:00')}, attrs=dict(start_time=start_time, end_time=end_time)) with TempFile() as filename: scn.save_datasets(filename=filename, writer='cf', unlimited_dims=['time']) with xr.open_dataset(filename) as f: self.assertSetEqual(f.encoding['unlimited_dims'], {'time'})
def get_swath(radiance_filename, myd03_dir): """ :param radiance_filename: MYD02 filename :param myd03_dir: root directory of MYD03 geolocational files :return swath: numpy.ndarray of size (15, HEIGHT, WIDTH) Uses the satpy Scene reader with the modis-l1b files. Issues reading files might be due to pyhdf not being installed - otherwise try pip install satpy[modis_0l1b] Creates a scene with the MYD02 and MYD03 files, and extracts them as multi-channel arrays. The lat and long are are appended as additional channels. """ # bands selected from MODIS composite = [ '1', '2', '29', '33', '34', '35', '36', '26', '27', '20', '21', '22', '23' ] # find a corresponding geolocational (MYD03) file for the provided radiance (MYD02) file geoloc_filename = find_matching_geoloc_file(radiance_filename, myd03_dir) # load the global scene using satpy global_scene = Scene(reader='modis_l1b', filenames=[radiance_filename, geoloc_filename]) # load composite, resolution of 1km global_scene.load(composite, resolution=1000) # load latitudes and longitudes, resolution 1km global_scene.load(['latitude', 'longitude'], resolution=1000) latitude = np.array(global_scene['latitude'].load()) longitude = np.array(global_scene['longitude'].load()) swath = [] for comp in composite: temp = np.array(global_scene[comp].load()) swath.append(temp[:MAX_HEIGHT, :MAX_WIDTH]) swath.append(latitude[:MAX_HEIGHT, :MAX_WIDTH]) swath.append(longitude[:MAX_HEIGHT, :MAX_WIDTH]) return np.array(swath, dtype=np.float16)
def test_temperature_difference(self, tmpdir, abi_l1b_c01_data_array): new_data_arr = abi_l1b_c01_data_array.copy() data = da.linspace(-10, 10, new_data_arr.size).reshape(new_data_arr.shape) new_data_arr.data = data new_data_arr.attrs["name"] = "test_temperature_difference" scn = Scene() scn["test_temperature_difference"] = new_data_arr out_fn = str(tmpdir + "test_temperature_difference.tif") scn.save_datasets(filename=out_fn) with rasterio.open(out_fn, "r") as out_ds: assert out_ds.count == 2 l_data = out_ds.read(1) # see polar2grid/tests/etc/enhancements/generic.yaml flat_l_data = l_data.ravel() data = data.ravel().compute() exp_out = np.round(np.linspace(5.0, 205.0, data.size)).astype(np.uint8) np.testing.assert_allclose(flat_l_data, exp_out)
def test_save_array(self): """Test saving an array to netcdf/cf.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) scn['test-array'] = xr.DataArray([1, 2, 3], attrs=dict(start_time=start_time, end_time=end_time, prerequisites=[DatasetID('hej')])) with TempFile() as filename: scn.save_datasets(filename=filename, writer='cf') with xr.open_dataset(filename) as f: self.assertTrue(np.all(f['test-array'][:] == [1, 2, 3])) expected_prereq = ("DatasetID(name='hej', wavelength=None, " "resolution=None, polarization=None, " "calibration=None, level=None, modifiers=())") self.assertEqual(f['test-array'].attrs['prerequisites'], expected_prereq)
def test_scene_dataset_values(self): """Test loading data.""" from satpy import Scene fname = os.path.join(self.base_dir, FILENAME) scn = Scene(reader='ascat_l2_soilmoisture_bufr', filenames=[fname]) for name in scn.available_dataset_names(): scn.load([name]) loaded_values = scn[name].values fill_value = scn[name].attrs['fill_value'] # replace nans in data loaded from file with the fill value defined in the .yaml # to make them comparable loaded_values_nan_filled = np.nan_to_num(loaded_values, nan=fill_value) key = scn[name].attrs['key'] original_values = MSG[key] # this makes each assertion below a separate test from unittest's point of view # (note: if all subtests pass, they will count as one test) with self.subTest(msg="Test failed for dataset: " + name): self.assertTrue( np.allclose(original_values, loaded_values_nan_filled))
def get_entire_cyclone(start_point, end_point, history=None, future=None): lat_0 = (start_point["USA_LAT"] + end_point["USA_LAT"]) / 2 lon_0 = (start_point["USA_LON"] + end_point["USA_LON"]) / 2 north_extent = (start_point["USA_R34_NE"] + start_point["USA_R34_NW"]) / 120 south_extent = (start_point["USA_R34_SE"] + start_point["USA_R34_SW"]) / 120 west_extent = (start_point["USA_R34_SW"] + start_point["USA_R34_NW"]) / 120 east_extent = (start_point["USA_R34_NE"] + start_point["USA_R34_NW"]) / 120 try: files, urls = get_data(DATA_DIRECTORY, start_point["ISO_TIME"].to_pydatetime(), end_point["ISO_TIME"].to_pydatetime(), north=lat_0 + north_extent, south=lat_0 - south_extent, west=lon_0 - west_extent, east=lon_0 + east_extent, dayOrNight="D", include_mod=False) except FileNotFoundError: return None scene = Scene(filenames=files, reader="viirs_l1b") t = scene.start_time - start_point["ISO_TIME"].to_pydatetime() metadata = interpolate(start_point, end_point, t) # for i, h in enumerate(history): # metadata[f"DELTA_SPEED_-{(len(history) - i) * 3}HR"] = metadata["USA_WIND"] - h["USA_WIND"] # for i, f in enumerate(future): # metadata[f"DELTA_SPEED_+{(i + 1) * 3}HR"] = f["USA_WIND"] - metadata["USA_WIND"] # if (i + 1) * 3 == 24: # metadata["24_HRS_LAT"] = f["USA_LAT"] # metadata["24_HRS_LON"] = f["USA_LON"] checkpath = os.path.join( CACHE_DIRECTORY, f"{metadata['NAME']}.{metadata['ISO_TIME'].strftime('%Y%m%d%H%M%S')}.gpz" ) if os.path.isfile(checkpath): return CycloneImage.load(checkpath) return CycloneImage(scene, metadata, load_mod=False)
def read(self, filename, fields=None, **kwargs): scene = Scene(reader=self.satpy_reader, filenames=[filename.path]) # If the user has not passed any fields to us, we load all per default. if fields is None: fields = scene.available_dataset_ids() # Load all selected fields scene.load(fields, **kwargs) if isinstance(fields[0], str): data_arrays = {field: scene.get(field) for field in fields} else: data_arrays = {field.name: scene.get(field) for field in fields} for name, array in data_arrays.items(): array.name = name dataset = xr.merge(data_arrays.values()) return dataset
def test_bounds_missing_time_info(self): """Test time bounds generation in case of missing time.""" from satpy import Scene import xarray as xr scn = Scene() start_timeA = datetime(2018, 5, 30, 10, 0) end_timeA = datetime(2018, 5, 30, 10, 15) test_arrayA = np.array([[1, 2], [3, 4]]).reshape(2, 2, 1) test_arrayB = np.array([[1, 2], [3, 5]]).reshape(2, 2, 1) scn['test-arrayA'] = xr.DataArray(test_arrayA, dims=['x', 'y', 'time'], coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, attrs=dict(start_time=start_timeA, end_time=end_timeA)) scn['test-arrayB'] = xr.DataArray(test_arrayB, dims=['x', 'y', 'time'], coords={'time': [np.datetime64('2018-05-30T10:05:00')]}) with TempFile() as filename: scn.save_datasets(filename=filename, writer='cf') with xr.open_dataset(filename, decode_cf=False) as f: self.assertTrue(np.all(f['time_bnds'][:] == np.array([-300., 600.])))
def test_load_chlor_a(self, input_files, exp_plat, exp_sensor, exp_rps, apply_quality_flags): """Test that we can load 'chlor_a'.""" reader_kwargs = {"apply_quality_flags": apply_quality_flags} scene = Scene(reader='seadas_l2', filenames=input_files, reader_kwargs=reader_kwargs) scene.load(['chlor_a']) data_arr = scene['chlor_a'] assert data_arr.attrs['platform_name'] == exp_plat assert data_arr.attrs['sensor'] == exp_sensor assert data_arr.attrs['units'] == 'mg m^-3' assert data_arr.dtype.type == np.float32 assert isinstance(data_arr.attrs["area"], SwathDefinition) assert data_arr.attrs["rows_per_scan"] == exp_rps data = data_arr.data.compute() if apply_quality_flags: assert np.isnan(data[2, 2]) assert np.count_nonzero(np.isnan(data)) == 1 else: assert np.count_nonzero(np.isnan(data)) == 0
def test_p2g_palettize(self, keep_palette, ds_name, tmpdir, abi_l1b_c01_data_array): if ds_name == "test_p2g_palettize3": shutil.copy(os.path.join(TEST_ETC_DIR, f"{ds_name}.npy"), tmpdir) new_data_arr = abi_l1b_c01_data_array.copy() data = da.linspace(180, 280, new_data_arr.size).reshape(new_data_arr.shape) new_data_arr.data = data new_data_arr.attrs["name"] = ds_name scn = Scene() scn[ds_name] = new_data_arr out_fn = str(tmpdir + f"{ds_name}_{keep_palette}.tif") with easy_cwd(tmpdir): scn.save_datasets(filename=out_fn, keep_palette=keep_palette) with rasterio.open(out_fn, "r") as out_ds: is_palette = keep_palette and "palettize" in ds_name num_bands = 1 if is_palette else 4 assert out_ds.count == num_bands if is_palette: assert out_ds.colormap(1) is not None
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 test_scene_loading(self, calibrate, *mocks): """Test masking of space pixels.""" from satpy import Scene nrows = 25 ncols = 100 calibrate.return_value = np.ones((nrows, ncols)) with _fake_hsd_handler() as fh: with mock.patch('satpy.readers.ahi_hsd.AHIHSDFileHandler') as fh_cls: fh_cls.return_value = fh fh.filename_info['total_segments'] = 1 fh.filename_info['segment'] = 1 fh.data_info['number_of_columns'] = ncols fh.data_info['number_of_lines'] = nrows scn = Scene(reader='ahi_hsd', filenames=['HS_H08_20210225_0700_B07_FLDK_R20_S0110.DAT']) scn.load(['B07']) im = scn['B07'] # Make sure space masking worked mask = im.to_masked_array().mask ref_mask = np.logical_not(get_geostationary_mask(fh.area).compute()) np.testing.assert_equal(mask, ref_mask)
def test_save_array_coords(self): """Test saving array with coordinates.""" from satpy import Scene import xarray as xr import numpy as np scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) coords = { 'x': np.arange(3), 'y': np.arange(1), } if CRS is not None: proj_str = ('+proj=geos +lon_0=-95.0 +h=35786023.0 ' '+a=6378137.0 +b=6356752.31414 +sweep=x ' '+units=m +no_defs') coords['crs'] = CRS.from_string(proj_str) scn['test-array'] = xr.DataArray([[1, 2, 3]], dims=('y', 'x'), coords=coords, attrs=dict( start_time=start_time, end_time=end_time, prerequisites=[DatasetID('hej')])) with TempFile() as filename: scn.save_datasets(filename=filename, writer='cf') import h5netcdf as nc4 with nc4.File(filename) as f: self.assertTrue(np.all(f['test-array'][:] == [1, 2, 3])) self.assertTrue(np.all(f['x'][:] == [0, 1, 2])) self.assertTrue(np.all(f['y'][:] == [0])) self.assertNotIn('crs', f) self.assertNotIn('_FillValue', f['x'].attrs) self.assertNotIn('_FillValue', f['y'].attrs) expected_prereq = ( "DatasetID(name='hej', wavelength=None, " "resolution=None, polarization=None, " "calibration=None, level=None, modifiers=())") self.assertEqual(f['test-array'].attrs['prerequisites'][0], expected_prereq)
def process_trio(trio, curr_idx, len_trio): """trio is a list of three parsed filenames (see the function parse_filename below). Given these three files, use Scene to load the appropriate channels. Then resample (colocate) to make the channels match up. Then save these colocated channels. Crop the NaN edges, tag with meta information (which files were used as input), And finally save the numpy arrays (so we don't need to recompute next time)""" dt = trio[0]["datetime"] log.info( f'{rgb(255,0,0)}Processing{reset} timestep {bold}{curr_idx + 1}/{len_trio}{reset} {blue}{dt}{reset} ' ) #load the sat data scn = Scene( reader='viirs_sdr', filenames=[f['path'] for f in trio if f['filename'].endswith(".h5")]) scn.load(viirs_channels + lat_long_both) #load and pair the reflectance Reflectance = xarray.open_dataset(find_ncfile(trio)['path']) swath_def = SwathDefinition(Reflectance['longitude'], Reflectance['latitude']) sm_refl = Reflectance['SM_Reflectance'] sm_refl.attrs['area'] = swath_def #bring reflectance back to the satpy "Scene" scn['SM_reflectance'] = sm_refl log.info(f'Resampling {blue}{dt}{reset}') resample_scn = scn.resample(scn['DNB'].attrs['area'], resampler='nearest') log.info(f'Cropping nan edges of {blue}{dt}{reset}') t = time.time() data = crop.crop_nan_edges(resample_scn, all_channels) log.debug( f'Cropping nan edges took {rgb(255,0,0)}{time.time() - t:.2f}{reset} seconds' ) data['channels'] = list(data) data['filenames'] = [f['filename'] for f in trio] data["datetime"] = dt return data
def load_himawari(indir, in_time, comp_type, timedelt, mode): """ Load a Himawari/AHI scene as given by img_time. img_time should be the *start* time for the scan, as the ending time will be automatically defined from this using timedelt The image will be loaded with Satpy, return value is a cartopy object Arguments: indir - directory holding the Himawari data in HSD (unzipped) format img_time - a datetime indicating the scene start time in UTC comp_type - the Satpy composite to create (true_color, B03, etc) timedelt - the scanning time delta (10 min for full disk AHI) mode - scanning mode (FD = Full disk, MESO = Mesoscale sector) Returns: sat_data - the satellite data object, unresampled """ if mode == 'MESO': tmp_t = in_time minu = tmp_t.minute minu = minu - (minu % 10) tmp_t = tmp_t.replace(minute=minu) tmp_t = tmp_t.replace(second=0) dt = (in_time - tmp_t).total_seconds() / 60. src_str = '*_R30' + str(int(dt/timedelt) + 1) + '*' dtstr = tmp_t.strftime("%Y%m%d_%H%M") files = glob(indir + '*' + dtstr + src_str + '.DAT') files.sort() else: files = ffar(start_time=in_time, end_time=in_time + timedelta(minutes=timedelt-1), base_dir=indir, reader='ahi_hsd') scn = Scene(reader='ahi_hsd', filenames=files) scn.load([comp_type], pad_data=False) return scn
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 load_sat_polar(in_reader, globber, chans, latpos, lonpos, deller): """Load data for a given sensor, find BT at particular lat/lon.""" storm_bbox = [ lonpos - deller, latpos - deller, lonpos + deller, latpos + deller ] files = glob(globber) if len(files) < 1: return None scn = Scene(files, reader=in_reader) scn.load(chans) lons = None lats = None output_dict = {} for chan in chans: try: if lons is None or lats is None: lons, lats = scn[chan].attrs['area'].get_lonlats() try: lats = lats.compute() lons = lons.compute() except: pass btdata = np.array(scn[chan]) pts = (lons < storm_bbox[0]).nonzero() btdata[pts] = np.nan pts = (lats < storm_bbox[1]).nonzero() btdata[pts] = np.nan pts = (lons > storm_bbox[2]).nonzero() btdata[pts] = np.nan pts = (lats > storm_bbox[3]).nonzero() btdata[pts] = np.nan output_dict[chan] = [ np.nanmin(btdata), scn[chan].wavelength.min, scn[chan].wavelength.central, scn[chan].wavelength.max ] except KeyError: output_dict[chan] = [None, None, None, None] return output_dict
def test_encoding_kwarg(self): """Test encoding of keyword arguments.""" from satpy import Scene import xarray as xr scn = Scene() start_time = datetime(2018, 5, 30, 10, 0) end_time = datetime(2018, 5, 30, 10, 15) scn['test-array'] = xr.DataArray([1, 2, 3], attrs=dict(start_time=start_time, end_time=end_time)) with TempFile() as filename: encoding = {'test-array': {'dtype': 'int8', 'scale_factor': 0.1, 'add_offset': 0.0, '_FillValue': 3}} scn.save_datasets(filename=filename, encoding=encoding, writer='cf') with xr.open_dataset(filename, mask_and_scale=False) as f: self.assertTrue(np.all(f['test-array'][:] == [10, 20, 30])) self.assertTrue(f['test-array'].attrs['scale_factor'] == 0.1) self.assertTrue(f['test-array'].attrs['_FillValue'] == 3) # check that dtype behave as int8 self.assertTrue(np.iinfo(f['test-array'][:].dtype).max == 127)
def get_swath_rgb(radiance_filename, myd03_dir, composite='true_color'): """ :param radiance_filename: MYD02 filename :param myd03_dir: root directory of MYD03 geolocational files :return visible RGB channels: numpy.ndarray of size (3, 2030, 1354) Uses the satpy Scene reader with the modis-l1b files. Issues reading files might be due to pyhdf not being installed - otherwise try pip install satpy[modis_0l1b] Creates a scene with the MYD02 file, and extracts the RGB channels from the 1, 4, 3 visible MODIS bands. """ # find a corresponding geolocational (MOD03) file for the provided radiance (MYD02) file geoloc_filename = find_matching_geoloc_file(radiance_filename, myd03_dir) global_scene = Scene(reader='modis_l1b', filenames=[radiance_filename, geoloc_filename]) # load it in, make sure resolution is 1000 to match our other datasets global_scene.load([composite], resolution=1000) rgb = np.array(global_scene[composite])[:, :MAX_HEIGHT, :MAX_WIDTH] return rgb
def get_fake_scene(): scene = Scene() start_time = dt.datetime(2020, 1, 1, 12) scene['VIS006'] = xr.DataArray( [[1, 2], [3, 4]], dims=('y', 'x'), attrs={ 'calibration': 'reflectance', 'sun_earth_distance_correction_applied': True, 'start_time': start_time, 'wavelength': WavelengthRange(0.56, 0.635, 0.71) }) scene['IR_108'] = xr.DataArray( [[5, 6], [7, 8]], dims=('y', 'x'), attrs={ 'calibration': 'brightness_temperature', 'start_time': start_time, 'wavelength': WavelengthRange(9.8, 10.8, 11.8) }) scene.attrs['sensor'] = {'seviri'} return scene