Skip to content

Commit

Permalink
Merge pull request #71 from jklenzing/develop
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
jklenzing committed Jul 3, 2019
2 parents 92ccec8 + 27d5f20 commit 096e7f9
Show file tree
Hide file tree
Showing 23 changed files with 225 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[report]
omit =
*/tests/*
16 changes: 8 additions & 8 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# Description

Addresses # (issue)

Please include a summary of the change and which issue is fixed. Please also
include relevant motivation and context. List any dependencies that are required
for this change. Please see ``CONTRIBUTING.md`` for more guidelines.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing functionality
to not work as expected)
- [ ] This change requires a documentation update
- This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details for
your test configuration

- [ ] Test A
- [ ] Test B
- Test A
- Test B

**Test Configuration**:
* Operating system
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*.mod

.DS_STORE
sami2py-1.00.namelist
*f.dat
*u.dat
time.dat
Expand Down Expand Up @@ -32,3 +31,4 @@ docs/.templates

# dont ignore dat files in the testing folder
!sami2py/tests/test_data/test/lon256/1999_256/*.dat
!sami2py/tests/test_data/test/lon256/1999_257/*.dat
2 changes: 1 addition & 1 deletion sami2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sys
import os

__version__ = str('0.1.1')
__version__ = str('0.1.2')

# get home directory
env_dir = sys.prefix
Expand Down
22 changes: 11 additions & 11 deletions sami2py/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
-------------------------------------------------------------------------------
run_model(year, day, lat=0, lon=0, alt=300,
f107=120, f107a=120, ap=0,
rmin=100, rmax=2000, gams=3, gamp=3, altmin=85.,
dthr=0.25, hrinit=0., hrpr=24., hrmax=48.,
dt0=30., maxstep=100000000, denmin=1.e-6,
nion1=1, nion2=7, mmass=48, h_scale=1, o_scale=1,
no_scale=1, o2_scale=1, he_scale=1, n2_scale=1, n_scale=1,
Tinf_scale=1, Tn_scale=1., euv_scale=1,
wind_scale=1, hwm_model=14,
fejer=True, ExB_drifts=np.zeros((10,2)), ve01=0., exb_scale=1,
alt_crit=150., cqe=7.e-14,
tag='test', clean=False, test=False, outn=False)
f107=120, f107a=120, ap=0,
rmin=100, rmax=2000, gams=3, gamp=3, altmin=85.,
dthr=0.25, hrinit=0., hrpr=24., hrmax=48.,
dt0=30., maxstep=100000000, denmin=1.e-6,
nion1=1, nion2=7, mmass=48, h_scale=1, o_scale=1,
no_scale=1, o2_scale=1, he_scale=1, n2_scale=1, n_scale=1,
Tinf_scale=1, Tn_scale=1., euv_scale=1,
wind_scale=1, hwm_model=14,
fejer=True, ExB_drifts=np.zeros((10, 2)), ve01=0., exb_scale=1,
alt_crit=150., cqe=7.e-14,
tag='test', clean=False, test=False, fmtout=True, outn=False)
Initializes a run of the SAMI2 model and archives the data.
-------------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions sami2py/_core_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import numpy as np
from .utils import generate_path, get_unformatted_data


class Model(object):
"""Python object to handle SAMI2 model output data
"""
Expand Down Expand Up @@ -121,13 +122,13 @@ def __repr__(self):

mod_keys = self.check_standard_model()
if mod_keys:
out.append('\nNo modifications to empirical models')
else:
out.append('\nMultipliers used')
out.append('----------------')
for mkey in mod_keys:
out.append(('{s}: {f}').format(s=mkey,
f=self.MetaData[mkey]))
else:
out.append('\nNo modifications to empirical models')

return '\n'.join(out)

Expand Down Expand Up @@ -156,7 +157,7 @@ def _load_model(self):
ni = 7

model_path = generate_path(self.tag, self.lon0, self.year, self.day,
self.test)
self.test)

# Get NameList
namelist_file = open(model_path + 'sami2py-1.00.namelist')
Expand Down Expand Up @@ -185,7 +186,7 @@ def _load_model(self):
ti = np.loadtxt(path.join(model_path, 'tif.dat'))
te = np.loadtxt(path.join(model_path, 'tef.dat'))

#get neutral values
# get neutral values
if self.outn:
denn = np.loadtxt(model_path+'dennf.dat')
u = np.loadtxt(model_path+'u4f.dat')
Expand All @@ -204,6 +205,8 @@ def _load_model(self):
dim0=dim0, dim1=dim1, reshape=True)
ti = get_unformatted_data(model_path, 'ti',
dim0=dim0, dim1=dim1, reshape=True)
# Temperatures have only one species
dim0 = nz*nf + 2
te = get_unformatted_data(model_path, 'te',
dim0=dim0, dim1=dim1, reshape=True)

Expand Down
92 changes: 75 additions & 17 deletions sami2py/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Unit tests for run_model.py
"""
import os
import filecmp
import numpy as np
import os
import shutil
from nose.tools import raises
import sami2py
Expand All @@ -15,42 +16,68 @@ def setup(self):
"""Setup function run before each test method to setup files needed
to run the test effectively
"""
self.format = True
self.ref_file = 'ref_f_sami2py-1.00.namelist'
self.model_path = generate_path('test', 0, 2012, 211, True)
if not os.path.exists(self.model_path):
os.makedirs(self.model_path)
filelist_fmt = ['glonf.dat', 'glatf.dat', 'zaltf.dat',
'denif.dat', 'vsif.dat', 'tif.dat', 'tef.dat',
'time.dat']
for filename in filelist_fmt:
open(fortran_dir + '/' + filename, 'w').close()
self.filelist = ['glonf.dat', 'glatf.dat', 'zaltf.dat', 'denif.dat',
'vsif.dat', 'tif.dat', 'tef.dat', 'time.dat']
for filename in self.filelist:
open(os.path.join(fortran_dir, filename), 'w').close()

def teardown(self):
"""Teardown function run before each test method to remove files needed
to run the test effectively
"""
filelist_fmt = ['glonf.dat', 'glatf.dat', 'zaltf.dat',
'denif.dat', 'vsif.dat', 'tif.dat', 'tef.dat',
'time.dat']
for filename in filelist_fmt:
os.remove(fortran_dir + '/' + filename)
for filename in self.filelist:
os.remove(os.path.join(fortran_dir, filename))
if os.path.exists(self.model_path):
path_to_remove = os.path.split(self.model_path)[0]
path_to_remove = os.path.split(path_to_remove)[0]
shutil.rmtree(path_to_remove)
del self.format, self.ref_file, self.model_path, self.filelist

def test_run_model_namelist(self):
"""The test to ensure that the namelist file is generated properly
"""
sami2py.run_model(year=2012, day=211, test=True)
sami2py.run_model(year=2012, day=211, test=True, fmtout=self.format)
namelist_file = self.model_path + 'sami2py-1.00.namelist'
ref_namelist = os.path.join(test_data_dir, self.ref_file)
assert filecmp.cmp(namelist_file, ref_namelist)

def test_run_model_namelist_w_invalid_hwm(self):
"""The test to ensure that the invalid hwm reverts to 14
"""
sami2py.run_model(year=2012, day=211, test=True, fmtout=self.format,
hwm_model=15)
namelist_file = self.model_path + 'sami2py-1.00.namelist'
ref_namelist = test_data_dir + '/reference_sami2py-1.00.namelist'
ref_namelist = os.path.join(test_data_dir, self.ref_file)
assert filecmp.cmp(namelist_file, ref_namelist)

def test_run_model_dat_files(self):
"""Test to ensure that the dat files are copied properly
"""
sami2py.run_model(year=2012, day=211, test=True)
assert os.stat(self.model_path + 'glonf.dat')
sami2py.run_model(year=2012, day=211, test=True, fmtout=self.format)
if self.format:
fname = 'glonf.dat'
else:
fname = 'glonu.dat'
assert os.stat(self.model_path + fname)

def test_run_model_ExB_files(self):
"""Test to ensure that the ExB files are copied properly
"""
sami2py.run_model(year=2012, day=211, test=True, fmtout=self.format,
fejer=False, ExB_drifts=np.zeros((10, 2)))
assert os.stat(self.model_path + 'exb.inp')

@raises(Exception)
def test_run_model_ExB_wrong_size(self):
"""Test to ensure that the ExB has proper shape
"""
sami2py.run_model(year=2012, day=211, test=True, fmtout=self.format,
fejer=False, ExB_drifts=np.zeros((1, 2)))

@raises(ValueError)
def test_input_format(self):
Expand All @@ -59,19 +86,50 @@ def test_input_format(self):
create the file name. Will happen for any variable in the namelist
set with the wrong type
"""
sami2py.run_model(year='2012', day='211', test=True)
sami2py.run_model(year='2012', day='211', test=True,
fmtout=self.format)

def test_fortran_executable(self):
"""Short run of fortran executable to ensure the code compiles
and runs
"""
tmp_archive_dir = sami2py.archive_dir
sami2py.utils.set_archive_dir(path=test_data_dir)
sami2py.run_model(year=2012, day=211,
sami2py.run_model(year=2012, day=211, fmtout=self.format,
dthr=0.05, hrinit=0.0, hrpr=0.0, hrmax=.11)
if os.path.isdir(tmp_archive_dir):
sami2py.utils.set_archive_dir(path=tmp_archive_dir)
else:
with open(sami2py.archive_path, 'w') as archive_file:
archive_file.write('')
sami2py.archive_dir = ''


class TestBasicModelRunUnformatted(TestBasicModelRun):
"""Basic tests of the run_model script w/ unformatted output"""

def setup(self):
"""Setup function run before each test method to setup files needed
to run the test effectively
"""
self.format = False
self.ref_file = 'ref_u_sami2py-1.00.namelist'
self.model_path = generate_path('test', 0, 2012, 211, True)
if not os.path.exists(self.model_path):
os.makedirs(self.model_path)
self.filelist = ['glonu.dat', 'glatu.dat', 'zaltu.dat', 'deniu.dat',
'vsiu.dat', 'tiu.dat', 'teu.dat', 'time.dat']
for filename in self.filelist:
open(os.path.join(fortran_dir, filename), 'w').close()

def teardown(self):
"""Teardown function run before each test method to remove files needed
to run the test effectively
"""
for filename in self.filelist:
os.remove(os.path.join(fortran_dir, filename))
if os.path.exists(self.model_path):
path_to_remove = os.path.split(self.model_path)[0]
path_to_remove = os.path.split(path_to_remove)[0]
shutil.rmtree(path_to_remove)
del self.format, self.ref_file, self.model_path, self.filelist
16 changes: 14 additions & 2 deletions sami2py/tests/test_core_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import os
import sys
import sami2py
from sami2py.utils import generate_path
import numpy as np
from nose.tools import raises


Expand Down Expand Up @@ -44,6 +42,13 @@ def test_model_instantiation(self):
test=True)
assert isinstance(model, sami2py.Model)

def test_model_instantiation_with_unformatted_files_and_mods(self):
"""Test that model object is instantiated as a sami2py_model
"""
model = sami2py.Model(tag='test', lon=256, year=1999, day=257,
test=True)
assert isinstance(model, sami2py.Model)

def test_model_plot(self):
"""Basic test that a reasonable plot has been created by testing the
resulting axis limits
Expand All @@ -69,3 +74,10 @@ def test_check_standard_model(self):
test=True)
keys = model.check_standard_model()
assert keys == list()

def test_model_repr(self):
"""Test that __repr__ returns a string of information."""
model = sami2py.Model(tag='test', lon=256, year=1999, day=256,
test=True)
repr_str = model.__repr__()
assert type(repr_str) is str
38 changes: 38 additions & 0 deletions sami2py/tests/test_data/ref_u_sami2py-1.00.namelist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
&go
fmtout = .false.,
maxstep = 100000000,
hrmax = 48.000000,
dt0 = 30.000000,
dthr = 0.250000,
hrpr = 24.000000,
grad_in = 300.000000,
glat_in = 0.000000,
glon_in = 0.000000,
fejer = .true.,
rmin = 100.000000,
rmax = 2000.000000,
altmin = 85.000000,
fbar = 120.000000,
f10p7 = 120.000000,
ap = 0,
year = 2012,
day = 211,
mmass = 48,
nion1 = 1,
nion2 = 7,
hrinit = 0.000000,
tvn0 = 1.000000,
tvexb0 = 1.000000,
ve01 = 0.000000,
gams = 3,
gamp = 3,
snn = 1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,
stn = 1.000000,
denmin = 1.000000e-06,
alt_crit = 150.000000,
cqe = 7.000000e-14,
Tinf_scl = 1.000000,
euv_scl = 1.000000,
hwm_mod = 14,
outn = .false.
&end
10 changes: 10 additions & 0 deletions sami2py/tests/test_data/test/lon256/1999_257/exb.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-2.256592115245508268e+01 -1.700658929016086063e+01
6.970157155494434598e+00 -1.785773092653965932e+01
7.715990594846469097e-01 -1.457794556537672204e+00
1.133009384321719537e+01 6.217447257350539047e+00
5.315180719049175551e+00 6.543500525980066485e+00
-4.754135936133724094e+00 7.493698025175492994e+00
-3.036451049580922046e+00 -1.789034697779917638e+00
-3.791099765373269648e+00 -8.665409647917849956e-01
2.083139968647748308e+00 -3.636326810220660732e+00
2.205828847907577828e+00 0.000000000000000000e+00
Loading

0 comments on commit 096e7f9

Please sign in to comment.