emodpy_malaria.weather.weather_set module#
Weather set module is implementing functionality for working with sets of weather files.
- class emodpy_malaria.weather.weather_set.WeatherSet(dir_path: str | Path | None = None, file_names: Dict[WeatherVariable, str] | None = None, weather_columns: Dict[WeatherVariable, str] | None = None)[source]#
Bases:
object
Representation of a set of weather files required by EMOD, for all or a subset of weather variables. Automate tasks for working with multiple weather files using WeatherData and WeatherMetadata objects. WeatherSet contains a dictionary of weather variables to WeatherData and WeatherMetadata objects. Supports: 1. Conversion from/to csv, dataframe (from_csv, to_csv, from_dataframe, to_dataframe) 2. Conversion from/to EMOD weather files, .bin and .bin.json (from_file, to_file)
- values() List[WeatherData] [source]#
Returns the list of WeatherData objects.
- property file_names: Dict[WeatherVariable, str]#
Dictionary of weather variables (keys) and weather file names (values).
- property attributes: WeatherAttributes#
- property weather_variables: List[WeatherVariable]#
The list of weather variables the weather set covers.
- property weather_columns: Dict[WeatherVariable, str]#
The list of weather columns.
- classmethod from_dataframe(df: pd.DateFrame, node_column: str = None, step_column: str = None, weather_columns: Dict[WeatherVariable, str] = None, attributes: WeatherAttributes = None) WeatherSet [source]#
Initializes WeatherSet object from a dataframe containing weather time series. The dataframe must have node ids, step and weather columns.
- Parameters:
df – Dataframe containing weather data.
node_column – (Optional) Column containing node ids. The default is “nodes”.
step_column – (Optional) Column containing node index for weather time series values. The default is “steps”.
weather_columns – (Optional) Dictionary of weather variables (keys) and weather column names (values). Defaults are WeatherVariables values are used: “airtemp”, “humidity”, “rainfall”, “landtemp”.
attributes – (Optional) Weather attribute object containing metadata for WeatherMetadata object.
- Returns:
WeatherSet object.
- classmethod from_csv(file_path: str | Path, node_column: str | None = None, step_column: str | None = None, weather_columns: Dict[WeatherVariable, str] | None = None, attributes: WeatherAttributes | None = None) WeatherSet [source]#
Initializes WeatherSet object from a dataframe containing weather time series. The csv file must have node ids, step and weather columns.
- Parameters:
file_path – The csv file path.
node_column – (Optional) Column containing node ids. The default is “nodes”.
step_column – (Optional) Column containing node index for weather time series values. The default is “steps”.
weather_columns – (Optional) Dictionary of weather variables (keys) and weather column names (values). Defaults are WeatherVariables values are used: “airtemp”, “humidity”, “rainfall”, “landtemp”.
attributes – (Optional) The weather attribute object containing metadata for WeatherMetadata object.
- Returns:
WeatherSet object.
- to_dataframe(node_column: str | None = None, step_column: str | None = None, weather_columns: Dict[WeatherVariable, str] | None = None) DataFrame [source]#
Creates a dataframe containing node ids, time steps and weather columns.
- Parameters:
node_column – (Optional) Column containing node ids. The default is “nodes”.
step_column – (Optional) Column containing node index for weather time series values. The default is “steps”.
weather_columns – (Optional) Dictionary of weather variables (keys) and weather column names (values). Defaults are WeatherVariables values are used: “airtemp”, “humidity”, “rainfall”, “landtemp”.
- Returns:
Dataframe containing node ids and weather time series.
- to_csv(file_path: str | Path, node_column: str | None = None, step_column: str | None = None, weather_columns: Dict[WeatherVariable, str] | None = None) DataFrame [source]#
Creates a csv file containing node ids, time steps and weather columns.
- Parameters:
file_path – The path of a csv file to be generated.
node_column – (Optional) Column containing node ids. The default is “nodes”.
step_column – (Optional) Column containing node index for weather time series values. The default is “steps”.
weather_columns – (Optional) Dictionary of weather variables (keys) and weather column names (values). Defaults are WeatherVariables values are used: “airtemp”, “humidity”, “rainfall”, “landtemp”.
- Returns:
Dataframe containing node ids and weather time series, used to create the csv file.
- classmethod from_files(dir_path: str | Path, prefix: str = '', file_names: Dict[WeatherVariable, str] | None = None) WeatherSet [source]#
Instantiates WeatherSet from to weather files which paths are determined based on given arguments.
- Parameters:
dir_path – Directory path containing weather files.
prefix – Weather files prefix, e.g. “dtk_15arcmin_”
file_names – Dictionary of weather variables (keys) and weather .bin file names (values).
- Returns:
WeatherSet object.
- to_files(dir_path: str | Path, file_names: Dict[WeatherVariable, str] | None = None) NoReturn [source]#
Saves WeatherSet to weather files which paths are determined based on given arguments.
- classmethod make_file_paths(dir_path: str | Path | None = None, prefix: str = 'dtk_15arcmin_', suffix: str = '{}_daily.bin', weather_variables: List[WeatherVariable] | None = None, weather_names: Dict[WeatherVariable, str] | None = None) Dict[WeatherVariable, str] [source]#
Construct file paths using the weather directory path, file name prefix/suffix and weather variable names. The logic of this method is the same as of “Path.glob” method, with two adjustments, added to make its use more convenient for working with weather files: - if prefix/suffix are not specified, defaults are used (see method arguments). - if suffix doesn’t end with “.bin” or “*”, “*.bin” is added (since, otherwise, no matches can be found).
- Parameters:
dir_path – (Optional) Directory path containing weather files.
prefix – (Optional) Weather file name prefix, usually a fixed string like “dtk_”.
suffix – (Optional) Weather file name suffix, usually containing a weather variable name parameter like “*{tag}*.bin”).
weather_names – (Optional) Dictionary of weather variables (keys) and custom weather variable names (values).
weather_variables – (Optional) Weather variables to be used in case custom weather names are not specified. In this case lowercase weather variable names are used, for example: AIR_TEMPERATURE -> air_temperature.
- Returns:
- Dictionary of weather variables (keys) and weather file paths.
For example, air temperature could be represented as: WeatherVariable.AIR_TEMPERATURE: “dtk_15arcmin_air_temperature_daily.bin”
- classmethod select_weather_files(dir_path: str | Path, prefix: str = '*', suffix: str = '*{}*.bin', weather_variables: List[WeatherVariable] | None = None, weather_names: Dict[WeatherVariable, str] | None = None) Dict[WeatherVariable, str] [source]#
Select a set of weather files using the weather directory path, file name prefix/suffix and weather variable names. The logic of this method is the same as of “Path.glob” method, with two adjustments, added to make its use more convenient for working with weather files: - if prefix/suffix are not specified, defaults are used (see method arguments). - if suffix doesn’t end with “.bin” or “*”, “*.bin” is added (since, otherwise, no matches can be found).
- Parameters:
dir_path – (Optional) Directory path containing weather files.
prefix – (Optional) Weather file name prefix, usually a fixed string like “dtk_”.
suffix – (Optional) Weather file name suffix, usually containing a weather variable name parameter like “*{tag}*.bin”).
weather_names – (Optional) Dictionary of weather variables (keys) and custom weather variable names (values).
weather_variables –
- (Optional) Weather variables to be used in case custom weather names are not specified.
In this case lowercase weather variable names are used, for example: AIR_TEMPERATURE -> air_temperature.
- Returns:
Dictionary of weather variables (keys) and weather file names. For example, WeatherVariable.AIR_TEMPERATURE: “dtk_15arcmin_air_temperature_daily.bin”