Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions reeds/get_last_iter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
This script contains the function to get the last iteration g00 file for a given case.
This is used in the batch call scripts to identify the last iteration of a solved year
for output processing.
"""


#%% ===========================================================================
### --- IMPORTS ---
### ===========================================================================
import sys
import argparse
from pathlib import Path

#%% ===========================================================================
### --- Function ---
### ===========================================================================

def get_last_g00(batch_case, max_year):
pattern = f'{batch_case}_{max_year}i*.g00'
matches = list(Path('g00files').glob(pattern))

if not matches:
print(f"ERROR: The run {batch_case} has not solved last modeled year {max_year}.", file=sys.stderr)
sys.exit(1)

last_file = max(
matches,
key=lambda f: int(f.stem[f.stem.rfind('i')+1:])
)

# Use forward slashes universally, works on both OS
print((Path('g00files') / last_file.name).as_posix())

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Get the last iteration g00 file for a given batch case.')
parser.add_argument('batch_case', type=str, help='The batch case name (e.g., v20260504_Pacific)')
parser.add_argument('max_year', type=int, help='The last modeled year to check for (e.g., 2032)')
args = parser.parse_args()


get_last_g00(args.batch_case, args.max_year)
21 changes: 12 additions & 9 deletions reeds/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,15 +1228,18 @@ def get_last_iteration(case, year=2050, datum=None, samples=None):
"""Get the last iteration of PRAS for a given case/year"""
if datum not in [None,'flow','energy']:
raise ValueError(f"datum must be in [None,'flow','energy'] but is {datum}")
infile = sorted(glob(
os.path.join(
case, 'ReEDS_Augur', 'PRAS',
f"PRAS_{year}i*"
+ (f'-{samples}' if samples is not None else '')
+ (f'-{datum}' if datum is not None else '')
+ '.h5'
)
))[-1]
pattern = f"PRAS_{year}i*" \
+ (f'-{samples}' if samples is not None else '') \
+ (f'-{datum}' if datum is not None else '') \
+ '.h5'
matches = list(Path(case, 'ReEDS_Augur', 'PRAS').glob(pattern))
if not matches:
print(f"ERROR: The run {case} has not solved last modeled year {year}.", file=sys.stderr)
sys.exit(1)
infile = max(
matches,
key=lambda f: int(f.stem[f.stem.rfind('i')+1:].split('-')[0])
)
iteration = int(
os.path.splitext(os.path.basename(infile))[0]
.split('-')[0].split('_')[1].split('i')[1]
Expand Down
8 changes: 5 additions & 3 deletions runbatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,12 +1426,14 @@ def write_batch_script(
if LINUXORMAC else
f'set "r={os.path.join("g00files", f"{batch_case}_{max(solveyears)}i0")}"\n'
)
### Otherwise, run for the last iteration (lexicographically sorted)
### Otherwise, run for the last iteration (selected numerically)
else:
OPATH.writelines(
f"for r in g00files/{batch_case}_*.g00; do true; done\n"
f'r=$(python {os.path.join(casedir, "reeds", "get_last_iter.py")} {batch_case} {max(solveyears)})\n'
if LINUXORMAC else
f'for %%i in (g00files\{batch_case}_*.g00) do (set "r=%%i")\n'
f'for /f "delims=" %%i in '
f'(\'python {os.path.join(casedir, "reeds", "get_last_iter.py")} {batch_case} {max(solveyears)}\')'
f' do set "r=%%i"\n'
)
OPATH.writelines(
"gams e_report.gms"
Expand Down
Loading