Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 16, 2024
1 parent aae1cfb commit 3fb942e
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions scepter/setup/scepter_helperFxns.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import re
import shutil
import subprocess

import pandas as pd


# %%
# --------------------------------------------------------------------------
Expand Down Expand Up @@ -178,28 +178,25 @@ def copy_files(src_dir: str, dst_dir: str):
# print(f"Copied {src_file} to {dst_file}")



def generate_timesteps(total_duration: float,
timestep: float
) -> list:
'''
Create a list of time steps given a total duration for
def generate_timesteps(total_duration: float, timestep: float) -> list:
"""
Create a list of time steps given a total duration for
a scepter run and a timestep. (for use in multi-run only).
Parameters
----------
total_duration : float
[years] value denoting the total duration of the
[years] value denoting the total duration of the
SCEPTER simulation
timestep : float
Returns
-------
list
list of start times for SCEPTER simulations
list of start times for SCEPTER simulations
for ex: [0,2,4,6,...]
'''
"""
timesteparr = []
current_time = 0
while current_time <= (total_duration - timestep):
Expand All @@ -208,50 +205,50 @@ def generate_timesteps(total_duration: float,
return timesteparr



def write_iter_file_with_marker(values: list,
current_index: int,
filename: str,
):
'''
def write_iter_file_with_marker(
values: list,
current_index: int,
filename: str,
):
"""
Write a file showing all timesteps and an arrow
toward the specific timestep handled by the given
scepter run (for use in multi-run only)
Parameters
----------
values : list
list of timesteps, generally the output of the
list of timesteps, generally the output of the
generate_timesteps function
current_index : int
the run number in the sequence of simulations
the run number in the sequence of simulations
(set by the 'counter' in the multi-run loop)
filename : str
the path to the file that notes which timestep is
used. default in the scepter multi-run script is
"multiyear-iter.res"
Returns
-------
'''
with open(filename, 'w') as file:
file.write(f"year \n")
"""
with open(filename, "w") as file:
file.write("year \n")
for i, value in enumerate(values):
if i == current_index:
file.write(f"{value} <--- \n")
else:
file.write(f"{value}\n")



def update_clim(inputfile: str,
outputfile: str,
timezero: float,
):
'''
SCEPTER climatology always starts at year zero. When you run a multi-year
run you need to modify t=0 depending on the timestep (e.g., for the run
def update_clim(
inputfile: str,
outputfile: str,
timezero: float,
):
"""
SCEPTER climatology always starts at year zero. When you run a multi-year
run you need to modify t=0 depending on the timestep (e.g., for the run
spanning years 2-4, climatology year 2 must become year zero). this function
updates the climatology to match the multi-run index and saves the result
in the new run's directory. (for use in multi-run only)
Expand All @@ -264,33 +261,33 @@ def update_clim(inputfile: str,
location where we'll write the output climatology file (the new timestep dir)
timezero : float
the time value that gets set to zero in this iteration
Returns
-------
'''
"""
# open the input and output files
with open(inputfile, 'r') as f_in, open(outputfile, 'w') as f_out:
with open(inputfile, "r") as f_in, open(outputfile, "w") as f_out:
# skip the header line and copy it to new file
header = next(f_in)
f_out.write(header) # write to output
# read the file line by line
for line in f_in:
# split each line into year and climate value
year, clim = line.strip().split('\t') # Adjust the delimiter as needed
year, clim = line.strip().split("\t") # Adjust the delimiter as needed

# convert year to an integer and subtract 5
year = float(year) - timezero

# check if the adjusted year is greater than or equal to tstep
if year >= 0:
formatted_yr = "{:.7f}".format(float(year))
formatted_clim = "{:.7f}".format(float(clim))
# write the adjusted year and temperature to the output file
# print(f"{formatted_yr}\t{formatted_clim}\n")
f_out.write(f"{formatted_yr}\t{formatted_clim}\n") # Adjust the delimiter as needed


f_out.write(
f"{formatted_yr}\t{formatted_clim}\n"
) # Adjust the delimiter as needed


def remove_duplicates(input_file: str):
Expand All @@ -312,10 +309,17 @@ def remove_duplicates(input_file: str):
with open(input_file, "r") as f:
lines = f.readlines()
header = lines[0] # Save the header
lines_without_tabs = [line.replace('\t', '') for line in lines] # remove tabs before comparing
lines_without_tabs = [
line.replace("\t", "") for line in lines
] # remove tabs before comparing
# add '\n' if a line doesn't have it (just for comparison sake)
lines_without_tabs2 = [element if element.endswith('\n') else element + '\n' for element in lines_without_tabs]
unique_lines = set(lines_without_tabs2[1:]) # remove duplicates from mineral names
lines_without_tabs2 = [
element if element.endswith("\n") else element + "\n"
for element in lines_without_tabs
]
unique_lines = set(
lines_without_tabs2[1:]
) # remove duplicates from mineral names

# check if the last entry ends with "\n" and remove it if needed
last_entry = list(unique_lines)[-1]
Expand Down

0 comments on commit 3fb942e

Please sign in to comment.