Skip to content

Commit f64d6fb

Browse files
authored
Merge pull request #99 from fact-project/fact_tools_1.0
WIP: adapt to fact-tools 1.0
2 parents a03c488 + 2dcfa1e commit f64d6fb

File tree

7 files changed

+149
-64
lines changed

7 files changed

+149
-64
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
github_deploy_key*
22
*.swp
3+
.pytest_cache
34

45
.pytest_cache
56
# Byte-compiled / optimized / DLL files

fact/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.19.0
1+
0.19.0

fact/analysis/scripts/radec.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111

1212

1313
def calc_ra_dec(events):
14-
events['obstime'] = pd.to_datetime(
15-
events['unix_time_utc_0'] * 1e6 + events['unix_time_utc_1'],
16-
unit='us',
17-
)
14+
events['obstime'] = pd.to_datetime(events['timestamp'])
1815

1916
events['ra_prediction'], events['dec_prediction'] = camera_to_equatorial(
2017
events['source_x_prediction'],
2118
events['source_y_prediction'],
22-
events['zd_tracking'],
23-
events['az_tracking'],
24-
events['obstime'],
19+
events['pointing_position_zd'],
20+
events['pointing_position_az'],
21+
events['obstime'].dt.to_pydatetime(),
2522
)
2623
return events
2724

@@ -40,8 +37,8 @@ def main(inputfile, chunksize, n_jobs, yes):
4037
e.g. for example for files analysed with the classifier-tools
4138
4239
The following keys have to be present in the h5py hdf5 file.
43-
* az_tracking
44-
* zd_tracking
40+
* pointing_position_az
41+
* pointing_position_zd
4542
* source_x_prediction
4643
* source_y_prediction
4744
* unix_time_utc
@@ -55,11 +52,11 @@ def main(inputfile, chunksize, n_jobs, yes):
5552
inputfile,
5653
key='events',
5754
columns=[
58-
'az_tracking',
59-
'zd_tracking',
55+
'pointing_position_az',
56+
'pointing_position_zd',
6057
'source_x_prediction',
6158
'source_y_prediction',
62-
'unix_time_utc',
59+
'timestamp',
6360
],
6461
chunksize=chunksize
6562
)

fact/analysis/scripts/theta.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
from fact.analysis import calc_theta_camera, calc_theta_offs_camera
44
from fact.io import read_h5py_chunked
55
from fact.io import create_empty_h5py_dataset, append_to_h5py_dataset
6-
from fact.coordinates.utils import array_to_time
76
from fact.instrument.constants import LOCATION
87
from astropy.coordinates import SkyCoord, AltAz
8+
from astropy.time import Time
99
from joblib import Parallel, delayed
1010
import h5py
1111
import click
1212

1313

1414
def calc_theta_source(df, source):
15-
16-
obstime = array_to_time(pd.to_datetime(
17-
df.unix_time_utc_0 * 1e6 + df.unix_time_utc_1, unit='us'
18-
))
15+
obstime = Time(pd.to_datetime(df['timestamp']).dt.to_pydatetime())
1916

2017
altaz = AltAz(location=LOCATION, obstime=obstime)
2118
source_altaz = source.transform_to(altaz)
@@ -25,16 +22,16 @@ def calc_theta_source(df, source):
2522
df.source_y_prediction,
2623
source_altaz.zen.deg,
2724
source_altaz.az.deg,
28-
zd_pointing=df.zd_tracking,
29-
az_pointing=df.az_tracking,
25+
zd_pointing=df['pointing_position_zd'],
26+
az_pointing=df['pointing_position_az'],
3027
)
3128
theta_offs = calc_theta_offs_camera(
3229
df.source_x_prediction,
3330
df.source_y_prediction,
3431
source_altaz.zen.deg,
3532
source_altaz.az.deg,
36-
zd_pointing=df.zd_tracking,
37-
az_pointing=df.az_tracking,
33+
zd_pointing=df['pointing_position_zd'],
34+
az_pointing=df['pointing_position_az'],
3835
n_off=5,
3936
)
4037

@@ -51,16 +48,16 @@ def calc_theta_coordinates(df):
5148
df.source_y_prediction,
5249
df.zd_source_calc,
5350
df.az_source_calc,
54-
zd_pointing=df.zd_tracking,
55-
az_pointing=df.az_tracking,
51+
zd_pointing=df['pointing_position_zd'],
52+
az_pointing=df['pointing_position_az'],
5653
)
5754
theta_offs = calc_theta_offs_camera(
5855
df.source_x_prediction,
5956
df.source_y_prediction,
6057
df.zd_source_calc,
6158
df.az_source_calc,
62-
zd_pointing=df.zd_tracking,
63-
az_pointing=df.az_tracking,
59+
zd_pointing=df['pointing_position_zd'],
60+
az_pointing=df['pointing_position_az'],
6461
n_off=5,
6562
)
6663

