def __init__(self, path_to_data, network=None): """ collects metadata from all files in path_to_data and saves metadata information in numpy file in folder path_to_data/python_metadata/ First initialization can take a minute or so if all ISMN data is present in path_to_data """ if not os.path.exists(os.path.join(path_to_data, 'python_metadata', 'metadata.npy')): self.metadata = metadata_collector.collect_from_folder( path_to_data) os.mkdir(os.path.join(path_to_data, 'python_metadata')) np.save( os.path.join(path_to_data, 'python_metadata', 'metadata.npy'), self.metadata) #np.savetxt(os.path.join(path_to_data,'python_metadata','metadata.npy'), self.metadata,delimiter=',') else: self.metadata = np.load( os.path.join(path_to_data, 'python_metadata', 'metadata.npy')) if network is not None: if type(network) is not list: network = [network] # initialize binary mask the size of metadata mask = np.zeros(self.metadata.shape[0], dtype=np.bool) for net in network: if net in self.metadata['network']: mask = mask | (self.metadata['network'] == net) else: raise ISMNError("Network {} not found".format(net)) self.metadata = self.metadata[mask] # initialize grid object for all stations self.grid = grids.BasicGrid(self.metadata['longitude'], self.metadata['latitude'], setup_kdTree=False)
def _init_grid(self): """ initialize the grid of the dataset """ x = self.ds.variables[self.lat_var][:] y = self.ds.variables[self.lon_var][:] xs, ys = np.meshgrid(x, y) self.grid = grids.BasicGrid(xs.flatten(), ys.flatten(), shape=(1200, 2400))
def __init__(self, path_to_data, network=None): """ collects metadata from all files in path_to_data and saves metadata information in numpy file in folder path_to_data/python_metadata/ First initialization can take a minute or so if all ISMN data is present in path_to_data """ if not os.path.exists( os.path.join(path_to_data, 'python_metadata', 'metadata.npy')): os.mkdir(os.path.join(path_to_data, 'python_metadata')) self.metadata = metadata_collector.collect_from_folder( path_to_data) np.save( os.path.join(path_to_data, 'python_metadata', 'metadata.npy'), self.metadata) #np.savetxt(os.path.join(path_to_data,'python_metadata','metadata.npy'), self.metadata,delimiter=',') else: self.metadata = np.load( os.path.join(path_to_data, 'python_metadata', 'metadata.npy')) if network is not None: if type(network) is not list: network = [network] # initialize binary mask the size of metadata mask = np.zeros(self.metadata.shape[0], dtype=np.bool) for net in network: if net in self.metadata['network']: mask = mask | (self.metadata['network'] == net) else: raise ISMNError("Network {} not found".format(net)) self.metadata = self.metadata[mask] # initialize grid object for all stations self.grid = grids.BasicGrid(self.metadata['longitude'], self.metadata['latitude'], setup_kdTree=False) # read cci landcover class names and their identifiers config = configparser.ConfigParser() config.optionxform = str config.read_file( open( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'classifications.ini'))) landcover = dict(config.items('LANDCOVER')) self.landcover = dict([(int(v), k) for k, v in landcover.items()]) self.climate = dict(config.items('KOEPPENGEIGER'))
def activate_network(self, network): """ Load or update metadata for reading one or multiple networks. Parameters --------- network : list or str Name of a network in the initialised path or multiple networks as a list of strings that are loaded. """ if not os.path.exists( os.path.join(self.path_to_data, 'python_metadata', 'metadata.npy')): os.mkdir(os.path.join(self.path_to_data, 'python_metadata')) self.metadata = metadata_collector.collect_from_folder( self.path_to_data) np.save( os.path.join(self.path_to_data, 'python_metadata', 'metadata.npy'), self.metadata) #np.savetxt(os.path.join(path_to_data,'python_metadata','metadata.npy'), self.metadata,delimiter=',') else: self.metadata = np.load(os.path.join(self.path_to_data, 'python_metadata', 'metadata.npy'), allow_pickle=True) if network is not None: if type(network) is not list: network = [network] # initialize binary mask the size of metadata mask = np.zeros(self.metadata.shape[0], dtype=np.bool) for net in network: if net in self.metadata['network']: mask = mask | (self.metadata['network'] == net) else: raise ISMNError("Network {} not found".format(net)) self.metadata = self.metadata[mask] # initialize grid object for all stations self.grid = grids.BasicGrid(self.metadata['longitude'], self.metadata['latitude'], setup_kdTree=False)
def setUp(self): """ Define test file. """ self.testfilename = os.path.join(mkdtemp(), 'test.nc') self.grid = grids.BasicGrid(np.arange(180), np.arange(180) - 90)
def _load_grid(self): lons = self.dataset.variables[self.lon_var][:] lats = self.dataset.variables[self.lat_var][:] self.grid = grids.BasicGrid(lons, lats)