Skip to content

Commit 2b23381

Browse files
Merge pull request #16 from CosmoStat/develop
v0.0.3 release
2 parents 4d98d03 + 51ab355 commit 2b23381

19 files changed

Lines changed: 654 additions & 84 deletions

cs_util/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
"""cs_util PACKAGE.
42
53
Provide a basic description of what your package contains.

cs_util/canfar.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
"""CANFAR TOOLS.
2+
3+
This module defines methods for managing CANFAR specific actions.
4+
5+
:Author: Samuel Farrens <samuel.farrens@cea.fr>
6+
Martin Kilbinger <martin.kilbinger@cea.fr>
7+
8+
"""
9+
10+
import os
11+
import sys
12+
from contextlib import redirect_stdout
13+
from io import StringIO
14+
15+
import vos.commands as vosc
16+
17+
18+
class vosError(Exception):
19+
"""VOS Error.
20+
21+
Generic error that is raised by the vosHandler.
22+
23+
"""
24+
25+
pass
26+
27+
28+
class vosHandler:
29+
"""VOS Handler.
30+
31+
This class manages the use of VOS commands.
32+
33+
Parameters
34+
----------
35+
command : str
36+
VOS command name
37+
38+
"""
39+
40+
def __init__(self, command):
41+
42+
self._avail_commands = tuple(vosc.__all__)
43+
self.command = command
44+
45+
@property
46+
def command(self):
47+
"""Set Command.
48+
49+
This method sets the VOS command property.
50+
51+
Raises
52+
------
53+
ValueError
54+
if value is not valid vos command
55+
56+
"""
57+
return self._command
58+
59+
@command.setter
60+
def command(self, value):
61+
62+
if value not in self._avail_commands:
63+
raise ValueError(
64+
f'vos command must be one of {self._avail_commands}'
65+
)
66+
67+
self._command = getattr(vosc, value)
68+
69+
def __call__(self, *args, **kwargs):
70+
"""Call Method.
71+
72+
This method allows class instances to be called as functions.
73+
74+
Raises
75+
------
76+
vosError
77+
if error in vos command occurs
78+
79+
"""
80+
try:
81+
self._command()
82+
83+
except Exception:
84+
raise vosError(
85+
f'Error in VOs command: {self._command.__name__}'
86+
)
87+
88+
89+
def download(source, target, verbose=False):
90+
"""Download.
91+
92+
Download file from vos.
93+
94+
Parameters
95+
----------
96+
source : str
97+
source path on vos
98+
target : str
99+
target path
100+
verbose : bool, optional, default=False
101+
verbose output if True
102+
103+
Returns
104+
-------
105+
status : bool
106+
status, True/False or success/failure
107+
108+
"""
109+
cmd = 'vcp'
110+
111+
if not os.path.exists(target):
112+
sys.argv = [cmd, source, target]
113+
if verbose:
114+
print(f'Downloading file {source} to {target}...')
115+
vcp = vosHandler(cmd)
116+
117+
vcp()
118+
if verbose:
119+
print('Download finished.')
120+
else:
121+
if verbose:
122+
print(f'Target file {target} exists, skipping download.')
123+
124+
125+
def dir_list(path, verbose=False):
126+
"""Set Directory List.
127+
128+
List content of path on vos.
129+
130+
Parameters
131+
----------
132+
path : str
133+
path on vos, starts with 'vos:cfis/...'
134+
verbose : bool, optional, default=False
135+
verbose output if True
136+
137+
Raises
138+
------
139+
HTTPError, KeyError
140+
if error occurs during vos command
141+
142+
Returns
143+
-------
144+
list
145+
file or directory at path, type is str
146+
147+
"""
148+
cmd = 'vls'
149+
sys.argv = [cmd, path]
150+
vls = vosHandler(cmd)
151+
152+
if verbose:
153+
print('Getting vos directory content from vls...')
154+
155+
f = StringIO()
156+
157+
try:
158+
with redirect_stdout(f):
159+
vls()
160+
except Exception:
161+
print('Error during vls command')
162+
raise
163+
164+
vls_out = f.getvalue()
165+
166+
return vls_out.split('\n')

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

0 commit comments

Comments
 (0)