Skip to content

Commit

Permalink
pep8 /typehint, matlab template
Browse files Browse the repository at this point in the history
unify api -- auto or manual geomag indices

make CI cover more by default

add azure

add octave CI

fix indices for test
  • Loading branch information
scivision committed Jul 21, 2019
1 parent 1154ebc commit 0a03b84
Show file tree
Hide file tree
Showing 20 changed files with 260 additions and 139 deletions.
4 changes: 2 additions & 2 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
image:
- Visual Studio 2017
- ubuntu1804
#- ubuntu1804 # bad xarray appveyor

stack: python 3.7
stack: python 3

environment:
MINGW_DIR: C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ matrix:
python: 3.7
install:
- python setup.py install
- pip install $(basename $TRAVIS_REPO_SLUG)[tests,full]
- pip install $(basename $TRAVIS_REPO_SLUG)[tests]
script:
- cd $HOME
- python -m pytest $TRAVIS_BUILD_DIR/tests -r a -v
Expand All @@ -33,6 +33,7 @@ matrix:
script:
- flake8
- mypy .
- pytest -r a -v
after_success:
- pytest --cov
- coveralls
Expand Down
11 changes: 6 additions & 5 deletions MSISE00.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
try:
from matplotlib.pyplot import show
import msise00.plots as msplots
import seaborn as sns
sns.set_style('ticks')
except ImportError as e:
print(e)
msplots = None # type: ignore
try:
import seaborn as sns
sns.set_style('ticks')
except ImportError:
pass


