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

Untested initial commit for the new IMF and F10.7 features needed for SWMF GM

parent 4067a386
......@@ -6,6 +6,69 @@ TODO: Move pandas dependancy elsewhere.
"""
import datetime as dt
from .tools import _make_line
def write_imf_input(imf_data, filename='IMF.dat', **kwargs):
"""Creates the IMF.dat input file for the SWMF BATS-R-US geospace model.
`imf_data` must be a dictionary of array_like objects with same length
in data. In swmfpy Pythonic versions are always preferred so the 'times'
must be `datetime.datetime` array.
imf_data = dict(times, bx, by, bz, vx, vy, vz, density, temperature)
Args:
imf_data (dict): This dictionary contains the solar wind data.
filename (str): (default: 'IMF.dat') Filename to write to.
**kwargs:
commands ([str]): (default: None) List of commands to write to imf
input file (indexed by line then by tabs on same
line). *Note*: Must be a list if have one command
str.
Raises:
TypeError: If commands is not a list or tuple. It must be at least a
one element list of strings.
Examples:
"""
columns_dat = ['year', 'month', 'day', 'hour', 'min', 'sec', 'msec',
'bx', 'by', 'bz', 'vx', 'vy', 'vz',
'density', 'temperature']
columns_dict = ['times',
'bx', 'by', 'bz', 'vx', 'vy', 'vz',
'density', 'temperature']
def _time_repr(time_raw):
'Represent time in a suitable format'
raw_str = dt.datetime.strftime(time_raw, '%Y %m %d %H %M %S %f')[:-3]
return raw_str.split()
with open(filename, 'w') as file_imf:
# header
file_imf.write('\t'.join(columns_dat)+'\n\n')
# write commands
commands = kwargs.get('commands', None)
if commands:
try:
isinstance(commands, (list, tuple))
except TypeError:
raise TypeError(f'{__name__}: commands must be list or tuple')
for command in commands:
file_imf.write(_make_line(command)+'\n')
# write dat file
file_imf.write('#START\n')
lines = []
for index, _time in enumerate(imf_data[columns_dict[0]]):
line = [_time_repr(_time)]
for key in columns_dict[1:]:
line += [str(imf_data[key][index])]
line = '\t'.join(line)
lines += line
file_imf.writelines(lines)
def read_wdc_ae(wdc_filename):
......
......@@ -8,6 +8,7 @@ __author__ = 'Qusai Al Shidi'
__email__ = 'qusai@umich.edu'
import logging
from .tools import _make_line
def replace_command(parameters, input_file, output_file='PARAM.in'):
......@@ -138,15 +139,6 @@ def read_command(command, paramin_file='PARAM.in', **kwargs):
# HIDDEN FUNCTIONS
def _make_line(value):
"""Makes the paramin line based on value type recursively"""
if isinstance(value, str):
return value
if isinstance(value, list):
return '\t\t\t'.join([_make_line(v) for v in value])
return str(value)
def _get_command(line):
"""Returns the 'COMMAND' if on line.
......
......@@ -7,7 +7,26 @@ __email__ = 'qusai@umich.edu'
def _import_error_string(library):
return f'Error importing drms. Maybe try `pip install -U {library} --user ` .'
return (
'Error importing drms. '
f'Maybe try `pip install -U {library} --user ` .'
)
def _make_line(value):
"""Makes the paramin line based on value type recursively
"""
if isinstance(value, str):
return value
if isinstance(value, (list, tuple)):
return '\t\t\t'.join([_make_line(v) for v in value])
try:
str(value)
except Exception as error:
raise TypeError(error,
'\nMust reduce to a str or a method that has',
'__str__ or __repr__')
return str(value)
def _nearest(pivot, items):
......
Supports Markdown
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