def get_area_def(hdf_file): generic_m3 = str(hdf_file) metadata = parseMeta(generic_m3) # Read the lats and lons from the MYD03 file print(f'reading {generic_m3}') m3_file = SD(str(generic_m3), SDC.READ) lats = m3_file.select('Latitude').get() lons = m3_file.select('Longitude').get() m3_file.end() proj_params = get_proj_params(generic_m3) swath_def = SwathDefinition(lons, lats) area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params) return (area_def, lons, lats, metadata, swath_def)
def setUp(self): """Set up the test case.""" chunks = 10 self.dst_area = AreaDefinition( 'euro40', 'euro40', None, { 'proj': 'stere', 'lon_0': 14.0, 'lat_0': 90.0, 'lat_ts': 60.0, 'ellps': 'bessel' }, 102, 102, (-2717181.7304994687, -5571048.14031214, 1378818.2695005313, -1475048.1403121399)) self.src_area = AreaDefinition( 'omerc_otf', 'On-the-fly omerc area', None, { 'alpha': '8.99811271718795', 'ellps': 'sphere', 'gamma': '0', 'k': '1', 'lat_0': '0', 'lonc': '13.8096029486222', 'proj': 'omerc', 'units': 'm' }, 50, 100, (-1461111.3603, 3440088.0459, 1534864.0322, 9598335.0457)) self.lons, self.lats = self.src_area.get_lonlats(chunks=chunks) xrlons = xr.DataArray(self.lons.persist()) xrlats = xr.DataArray(self.lats.persist()) self.src_swath = SwathDefinition(xrlons, xrlats)
def main(): """ run the test """ generic_m3 = a301.data_dir / Path("m3_file_2018_10_1.hdf") if not generic_m3.exists(): raise ValueError(f"couldn't find {generic_m3}") modis_meta = parseMeta(generic_m3) print(f"working on {generic_m3}, originally was { modis_meta['filename']}") m3_file = SD(str(generic_m3), SDC.READ) lats = m3_file.select('Latitude').get() lons = m3_file.select('Longitude').get() stars = '*' * 40 print( f"{stars}\nlats.shape, lons.shape: {lats.shape},{lons.shape}\n{stars}") m3_file.end() generic_rad = a301.data_dir / Path("rad_file_2018_10_1.hdf") if not generic_rad.exists(): raise ValueError(f"couldn't find {generic_rad}") rad_file = SD(str(generic_rad), SDC.READ) ch30 = rad_file.select('ch30').get() print(f"working on {generic_rad}, originally was {rad_file.filename}") print(f"{stars}\narray shape is: {ch30.shape}\n{stars}") rad_file.end() print(f'reading {generic_m3}') from pyresample import SwathDefinition proj_params = get_proj_params(generic_m3) swath_def = SwathDefinition(lons, lats) area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params) fill_value = -9999. area_name = 'modis swath 5min granule' image_30 = kd_tree.resample_nearest(swath_def, ch30.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) print(f'\ndump area definition:\n{area_def}\n') print((f'\nx and y pixel dimensions in meters:' f'\n{area_def.pixel_size_x}\n{area_def.pixel_size_y}\n'))
def runit(): from pyresample import SwathDefinition, kd_tree, geometry proj_params = get_proj_params(m5_file) swath_def = SwathDefinition(lons_5km, lats_5km) area_def_lr = swath_def.compute_optimal_bb_area(proj_dict=proj_params) area_def_lr.name = "ir wv retrieval modis 5 km resolution (lr=low resolution)" area_def_lr.area_id = 'modis_ir_wv' area_def_lr.job_id = area_def_lr.area_id fill_value = -9999. image_wv_ir = kd_tree.resample_nearest(swath_def, wv_ir_scaled.ravel(), area_def_lr, radius_of_influence=5000, nprocs=2, fill_value=fill_value) image_wv_ir[image_wv_ir < -9000] = np.nan print(f'\ndump area definition:\n{area_def_lr}\n') print((f'\nx and y pixel dimensions in meters:' f'\n{area_def_lr.pixel_size_x}\n{area_def_lr.pixel_size_y}\n')) pdb.set_trace()
def swath_resample(swath: dict, trg_proj: AreaDefinition): """ resamples swath data into a new grid defined by trg_proj. trg_proj is constructed using pyresample (see map_proj) Parameters ---------- swath: dict dictionary with keys radius_of_influence float: search distance in m for data resampling channels ma.array: ma.dstack masked array with the data being resampled latitude array: latitude with swath pixel_control_points and number_of_lines longitude ndarray: longitude with swath pixel_control_points and number_of_lines trg_proj: AreaDefinition target projection for data resampling Returns ------- ma.array result with data resampled to trg_proj """ warnings.filterwarnings('ignore') epsilon = swath.pop('epsilon') radius_of_influence = swath.pop('radius_of_influence') src_sds = swath.pop('channels') src_proj = SwathDefinition(lons=swath.pop('longitude'), lats=swath.pop('latitude')) nprocs = 4 if len(src_sds.shape) > 2 else 1 result = resample_nearest(src_proj, src_sds, trg_proj, fill_value=None, epsilon=epsilon, nprocs=nprocs, radius_of_influence=radius_of_influence) return result
def main(): """ run the test """ stars = '*' * 50 print(f'\n{stars}\nrunning test: {__file__}\n{stars}\n') generic_m3 = a301.data_dir / Path("m3_file_2018_10_1.hdf") if not generic_m3.exists(): raise ValueError(f"couldn't find {generic_m3}") modis_meta = parseMeta(generic_m3) print(f"working on {generic_m3}, originally was { modis_meta['filename']}") m3_file = SD(str(generic_m3), SDC.READ) lats_1km = m3_file.select('Latitude').get() lons_1km = m3_file.select('Longitude').get() stars = '*' * 40 print( f"{stars}\nlats_1km.shape, lons_1km.shape: {lats_1km.shape},{lons_1km.shape}\n{stars}" ) m3_file.end() generic_m5 = a301.data_dir / Path("myd05_l2_10_7.hdf") if not generic_m5.exists(): raise ValueError(f"couldn't find {generic_m5}") m5_file = SD(str(generic_m5), SDC.READ) vapor_ir = m5_file.select('Water_Vapor_Infrared').get() vapor_near_ir = m5_file.select('Water_Vapor_Near_Infrared').get() lats_5km = m5_file.select('Latitude').get() lons_5km = m5_file.select('Longitude').get() m5_file.end() print('through') m5_meta = parseMeta(generic_m5) print(f"working on {generic_m5}, originally was {m5_meta['filename']}") print( f"{stars}\nnearir vapor array shape is: {vapor_near_ir.shape}\n{stars}" ) print(f"{stars}\nir vapor array shape is: {vapor_ir.shape}\n{stars}") print(f"{stars}\nlats_5km arrayshape is: {lats_5km.shape}\n{stars}") print(f"{stars}\nlons_5km arrayshape is: {lons_5km.shape}\n{stars}") # print(f'reading {generic_m3}') from pyresample import SwathDefinition proj_params = get_proj_params(generic_m3) swath_def = SwathDefinition(lons_1km, lats_1km) area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params) fill_value = -9999. area_name = 'modis swath 5min granule' image_nearir = kd_tree.resample_nearest(swath_def, vapor_near_ir.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) print( f'was able to regrid the nearir image, xy shape is {image_nearir.shape}' ) proj_params = get_proj_params(generic_m5) swath_def = SwathDefinition(lons_5km, lats_5km) area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params) fill_value = -9999. area_name = 'modis swath 5min granule' image_ir = kd_tree.resample_nearest(swath_def, vapor_ir.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) print(f'was able to regrid the ir image, xy shape is {image_ir.shape}') print('data looks good, ready to go')
ds['lon'] = np.mod(ds.lon + 180, 360) - 180 if isat == 1: #change RSS data to conform with JPL definitions ds = ds.rename({ 'row_time': 'time', 'ice_concentration': 'fice' }) #first do a quick check using resample to project the orbit onto a grid #and quickly see if there is any data in the cruise area on that day #if there is, then continue to collocation x = ds['lon'].fillna(-89).data y = ds['lat'].fillna(-89).data z = ds['smap_sss'].data lons, lats, data = x, y, z swath_def = SwathDefinition(lons, lats) # Resample swath to a fixed 0.01 x 0.01 grid, represented by the variable grid_def: # https://stackoverflow.com/questions/58065055/floor-and-ceil-with-number-of-decimals #changed to be just the region of the usv cruise to make grid even smaller (hopefully) #when working with global orbital data, work with usv BUT #when working with granules use ds instead of ds_usv so you just do granule region grid_def_lon_min, grid_def_lon_max = np.round( ds_day.lon.min().data - 0.5 * 10**(-2), 2), np.round(ds_day.lon.max().data + 0.5 * 10**(-2), 2) grid_def_lat_min, grid_def_lat_max = np.round( ds_day.lat.min().data - 0.5 * 10**(-2), 2), np.round(ds_day.lat.max().data + 0.5 * 10**(-2), 2) grid_def_lons, grid_def_lats = np.arange( grid_def_lon_min, grid_def_lon_max + 0.1, 0.1), np.arange(grid_def_lat_max, grid_def_lat_min - 0.1,
ABI_loc = '/localdata/cases/' + case + '/ABI/' + v[ABIp][0] + '/' GLM_loc = '/localdata/cases/' + case + '/GLM5/' save_pic_loc = '/localdata/cases/' + case + '/resampled_pics/' save_file_loc = '/localdata/cases/' + case + '/resampled_data/' ABI_filelist = sorted(os.listdir(ABI_loc)) halfway = len(ABI_filelist) / 2 # In[38]: #Setting up the resampled swaths lat2 = np.load('/localdata/coordinates/2km_lat_grid.npy') lon2 = np.load('/localdata/coordinates/2km_lon_grid.npy') lat10 = np.load('/localdata/coordinates/10km_lat.npy') lon10 = np.load('/localdata/coordinates/10km_lon.npy') swath_def10 = SwathDefinition(lons=lon10, lats=lat10) swath_def2 = SwathDefinition(lons=lon2, lats=lat2) # ## Step 1: Generating the composite datasets # In[39]: GLM_composite_a = np.zeros((1, 150, 250)).astype(np.float16) GLM_composite_b = np.zeros((1, 150, 250)).astype(np.float16) ABI_composite_a = np.zeros((1, 150, 250)).astype(np.float16) ABI_composite_b = np.zeros((1, 150, 250)).astype(np.float16) for file in sorted(os.listdir(ABI_loc)): #Loading in the ABI data ABI_data = nc.Dataset(ABI_loc + file, 'r') ABI_x = ABI_data.variables['x'][:]
metadata = parseMeta(generic_m3) # Read the lats and lons from the MYD03 file print(f'reading {generic_m3}') m3_file = SD(str(generic_m3), SDC.READ) lats = m3_file.select('Latitude').get() lons = m3_file.select('Longitude').get() m3_file.end() dir(metadata) # %% {"scrolled": true} from pyresample import load_area, save_quicklook, SwathDefinition proj_params = get_proj_params(generic_m3) swath_def = SwathDefinition(lons, lats) area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params) area_def # %% print(swath_def.shape) print(area_def.shape) # %% {"scrolled": false} #p_utm = Proj(crs) p_lonlat = Proj(proj='latlong', datum='WGS84') stn_lon = -105.237 stn_lat = 40.125 stn_x, stn_y = proj_transform(p_lonlat, Proj(area_def.proj_dict), stn_lon, stn_lat) min_x, min_y = proj_transform(p_lonlat, Proj(area_def.proj_dict),
# Let swath_def.compute_optimal_bb_area choose the extent and dimensions for # the low resolution (lr) image. The cell below let's pyresample create the # area_def object, which we will reuse for the 1 km watervapor retrieval to # get both onto the same grid. # # The cell below produces: # # * `image_wv_ir` -- resampled 5 km infrared water vapor # * `area_def_lr` -- area_def used for the resample # %% from pyresample import SwathDefinition, kd_tree, geometry proj_params_5km = get_proj_params(m5_file_str) proj_params_1km = get_proj_params(m3_file_str) swath_def = SwathDefinition(lons_5km, lats_5km) area_def_lr = swath_def.compute_optimal_bb_area(proj_dict=proj_params_5km) #area_def_lr.name = "ir wv retrieval modis 5 km resolution (lr=low resolution)" #area_def_lr.area_id = "modis_ir_wv" #area_def_lr.job_id = area_def_lr.area_id fill_value = -9999.0 image_wv_ir = kd_tree.resample_nearest( swath_def, wv_ir_scaled.ravel(), area_def_lr, radius_of_influence=5000, nprocs=2, fill_value=fill_value, ) image_wv_ir[image_wv_ir < -9000] = np.nan print(f"\ndump area definition:\n{area_def_lr}\n")
def run(input_dir, output_dir): DATA_DIR_2 = os.path.join(input_dir, "ch2") DATA_DIR_6 = os.path.join(input_dir, "ch6") DATA_DIR_7 = os.path.join(input_dir, "ch7") DATA_DIR_14 = os.path.join(input_dir, "ch14") # Get contents of data dir for ch 7 data_list_7 = os.listdir(DATA_DIR_7) if ".DS_Store" in data_list_7: data_list_7.remove(".DS_Store") # For mac users data_list_7 = sorted(data_list_7) # Get contents of data dir for ch14 data_list_14 = os.listdir(DATA_DIR_14) if ".DS_Store" in data_list_14: data_list_14.remove(".DS_Store") # For mac users data_list_14 = sorted(data_list_14) # Get contents of data dir for ch 2 data_list_2 = os.listdir(DATA_DIR_2) if ".DS_Store" in data_list_2: data_list_2.remove(".DS_Store") # For mac users data_list_2 = sorted(data_list_2) # Get contents of data dir for ch 6 data_list_6 = os.listdir(DATA_DIR_6) if ".DS_Store" in data_list_6: data_list_6.remove(".DS_Store") # For mac users data_list_6 = sorted(data_list_6) # Load ch7 for projection constants first_ds_name = data_list_7[0] first_ds_path = os.path.join(DATA_DIR_7, first_ds_name) first_ds = GOES.open_dataset(first_ds_path) var_ch02, lons, lats = first_ds.image("Rad", domain=[LLLon, URLon, LLLat, URLat]) var_ch02, lons, lats = var_ch02.data, lons.data, lats.data HEIGHT = var_ch02.shape[0] WIDTH = var_ch02.shape[1] # Setup projection constants used throughout the script. tiff_path = os.path.join(TIFF_DIR, "0.tif") p_crs = CRS.from_epsg(3857) p_latlon = CRS.from_proj4("+proj=latlon") crs_transform = Transformer.from_crs(p_latlon, p_crs) ll_x, ll_y = crs_transform.transform(LLLon, LLLat) ur_x, ur_y = crs_transform.transform(URLon, URLat) area_extent = (ll_x, ll_y, ur_x, ur_y) ul_x = ll_x # Why these? ul_y = ur_y area_id = "California Coast" description = "See area ID" proj_id = "Mercator" pixel_size_x = (ur_x - ll_x) / (WIDTH - 1) pixel_size_y = (ur_y - ll_y) / (HEIGHT - 1) new_affine = Affine(pixel_size_x, 0.0, ul_x, 0.0, -pixel_size_y, ul_y) area_def = AreaDefinition(area_id, description, proj_id, p_crs, WIDTH, HEIGHT, area_extent) fill_value = np.nan # Load ch7 for land masking first_ds_name = data_list_7[0] first_ds_path = os.path.join(DATA_DIR_7, first_ds_name) first_ds = GOES.open_dataset(first_ds_path) var_ch07, lons, lats = first_ds.image("Rad", domain=[LLLon, URLon, LLLat, URLat]) var_ch07, lons, lats = var_ch07.data, lons.data, lats.data swath_def = SwathDefinition(lons, lats) first_ds = None # Free the memory from these big datasets var_ch07 = kd_tree.resample_nearest(swath_def, var_ch07.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) ###### New land masking system ####################### with rasterio.open( tiff_path, "w", driver="GTiff", height=HEIGHT, width=WIDTH, count=1, #???? dtype=var_ch07.dtype, crs=p_crs, transform=new_affine, nodata=fill_value, ) as dst: dst.write(np.reshape(var_ch07, (1, HEIGHT, WIDTH))) src = rasterio.open(tiff_path, mode='r+') geodf = geopandas.read_file(LAND_POLYGON_SHAPE) land_masking, other_affine = mask.mask(src, geodf[['geometry' ]].values.flatten(), invert=True, filled=False) land_masking = np.ma.getmask(land_masking) land_masking = np.reshape(land_masking, (HEIGHT, WIDTH)) src.close() # Free memory src = None geodf = None ############################################################ # Init multi-tracker trackers = MultiTrackerImproved(cv2.TrackerCSRT_create) image_list = [] # BTD_list = [] refl_ch2_list = [] refl_ch6_list = [] i = 0 for ds_name_7 in data_list_7: ds_name_14 = data_list_14[i] ds_name_2 = data_list_2[i] ds_name_6 = data_list_6[i] ds_path_7 = os.path.join(DATA_DIR_7, ds_name_7) ds_path_14 = os.path.join(DATA_DIR_14, ds_name_14) ds_path_2 = os.path.join(DATA_DIR_2, ds_name_2) ds_path_6 = os.path.join(DATA_DIR_6, ds_name_6) # Load channel 2 ds_2 = GOES.open_dataset(ds_path_2) var_ch02, lons, lats = ds_2.image("Rad", domain=[LLLon, URLon, LLLat, URLat]) var_ch02, lons, lats = var_ch02.data, lons.data, lats.data swath_def = SwathDefinition(lons, lats) var_ch02 = kd_tree.resample_nearest(swath_def, var_ch02.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) # Load channel 2 reflectivity ds_2 = GOES.open_dataset(ds_path_2) refl_var_ch02, lons, lats = ds_2.image( "Rad", up_level=True, domain=[LLLon, URLon, LLLat, URLat]) refl_var_ch02 = refl_var_ch02.refl_fact_to_refl(lons, lats).data swath_def = SwathDefinition(lons.data, lats.data) refl_var_ch02 = kd_tree.resample_nearest(swath_def, refl_var_ch02.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) # Load channel 6 reflectivity ds_6 = GOES.open_dataset(ds_path_6) refl_var_ch06, lons, lats = ds_6.image( "Rad", up_level=True, domain=[LLLon, URLon, LLLat, URLat]) refl_var_ch06 = refl_var_ch06.refl_fact_to_refl(lons, lats).data swath_def = SwathDefinition(lons.data, lats.data) refl_var_ch06 = kd_tree.resample_nearest(swath_def, refl_var_ch06.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) # Load channel 7 ds_7 = GOES.open_dataset(ds_path_7) var_ch07, lons, lats = ds_7.image("Rad", domain=[LLLon, URLon, LLLat, URLat]) var_ch07, lons, lats = var_ch07.data, lons.data, lats.data swath_def = SwathDefinition(lons, lats) var_ch07 = kd_tree.resample_nearest(swath_def, var_ch07.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) # Load channel 14 ds_14 = GOES.open_dataset(ds_path_14) var_ch14, lons, lats = ds_14.image("Rad", domain=[LLLon, URLon, LLLat, URLat]) var_ch14, lons, lats = var_ch14.data, lons.data, lats.data swath_def = SwathDefinition(lons, lats) var_ch14 = kd_tree.resample_nearest(swath_def, var_ch14.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value) # Make BTD var = calc_BTD.main_func(var_ch14, var_ch07, 14, 7) # Skip day if it has bad data if np.isnan(var).any(): i = i + 1 continue # Make copy of the BTD for use as a backround in cv2 image output # Maps the BTD values to a range of [0,255] # BTD = copy.deepcopy(var) BTD_img = copy.deepcopy(var) min_BTD = np.nanmin(BTD_img) if min_BTD < 0: BTD_img = BTD_img + np.abs(min_BTD) max_BTD = np.nanmax(BTD_img) BTD_img = BTD_img / max_BTD # BTD_img = cv2.cvtColor(BTD_img*255, cv2.COLOR_GRAY2BGR) # BTD_img_trackers = copy.deepcopy(BTD_img) # Next two lines are for new BTD data for trackers # BTD_img_trackers = np.array(BTD_img_trackers).astype('uint8') # Since it seems the trackers need images of type uint8 # Filter out the land var[land_masking] = np.nan # Create mask array for the highest clouds high_cloud_mask = calc_BTD.bt_ch14_temp_conv( var_ch14) < 5 # TODO: Make this more robust #### Use reflectivity of channel 2 and BT of channel 14 to filter out open ocean data ########### BT = calc_BTD.bt_ch14_temp_conv(var_ch14) BT = BT[np.logical_and( np.logical_not(land_masking), np.logical_not(high_cloud_mask) )] # Filter out the land since golden arches works best when only over water var_ch02 = var_ch02[np.logical_and( np.logical_not(land_masking), np.logical_not(high_cloud_mask) )] # Filter out the land since golden arches works best when only over water BT_and_CH02 = np.vstack((BT, var_ch02)).T BT_and_CH02_sample, _ = train_test_split(BT_and_CH02, train_size=10000) clusterer = DBSCAN(eps=1.5, min_samples=100) # Found through extensive testing classifier = DecisionTreeClassifier() inductive_cluster = InductiveClusterer( clusterer, classifier).fit(BT_and_CH02_sample) IC_labels = inductive_cluster.predict(BT_and_CH02) + 1 all_labels = np.unique(IC_labels) min_refl = np.Inf open_ocean_label = 0 for j in all_labels: labeled_refl_array = var_ch02[IC_labels == j] mean_refl = np.nanmean(labeled_refl_array) if mean_refl < min_refl: open_ocean_label = j min_refl = mean_refl golden_arch_mask_ocean = IC_labels == open_ocean_label golden_arch_mask = np.zeros(var.shape, dtype=bool) golden_arch_mask[np.logical_and( np.logical_not(land_masking), np.logical_not(high_cloud_mask))] = golden_arch_mask_ocean var = np.where(golden_arch_mask, np.nan, var) ############################################################################################### #Filter out the cold high altitude clouds var = np.where(high_cloud_mask, np.nan, var) var = feature.canny(var, sigma=2.2, low_threshold=0, high_threshold=1.2) var = np.where(var == np.nan, 0, var) ## Skimage hough line transform ################################# var = np.array(var).astype('uint8') img = cv2.cvtColor(var * 255, cv2.COLOR_GRAY2BGR) # Was 0, 30, 1 threshold = 0 minLineLength = 30 maxLineGap = 2 theta = np.linspace(-np.pi, np.pi, 1000) lines = transform.probabilistic_hough_line(var, threshold=threshold, line_length=minLineLength, line_gap=maxLineGap, theta=theta) ############################################################# #### TRACKER ################# trackers.update(img, i) if lines is not None: for line in lines: p0, p1 = line x1 = p0[0] y1 = p0[1] x2 = p1[0] y2 = p1[1] min_x = np.minimum(x1, x2) min_y = np.minimum(y1, y2) max_x = np.maximum(x1, x2) max_y = np.maximum(y1, y2) rect = (min_x - 2, min_y - 2, max_x - min_x + 4, max_y - min_y + 4 ) #TODO: Maybe expand the size of the boxes a bit? trackers.add_tracker(img, rect, len(data_list_7)) ############################### image_list.append(BTD_img) # BTD_list.append(BTD) refl_ch2_list.append(refl_var_ch02) refl_ch6_list.append(refl_var_ch06) print("Image " + str(i) + " Calculated") i = i + 1 # TODO: Remove BTD_list in all areas if I am not using it for real final pngs for i in range(len(image_list)): label_name = "labels" data_name = "data" filename = str(i) + ".tif" data_file_path = os.path.join(output_dir, data_name, filename) label_file_path = os.path.join(output_dir, label_name, filename) boxes = trackers.get_boxes(i) BTD_img = image_list[i] # BTD = BTD_list[i] refl_var_ch02 = refl_ch2_list[i] refl_var_ch06 = refl_ch6_list[i] # Make box plots for trackers # Also make and highlight the labels labels = np.zeros([BTD_img.shape[0], BTD_img.shape[1]], dtype=np.float32) for box in boxes: (x, y, w, h) = [int(v) for v in box] if w > 0 and h > 0 and x >= 0 and y >= 0 and y + h <= BTD_img.shape[ 0] and x + w <= BTD_img.shape[1] and y < BTD_img.shape[ 0] and x < BTD_img.shape[1]: ch2_slice = refl_var_ch02[y:y + h, x:x + w] ch6_slice = refl_var_ch06[y:y + h, x:x + w] labels_slice = labels[y:y + h, x:x + w] labels_slice = np.where( np.logical_and(ch6_slice >= 0.28, ch2_slice >= 0.3), 1.0, labels_slice) labels[y:y + h, x:x + w] = labels_slice # Add red for labels with rasterio.open( data_file_path, "w", driver="GTiff", height=HEIGHT, width=WIDTH, count=1, #???? dtype=BTD_img.dtype, crs=p_crs, transform=new_affine, nodata=fill_value, ) as dst: dst.write(np.reshape(BTD_img, (1, HEIGHT, WIDTH))) with rasterio.open( label_file_path, "w", driver="GTiff", height=HEIGHT, width=WIDTH, count=1, #???? dtype=labels.dtype, crs=p_crs, transform=new_affine, nodata=fill_value, ) as dst: dst.write(np.reshape(labels, (1, HEIGHT, WIDTH))) # BTD_img = cv2.addWeighted(BTD_img, 1.0, labels, 0.5, 0) # cv2.imwrite(file_path, BTD_img) print("Image " + str(i) + " Complete")
description = "See area ID" proj_id = "Mercator" pixel_size_x = (ur_x - ll_x)/(WIDTH - 1) pixel_size_y = (ur_y - ll_y)/(HEIGHT - 1) new_affine = Affine(pixel_size_x, 0.0, ul_x, 0.0, -pixel_size_y, ul_y) area_def = AreaDefinition(area_id, description, proj_id, p_crs, WIDTH, HEIGHT, area_extent) fill_value = np.nan # Load ch7 for land masking first_ds_name = data_list_7[0] first_ds_path = os.path.join(DATA_DIR_7, first_ds_name) first_ds = GOES.open_dataset(first_ds_path) var_ch07, lons, lats = first_ds.image("Rad", domain=[LLLon, URLon, LLLat, URLat]) var_ch07, lons, lats = var_ch07.data, lons.data, lats.data swath_def = SwathDefinition(lons, lats) first_ds = None # Free the memory from these big datasets var_ch07 = kd_tree.resample_nearest( swath_def, var_ch07.ravel(), area_def, radius_of_influence=5000, nprocs=2, fill_value=fill_value ) ###### New land masking system ####################### with rasterio.open( tiff_path, "w", driver="GTiff",
# ds.close() # file_counter_no_overlap = file_counter_no_overlap + 1 print('File '+str(file_idx+1)+': No overlap') continue # Else, get also time and data (SST) xlat,xlon,sat_time,var_data=get_orbital_data_slstr_l2p(file) # var_data = ds['sea_surface_temperature'] # sat_time = ds['time'] + ds['sst_dtime'] # ds.close() print('\nFile '+str(file_idx+1)+': OVERLAP') print('File name:',file) var_data = xr.DataArray.squeeze(var_data) amsr_data = np.squeeze(var_data.data) sat_time = np.squeeze(sat_time.data[:,:,0]).astype('datetime64[ms]') swath_def = SwathDefinition(amsr_lons, amsr_lats) # Resample swath to a fixed 0.01 x 0.01 grid, represented by the variable grid_def: # https://stackoverflow.com/questions/58065055/floor-and-ceil-with-number-of-decimals grid_def_lon_min = np.round(amsr_lons_min - 0.5 * 10**(-2), 2) grid_def_lon_max = np.round(amsr_lons_max + 0.5 * 10**(-2), 2) grid_def_lat_min = np.round(amsr_lats_min - 0.5 * 10**(-2), 2) grid_def_lat_max = np.round(amsr_lats_max + 0.5 * 10**(-2), 2) grid_def_lons = np.arange(grid_def_lon_min,grid_def_lon_max+0.01,0.01) grid_def_lats = np.arange(grid_def_lat_max,grid_def_lat_min-0.01,-0.01) grid_mesh_lons,grid_mesh_lats = np.meshgrid(grid_def_lons,grid_def_lats) # Since we have the lon and lat values for the area, we define a grid instead of an area: # https://pyresample.readthedocs.io/en/latest/geo_def.html#griddefinition grid_def = GridDefinition(lons=grid_mesh_lons,lats=grid_mesh_lats)