def nearest_time(self, layer, time): """ Return the time index and time value that is closest """ with self.dataset() as nc: time_vars = nc.get_variables_by_attributes(standard_name='time') if len(time_vars) == 1: time_var = time_vars[0] else: # if there is more than variable with standard_name = time # fine the appropriate one to use with the layer var_obj = nc.variables[layer.access_name] time_var_name = find_appropriate_time(var_obj, time_vars) time_var = nc.variables[time_var_name] units = time_var.units if hasattr(time_var, 'calendar'): calendar = time_var.calendar else: calendar = 'gregorian' num_date = round(nc4.date2num(time, units=units, calendar=calendar)) times = time_var[:] time_index = bisect.bisect_right(times, num_date) try: times[time_index] except IndexError: time_index -= 1 return time_index, times[time_index]
def update_time_cache(self): with self.dataset() as nc: if nc is None: logger.error("Failed update_time_cache, could not load dataset " "as a netCDF4 object") return time_cache = {} layer_cache = {} time_vars = nc.get_variables_by_attributes(standard_name='time') for time_var in time_vars: time_cache[time_var.name] = nc4.num2date( time_var[:], time_var.units, getattr(time_var, 'calendar', 'standard') ) for ly in self.all_layers(): try: layer_cache[ly.access_name] = find_appropriate_time(nc.variables[ly.access_name], time_vars) except ValueError: layer_cache[ly.access_name] = None full_cache = {'times': time_cache, 'layers': layer_cache} logger.info("Built time cache for {0}".format(self.name)) caches['time'].set(self.time_cache_file, full_cache, None) return full_cache
def nearest_time(self, layer, time): """ Return the time index and time value that is closest """ with self.dataset() as nc: time_vars = nc.get_variables_by_attributes(standard_name='time') if not time_vars: return None, None if len(time_vars) == 1: time_var = time_vars[0] else: # if there is more than variable with standard_name = time # fine the appropriate one to use with the layer var_obj = nc.variables[layer.access_name] time_var_name = find_appropriate_time(var_obj, time_vars) time_var = nc.variables[time_var_name] units = time_var.units if hasattr(time_var, 'calendar'): calendar = time_var.calendar else: calendar = 'gregorian' num_date = round(nc4.date2num(time, units=units, calendar=calendar)) times = time_var[:] time_index = np.searchsorted(times, num_date, side='left') time_index = min(time_index, len(times) - 1) # Don't do over the length of time return time_index, times[time_index]
def times(self, layer): with self.topology() as nc: time_vars = nc.get_variables_by_attributes(standard_name='time') if len(time_vars) == 1: time_var = time_vars[0] else: # if there is more than variable with standard_name = time # fine the appropriate one to use with the layer var_obj = nc.variables[layer.access_name] time_var_name = find_appropriate_time(var_obj, time_vars) time_var = nc.variables[time_var_name] return nc4.num2date(time_var[:], units=time_var.units)
def times(self, layer): with self.topology() as nc: time_vars = nc.get_variables_by_attributes(standard_name='time') if len(time_vars) == 1: time_var = time_vars[0] else: # if there is more than variable with standard_name = time # fine the appropriate one to use with the layer var_obj = nc.variables[layer.access_name] time_var_name = find_appropriate_time(var_obj, time_vars) time_var = nc.variables[time_var_name] return netCDF4.num2date(time_var[:], units=time_var.units)
def setup_getfeatureinfo(self, ncd, variable_object, request, location=None): location = location or 'face' try: latitude = request.GET['latitude'] longitude = request.GET['longitude'] # Find closest cell or node (only node for now) if location == 'face': tree = rtree.index.Index(self.face_tree_root) elif location == 'node': tree = rtree.index.Index(self.node_tree_root) else: raise NotImplementedError("No RTree for location '{}'".format(location)) nindex = list(tree.nearest((longitude, latitude, longitude, latitude), 1, objects=True))[0] closest_x, closest_y = tuple(nindex.bbox[2:]) geo_index = nindex.object except BaseException: raise finally: tree.close() # Get time indexes time_var_name = find_appropriate_time(variable_object, ncd.get_variables_by_attributes(standard_name='time')) time_var = ncd.variables[time_var_name] if hasattr(time_var, 'calendar'): calendar = time_var.calendar else: calendar = 'gregorian' start_nc_num = round(nc4.date2num(request.GET['starting'], units=time_var.units, calendar=calendar)) end_nc_num = round(nc4.date2num(request.GET['ending'], units=time_var.units, calendar=calendar)) all_times = time_var[:] start_nc_index = bisect.bisect_right(all_times, start_nc_num) end_nc_index = bisect.bisect_right(all_times, end_nc_num) try: all_times[start_nc_index] except IndexError: start_nc_index = all_times.size - 1 try: all_times[end_nc_index] except IndexError: end_nc_index = all_times.size - 1 if start_nc_index == end_nc_index: if start_nc_index > 0: start_nc_index -= 1 elif end_nc_index < all_times.size: end_nc_index += 1 return_dates = nc4.num2date(all_times[start_nc_index:end_nc_index], units=time_var.units, calendar=calendar) return geo_index, closest_x, closest_y, start_nc_index, end_nc_index, return_dates
def setup_getfeatureinfo(self, ncd, variable_object, request, location=None): location = location or 'face' try: latitude = request.GET['latitude'] longitude = request.GET['longitude'] # Find closest cell or node (only node for now) if location == 'face': tree = rtree.index.Index(self.face_tree_root) elif location == 'node': tree = rtree.index.Index(self.node_tree_root) else: raise NotImplementedError( "No RTree for location '{}'".format(location)) nindex = list( tree.nearest((longitude, latitude, longitude, latitude), 1, objects=True))[0] closest_x, closest_y = tuple(nindex.bbox[2:]) geo_index = nindex.object except BaseException: raise finally: tree.close() # Get time indexes time_var_name = find_appropriate_time( variable_object, ncd.get_variables_by_attributes(standard_name='time')) time_var = ncd.variables[time_var_name] if hasattr(time_var, 'calendar'): calendar = time_var.calendar else: calendar = 'gregorian' start_nc_num = round( nc4.date2num(request.GET['starting'], units=time_var.units, calendar=calendar)) end_nc_num = round( nc4.date2num(request.GET['ending'], units=time_var.units, calendar=calendar)) all_times = time_var[:] start_nc_index = bisect.bisect_right(all_times, start_nc_num) end_nc_index = bisect.bisect_right(all_times, end_nc_num) try: all_times[start_nc_index] except IndexError: start_nc_index = all_times.size - 1 try: all_times[end_nc_index] except IndexError: end_nc_index = all_times.size - 1 if start_nc_index == end_nc_index: if start_nc_index > 0: start_nc_index -= 1 elif end_nc_index < all_times.size: end_nc_index += 1 return_dates = nc4.num2date(all_times[start_nc_index:end_nc_index], units=time_var.units, calendar=calendar) return geo_index, closest_x, closest_y, start_nc_index, end_nc_index, return_dates