@@ -106,12 +103,12 @@ def main(inputfile, source, chunksize, yes):
106103
inputfile,
107104
key='events',
108105
columns=[
109-
'az_tracking',
110-
'zd_tracking',
106+
'pointing_position_az',
107+
'pointing_position_zd',
111108
'source_x_prediction',
112109
'source_y_prediction',
113-
'az_source_calc',
114-
'zd_source_calc',
110+
'source_position_az',
111+
'source_position_zd',
115112
],
116113
chunksize=chunksize
117114
)
@@ -122,25 +119,25 @@ def main(inputfile, source, chunksize, yes):
122119
for df, start, stop in df_it
123120
)
124121
else:
125-
crab = SkyCoord.from_name(source)
122+
source = SkyCoord.from_name(source)
126123

127124
df_it = read_h5py_chunked(
128125
inputfile,
129126
key='events',
130127
columns=[
131-
'az_tracking',
132-
'zd_tracking',
128+
'pointing_position_az',
129+
'pointing_position_zd',
133130
'source_x_prediction',
134131
'source_y_prediction',
135-
'unix_time_utc',
132+
'timestamp',
136133
],
137134
chunksize=chunksize
138135
)
139136

140137
with Parallel(-1, verbose=10) as pool:
141138

142139
dfs = pool(
143-
delayed(calc_theta_source)(df, crab)
140+
delayed(calc_theta_source)(df, source)
144141
for df, start, stop in df_it
145142
)
146143

fact/coordinates/camera_frame.py

+38-12
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
FunctionTransform,
66
)
77

8-
try:
9-
from astropy.coordinates import CoordinateAttribute, TimeAttribute, EarthLocationAttribute
10-
except ImportError:
11-
# for astropy <= 2.0.0
12-
from astropy.coordinates import (
13-
CoordinateAttribute,
14-
TimeFrameAttribute as TimeAttribute,
15-
EarthLocationAttribute,
16-
)
8+
from astropy.coordinates import (
9+
CoordinateAttribute,
10+
TimeAttribute,
11+
EarthLocationAttribute,
12+
Attribute
13+
)
1714

1815
from astropy.coordinates.matrix_utilities import rotation_matrix
1916
from astropy.coordinates.representation import CartesianRepresentation
@@ -27,11 +24,31 @@
2724

2825

2926
class CameraFrame(BaseCoordinateFrame):
30-
'''Astropy CoordinateFrame representing coordinates in the CameraPlane'''
27+
'''
28+
Astropy CoordinateFrame representing coordinates in the CameraPlane
29+
30+
Attributes
31+
----------
32+
pointing_direction: astropy.coordinates.AltAz
33+
The pointing direction of the telescope
34+
obstime: astropy.Time
35+
The timestamp of the observation, only needed to directly transform
36+
to Equatorial coordinates, transforming to AltAz does not need this.
37+
location: astropy.coordinates.EarthLocation
38+
The location of the observer, only needed to directly transform
39+
to Equatorial coordinates, transforming to AltAz does not need this,
40+
default is FACT's location
41+
rotated: bool
42+
True means x points right and y points up when looking on the camera
43+
from the dish, which is the efinition of FACT-Tools >= 1.0 and Mars.
44+
False means x points up and y points left,
45+
which is definition in the original FACTPixelMap file.
46+
'''
3147
default_representation = PlanarRepresentation
3248
pointing_direction = CoordinateAttribute(frame=AltAz, default=None)
3349
obstime = TimeAttribute(default=None)
3450
location = EarthLocationAttribute(default=LOCATION)
51+
rotated = Attribute(default=True)
3552

3653

3754
@frame_transform_graph.transform(FunctionTransform, CameraFrame, AltAz)
@@ -42,6 +59,9 @@ def camera_to_altaz(camera, altaz):
4259
x = camera.x.copy()
4360
y = camera.y.copy()
4461

62+
if camera.rotated is True:
63+
x, y = y, -x
64+
4565
z = 1 / np.sqrt(1 + (x / focal_length)**2 + (y / focal_length)**2)
4666
x *= z / focal_length
4767
y *= z / focal_length
@@ -78,9 +98,15 @@ def altaz_to_camera(altaz, camera):
7898
cartesian = cartesian.transform(rot_z_az)
7999
cartesian = cartesian.transform(rot_y_zd)
80100

101+
x = (cartesian.x * focal_length / cartesian.z).copy()
102+
y = (cartesian.y * focal_length / cartesian.z).copy()
103+
104+
if camera.rotated is True:
105+
x, y = -y, x
106+
81107
return CameraFrame(
82-
x=cartesian.x * focal_length / cartesian.z,
83-
y=cartesian.y * focal_length / cartesian.z,
108+
x=x,
109+
y=y,
84110
pointing_direction=camera.pointing_direction,
85111
obstime=altaz.obstime,
86112
location=camera.location,

0 commit comments

Comments
 (0)