Commit c68c1b0e authored by Carl Schaffer's avatar Carl Schaffer
Browse files

improving wavelength retrieval from filename, adding test

parent ce2c3409
......@@ -10,12 +10,12 @@ import yaml
from astropy.io import fits
from sunpy.coordinates.sun import earth_distance
from kis_tools.bbi.util import get_bbi_wl
from kis_tools.generic.settings import settings
from kis_tools.bbi.util import get_wl
from kis_tools.generic.settings.settings import get_settings
from kis_tools.interface import BasicArgParser
from kis_tools.util.util import setup_outdir
from .bbi_sort_observations import get_obs_info
from kis_tools.generic.settings.settings import get_settings
from ..util.util import add_history, date_from_fn
from ..util.util import get_filter
......@@ -412,7 +412,7 @@ def run_bbi_header_conversion(args):
new_header = convert_bbi_header(
infile,
)
wl = get_wl(new_header["FILENAME"])
wl = get_bbi_wl(new_header["FILENAME"])
out_dir = join(out_folder, new_header["POINT_ID"] + f"_WL{wl}")
setup_outdir(out_dir)
......
......@@ -4,7 +4,7 @@ from itertools import groupby
from os.path import join, basename
from pprint import pprint
from kis_tools.bbi.util import get_wl
from kis_tools.bbi.util import get_bbi_wl
from kis_tools.util.util import merge_to_gif, gif_to_mp4
......@@ -18,8 +18,8 @@ def bbi_gen_gif(folder):
if not files:
continue
files.sort(key=get_wl)
grouped = groupby(files, get_wl)
files.sort(key=get_bbi_wl)
grouped = groupby(files, get_bbi_wl)
for group, grouped_files in grouped:
grouped_files = list(grouped_files)
......
import re
def get_wl(fn):
"""Extract wavelength from string"""
return int(re.search(r"WL(\d+)", fn).group(1))
class NoWavelengthError(Exception):
pass
def get_bbi_wl(fn):
"""Extract wavelength from string formatted as WL656 indicating 656nm wavelenght.
Returns: wavelength in nm
"""
match = re.search(r"WL(\d+)", fn)
if not match:
raise NoWavelengthError(f"Could not determine wavelength from {fn}, expeced a wavelength format like 'WLXXX'")
return int(match.group(1))
......@@ -9,6 +9,7 @@ from pprint import pprint
from unittest import TestCase
from kis_tools.bbi.bbi_header import get_generic_header
from kis_tools.bbi.util import get_bbi_wl, NoWavelengthError
from kis_tools.generic.settings.settings import get_settings
......@@ -21,3 +22,11 @@ class TestBBI(TestCase):
def test_bbi_header(self):
header = get_generic_header()
pprint(header)
def test_get_wl(self):
working_teststrings = ["20170522_003_WL854", "bbi_20170523-092204_001_WL589_location_preview.png", ]
for ts in working_teststrings:
self.assertIsInstance(get_bbi_wl(ts), int)
error_teststrings = ["20170522_003_WLABC", "bbi_20170523-092204_001_location_preview.png", ]
for ts in error_teststrings:
self.assertRaises(NoWavelengthError, get_bbi_wl, ts)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment