def data_for_variable(self, variable, min_depth=None, max_depth=None): """ function to go through all the depth_from, depth_to, sensor combinations for the given variable and yields ISMNTimeSeries if a match is found. if min_depth and/or max_depth where given it only returns a ISMNTimeSeries if depth_from >= min_depth and/or depth_to <= max_depth Parameters ---------- variable: string variable to read one of * 'soil moisture', * 'soil temperature', * 'soil suction', * 'precipitation', * 'air temperature', * 'field capacity', * 'permanent wilting point', * 'plant available water', * 'potential plant available water', * 'saturation', * 'silt fraction', * 'snow depth', * 'sand fraction', * 'clay fraction', * 'organic carbon', * 'snow water equivalent', * 'surface temperature', * 'surface temperature quality flag original' min_depth : float, optional depth_from of variable has to be >= min_depth in order to be included. max_depth : float, optional depth_to of variable has to be <= max_depth in order to be included. Returns ------- time_series : iterator(ismn.readers.ISMNTimeSeries) ISMNTimeSeries object containing data and metadata """ if min_depth is None: min_depth = np.min(self.depth_from) if max_depth is None: max_depth = np.max(self.depth_to) for var, d1, d2, filename in zip(self.variables, self.depth_from, self.depth_to, self.filenames): if var != variable: continue if ((d1 >= min_depth) & (d2 <= max_depth)): yield readers.read_data(filename)
def read_ts(self, idx): """ read a time series directly by the id Parameters ---------- idx : int id into self.metadata, best one of those returned from get_dataset_ids() Returns ------- timeseries : pandas.DataFrame of the read data """ ts = readers.read_data(self.metadata['filename'][idx]) return ts.data
def data_import(): """Creates a dictionary with longitude, latitude and soil moisture data as values for each ISMN file located in ./data/ISMN_Filt . The key of each dictionary entry is created as a combination of network name, station name and sensor type. Also a CSV file with each key and the corresponding coordinates ( latitude & longitude) is created in the directory ./data/ :return: Dictionary containing ISMN data. """ sm_files = [ f for f in glob.glob("./data/ISMN_Filt/**/*_sm_*.stm", recursive=True) ] dict_ismn = {} long = [] lat = [] station = [] for i in sm_files: data = ismn.read_data(i) header_elements, filename_elements = ismn.get_info_from_file(i) dict_ismn[header_elements[1] + "-" + header_elements[2] + "-" + header_elements[8]] = [ header_elements[3], header_elements[4], data.data ] long.append(header_elements[3]) lat.append(header_elements[4]) station.append(header_elements[1] + "-" + header_elements[2] + "-" + header_elements[8]) with open('./data/stations.csv', 'w', newline='') as csvfile: filewriter = csv.writer(csvfile, delimiter=',') filewriter.writerow(station) filewriter.writerow(long) filewriter.writerow(lat) return dict_ismn
def read_variable(self, variable, depth_from=None, depth_to=None, sensor=None): """ actually reads the given variable from the file. Parameters are required until any ambiguity is resolved. If there is only one depth for the given variable then only variable is required. If there are multiple depths at least depth_from is required. If there are multiple depth_to possibilities for one variable-depth_from combination also depth_to has to be specified. If 2 sensors are measuring the same variable in the same depth then also the sensor has to be specified. Parameters ---------- variable: string variable to read one of * 'soil moisture', * 'soil temperature', * 'soil suction', * 'precipitation', * 'air temperature', * 'field capacity', * 'permanent wilting point', * 'plant available water', * 'potential plant available water', * 'saturation', * 'silt fraction', * 'snow depth', * 'sand fraction', * 'clay fraction', * 'organic carbon', * 'snow water equivalent', * 'surface temperature', * 'surface temperature quality flag original' depth_from : float, optional shallower depth of layer the variable was measured at depth_to : float, optional deeper depth of layer the variable was measured at sensor : string, optional name of the sensor Returns ------- data : readers.ISMNTimeSeries ISMNTimeSeries object containing the relevant metadata for the time series as well as a .data pointing to a pandas.DataFrame Raises ------ ISMNError: if not all ambiguity was resolved by the given input parameters or if no data was found for the given input parameters """ if depth_from is None: depth_f, depth_t = self.get_depths(variable) if depth_f.size > 1: raise ISMNError("there are multiple depths for this variable" "Please specify the one you want to read") elif depth_f.size == 1: depth_from = depth_f[0] elif depth_f.size == 0: raise ISMNError("there are no depths for this variable" "Something went wrong") if depth_to is None: depth_f, depth_t = self.get_depths(variable) if depth_t.size > 1: raise ISMNError( "there are multiple depths with the same depth_from value" "Please specify the depth_to value you want to read") elif depth_t.size == 1: depth_to = depth_t[0] elif depth_t.size == 0: raise ISMNError("there are no depths for this variable" "Something went wrong") if sensor is None: sensors = self.get_sensors(variable, depth_from, depth_to) if sensors.size > 1: raise ISMNError( "there are multiple sensors for this combination of " "variable, depth_to, depth_from. Please specify which one " "you want to read") elif sensors.size == 1: sensor = sensors[0] elif sensors.size == 0: raise ISMNError( "there are no sensors for this variable, depth_from, depth_to " "combination. Please make sure you specified valid depths") index_filename = np.where((variable == self.variables) & (depth_from == self.depth_from) & (depth_to == self.depth_to) & (sensor == self.sensors))[0] if index_filename.size != 1: raise ISMNError( "There is no data for this combination of variable, depth_from, " "depth_to and sensor. Please check.") else: return readers.read_data(self.filenames[index_filename[0]])