Skip to content

Commit a3b35cc

Browse files
Merge pull request #6 from martinkilbinger/cfis
cfis module added
2 parents aa7da86 + 3617a16 commit a3b35cc

4 files changed

Lines changed: 245 additions & 2 deletions

File tree

cs_util/cat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
:Description: This script contains methods to read and write galaxy catalogues.
66
7-
:Author: Martin Kilbinger
7+
:Author: Martin Kilbinger <martin.kilbinger@cea.fr>
88
99
"""
1010

cs_util/cfis.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""CFIS.
2+
3+
:Name: cfis.py
4+
5+
:Description: This script contains CFIS-specific methods.
6+
7+
:Author: Martin Kilbinger <martin.kilbinger@cea.fr>
8+
9+
"""
10+
11+
12+
import re
13+
import numpy as np
14+
from astropy import units
15+
from astropy import coordinates as coords
16+
17+
18+
class Cfis(object):
19+
"""Cfis
20+
21+
Class for CFIS image properties.
22+
23+
"""
24+
size = {'tile' : 0.5 * units.deg}
25+
26+
27+
def get_tile_number(tile_name):
28+
"""Get Tile Number.
29+
30+
Return tile number of given image tile name.
31+
32+
Parameters
33+
----------
34+
tile_name : str
35+
tile name
36+
37+
Raises
38+
------
39+
ValueError
40+
if tile name does not match expected pipeline numbering scheme
41+
42+
Returns
43+
-------
44+
tuple
45+
tile number for x and tile number for y
46+
47+
"""
48+
m = re.search(r'(\d{3})[\.-](\d{3})', tile_name)
49+
if m is None or len(m.groups()) != 2:
50+
raise ValueError(
51+
f'Image name \'{tile_name}\' does not match tile name syntax'
52+
)
53+
54+
nix = m.groups()[0]
55+
niy = m.groups()[1]
56+
57+
return nix, niy
58+
59+
60+
def get_tile_coord_from_nixy(nix, niy):
61+
"""Get Tile Coord From Nixy.
62+
63+
Return coordinates corresponding to tile with number (nix,niy).
64+
65+
Parameters
66+
----------
67+
nix : str or list
68+
tile number(s) for x coordinate(s);
69+
niy : str or list
70+
tile number(s) for y coordinate(s)
71+
72+
See also
73+
--------
74+
get_tile_number_from_coord
75+
76+
Returns
77+
-------
78+
tuple
79+
right ascension and declination
80+
81+
"""
82+
number_pattern = re.compile(r'\d{3}')
83+
84+
# Transform from str to int
85+
if not np.isscalar(nix):
86+
# Check input numbers
87+
if not all(
88+
[bool(number_pattern.fullmatch(number)) for number in nix + niy]
89+
):
90+
raise ValueError('Input needs to be three-digit numbers')
91+
92+
# Transform to list
93+
xi = np.array(nix).astype(int)
94+
yi = np.array(niy).astype(int)
95+
else:
96+
if not all(
97+
[bool(number_pattern.fullmatch(number)) for number in [nix, niy]]
98+
):
99+
raise ValueError('Input needs to be three-digit numbers')
100+
101+
xi = int(nix)
102+
yi = int(niy)
103+
104+
# Declination
105+
d = yi / 2 - 90
106+
dec = coords.Angle(d, unit='deg')
107+
108+
# Right ascension
109+
r = xi / 2 / np.cos(dec.radian)
110+
ra = coords.Angle(r, unit='deg')
111+
112+
return ra, dec

cs_util/tests/test_cfis.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""UNIT TESTS FOR CFIS SUBPACKAGE.
2+
3+
This module contains unit tests for the cfis subpackage.
4+
5+
"""
6+
7+
import os
8+
9+
import numpy as np
10+
from numpy import testing as npt
11+
from astropy import units
12+
13+
from unittest import TestCase
14+
15+
from cs_util import cfis
16+
17+
18+
class CfisTestCase(TestCase):
19+
"""Test case for the ``cfis`` module."""
20+
21+
def setUp(self):
22+
"""Set test parameter values."""
23+
24+
self._size_tile = 0.5 * units.deg
25+
26+
self._tile_number_ok = ['270.283', '188-308']
27+
self._nix = ['270', '188']
28+
self._niy = ['283', '308']
29+
self._dec = [51.5, 64]
30+
self._ra = [216.86237, 214.43017]
31+
self._unit = units.deg
32+
33+
self._nix_nok = ['23x', '1234']
34+
self._tile_number_nok = '12x.456'
35+
36+
def tearDown(self):
37+
"""Unset test parameter values."""
38+
self._tile_number_ok = None
39+
self._nix = None
40+
self._niy = None
41+
self._dec = None
42+
self._ra = None
43+
self._unit = None
44+
self._tile_number_nok = None
45+
46+
def test_Cfis(self):
47+
"""Test ``cs_util.Cfis`` class."""
48+
self.assertTrue(self._size_tile == cfis.Cfis().size['tile'])
49+
50+
def test_get_tile_number(self):
51+
"""Test ``cs_util.get_tile_number`` method."""
52+
53+
# Test return values for valid input tile numbers
54+
for idx, tile_number_ok in enumerate(self._tile_number_ok):
55+
nix, niy = cfis.get_tile_number(tile_number_ok)
56+
self.assertTrue(
57+
(nix == self._nix[idx]) and (niy == self._niy[idx]),
58+
msg=f'{nix}!={self._nix[idx]} or {niy}!={self._niy[idx]}',
59+
)
60+
61+
self.assertRaises(
62+
ValueError,
63+
cfis.get_tile_number,
64+
self._tile_number_nok
65+
)
66+
67+
def test_get_tile_coord_from_nixy(self):
68+
"""Test ``cs_util.get_tile_coord_from_nixy`` method."""
69+
70+
# Call with scalar arguments
71+
for idx in range(len(self._nix)):
72+
ra, dec = cfis.get_tile_coord_from_nixy(
73+
self._nix[idx],
74+
self._niy[idx],
75+
)
76+
77+
# Test values
78+
npt.assert_almost_equal(
79+
ra.value,
80+
self._ra[idx],
81+
err_msg=f'{ra}!={self._ra[idx]}',
82+
decimal=5,
83+
)
84+
npt.assert_almost_equal(
85+
dec.value,
86+
self._dec[idx],
87+
err_msg=f'{dec}!={self._dec[idx]}',
88+
)
89+
90+
# Test units
91+
self.assertTrue(ra.unit == self._unit)
92+
self.assertTrue(dec.unit == self._unit)
93+
94+
# Call with list arguments
95+
ra, dec = cfis.get_tile_coord_from_nixy(self._nix, self._niy)
96+
for idx in range(len(self._nix)):
97+
98+
# Test values
99+
npt.assert_almost_equal(
100+
ra[idx].value,
101+
self._ra[idx],
102+
err_msg=f'{ra[idx]}!={self._ra[idx]}',
103+
decimal=5,
104+
)
105+
npt.assert_almost_equal(
106+
dec[idx].value,
107+
self._dec[idx],
108+
err_msg=f'{dec[idx]}!={self._dec[idx]}',
109+
)
110+
# Test units
111+
self.assertTrue(ra[idx].unit == self._unit)
112+
self.assertTrue(dec[idx].unit == self._unit)
113+
114+
# Test exception for invalid input
115+
self.assertRaises(
116+
ValueError,
117+
cfis.get_tile_coord_from_nixy,
118+
self._nix_nok,
119+
self._niy,
120+
)
121+
self.assertRaises(
122+
ValueError,
123+
cfis.get_tile_coord_from_nixy,
124+
self._niy,
125+
self._nix_nok,
126+
)
127+
self.assertRaises(
128+
ValueError,
129+
cfis.get_tile_coord_from_nixy,
130+
self._nix_nok[0],
131+
self._niy[0],
132+
)

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
astropy
22
datetime
3-
jinja2==3.0
43
numpy
54
matplotlib
65
vos

0 commit comments

Comments
 (0)