Commit a08b83ed authored by Qusai Al Shidi's avatar Qusai Al Shidi 💬
Browse files

paramin_replace usable

parent 3d2f2e05
......@@ -7,6 +7,7 @@ __version__ = "0.0.1"
__maintainer__ = "Qusai Al Shidi"
__email__ = ""
import logging
import datetime as dt
import numpy as np
import pandas as pd
......@@ -223,30 +224,33 @@ def read_gm_log(filename, colnames=None, index_by_time=True):
return data
def replace_paramin_values(param_file, replace, output_file=""):
def paramin_replace(input_file, replace, output_file=""):
"""Replace values for the parameters in a file.
Note, if you have repeat commands this will replace all the repeats.
param_file (str): String of file name.
replace (dict): Dictionary of strings with format
replace["#COMMAND"]["parameter"] = "value"
input_file (str): String of file name.
replace (dict): Dictionary of strs with format
replace = {"#COMMAND": ["value", "comments", ...]}
This is case sensitive.
output_file (str): (default "") The output file to write to.
A value of None will not output a file.
A list of lines of the file that would be outputted.
change["#SOLARWINDFILE"]["UseSolarWindFile"] = "T"
change["#SOLARWINDFILE"]["NameSolarWindFile"] = "new_imf.dat"
change["#DOAMR"]["DnAmr"] = 200
change["#SOLARWINDFILE"] = [["T", "UseSolarWindFile"],
["new_imf.dat", "NameSolarWindFile"]]
# This will overwrite
replace_paramin_option("", change)
swmfpy.paramin_replace("", change)
# TODO This will replace all for repeat commands.
with open(param_file, 'rt') as paramin:
logger = logging.getLogger() # For debugging
# Read and replace paramin file by making a temp list
with open(input_file, 'rt') as paramin:
command = None # Top level #COMMAND
# Compile lines in a list before editing/writing it
lines = list(paramin)
......@@ -254,14 +258,14 @@ def replace_paramin_values(param_file, replace, output_file=""):
words = line.split()
if words and words[0] in replace.keys():
command = words[0] # Current command
# Start replace once we're in a specified command
elif command in replace.keys():
for param, value in replace[command].items():
newline = value
if param in words:
newline += "\t\t\t" + param
print("Replacing:", line, "with:", newline)
lines[line_num] = newline + '\n'
for param, value in enumerate(replace[command]):
newline = ""
for text in value:
newline += text + "\t\t\t""Replacing:\n" + line
+ "with:\n" + newline)
# Lines will be replaced in order
lines[line_num+param+1] = newline + '\n'
# Write the file
if output_file is None:
return lines # Break if None output_file (not default behaviour)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment