Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to main PSG class #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions pypsg/psg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import requests
from pkg_resources import resource_string
from io import StringIO
Expand All @@ -8,11 +9,17 @@


class PSG():
def __init__(self, server_url='https://psg.gsfc.nasa.gov/api.php', timeout_seconds=10, api_key=None):
def __init__(self, server_url='https://psg.gsfc.nasa.gov/api.php',
timeout_seconds=10, api_key=None,
default_config='resources/default.config'):
self._server_url = server_url
self._timeout_seconds = timeout_seconds
self._api_key = api_key
self.default_config_str = resource_string(__name__, 'resources/default.config').decode('utf-8')
if os.path.isabs(default_config):
with open(default_config, "r") as foo:
self.default_config_str = foo.read()
else:
self.default_config_str = resource_string(__name__, default_config).decode('utf-8')
self.default_config = self.config_str_to_dict(self.default_config_str)

print('Testing connection to PSG at {} ...'.format(self._server_url))
Expand Down Expand Up @@ -45,7 +52,7 @@ def config_dict_to_str(config_dict):
ret.append('<{}>{}'.format(key, str(value)))
return '\n'.join(ret)

def run(self, config=None, config_str=None):
def run(self, config=None, config_str=None, watm=None, otype=None):
if config_str is None:
if config is None:
raise ValueError('Expecting either config or config_str.')
Expand All @@ -56,6 +63,10 @@ def run(self, config=None, config_str=None):
data = {'file': config_str}
if self._api_key is not None:
data['key'] = self._api_key
if watm is not None:
data['watm'] = watm
if otype is not None:
data['type'] = otype
reply = requests.post(self._server_url, data=data, timeout=self._timeout_seconds)
time_duration = time.time() - time_start
if reply.status_code == requests.codes.ok:
Expand All @@ -72,6 +83,10 @@ def run(self, config=None, config_str=None):
reply_data.append(line)

reply_header_str = '\n'.join(reply_header)
reply_data_np = np.loadtxt(StringIO('\n'.join(reply_data)))
try:
reply_data_np = np.loadtxt(StringIO('\n'.join(reply_data)))
return {'header': reply_header_str, 'spectrum': reply_data_np, 'duration_seconds': time_duration}
except:
return {'config': reply_raw}


return {'header': reply_header_str, 'spectrum': reply_data_np, 'duration_seconds': time_duration}