Coverage for tests/test_astrometry.py: 23%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

54 statements  

1from numpy.testing import run_module_suite 

2 

3from spectractor import parameters 

4from spectractor.astrometry import Astrometry 

5from spectractor.logbook import LogBook 

6from spectractor.config import load_config 

7from spectractor.tools import set_wcs_output_directory, set_wcs_file_name 

8from spectractor.extractor.images import Image, find_target 

9import os 

10import subprocess 

11import numpy as np 

12import unittest 

13 

14 

15# TODO: DM-33441 Fix broken spectractor tests 

16@unittest.skip('Skipping test for LSST testing framework') 

17def test_astrometry(): 

18 file_names = ['tests/data/reduc_20170530_134.fits'] # 'tests/data/reduc_20170605_028.fits'] 

19 

20 load_config('./config/ctio.ini') 

21 logbook = LogBook(logbook='./ctiofulllogbook_jun2017_v5.csv') 

22 parameters.VERBOSE = True 

23 parameters.DEBUG = True 

24 

25 radius = 500 

26 maxiter = 10 

27 

28 for file_name in file_names: 

29 wcs_output_directory = set_wcs_output_directory(file_name) 

30 if os.path.isdir(wcs_output_directory): 

31 subprocess.check_output(f"rm -rf {wcs_output_directory}", shell=True) 

32 tag = file_name.split('/')[-1].replace('sim', 'reduc') 

33 disperser_label, target, xpos, ypos = logbook.search_for_image(tag) 

34 if target is None or xpos is None or ypos is None: 

35 continue 

36 a = Astrometry(file_name, target, disperser_label) 

37 extent = ((int(max(0, xpos - radius)), int(min(xpos + radius, parameters.CCD_IMSIZE))), 

38 (int(max(0, ypos - radius)), int(min(ypos + radius, parameters.CCD_IMSIZE)))) 

39 gaia_min_residuals = a.run_full_astrometry(extent=extent, maxiter=maxiter) 

40 # checks 

41 assert os.path.isdir(wcs_output_directory) 

42 assert os.path.isfile(set_wcs_file_name(file_name)) 

43 assert a.data is not None 

44 assert np.sum(a.data) > 1e-10 

45 assert gaia_min_residuals < 0.8 

46 # assert np.all(np.abs([dra_median, ddec_median]) < 1e-3) 

47 if file_name == 'tests/data/reduc_20170605_028.fits': 

48 assert len(a.sources) > 200 

49 assert np.isclose(a.target_radec_position_after_pm.ra.value, 224.97283917) 

50 assert np.isclose(a.target_radec_position_after_pm.dec.value, -54.30209) 

51 a.my_logger.warning(f"{a.wcs.wcs.crval}") 

52 assert np.isclose(a.wcs.wcs.crval[0], 224.9718998, atol=0.03) 

53 assert np.isclose(a.wcs.wcs.crval[1], -54.28912925, atol=0.03) 

54 if file_name == 'tests/data/sim_20170530_134.fits': 

55 im = Image(file_name, target_label=target) 

56 parameters.SPECTRACTOR_FIT_TARGET_CENTROID = "WCS" 

57 x0_wcs, y0_wcs = find_target(im, guess=[xpos, ypos], rotated=False) 

58 parameters.SPECTRACTOR_FIT_TARGET_CENTROID = "fit" 

59 x0, y0 = find_target(im, guess=[xpos, ypos], rotated=False) 

60 im.my_logger.warning(f"\n\tTrue {target} position: " 

61 f"{np.array([float(im.header['X0_T']), float(im.header['Y0_T'])])}" 

62 f"\n\tFound {target} position with WCS: {np.array([x0_wcs, y0_wcs])}" 

63 f"\n\tFound {target} position with 2D fit: {np.array([x0, y0])}") 

64 assert np.abs(x0_wcs - float(im.header['X0_T'])) < 0.5 

65 assert np.abs(y0_wcs - float(im.header['Y0_T'])) < 1 

66 

67 

68if __name__ == "__main__": 68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never true

69 run_module_suite()