Skip to content

Commit

Permalink
de-flake
Browse files Browse the repository at this point in the history
  • Loading branch information
pothiers committed Apr 17, 2020
1 parent 1a11d8f commit 6f20415
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 70 deletions.
39 changes: 5 additions & 34 deletions astroquery/noirlab/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
This does DB access through web-services.
"""
import json
import astropy.io.fits as pyfits
import astropy.table
from ..query import BaseQuery
from ..utils import async_to_sync
from ..utils.class_or_instance import class_or_instance
from . import conf
#!import requests


__all__ = ['Noirlab', 'NoirlabClass'] # specifies what to import
Expand All @@ -32,7 +30,7 @@ def __init__(self, which='file'):
"""
self._api_version = None
self._adsurl = f'{self.NAT_URL}/api/adv_search'

if which == 'hdu':
self.siaurl = f'{self.NAT_URL}/api/sia/vohdu'
self._adss_url = f'{self._adsurl}/hasearch'
Expand Down Expand Up @@ -79,7 +77,7 @@ def service_metadata(self, cache=True):
url = f'{self.siaurl}?FORMAT=METADATA&format=json'
response = self._request('GET', url, timeout=self.TIMEOUT, cache=cache)
return response.json()[0]

@class_or_instance
def query_region(self, coordinate, radius=0.1, cache=True):
"""Query for NOIRLab observations by region of the sky.
Expand Down Expand Up @@ -111,7 +109,6 @@ def query_region(self, coordinate, radius=0.1, cache=True):
response.raise_for_status()
return astropy.table.Table(data=response.json())


def core_fields(self, cache=True):
"""List the available CORE fields. CORE fields are faster to search
than AUX fields.."""
Expand All @@ -121,7 +118,6 @@ def core_fields(self, cache=True):
response.raise_for_status()
return response.json()


def aux_fields(self, instrument, proctype, cache=True):
"""List the available AUX fields. AUX fields are ANY fields in the
Archive FITS files that are not core DB fields. These are generally
Expand All @@ -145,19 +141,6 @@ def categoricals(self, cache=True):
response.raise_for_status()
return response.json()

#! @class_or_instance
#! def _query_ads(self, jdata, limit=1000):
#! print(f'DBG-0: ADS jdata={jdata}')
#! adsurl = f'{self.adsurl}/?limit={limit}'
#! print(f'DBG-0: adsurl = {adsurl}')
#! # Following fails
#! # #! response = self._request('POST',adsurl, data=json.dumps(jdata))
#! response = requests.post(adsurl, json=jdata)
#! print(f'DBG-0: ADS response={response}')
#! print(f'DBG-0: ADS response.content={response.content}')
#! print(f'DBG-0: ADS response.json()={response.json()}')
#! return astropy.table.Table(data=response.json())

@class_or_instance
def query_metadata(self, qspec, limit=1000, cache=True):
self._validate_version()
Expand All @@ -168,22 +151,9 @@ def query_metadata(self, qspec, limit=1000, cache=True):
else:
jdata = qspec

print(f'DBG-0: query_metadata.url = {url}')
# headers = {'accept': 'application/json'}
# headers = {'Content-Type': 'application/json'}

# Following fails:
response = self._request('POST', url, json=jdata, timeout=self.TIMEOUT,)

#! response = requests.post(url,
#! timeout=self.TIMEOUT,
#! json=jdata)
response = self._request('POST', url, json=jdata, timeout=self.TIMEOUT)
response.raise_for_status()
# #!print(f'DBG-0: ADS response={response}')
# #!print(f'DBG-0: ADS response.content={response.content}')
# #!print(f'DBG-0: ADS response.json()={response.json()}')
return astropy.table.Table(rows=response.json())
#return response.json() #@@@ Should return table

def retrieve(self, fileid, cache=True):
url = f'{self.NAT_URL}/api/retrieve/{fileid}/'
Expand All @@ -203,5 +173,6 @@ def get_token(self, email, password, cache=True):
timeout=self.TIMEOUT)
response.raise_for_status()
return response.json()



Noirlab = NoirlabClass()
63 changes: 27 additions & 36 deletions astroquery/noirlab/tests/test_noirlab_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.tests.helper import remote_data
import astropy.io.fits as pyfits
# Local packages
from .. import Noirlab
from . import expected as exp
# #!import pytest

