def load_data(self): with open_dataset(self.dataset_config) as dataset: if self.time < 0: self.time += len(dataset.timestamps) time = np.clip(self.time, 0, len(dataset.timestamps) - 1) for idx, v in enumerate(self.variables): var = dataset.variables[v] if not (set(var.dimensions) & set(dataset.depth_dimensions)): for potential in dataset.variables: if potential in self.variables: continue pot = dataset.variables[potential] if (set(pot.dimensions) & set(dataset.depth_dimensions)): if len(pot.dimensions) > 3: self.variables[idx] = potential.key value = parallel = perpendicular = magnitude = None variable_names = self.get_variable_names(dataset, self.variables) variable_units = self.get_variable_units(dataset, self.variables) scale_factors = self.get_variable_scale_factors( dataset, self.variables) # Load data sent from primary/Left Map if len(self.variables) > 1: # Only velocity has 2 variables v = [] for name in self.variables: v.append(dataset.variables[name]) distances, times, lat, lon, bearings = geo.path_to_points( self.points, 100) transect_pts, distance, x, dep = dataset.get_path_profile( self.points, time, self.variables[0], 100) transect_pts, distance, y, dep = dataset.get_path_profile( self.points, time, self.variables[1], 100) # Calculate vector components x = np.multiply(x, scale_factors[0]) y = np.multiply(y, scale_factors[1]) r = np.radians(np.subtract(90, bearings)) theta = np.arctan2(y, x) - r magnitude = np.sqrt(x**2 + y**2) parallel = magnitude * np.cos(theta) perpendicular = magnitude * np.sin(theta) else: # Get data for one variable transect_pts, distance, value, dep = dataset.get_path_profile( self.points, time, self.variables[0]) value = np.multiply(value, scale_factors[0]) if len(self.variables) == 2: variable_names = [ self.get_vector_variable_name(dataset, self.variables) ] variable_units = [ self.get_vector_variable_unit(dataset, self.variables) ] # If a colourmap has not been manually specified by the # Navigator... if self.cmap is None: self.cmap = colormap.find_colormap(variable_names[0]) self.timestamp = dataset.timestamps[int(time)] self.depth = dep self.depth_unit = "m" self.transect_data = { "points": transect_pts, "distance": distance, "data": value, "name": variable_names[0], "unit": variable_units[0], "parallel": parallel, "perpendicular": perpendicular, "magnitude": magnitude, } if self.surface is not None: surface_pts, surface_dist, t, surface_value = \ dataset.get_path( self.points, 0, time, self.surface, ) vc = self.dataset_config.variable[dataset.variables[ self.surface]] surface_unit = vc.unit surface_name = vc.name surface_factor = vc.scale_factor surface_value = np.multiply(surface_value, surface_factor) self.surface_data = { "config": vc, "points": surface_pts, "distance": surface_dist, "data": surface_value, "name": surface_name, "unit": surface_unit } # Load data sent from Right Map (if in compare mode) if self.compare: def interpolate_depths(data, depth_in, depth_out): output = [] for i in range(0, depth_in.shape[0]): f = interp1d( depth_in[i], data[:, i], bounds_error=False, assume_sorted=True, ) output.append( f(depth_out[i].view(np.ma.MaskedArray).filled())) return np.ma.masked_invalid(output).transpose() self.compare_config = DatasetConfig(self.compare['dataset']) with open_dataset(self.compare_config) as dataset: # Get and format date self.compare['date'] = np.clip(np.int64(self.compare['time']), 0, len(dataset.timestamps) - 1) self.compare['date'] = dataset.timestamps[int( self.compare['date'])] # 1 variable if len(self.compare['variables']) == 1: # Get and store the "nicely formatted" string for the variable name self.compare['name'] = self.get_variable_names( dataset, self.compare['variables'])[0] # Find correct colourmap if (self.compare['colormap'] == 'default'): self.compare['colormap'] = colormap.find_colormap( self.compare['name']) else: self.compare['colormap'] = colormap.find_colormap( self.compare['colormap']) climate_points, climate_distance, climate_data, cdep = \ dataset.get_path_profile(self.points, self.compare['time'], self.compare['variables'][0]) self.compare['unit'] = dataset.variables[ self.compare['variables'][0]].unit self.__fill_invalid_shift(climate_data) if (self.depth.shape != cdep.shape) or \ (self.depth != cdep).any(): # Need to interpolate the depths climate_data = interpolate_depths( climate_data, cdep, self.depth) if self.transect_data['data'] is None: self.transect_data['magnitude'] -= climate_data self.transect_data['parallel'] -= climate_data self.transect_data['perpendicular'] -= climate_data else: self.transect_data['compare_data'] = climate_data # Velocity variables else: # Get and store the "nicely formatted" string for the variable name self.compare['name'] = self.get_vector_variable_name( dataset, self.compare['variables']) climate_pts, climate_distance, climate_x, cdep = \ dataset.get_path_profile( self.points, self.compare['time'], self.compare['variables'][0], 100 ) climate_pts, climate_distance, climate_y, cdep = \ dataset.get_path_profile( self.points, self.compare['time'], self.compare['variables'][0], 100 ) climate_distances, ctimes, clat, clon, bearings = \ geo.path_to_points(self.points, 100) r = np.radians(np.subtract(90, bearings)) theta = np.arctan2(climate_y, climate_x) - r mag = np.sqrt(climate_x**2 + climate_y**2) if np.all(self.depth != cdep): theta = interpolate_depths(theta, cdep, self.depth) self.__fill_invalid_shift(theta) mag = interpolate_depths(mag, cdep, self.depth) self.__fill_invalid_shift(mag) self.compare['parallel'] = mag * np.cos(theta) self.compare['perpendicular'] = mag * np.sin(theta) """ if self.transect_data['parallel'] is None: self.transect_data['data'] -= mag else: self.transect_data['parallel'] -= climate_parallel self.transect_data['perpendicular'] -= climate_perpendicular """ # Bathymetry with Dataset(current_app.config['BATHYMETRY_FILE'], 'r') as dataset: bath_x, bath_y = bathymetry(dataset.variables['y'], dataset.variables['x'], dataset.variables['z'], self.points) self.bathymetry = {'x': bath_x, 'y': bath_y}
def load_data(self): vars_to_load = self.variables if self.surface: vars_to_load.append(self.surface) with open_dataset(self.dataset_config, timestamp=self.time, variable=vars_to_load) as dataset: for idx, v in enumerate(self.variables): var = dataset.variables[v] if not (set(var.dimensions) & set(dataset.nc_data.depth_dimensions)): for potential in dataset.variables: if potential in self.variables: continue pot = dataset.variables[potential] if set(pot.dimensions) & set( dataset.nc_data.depth_dimensions): if len(pot.dimensions) > 3: self.variables[idx] = potential.key value = parallel = perpendicular = magnitude = None variable_names = self.get_variable_names(dataset, self.variables) variable_units = self.get_variable_units(dataset, self.variables) # Load data sent from primary/Left Map if len(self.variables) > 1: # Only velocity has 2 variables v = [] for name in self.variables: v.append(dataset.variables[name]) distances, times, lat, lon, bearings = geo.path_to_points( self.points, 100) # Calculate vector components transect_pts, distance, x, dep = dataset.get_path_profile( self.points, self.variables[0], self.time, numpoints=100) transect_pts, distance, y, dep = dataset.get_path_profile( self.points, self.variables[1], self.time, numpoints=100) r = np.radians(np.subtract(90, bearings)) theta = np.arctan2(y, x) - r magnitude = np.sqrt(x**2 + y**2) parallel = magnitude * np.cos(theta) perpendicular = magnitude * np.sin(theta) else: # Get data for one variable transect_pts, distance, value, dep = dataset.get_path_profile( self.points, self.variables[0], self.time) if len(self.variables) == 2: variable_names = [ self.get_vector_variable_name(dataset, self.variables) ] variable_units = [ self.get_vector_variable_unit(dataset, self.variables) ] # If a colourmap has not been manually specified by the # Navigator... if self.cmap is None: self.cmap = colormap.find_colormap(variable_names[0]) self.iso_timestamp = dataset.nc_data.timestamp_to_iso_8601( self.time) self.depth = dep self.depth_unit = "m" self.transect_data = { "points": transect_pts, "distance": distance, "data": value, "name": variable_names[0], "unit": variable_units[0], "parallel": parallel, "perpendicular": perpendicular, "magnitude": magnitude, } if self.surface: surface_pts, surface_dist, _, surface_value = dataset.get_path( self.points, 0, self.surface, self.time) vc = self.dataset_config.variable[dataset.variables[ self.surface]] surface_unit = vc.unit surface_name = vc.name surface_value = np.multiply(surface_value, surface_factor) self.surface_data = { "config": vc, "points": surface_pts, "distance": surface_dist, "data": surface_value, "name": surface_name, "unit": surface_unit, } # Load data sent from Right Map (if in compare mode) if self.compare: def interpolate_depths(data, depth_in, depth_out): output = [] for i in range(0, depth_in.shape[0]): f = interp1d( depth_in[i], data[:, i], bounds_error=False, assume_sorted=True, ) output.append( f(depth_out[i].view(np.ma.MaskedArray).filled())) return np.ma.masked_invalid(output).transpose() self.compare_config = DatasetConfig(self.compare["dataset"]) self.compare["time"] = int(self.compare["time"]) with open_dataset( self.compare_config, timestamp=self.compare["time"], variable=self.compare["variables"], ) as dataset: self.compare[ "iso_timestamp"] = dataset.nc_data.timestamp_to_iso_8601( self.compare["time"]) # 1 variable if len(self.compare["variables"]) == 1: # Get and store the "nicely formatted" string for the variable name self.compare["name"] = self.get_variable_names( dataset, self.compare["variables"])[0] # Find correct colourmap if self.compare["colormap"] == "default": self.compare["colormap"] = colormap.find_colormap( self.compare["name"]) else: self.compare["colormap"] = colormap.find_colormap( self.compare["colormap"]) ( climate_points, climate_distance, climate_data, cdep, ) = dataset.get_path_profile(self.points, self.compare["variables"][0], self.compare["time"]) self.compare["unit"] = dataset.variables[ self.compare["variables"][0]].unit self.__fill_invalid_shift(climate_data) if (self.depth.shape != cdep.shape) or (self.depth != cdep).any(): # Need to interpolate the depths climate_data = interpolate_depths( climate_data, cdep, self.depth) if self.transect_data["data"] is None: self.transect_data["magnitude"] -= climate_data self.transect_data["parallel"] -= climate_data self.transect_data["perpendicular"] -= climate_data else: self.transect_data["compare_data"] = climate_data # Velocity variables else: # Get and store the "nicely formatted" string for the variable name self.compare["name"] = self.get_vector_variable_name( dataset, self.compare["variables"]) ( climate_pts, climate_distance, climate_x, cdep, ) = dataset.get_path_profile( self.points, self.compare["variables"][0], self.compare["time"], numpoints=100, ) ( climate_pts, climate_distance, climate_y, cdep, ) = dataset.get_path_profile( self.points, self.compare["variables"][0], self.compare["time"], numpoints=100, ) ( climate_distances, ctimes, clat, clon, bearings, ) = geo.path_to_points(self.points, 100) r = np.radians(np.subtract(90, bearings)) theta = np.arctan2(climate_y, climate_x) - r mag = np.sqrt(climate_x**2 + climate_y**2) if np.all(self.depth != cdep): theta = interpolate_depths(theta, cdep, self.depth) self.__fill_invalid_shift(theta) mag = interpolate_depths(mag, cdep, self.depth) self.__fill_invalid_shift(mag) self.compare["parallel"] = mag * np.cos(theta) self.compare["perpendicular"] = mag * np.sin(theta) """ if self.transect_data['parallel'] is None: self.transect_data['data'] -= mag else: self.transect_data['parallel'] -= climate_parallel self.transect_data['perpendicular'] -= climate_perpendicular """ # Bathymetry with Dataset(current_app.config["BATHYMETRY_FILE"], "r") as dataset: bath_x, bath_y = bathymetry( dataset.variables["y"], dataset.variables["x"], dataset.variables["z"], self.points, ) self.bathymetry = {"x": bath_x, "y": bath_y}