API Reference¶
This page provides an auto-generated API reference for the key components of the hydrodatasource library.
Data Resolver (Unified Data Interface)¶
hydrodatasource.configs.data_resolver.open_dataset(dataset_id, *, source=None, ctx=None, **reader_kwargs)
¶
Resolve a dataset id and return an instantiated reader object.
Thin wrapper around hydrodataset's open_dataset that injects
hydrodatasource-specific datasets (HDS_DATASETS) and reader aliases
(_HDS_READER_ALIASES) into the resolution context. Both hydrodataset
datasets (e.g. 'camels_us') and hydrodatasource datasets
(e.g. 'songliao_event') are supported via a single call.
Parameters¶
dataset_id : str
Dataset identifier from the registry (e.g. 'camels_us', 'songliao_event').
source : str, optional
Storage backend: 'local' or 'cloud'. When None (default), falls back
to storage.default_source from the settings.
ctx : ResolverContext, optional
Resolution context. When None, HDS defaults are used automatically.
**reader_kwargs
Extra keyword arguments forwarded to the reader constructor
(e.g. time_unit=["1D"] for SelfMadeHydroDataset).
Returns¶
object An instance of the reader class registered for dataset_id.
Raises¶
DatasetResolutionError If any resolution step fails.
Examples¶
ds = open_dataset("songliao_event") ds = open_dataset("camels_us", source="cloud") ds = open_dataset("songliao_event", time_unit=["1D"])
hydrodatasource.configs.data_resolver.resolve_data_path(dataset_id, *, source=None, ctx=None)
¶
Resolve a dataset id to an absolute data path (URI).
Thin wrapper around hydrodataset's resolve_data_path that injects hydrodatasource-specific datasets (HDS_DATASETS) and reader aliases (_HDS_READER_ALIASES) into the resolution context.
Parameters¶
dataset_id : str
Dataset identifier from the registry (e.g. 'camels_us', 'songliao_event').
source : str, optional
Storage backend: 'local' or 'cloud'. When None (default), falls back
to storage.default_source from the settings.
ctx : ResolverContext, optional
Resolution context bundling project_root, storage config, registry
overrides, and extra aliases. When None (default), a new context
is created with HDS_DATASETS and _HDS_READER_ALIASES injected.
When provided, caller's extras are merged with HDS defaults.
Returns¶
str Absolute URI pointing to the dataset's data directory.
Raises¶
DatasetResolutionError If any resolution step fails.
Examples¶
Default: zero-boilerplate, reads ~/hydro_setting.yml¶
uri = resolve_data_path("songliao_event")
Custom storage root via ResolverContext¶
ctx = ResolverContext(storage={"local": {"root": "/custom/data"}}) uri = resolve_data_path("songliao_event", ctx=ctx)
READER_ALIASES maps every reader alias to its module/class, and HDS_DATASETS is the in-code
registry of hydrodatasource datasets (e.g. songliao_event). ResolverContext and
DatasetResolutionError are re-exported from hydrodataset.
Reader¶
HydroData (base class)¶
hydrodatasource.reader.data_source.HydroData
¶
Bases: ABC
Abstract base class for reading multi-modal hydrological data sources.
Single construction pattern: HydroData(uri).
Parameters¶
uri : str or Path Absolute path or S3 URI pointing directly to the data directory.
SelfMadeHydroDataset¶
hydrodatasource.reader.data_source.SelfMadeHydroDataset
¶
Bases: HydroData
A class for reading hydrodataset, but not really ready-datasets, just some data directorys organized like a HydroDataset.
NOTE: We compile forcing data and attr data into a directory, organized like a ready dataset -- like Caravan. Only two directories are needed: attributes and timeseries
__init__(uri=None, time_unit=None, **kwargs)
¶
Initialize a self-made Caravan-style dataset.
Parameters¶
uri : str, optional Absolute URI pointing directly to the data directory. time_unit : list, optional Time units to process, by default None. kwargs : dict, optional Additional keyword arguments, by default None.
cache_timeseries_xrdataset(**kwargs)
¶
Save all timeseries data in separate NetCDF files for each time unit.
Parameters¶
t_range : list, optional Time range for the data, by default ["1980-01-01", "2023-12-31"] kwargs : dict, optional batchsize -- Number of basins to process per batch, by default 100 time_units -- List of time units to process, by default None start0101_freq -- for freq setting, if the start date is 01-01, set True, by default False offset_to_utc -- whether to offset the time to UTC, by default False start_hour_in_a_day -- the start hour in a day (0-23), by default 2 which means 2-5-8-11-14-17-20-23 UTC. Chinese basins data always use 08:00 with Beijing Time, so we set the default value to 2. Only applicable for sub-daily intervals (currently only "3h" is supported)
cache_xrdataset(t_range=None, time_units=None)
¶
Save all data in a netcdf file in the cache directory
get_attributes_cols()
¶
the constant cols in this data_source
get_timeseries_cols()
¶
the relevant cols in this data_source
read_area(gage_id_lst=None)
¶
read area of each basin/unit
read_attributes(object_ids=None, constant_cols=None, **kwargs)
¶
2d data (site_num * var_num), non-time-series data
read_mean_prcp(gage_id_lst=None, unit='mm/d')
¶
read mean precipitation of each basin default unit is mm/d, but one can chose other units and we will convert the unit to the specified unit
Parameters¶
gage_id_lst : list, optional the list of gage ids, by default None unit : str, optional the unit of precipitation, by default "mm/d"
Returns¶
xr.Dataset the mean precipitation of each basin
read_timeseries(object_ids=None, t_range_list=None, relevant_cols=None, **kwargs)
¶
Returns a dictionary containing data with different time scales.
Parameters¶
object_ids : list, optional List of object IDs. Defaults to None. t_range_list : list, optional List of time ranges. Defaults to None. relevant_cols : list, optional List of relevant columns. Defaults to None. **kwargs : dict, optional Additional keyword arguments. time_units : list, optional List of time units to process start0101_freq : bool, optional For freq setting, if the start date is 01-01, set True offset_to_utc : bool, optional Whether to offset the time to UTC start_hour_in_a_day : int, optional The start hour in a day for sub-daily intervals (0-23). Default is 2.
Returns¶
dict A dictionary containing data with different time scales.
read_ts_xrdataset(gage_id_lst=None, t_range=None, var_lst=None, **kwargs)
¶
Read time-series xarray dataset from multiple NetCDF files and organize them by time units.
Parameters:¶
gage_id_lst: list List of gage IDs to select. t_range: list List of two elements [start_time, end_time] to select time range. var_lst: list List of variables to select. **kwargs Additional arguments.
Returns:¶
dict: A dictionary where each key is a time unit and each value is an xarray.Dataset containing the selected gage IDs, time range, and variables.
Other Readers¶
hydrodatasource.reader.data_source.LongTermDataset
¶
Bases: SelfMadeHydroDataset
cache_global_dataset(object_ids=None, t_range_list=None)
¶
读取 CSV 数据并将其转换为 xarray 数据集并保存为 NetCDF 文件。
hydrodatasource.reader.data_source.SelfMadeForecastDataset
¶
Bases: SelfMadeHydroDataset
For selfmadehydrodataset, we design a new file format for forecast data from GFS et al.
__init__(uri=None, time_unit=None, **kwargs)
¶
Initialize a class for reading forecast data.
Parameters¶
uri : str Absolute path or S3 URI pointing to the forecast data directory. time_unit : list, optional Unit of one time period, by default None.
cache_forecast_xrdataset(t_range=None, **kwargs)
¶
Save all forecast data in separate NetCDF files for each batch of basins and time units.
Parameters¶
t_range : list, optional Time range for the forecast_date, by default None kwargs : dict, optional batchsize -- Number of basins to process per batch, by default 100 variables -- List of variables to process, by default None time_units -- List of time units to process, by default self.time_unit prefix -- Prefix for the NetCDF file names, by default self.dataset_name
read_forecast(object_ids=None, t_range_list=None, relevant_cols=None, **kwargs)
¶
Read forecast data (hourly/daily forecasts) from CSV files, where each basin has its own file named after the basin_id. The time range in the parameters refers to the forecast_date (i.e., the target period of the forecast), not the date (the execution time of the forecast).
Parameters¶
object_ids : list List of basin IDs. t_range_list : list Time range for the target forecast period [start_time, end_time]. relevant_cols : list List of variable names to be read. Returns
dict {basin_id: pd.DataFrame}, where each basin has a DataFrame filtered by the time range and variables.
read_forecast_xrdataset(gage_id_lst, t_range, var_lst, **kwargs)
¶
read_ts_xrdataset(gage_id_lst, t_range, var_lst, **kwargs)
¶
hydrodatasource.reader.data_source.StationHydroDataset
¶
Bases: SelfMadeHydroDataset
A class for reading hydrodataset with additional station data.
This class extends SelfMadeHydroDataset to handle datasets that include a stations folder containing individual station data and basin-station relationship information.
Directory structure: - attributes/ - shapes/ - timeseries/ - stations/ - 1D/ # Daily data for all stations - 3h/ # 3h data for all stations - basin_station_info/ # Basin-station relationship info - all_basin_station_mapping.csv - basin_summary.csv - basin_xxx_stations.csv - adjacency_xxx_True.csv
__init__(uri=None, time_unit=None, **kwargs)
¶
Initialize StationHydroDataset.
Parameters¶
uri : str Absolute path or S3 URI pointing to the station dataset directory. time_unit : list, optional Time units for the data, by default None **kwargs : dict Additional keyword arguments passed to parent class
cache_adjacency_xrdataset()
¶
Cache adjacency matrices for all basins as NetCDF files.
cache_all_station_data(**kwargs)
¶
Cache all station-related data including timeseries, info and adjacency.
cache_station_info_xrdataset()
¶
Cache station information and basin-station relationships.
cache_station_timeseries_xrdataset(**kwargs)
¶
Cache all station timeseries data in separate NetCDF files.
Parameters¶
**kwargs : dict batchsize -- Number of stations to process per batch, by default 100 time_units -- List of time units to process, by default None start0101_freq -- Whether to use start0101 frequency, by default False
get_stations_by_basin(basin_id)
¶
read_adjacency_xrdataset(basin_id)
¶
read_basin_adjacency(basin_id)
¶
read_basin_stations(basin_id)
¶
read_station_info()
¶
Read basic station information and basin-station mapping.
read_station_info_xrdataset(**kwargs)
¶
Read station information from cached NetCDF files.
Returns¶
tuple (basin_station_mapping_dataset, basin_summary_dataset)
read_station_object_ids()
¶
Get all station IDs.
read_station_timeseries(station_ids=None, t_range_list=None, relevant_cols=None, **kwargs)
¶
Read timeseries data for stations.
Parameters¶
station_ids : list, optional List of station IDs, by default None t_range_list : list, optional Time range [start_time, end_time], by default None relevant_cols : list, optional List of relevant columns, by default None **kwargs : dict Additional keyword arguments
Returns¶
dict Dictionary containing data with different time scales
read_station_ts_xrdataset(station_id_lst=None, t_range=None, var_lst=None, **kwargs)
¶
Read station timeseries data from cached NetCDF files.
Parameters¶
station_id_lst : list, optional List of station IDs to select, by default None t_range : list, optional Time range [start_time, end_time], by default None var_lst : list, optional List of variables to select, by default None **kwargs : dict Additional arguments
Returns¶
dict Dictionary with time units as keys and xarray.Dataset as values
set_data_source_describe()
¶
Set data source description including stations directory.
hydrodatasource.reader.data_source.TgHydroDatasource
¶
Bases: SelfMadeHydroDataset
TG流域数据集 - 继承自SelfMadeHydroDataset,添加LSTM预测数据和图网络结构支持
该类在标准的水文数据集基础上,增加了对LSTM预测数据和图网络结构的支持, 主要用于图神经网络相关的水文建模任务。 catch中应包含LSTM预测结果NC文件,文件名格式为lstmpred.nc
数据目录结构: - attributes/ # 控制流域属性数据 - timeseries/ # 控制流域时间序列数据 - shapes/ # 控制流域形状文件 - intermediate/ # 区间流域数据 ├── attributes/ # 区间流域属性数据,包含拓扑关系 ├── timeseries/ # 区间流域时间序列数据 └── shapes/ # 区间流域形状文件
__init__(uri=None, time_unit=None, **kwargs)
¶
Initialize TG basin dataset.
Parameters¶
uri : str Absolute path or S3 URI pointing to the TG dataset directory. time_unit : list, optional Time unit list, by default None. **kwargs : dict Other parameters.
cache_intermediate_attributes_xrdataset()
¶
Convert intermediate attributes to a single dataset and cache as NetCDF.
cache_intermediate_timeseries_xrdataset(**kwargs)
¶
Cache intermediate timeseries into NetCDF files per time unit, similar to parent cache_timeseries_xrdataset.
classify_nodes(basin_names=None)
¶
分类节点为源头节点和汇流节点。
- 默认在全局图
self.dg上运行; - 若
basin_names为 None,则使用图中的所有节点,按数据集顺序(与read_object_ids对齐)。
Returns¶
tuple (source_nodes, confluence_nodes, node_mask)
get_name()
¶
返回数据源名称
read_attr_xrdataset(gage_id_lst=None, var_lst=None, **kwargs)
¶
读取属性数据,兼容基础目录与 intermediate 目录的缓存。
- 默认沿用父类逻辑读取基础缓存;
- 当设置
prefer_intermediate=True或combine=True时,读取/生成 intermediate 缓存; - intermediate 缓存以
dataset_name_intermediate作为前缀独立保存。
read_graph_data(object_ids=None, as_numpy=False, include_optional=True)
¶
统一返回下游所需的图拓扑结构,满足最大兼容性约定。
参数¶
object_ids : list, optional 指定参与图构建的节点集合;不传则使用数据集可用节点与图节点的交集 as_numpy : bool, default False 将 edge_index 返回为 numpy.int64 的 ndarray;默认返回 torch.long 的 Tensor include_optional : bool, default True 是否包含可选扩展字段(目前支持 upstream_indices)
返回¶
dict { 'edge_index': [2, E] 的 torch.LongTensor 或 numpy.ndarray, 'node_ids': List[str],长度为 num_nodes,顺序与索引一致, 'node_mask': List[bool],长度为 num_nodes(汇流 True / 源头 False), 可选:'upstream_indices': List[List[int]] }
read_intermediate_timeseries(object_ids=None, t_range_list=None, relevant_cols=None, **kwargs)
¶
Read timeseries from the intermediate/timeseries directory.
Parameters¶
object_ids : list, optional Basin IDs to read t_range_list : list, optional [start_time, end_time] relevant_cols : list, optional Variables to read **kwargs : dict time_units, start0101_freq, offset_to_utc, start_hour_in_a_day
Returns¶
dict
read_ts_xrdataset(gage_id_lst=None, t_range=None, var_lst=None, **kwargs)
¶
读取时间序列数据,兼容基础目录与 intermediate 目录的缓存读取。
- 默认沿用父类逻辑读取基础缓存;
- 当设置
prefer_intermediate=True或存在combine=True时,读取/生成 intermediate 缓存; - intermediate 缓存以
dataset_name_intermediate作为前缀独立保存。
hydrodatasource.reader.floodevent.FloodEventDatasource
¶
Bases: SelfMadeHydroDataset
Flood event dataset processing class
Inherits from SelfMadeHydroDataset, specifically designed for processing individual flood event data, including event extraction functions.
__init__(uri=None, time_unit=None, *, rain_key='rain', pet_key='ES', net_rain_key='net_rain', obs_flow_key='inflow', warmup_length=0, **kwargs)
¶
Initialize the flood event dataset.
Parameters¶
uri : str Absolute path or S3 URI pointing to the data directory. time_unit : list of str, optional List of time units, default is ["3h"]. rain_key : str, optional Key name for rain data, default is "rain". net_rain_key : str, optional Key name for net rain data, default is "net_rain". obs_flow_key : str, optional Key name for observed flow data, default is "inflow". warmup_length : int, optional Number of time steps to include before flood event starts as warmup period, default is 0. **kwargs Additional keyword arguments passed to the parent class.
adjust_warmup_time_to_augmented_year(warmup_df, augmented_start_time)
¶
check_event_data_nan(all_event_data, exclude_warmup=True)
¶
Check for NaN values in rainfall and runoff data for all flood events.
This method leverages the class's attributes (net_rain_key, obs_flow_key, warmup_length) and can optionally exclude warmup period from NaN checking.
Parameters¶
all_event_data : list of dict List of event dictionaries, each containing net rainfall, runoff, filepath, etc. exclude_warmup : bool, optional Whether to exclude warmup period data from NaN checking, by default True. When True, only checks data points where flood_event_markers > 0 or excludes the first warmup_length data points if markers are not available.
Raises¶
ValueError If any NaN values are found in the non-warmup data, raises an exception and prints detailed information.
Notes¶
This method uses the class attributes: - self.net_rain_key: Key name for net rainfall data - self.obs_flow_key: Key name for observed flow data - self.warmup_length: Number of warmup time steps
When exclude_warmup=True: 1. If 'flood_event_markers' exists in event data, only checks where markers > 0 2. Otherwise, skips the first self.warmup_length data points
concatenate_warmup_and_augmented_data(warmup_df, augmented_file_path, use_score_as_weight=False, score_threshold=60)
¶
create_xarray_dataset_from_augdf(df, station_id, time_unit='3h')
¶
discover_augmented_files(augmented_files_dir, source_event=None, modified_by=None, time_range=None, latest_only=False)
¶
发现增强数据文件的智能接口
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
augmented_files_dir
|
str
|
增强数据文件目录 |
required |
source_event
|
Optional[str]
|
源事件名过滤 (如 "event_1994081520_1994081805") |
None
|
modified_by
|
Optional[List[str]]
|
修改者列表过滤 |
None
|
time_range
|
Optional[Tuple[str, str]]
|
修改时间范围过滤 ("2025-01-01", "2025-12-31") |
None
|
latest_only
|
bool
|
是否只返回每个源事件的最新版本 |
False
|
Returns:
| Type | Description |
|---|---|
List[Dict]
|
List[Dict]: 文件信息列表,包含文件路径、元数据等 |
extract_flood_events(df, include_peak_obs=True)
¶
get_constants()
¶
Get the constant values used by this datasource.
Returns¶
dict Dictionary containing constant values with keys: - 'net_rain_key': Key name for net rain data - 'obs_flow_key': Key name for observed flow data - 'delta_t_hours': Time step in hours (derived from time_unit) - 'delta_t_seconds': Time step in seconds - 'warmup_length': Number of warmup time steps
get_user_contributions_summary(augmented_files_dir)
¶
获取用户贡献统计
get_warmup_period_data(original_start_time, original_end_time, station_id, warmup_hours=240)
¶
load_1basin_flood_events(station_id=None, flow_unit='mm/3h', include_peak_obs=True, verbose=True, **kwargs)
¶
parse_augmented_file_metadata(augmented_file_path)
¶
process_augmented_files_by_discovery(station_ids, augmented_files_dir, source_event=None, modified_by=None, time_range=None, latest_only=True, warmup_hours=240, time_unit='3h', score_threshold=60, use_score_as_weight=False)
¶
Process augmented data files based on file discovery.
Parameters¶
station_ids : Union[str, List[str]] Station ID or list of station IDs. augmented_files_dir : str Directory containing augmented data files. source_event : Optional[str], optional Filter by source event name. modified_by : Optional[List[str]], optional Filter by list of modifiers. time_range : Optional[Tuple[str, str]], optional Filter by modification time range. latest_only : bool, optional Whether to process only the latest version for each source event. warmup_hours : int, optional Number of warmup hours. time_unit : str, optional Time unit. score_threshold : int, optional Score threshold (default: 60). Behavior depends on use_score_as_weight: - If use_score_as_weight=False: Only events with Score >= score_threshold are included (filtering mode) - If use_score_as_weight=True: Events with Score < score_threshold get flood_event=1, events with Score >= score_threshold get flood_event=score (weighting mode) use_score_as_weight : bool, optional If True, use score-based weighting mode where flood_event stores actual scores (1-100) instead of binary values (0/1). Default is False (threshold filtering mode). - False (filtering): flood_event=0/1, only score>=threshold included - True (weighting): flood_event=-1/0 (non-flood), 1 (low-quality), or score (high-quality)
Returns¶
Optional[str] Path to the cache file, or None if processing fails.
read_ts_xrdataset_augmented(gage_id_lst=None, t_range=None, var_lst=None, time_unit='3h', **kwargs)
¶
rename_dataframe_columns(df, custom_mapping=None)
¶
hydrodatasource.reader.gages.Gages
¶
Bases: HydroData
cache_timeseries_xrdataset(trange4cache=None, **kwargs)
¶
Save all timeseries data in separate NetCDF files for each time unit.
Parameters¶
trange4cache : list, optional Time range for caching data, by default ["1980-01-01", "2023-12-31"] kwargs : dict, optional batchsize -- Number of basins to process per batch, by default 100 time_units -- List of time units to process, by default None start0101_freq -- for freq setting, if the start date is 01-01, set True, by default False
cache_xrdataset(t_range=None, time_units=None)
¶
Save all data in a netcdf file in the cache directory
get_constant_cols()
¶
all readable attrs in GAGES-II
read_attr_all(gages_ids)
¶
read_attr_origin(gages_ids, attr_lst)
¶
read_constant_cols(object_ids=None, constant_cols=None, **kwargs)
¶
read_target_cols(usgs_id_lst=None, t_range_list=None, target_cols=None, **kwargs)
¶
read_ts_xrdataset(gage_id_lst=None, t_range=None, var_lst=None, **kwargs)
¶
Read time-series xarray dataset from multiple NetCDF files and organize them by time units.
Parameters:¶
gage_id_lst: list List of gage IDs to select. t_range: list List of two elements [start_time, end_time] to select time range. var_lst: list List of variables to select. **kwargs Additional arguments.
Returns:¶
dict: A dictionary where each key is a time unit and each value is an xarray.Dataset containing the selected gage IDs, time range, and variables.
hydrodatasource.reader.grdc.Grdc
¶
Bases: HydroData
Reading GRDC streamflow data.
cache_grdc_daily(station_ids=None, time_range=None, batch_size=1000)
¶
Save GRDC daily data to a NetCDF file.
Parameters¶
station_ids: list of str List of station IDs to read data for. time_range: list of str List of [start_time, end_time] in UTC and ISO format strings e.g. ['YYYY-MM-DDTHH:MM:SSZ', 'YYYY-MM-DDTHH:MM:SSZ']. batch_size: int Number of stations to process in each batch
map_station_to_continent(station_id)
¶
Maps a station ID to its corresponding continent based on rules.
read_grdc_daily_data(station_id, time_range, parameter='Q', column='streamflow')
¶
read daily river discharge data from Global Runoff Data Centre (GRDC).
Requires the GRDC daily data files in a local directory. The GRDC daily data files can be ordered at https://www.bafg.de/GRDC/EN/02_srvcs/21_tmsrs/riverdischarge_node.html
Parameters¶
1 2 3 4 5 6 7 8 | |
Returns:
| Type | Description |
|---|---|
|
grdc data in a dataframe and metadata. |
Examples:
.. code-block:: python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
read_site_info()
¶
Reads the shapefile and extracts the 'id' column as a list.
read_streamflow_xrdataset(station_id_lst=None, time_range=None, **kwargs)
¶
Read GRDC daily data from multiple NetCDF files and organize them by station IDs.
Parameters¶
station_id_lst : list List of station IDs to select. time_range : list List of two elements [start_time, end_time] to select time range. **kwargs: Additional arguments.
Returns¶
dict: A dictionary where each key is a station ID and each value is an xarray.Dataset containing the selected station IDs, time range, and variable.
hydrodatasource.reader.rainfall_reader.RainfallReader
¶
hydrodatasource.reader.rsvr_inflow_reader.RsvrInflowReader
¶
Processor¶
Basin Mean Rainfall¶
hydrodatasource.processor.basin_mean_rainfall.basin_mean_func(df, weights_dict=None)
¶
Generic basin averaging method that supports both arithmetic mean and weighted mean (e.g. Thiessen polygon weights)
When some columns have missing values in a row, the function automatically switches to arithmetic mean for that row instead of using weights. This ensures robustness when dealing with incomplete data.
Parameters¶
df : DataFrame Time series DataFrame for multiple stations, with station names as column names; each column should be a time series of rainfall data for a specific station weights_dict : dict, optional Dictionary with tuple of station names as keys and list of weights as values. If None, arithmetic mean is used.
the keys of list must be in the same order as the columns of df.
hence, an easy way is you give your df with a sorted column names and then use the same order to create the keys of weights_dict. for example: weights_dict = { ("st1", "st2", "st3", "st4"): [0.25, 0.5, 0.1, 0.15], } df = df[["st1", "st2", "st3", "st4"]] then the keys of weights_dict must be in the same order as the columns of df.
NOTE
we set the format of weights_dict like this because we want to extend it to match the weights_dict key based on the missing data situation and the key in weights_dict. This is a TODO item. if the key in weights_dict matches the columns of df, we use the weights in weights_dict; if the key in weights_dict does not match the columns of df, we use the arithmetic mean. For example, if the columns of df are ["st1", "st2", "st3", "st4"], and the weights_dict is: weights_dict = { ("st1", "st2", "st3", "st4"): [0.25, 0.5, 0.1, 0.15], ("st1", "st2", "st3"): [0.25, 0.5, 0.1], ("st3", "st4"): [0.1, 0.15], } then when st4 has missing data, we use the weights in ("st1", "st2", "st3") to calculate the weighted mean; and when st1 and st2 have missing data, we use the weights in ("st3", "st4") to calculate the weighted mean. Otherwise, we use the arithmetic mean.
But this function is not finished yet, and the weights_dict now only supports the case that the keys of weights_dict has all the columns of df; if any column in df is missing, the function will use the arithmetic mean.
Returns¶
Series Basin-averaged time series
Rainfall-Runoff Event Identification¶
hydrodatasource.processor.dmca_esr.get_rr_events(rain, flow, basin_area, max_window=100, max_flow_min=None)
¶
use DMCA-ESR method to identify rainfall-runoff events
Parameters¶
rain : xr.DataArray the rainfall data flow : xr.DataArray the streamflow data basin_area : xr.Dataset a dataset with a variable named area and for each basin max_window: int number of time intervals for find events; default 100 (for hourly) max_flow_min: list the minimum of max flow for each basin value below this will not be considered to look for an event; default 100 m^3/s Returns
dict the rainfall-runoff events for each basin
Raises¶
ValueError Invalid unit format ValueError Unsupported unit
Cleaner¶
Cleaner (base class)¶
hydrodatasource.cleaner.cleaner.Cleaner
¶
RainfallCleaner¶
hydrodatasource.cleaner.rainfall_cleaner.RainfallCleaner
¶
Bases: Cleaner
__init__(data_folder, output_folder)
¶
data_check_hourly_extreme(basin_id, climate_extreme_value=None, modify=False)
¶
Check if the daily precipitation values at chosen stations are within a reasonable range. Values larger than the climate extreme value are treated as anomalies. If no climate_extreme_value is provided, the maximum value in the data is used.
Parameters¶
climate_extreme_value : float, optional Climate extreme threshold for the region, calculated as 95% of the maximum observed DRP. If not provided, will be calculated as 95% of the maximum DRP value in the data.
Returns¶
df_anomaly_stations_periods : pd.DataFrame DataFrame of anomalies with columns: 'STCD', 'TM', 'DRP'.
data_check_time_series(basin_id, check_type=None, gradient_limit=None, window_size=None, consistent_value=None, modify=False)
¶
Check daily precipitation values at chosen stations for gradient or time consistency anomalies.
Parameters¶
basin_id: str Basin ID. check_type : str Type of check to perform: "gradient" for gradient check, "consistency" for time consistency check. gradient_limit : float, optional Maximum allowable gradient change in precipitation between consecutive days. Used in "gradient" check. Default is 10 mm. window_size : int, optional Size of the window (in hours) to check for time consistency (used in "consistency" check). Default is 24 hours. consistent_value : float, optional The specific precipitation value to check for consistency (used in "consistency" check). Default is 0.1 mm.
Returns¶
pd.DataFrame DataFrame of detected anomalies with columns: 'STCD', 'TM', 'DRP', 'Issue' (where applicable).
data_check_yearly(basin_id, year_range=None, diff_range=None, min_true_percentage=0.75, min_consecutive_years=3, modify=False)
¶
计算遥感数据与站点数据之间的降水差异,评估站点可靠性,并返回可信任的站点列表。
参数:¶
basin_id : str Basin ID year_range : list, 可选 要筛选的年份范围,默认是 [2010, 2024]。 diff_range : list, 可选 站点数据和遥感数据之间的ratio差异范围 0.5 means station data is 0.5 times of reanalysis data 2.0 means station data is 2 times of reanalysis data min_true_percentage : float, 可选 要求可信年份的最小比例,默认 0.75。 min_consecutive_years : int, 可选 最小连续可信年份数,默认 3。
返回:¶
result_df : pd.DataFrame 可信站点的 DataFrame,包含 'STCD'、'Latitude'、'Longitude' 和 'Reason' 列。
rainfall_clean(basin_id, **kwargs)
¶
the station gauged rainfall data cleaning pipeline
read_and_concat_csv(basin_id)
¶
读取并合并文件夹下的所有 CSV 文件
ReservoirInflowBacktrack¶
hydrodatasource.cleaner.rsvr_inflow_cleaner.ReservoirInflowBacktrack
¶
Bases: Cleaner
__init__(data_folder, output_folder)
¶
Back-calculating inflow of reservior
Parameters¶
data_folder : str the folder of reservoir data output_folder : type where we put inflow data
back_calculation(rsvr_id, clean_w_path, original_file, output_folder)
¶
Back-calculate inflow from reservoir storage data NOTE: each time has three columns: I Q W -- I is the inflow, Q is the outflow, W is the reservoir storage Generally, in sql database, a time means the end of previous time period For example, a hourly database, 13:00 means 12:00-13:00 period because the data is GOT at 13:00 (we cannot observe future) Hence, for this function, W means the storage at the end of the time period, I and Q means the inflow and outflow of the time period So we need to use W of the previous time as the initial water storage of the time period. Hence, I1 = Q1 + (W1 - W0)
Parameters¶
rsvr_id : str The ID of the reservoir data_path : str the path to the cleaned_w_data file original_file: str the path to the original file output_folder : str where to save the back calculated data
Returns¶
str the path to the result file
clean_w(rsvr_id, file_path, output_folder, fit_method='quadratic', zw_curve_std_times=3.0, remove_zw_outliers=False)
¶
Remove abnormal reservoir capacity data
Parameters¶
rsvr_id : str The ID of the reservoir file_path : str Path to the input file output_folder : str Path to the output folder fit_method : str, optional z-w curve fitting method, by default "quadratic" TODO: MORE METHODS need to be supported; power is also need to be debugged zw_curve_std_times: float, optional the times of standard deviation to remove outliers, by default 3 remove_zw_outliers: bool, optional whether to remove outliers for z-w curve fitting, by default False
Returns¶
str Path to the cleaned data file
delete_negative_inq(rsvr_id, inflow_data_path, original_file, output_folder, negative_deal_window=7, negative_deal_stride=4)
¶
remove negative inflow values with a rolling window the negative value will be adjusted to positvie ones to make the total inflow consistent for example, 1, -1, 1, -1 will be adjusted to 0, 0, 0, 0 so that wate balance is kept but note that as the window has stride, maybe the final few values will not be adjusted
Parameters¶
rsvr_id : str the id of the reservoir inflow_data_path : str the data file after back_calculation original_file : str the original file output_folder : str where to save the data negative_deal_window : int, optional the window to deal with negative values, by default 7 negative_deal_stride : int, optional the stride of window, by default 4
Returns¶
str the path to the result file
insert_inq(rsvr_id, inflow_data_path, original_file, output_folder)
¶
make inflow data as hourly data as original data is not strictly hourly data and insert inq with linear interpolation
Parameters¶
rsvr_id : str the id of the reservoir inflow_data_path : str the data file after delete negative inflow values original_file : str the original file output_folder : str where to save the data
Returns¶
str the path to the result file
StreamflowCleaner¶
hydrodatasource.cleaner.streamflow_cleaner.StreamflowCleaner
¶
Bases: Cleaner
FFT(streamflow_data)
¶
对流量数据进行迭代的傅里叶滤波处理,包括非负值调整和流量总量调整。 :cutoff_frequency: 傅里叶滤波的截止频率。 :time_step: 数据采样间隔。 :iterations: 迭代次数。
data_balanced(origin_data, transform_data)
¶
对一维流量数据进行总量平衡变换。 :origin_data: 原始一维流量数据。 :transform_data: 平滑转换后的一维流量数据。
kalman_filter(streamflow_data)
¶
对流量数据应用卡尔曼滤波进行平滑处理,并保持流量总量平衡。 :param streamflow_data: 原始流量数据
lowpass_filter(streamflow_data)
¶
对一维流量数据应用调整后的低通滤波器。 :cutoff_frequency: 低通滤波器的截止频率。 :sampling_rate: 数据的采样率。 :order: 滤波器的阶数,默认为5。
moving_average(streamflow_data)
¶
对流量数据应用滑动平均进行平滑处理,并保持流量总量平衡。 :param streamflow_data: 输入的流量数据数组 :return: 平滑处理后的流量数据
moving_average_difference(streamflow_data)
¶
对流量数据应用滑动平均差算法进行平滑处理,并保持流量总量平衡。 :window_size: 滑动窗口的大小
robust_fitting(streamflow_data, k=1.5)
¶
对流量数据应用抗差修正算法进行平滑处理,并保持流量总量平衡。 默认采用二次曲线进行拟合优化,该算法处理性能较差
wavelet(streamflow_data)
¶
对一维流量数据进行小波变换分析前后拓展数据以减少边缘失真,然后调整总流量。 :cwt_row: 小波变换中使用的特定宽度。