def resample(self, target_area_def): """Resamples image to area definition using nearest neighbour approach in projection coordinates. Parameters ---------- target_area_def : object Target area definition as AreaDefinition object Returns ------- image_container : object ImageContainerQuick object of resampled area """ resampled_image = grid.get_resampled_image(target_area_def, self.geo_def, self.image_data, fill_value=self.fill_value, nprocs=self.nprocs, segments=self.segments) return ImageContainerQuick(resampled_image, target_area_def, fill_value=self.fill_value, nprocs=self.nprocs, segments=self.segments)
def convert_files(): """ Converts all files in a directory of .nc files into 500x500 .npy files of California brightness temperatures, and deletes the originals. If the conversion fails, the file name is written to a log. """ convert_log = open("../../GOES_Files/convert_log.txt", "w") fail_log = open("../../GOES_Files/fail_log.txt", "w") for file in os.listdir('../../GOES_Files/nc_files/'): try: data = xr.open_dataset('../../GOES_Files/nc_files/' + file) dat = data.metpy.parse_cf('Rad') geos = dat.metpy.cartopy_crs rad = dat.data fk1 = float(data.metpy.parse_cf('planck_fk1')) fk2 = float(data.metpy.parse_cf('planck_fk2')) bc1 = float(data.metpy.parse_cf('planck_bc1')) bc2 = float(data.metpy.parse_cf('planck_bc2')) bt = (fk2 / (xr.ufuncs.log(fk1 / rad + 1)) - bc1) / bc2 bt = np.asarray(bt) goes_params = geos.proj4_params source_area = geometry.AreaDefinition.from_cf(data) target_area = geometry.AreaDefinition.from_extent( 'CA', pc_params, shape, target_extents) result = grid.get_resampled_image(target_area, source_area, bt) np.save('../../GOES_Files/npy_files/' + file[:-3], result) data.close() os.remove('../../GOES_Files/nc_files/' + file) convert_log.write('\n' + file) except: fail_log.write('\n' + file) convert_log.close() fail_log.close()
def test_resampled_image_mp(self): data = np.fromfunction(lambda y, x: y*x*10**-6, (3712, 3712)) target_def = self.area_def source_def = self.msg_area res = grid.get_resampled_image(target_def, source_def, data, nprocs=2, segments=1) cross_sum = res.sum() expected = 399936.39392500359 self.assertAlmostEqual(cross_sum, expected, msg='Resampling of image mp failed')
def test_resampled_image_mp(self): data = np.fromfunction(lambda y, x: y * x * 10 ** -6, (3712, 3712)) target_def = self.area_def source_def = self.msg_area res = grid.get_resampled_image( target_def, source_def, data, nprocs=2, segments=1) cross_sum = res.sum() expected = 399936.39392500359 self.assertAlmostEqual( cross_sum, expected, msg='Resampling of image mp failed')
def test_resampled_image_masked(self): # Generate test image with masked elements data = np.ma.ones(self.msg_area.shape) data.mask = np.zeros(data.shape) data.mask[253:400, 1970:2211] = 1 # Resample image using multiple segments target_def = self.area_def source_def = self.msg_area res = grid.get_resampled_image( target_def, source_def, data, segments=4, fill_value=None) # Make sure the mask has been preserved self.assertGreater(res.mask.sum(), 0, msg='Resampling did not preserve the mask')