# performs similar tests as test_module.py, but performs
# the actual HTTP request rather than monkeypatching them.
Expand All @@ -20,9 +18,9 @@
@remote_data
class TestNoirlabClass(object):

###############################################################
### (2) SIA; /api/sia/
###
# ###############################################################
# ### (2) SIA; /api/sia/
# ###
# voimg, vohdu

def test_service_metadata(self):
Expand All @@ -32,7 +30,7 @@ def test_service_metadata(self):
print(f'DBG: test_service_metadata={actual}')
expected = exp.service_metadata
assert actual == expected

def test_query_region_0(self):
"""Search FILES using default type (which) selector"""

Expand Down Expand Up @@ -64,19 +62,19 @@ def test_query_region_2(self):
expected = exp.query_region_2
assert expected.issubset(actual)

###############################################################
### (7) Advanced Search; /api/adv_search/
###
# ###############################################################
# ### (7) Advanced Search; /api/adv_search/
# ###
#
# (2) aux_{file,hdu}_fields/<instrument>/<proctype>
# (2) core_{file,hdu}_fields/
# [(2) {f,h}adoc JUST LINK to these]
# (2) {f,h}asearch
# cat_list

##
## File (default type)
##
# ##
# ## File (default type)
# ##

def test_aux_file_fields(self):
"""List the available AUX FILE fields."""
Expand All @@ -89,36 +87,35 @@ def test_aux_file_fields(self):
def test_core_file_fields(self):
"""List the available CORE FILE fields."""
r = Noirlab().core_fields()
actual = r # set(list(r['md5sum']))
actual = r
print(f'DBG: test_core_file_fields={actual}')
expected = exp.core_file_fields
assert actual == expected

def test_query_file_metadata(self):
"""Search FILE metadata."""
qspec = {
"outfields" : [
"outfields": [
"md5sum",
"archive_filename",
"original_filename",
"instrument",
"proc_type"
],
"search" : [
"search": [
['original_filename', 'c4d_', 'contains']
]
}

r = Noirlab().query_metadata(qspec, limit=3)
actual = r # set(list(r['md5sum']))
actual = r
print(f'DBG: test_query_file_metadata={actual.pformat_all()}')
expected = exp.query_file_metadata
assert actual.pformat_all() == expected


##
## HDU
##
# ##
# ## HDU
# ##

def test_aux_hdu_fields(self):
"""List the available AUX HDU fields."""
Expand All @@ -131,50 +128,48 @@ def test_aux_hdu_fields(self):
def test_core_hdu_fields(self):
"""List the available CORE HDU fields."""
r = Noirlab(which='hdu').core_fields()
actual = r # set(list(r['md5sum']))
actual = r
print(f'DBG: test_core_hdu_fields={actual}')
expected = exp.core_hdu_fields
assert actual == expected

def test_query_hdu_metadata(self):
"""Search HDU metadata."""
qspec = {
"outfields" : [
"outfields": [
"fitsfile__archive_filename",
"fitsfile__caldat",
"fitsfile__instrument",
"fitsfile__proc_type",
"AIRMASS" # AUX field. Slows search
],
"search" : [
"search": [
["fitsfile__caldat", "2017-08-14", "2017-08-16"],
["fitsfile__instrument", "decam"],
["fitsfile__proc_type", "raw"]
]
}

r = Noirlab(which='hdu').query_metadata(qspec, limit=3)
actual = r # set(list(r['md5sum']))
actual = r
print(f'DBG: test_query_hdu_metadata={actual.pformat_all()}')
expected = exp.query_hdu_metadata
assert actual.pformat_all() == expected

##
## Agnostic
##
# ##
# ## Agnostic
# ##

def test_categoricals(self):
"""List categories."""
r = Noirlab().categoricals()
actual = r # set(list(r['md5sum']))
actual = r
print(f'DBG: test_categoricals={actual}')
expected = exp.categoricals
assert actual == expected



###############################################################
### (3) Other
# ##############################################################
# ### (3) Other
# get_token
# retrieve/<md5>
# version
Expand All @@ -186,7 +181,6 @@ def test_retrieve(self):
expected = exp.retrieve
assert actual == expected


def test_version(self):
r = Noirlab().version()
assert r < 3.0
Expand All @@ -197,6 +191,3 @@ def test_get_token(self):
'No active account found with the given credentials'}
print(f'DBG: test_get_token={actual}')
assert actual == expected



0 comments on commit 6f20415

Please sign in to comment.