grat.reports.writers
¶
Writes report dataframes to csv files, and manages the output folder.
ReportFile
¶
Bases: NamedTuple
Where one report is written.
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
str
|
basename written inside the run's output folder. |
append |
bool
|
True when each water year appends to a single file, in which case the header is written only for the first year. |
check_output_path_length
¶
Fails early if the longest report path under output_parent would be too long.
Without this, the run gets as far as writing the first per-year report and then
raises a FileNotFoundError that doesn't say why.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_parent
|
directory the timestamped results folder will be created in. |
required | |
max_length
|
longest path allowed. Defaults to Windows' limit. |
WINDOWS_MAX_PATH
|
Raises:
| Type | Description |
|---|---|
ValueError
|
the longest report path would be over |
Source code in src/grat/reports/writers.py
construct_output_folder_name
¶
Creates a new unique folder using the current datetime to store the exported csv results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_to_folder
|
str
|
path to the output parent directory where the results folder will be created |
required |
Returns: path to the result folder that was just created
Source code in src/grat/reports/writers.py
zip_output_folder
¶
Zips up the output folder as a zipfile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_folder_path
|
path to zip up |
required | |
custom_name
|
optional custom name for the zip file |
None
|
Returns: path to the output zip file
Source code in src/grat/reports/writers.py
write_csv
¶
Writes a dataframe to a csv file inside the output folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
dataframe to write. |
required | |
output_folder
|
folder the file is written into. |
required | |
filename
|
basename of the output file. |
required | |
append
|
when True, append to any existing file and write the header only if the file is not already there. Used by the reports that build up a row per water year. |
False
|
Returns: path to the csv file that was written
Source code in src/grat/reports/writers.py
write_report
¶
Writes a dataframe to the file that REPORT_FILES gives for key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
dataframe to write. |
required | |
output_folder
|
folder the file is written into. |
required | |
key
|
entry in |
required |
Returns: path to the csv file that was written
Raises:
| Type | Description |
|---|---|
KeyError
|
|
Source code in src/grat/reports/writers.py
read_report
¶
Reads a report back from the folder it was written into.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_folder
|
the run's output folder. |
required | |
key
|
entry in |
required | |
year
|
water year, for the reports written into a per-year subfolder. |
None
|
|
dtype
|
optional column types, passed through to |
None
|
Returns: the report as a dataframe
Raises:
| Type | Description |
|---|---|
KeyError
|
|
FileNotFoundError
|
that report is not in |