Skip to content

Commit

Permalink
adding frames example test
Browse files Browse the repository at this point in the history
  • Loading branch information
havok2063 committed Nov 27, 2023
1 parent 893b384 commit 8950cc6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
36 changes: 35 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import importlib

import pandas as pd
from astropy.io import fits

"""
Here you can add fixtures that will be used for all the tests in this
Expand All @@ -36,15 +37,19 @@ def mock_sas(tmp_path_factory, session_mocker):
os.environ = os_copy


@pytest.fixture()
@pytest.fixture(scope='module')
def datadir():
""" fixture factory to create empty data directories for a list of mjds """

path = pathlib.Path(os.getenv("LVM_DATA_S"))
path.mkdir(parents=True, exist_ok=True)
outs = []

def _datadir(mjds):
for mjd in mjds:
(path / f'{mjd}').mkdir(exist_ok=True)
outs.append(path / f'{mjd}')
return outs
yield _datadir


Expand Down Expand Up @@ -90,3 +95,32 @@ def multimeta(make_multi):
""" fixture to create a metadata frame with multiple exposures """
yield make_multi(expnum=[6818, 6819])


def create_fake_fits(path, tileid=1111, mjd=61234, expnum=6817, cameras=None):
""" create a fake raw frame FITS file """
out = {}
cameras = cameras or ['b1']
for cam in cameras:
# create fake header
filename = f'sdR-s-{cam}-{expnum:0>8}.fits.gz'
hdr = {'TILE_ID': tileid, 'MJD': mjd, 'EXPOSURE': expnum, "CCD": cam,
'EXPTIME': 900.0,
'IMAGETYP': 'object', 'FILENAME': filename, 'SPEC': f'sp{cam[-1]}',
'OBSTIME': '2023-10-18T07:56:23.289', 'OBSERVAT': 'LCO',
'TELESCOP': 'SDSS 0.16m', 'SURVEY': 'LVM'}
prim = fits.PrimaryHDU(header=fits.Header(hdr))

# create fake file
full = path / filename
hdulist = fits.HDUList(prim)
hdulist.writeto(full)
out[cam] = full
return out


@pytest.fixture(scope='module')
def make_fits(datadir):
""" fixture to create fake fits files """
mjd = 61234
paths = datadir([mjd])
create_fake_fits(paths[0], mjd=mjd, cameras=['b1', 'b2', 'b3'])
29 changes: 29 additions & 0 deletions tests/utils/test_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# encoding: utf-8
#

import importlib
import pytest

import lvmdrp.utils.metadata
from lvmdrp.utils.metadata import get_frames_metadata


@pytest.fixture(autouse=True)
def mock_meta():
""" fixture to reload the metadata module
This is so the global METADATA_PATH variable
is using the correct test paths
"""
importlib.reload(lvmdrp.utils.metadata)


def test_get_frames_metadata(make_fits):
""" test we can extract metadata from a fits file """
meta = get_frames_metadata(61234)
assert len(meta) == 3
assert 61234 in meta['mjd'].unique()
assert 1111 in meta['tileid'].unique()
assert 6817 in meta['expnum'].unique()
assert meta.iloc[0]['name'] == 'sdR-s-b1-00006817.fits'
assert set(meta['camera']) == {'b1', 'b2', 'b3'}

0 comments on commit 8950cc6

Please sign in to comment.