def main():
p = ArgumentParser(description='calls MSISE-00 from Python, save to NetCDF4 and/or plot')
p.add_argument('-t', '--time',
help='time: (single time or START STOP (1 hour time step) or list of times)',
nargs='+', required=True)
p.add_argument('-t', '--time', help='time or times', nargs='+', required=True)
p.add_argument('-a', '--altkm', help='altitude (km). scalar, or (start,stop,step) or list of alts.',
type=float, nargs='+', required=True)
p.add_argument('-c', '--latlon', help='geodetic latitude/longitude (deg)',
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Zenodo DOI](https://zenodo.org/badge/32971905.svg)](https://zenodo.org/badge/latestdoi/32971905)

[![Build Status](https://dev.azure.com/mhirsch0512/MSISE00/_apis/build/status/space-physics.msise00?branchName=master)](https://dev.azure.com/mhirsch0512/MSISE00/_build/latest?definitionId=5&branchName=master)
[![Build Status](https://travis-ci.com/space-physics/msise00.svg?branch=master)](https://travis-ci.com/space-physics/msise00)
[![Coverage Status](https://coveralls.io/repos/github/space-physics/msise00/badge.svg?branch=master)](https://coveralls.io/github/space-physics/msise00?branch=master)
[![Build status](https://ci.appveyor.com/api/projects/status/oje6aax4lx4tcryt?svg=true)](https://ci.appveyor.com/project/scivision/msise00)
Expand Down Expand Up @@ -115,13 +116,6 @@ Matlab and GNU Octave users can seamlessly access Python modules, as demonstrate
runtests('tests')
```

### Time Utiliies

Time utilities of general interest include:

* `msise00.todatetime`
* `msise00.todt64`

## Fortran source

The MSISE00 Fortran source code may also be used directly.
Expand Down
65 changes: 65 additions & 0 deletions azure-pipelines.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
jobs:

- job: Ubuntu
pool:
vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.6'
addToPath: true
- script: sudo apt install -yq --no-install-recommends gfortran
displayName: 'Install Prereqs'
- script: pip install -e .[tests]
displayName: 'Build Python'
- script: pytest -r a -v
displayName: 'PyTest'

- job: MacOS
pool:
vmImage: macOS-10.14

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.6'
addToPath: true
- script: brew install gcc
displayName: 'Install Prereqs'
- script: pip install -e .[tests]
displayName: 'Build Python'
- script: pytest -r a -v
displayName: 'PyTest'

- job: MacOS
pool:
vmImage: macOS-10.14

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.6'
addToPath: true
- script: brew install gcc octave
displayName: 'Install Prereqs'
- script: pip install -e .[tests]
displayName: 'Build Python'
- script: pytest -r a -v
displayName: 'PyTest'

- job: WindowsMinGW
pool:
vmImage: windows-2019

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.6'
addToPath: true
- script: cinst -y mingw
displayName: 'Install Prereqs'
- script: pip install -e .[tests]
displayName: 'Build Python'
- script: pytest -r a -v
displayName: 'PyTest'
65 changes: 65 additions & 0 deletions matlab/build.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function build(build_sys, srcdir, builddir)
% build with 'meson' or 'cmake'

validateattributes(build_sys, {'char'}, {'vector'})

if nargin < 3
cwd = fileparts(mfilename('fullpath'));
srcdir = [cwd, filesep,'..', filesep, 'src'];
builddir = [cwd, filesep,'..',filesep,'build'];
end

assert(exist(srcdir,'dir')==7, ['source directory ',srcdir,' does not exist'])
assert(exist(builddir,'dir')==7, ['build directory ',builddir,' does not exist'])

switch build_sys
case 'meson', build_meson(srcdir, builddir)
case 'cmake', build_cmake(srcdir, builddir)
otherwise error('Specifiy "meson" or "cmake" to build this project.')
end

end


function build_cmake(srcdir, builddir)
%% build using CMake

validateattributes(srcdir,{'char'},{'vector'})
validateattributes(builddir,{'char'},{'vector'})

runcmd('cmake --version')

if ispc
wopts = '-G "MinGW Makefiles" -DCMAKE_SH="CMAKE_SH-NOTFOUND"';
else
wopts = '';
end

ccmd = ['cmake ', wopts, ' -S ', srcdir, ' -B ', builddir];

runcmd(ccmd)

runcmd(['cmake --build ',builddir, ' --parallel'])

end


function build_meson(srcdir, builddir)
%% build using Meson + Ninja

validateattributes(srcdir,{'char'},{'vector'})
validateattributes(builddir,{'char'},{'vector'})

disp('Meson: ')
runcmd('meson --version')

disp('Ninja: ')
runcmd('ninja --version')

if ~exist([builddir,filesep,'build.ninja'], 'file')
runcmd(['meson setup ',builddir,' ',srcdir])
end

runcmd(['ninja -C ',builddir])

end
1 change: 0 additions & 1 deletion matlab/runcmd.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ function runcmd(cmd)
[status, ret] = system(cmd);
if status~=0, error(ret), end
disp(ret)

end
15 changes: 0 additions & 15 deletions matlab/setup.m

This file was deleted.

22 changes: 0 additions & 22 deletions matlab/setup_cmake.m

This file was deleted.

20 changes: 0 additions & 20 deletions matlab/setup_meson.m

This file was deleted.

60 changes: 32 additions & 28 deletions msise00/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import xarray
import numpy as np
import subprocess
from typing import Union, List
import typing
from pathlib import Path
import io
import shutil
Expand All @@ -31,30 +31,31 @@
first = True


def run(time: datetime, altkm: float,
glat: Union[float, np.ndarray], glon: Union[float, np.ndarray], *,
f107a: float = None, f107: float = None, Ap: int = None) -> xarray.Dataset:
def run(time: datetime,
altkm: float,
glat: typing.Union[float, np.ndarray],
glon: typing.Union[float, np.ndarray],
indices: typing.Dict[str, typing.Any] = {}) -> xarray.Dataset:
"""
loops the rungtd1d function below. Figure it's easier to troubleshoot in Python than Fortran.
"""
glat = np.atleast_2d(glat)
glon = np.atleast_2d(glon) # has to be here
# %% altitude 1-D
if glat.size == 1 and glon.size == 1 and isinstance(time, (str, date, datetime, np.datetime64)):
atmos = rungtd1d(time, altkm, glat.squeeze()[()], glon.squeeze()[()],
f107a=f107a, f107=f107, Ap=Ap)
atmos = rungtd1d(time, altkm, glat.squeeze()[()], glon.squeeze()[()], indices)
# %% lat/lon grid at 1 altitude
else:
atmos = loopalt_gtd(time, glat, glon, altkm,
f107a=f107a, f107=f107, Ap=Ap)
atmos = loopalt_gtd(time, glat, glon, altkm, indices)

return atmos


def loopalt_gtd(time: datetime,
glat: Union[float, np.ndarray], glon: Union[float, np.ndarray],
altkm: Union[float, List[float], np.ndarray], *,
f107a: float = None, f107: float = None, Ap: int = None) -> xarray.Dataset:
glat: typing.Union[float, np.ndarray],
glon: typing.Union[float, np.ndarray],
altkm: typing.Union[float, typing.Sequence[float], np.ndarray],
indices: typing.Dict[str, typing.Any] = {}) -> xarray.Dataset:
"""
loop over location and time
Expand All @@ -78,8 +79,7 @@ def loopalt_gtd(time: datetime,
for j in range(glat.shape[1]):
# atmos = xarray.concat((atmos, rungtd1d(t, altkm, glat[i,j], glon[i,j])),
# data_vars='minimal',coords='minimal',dim='lon')
atm = rungtd1d(t, altkm, glat[i, j], glon[i, j],
f107a=f107a, f107=f107, Ap=Ap)
atm = rungtd1d(t, altkm, glat[i, j], glon[i, j], indices)
atmos = xarray.merge((atmos, atm))

atmos.attrs = atm.attrs
Expand All @@ -89,25 +89,24 @@ def loopalt_gtd(time: datetime,

def rungtd1d(time: datetime,
altkm: np.ndarray,
glat: float, glon: float, *,
f107a: float = None, f107: float = None, Ap: int = None) -> xarray.Dataset:
glat: float,
glon: float,
indices: typing.Dict[str, typing.Any] = {}) -> xarray.Dataset:
"""
This is the "atomic" function looped by other functions
"""
time = todatetime(time)
# %% get solar parameters for date
if f107a and f107a and Ap:
pass
else:
f107Ap = gi.getApF107(time, smoothdays=81)
f107a = f107Ap['f107s'].item()
f107 = f107Ap['f107'].item()
Ap = f107Ap['Ap'].item()
if not indices:
indices = gi.getApF107(time, smoothdays=81).squeeze()
# %% dimensions
altkm = np.atleast_1d(altkm)
assert altkm.ndim == 1
assert isinstance(glon, (int, float))
assert isinstance(glat, (int, float))
if altkm.ndim != 1:
raise ValueError('altitude read incorrectly')
if not isinstance(glon, (int, float, np.int32, np.int64)):
raise TypeError('single longitude only')
if not isinstance(glat, (int, float, np.int32, np.int64)):
raise TypeError('single latitude only')

# %%
iyd = time.strftime('%y%j')
Expand All @@ -119,7 +118,10 @@ def rungtd1d(time: datetime,
cmd = [EXE,
iyd, str(time.hour), str(time.minute), str(time.second),
str(glat), str(glon),
str(f107a), str(f107), str(Ap), str(a)]
str(indices['f107s']),
str(indices['f107']),
str(indices['Ap']),
str(a)]

ret = subprocess.check_output(cmd,
universal_newlines=True,
Expand All @@ -134,7 +136,9 @@ def rungtd1d(time: datetime,

atmos = xarray.Dataset(dsf,
coords={'time': [time], 'alt_km': altkm, 'lat': [glat], 'lon': [glon], },
attrs={'Ap': Ap, 'f107': f107, 'f107a': f107a,
'species': species})
attrs={'species': species,
'f107s': indices['f107s'],
'f107': indices['f107'],
'Ap': indices['Ap']})

return atmos

0 comments on commit 0a03b84

Please sign in to comment.