-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#224 Add scripts to generate csv per transect
- Loading branch information
1 parent
8f2ada7
commit a9a7e41
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import pandas as pd | ||
|
||
def create_csv_per_transect( | ||
timeseries_df: pd.DataFrame, | ||
save_path: str, | ||
file_extension: str = "_timeseries_raw.csv", | ||
) -> None: | ||
""" | ||
Create a CSV file per transect from a given timeseries DataFrame. | ||
Args: | ||
timeseries_df (pd.DataFrame): The timeseries DataFrame containing transect data. | ||
save_path (str): The path to save the CSV files. | ||
file_extension (str, optional): The file extension for the CSV files. Defaults to "_timeseries_raw.csv". | ||
Returns: | ||
None | ||
""" | ||
for transect_id in timeseries_df.columns: | ||
if transect_id == "dates": | ||
continue | ||
print(f"Processing {transect_id}") | ||
# 3. Save the csv file per transect | ||
df = pd.DataFrame( | ||
{ | ||
"dates": timeseries_df["dates"], | ||
transect_id: timeseries_df[transect_id], | ||
|
||
}, | ||
) | ||
# Save to csv file | ||
fn = f"{transect_id}{file_extension}" | ||
file_path = os.path.join(save_path, fn) | ||
df.to_csv( | ||
file_path, sep=",", index=False | ||
) | ||
|
||
|
||
|
||
# 1. Enter the path to the csv file that contains the time series | ||
# - replace the path below with the path to the csv file | ||
input_file = r"C:\development\doodleverse\coastseg\CoastSeg\sessions\rym1_model_extract_shorelines\transect_time_series.csv" | ||
# 2. The output file will be saved in the same directory as the input file | ||
save_location = os.path.dirname(os.path.abspath(input_file)) | ||
|
||
|
||
if not os.path.exists(input_file): | ||
print(f"File not found: {input_file}") | ||
exit() | ||
|
||
# read the csv file | ||
tide_corrected_timeseries_df = pd.read_csv(input_file) | ||
|
||
# save a csv for each transect that was tidally corrected | ||
create_csv_per_transect( | ||
tide_corrected_timeseries_df, | ||
save_location, | ||
) | ||
print(f"Saved the time series for each transect with intersections with the shoreline to {save_location}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
import pandas as pd | ||
|
||
def save_csv_per_id( | ||
df: pd.DataFrame, | ||
save_location: str, | ||
filename: str = "timeseries_tidally_corrected.csv", | ||
id_column_name: str = "transect_id", | ||
): | ||
new_df = pd.DataFrame() | ||
unique_ids = df[id_column_name].unique() | ||
for uid in unique_ids: | ||
new_filename = f"{uid}_{filename}" | ||
new_df = df[df[id_column_name] == uid] | ||
new_df.to_csv(os.path.join(save_location, new_filename)) | ||
|
||
# 1. Enter the path to the csv file that contains the tide corrected time series | ||
# - replace the path below with the path to the csv file | ||
input_file = r"C:\development\doodleverse\coastseg\CoastSeg\sessions\rym1_model_extract_shorelines\transect_time_series_tidally_corrected.csv" | ||
# 2. The output file will be saved in the same directory as the input file | ||
save_location = os.path.dirname(os.path.abspath(input_file)) | ||
|
||
|
||
if not os.path.exists(input_file): | ||
print(f"File not found: {input_file}") | ||
exit() | ||
|
||
# read the csv file | ||
timeseries_df = pd.read_csv(input_file) | ||
if "Unnamed: 0" in timeseries_df.columns: | ||
timeseries_df.drop(columns=["Unnamed: 0"], inplace=True) | ||
|
||
# save a csv for each transect that was tidally corrected | ||
save_csv_per_id( | ||
timeseries_df, | ||
save_location, | ||
filename="timeseries_tidally_corrected.csv", | ||
) | ||
print(f"Saved the tidally corrected time series for each transect with intersections with the shoreline to {save_location} ") |