Skip to content

Commit

Permalink
Merge pull request #99 from fact-project/fact_tools_1.0
Browse files Browse the repository at this point in the history
WIP: adapt to fact-tools 1.0
  • Loading branch information
maxnoe authored Mar 8, 2018
2 parents a03c488 + 2dcfa1e commit f64d6fb
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github_deploy_key*
*.swp
.pytest_cache

.pytest_cache
# Byte-compiled / optimized / DLL files
Expand Down
2 changes: 1 addition & 1 deletion fact/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.19.0
0.19.0
21 changes: 9 additions & 12 deletions fact/analysis/scripts/radec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@


def calc_ra_dec(events):
events['obstime'] = pd.to_datetime(
events['unix_time_utc_0'] * 1e6 + events['unix_time_utc_1'],
unit='us',
)
events['obstime'] = pd.to_datetime(events['timestamp'])

events['ra_prediction'], events['dec_prediction'] = camera_to_equatorial(
events['source_x_prediction'],
events['source_y_prediction'],
events['zd_tracking'],
events['az_tracking'],
events['obstime'],
events['pointing_position_zd'],
events['pointing_position_az'],
events['obstime'].dt.to_pydatetime(),
)
return events

Expand All @@ -40,8 +37,8 @@ def main(inputfile, chunksize, n_jobs, yes):
e.g. for example for files analysed with the classifier-tools
The following keys have to be present in the h5py hdf5 file.
* az_tracking
* zd_tracking
* pointing_position_az
* pointing_position_zd
* source_x_prediction
* source_y_prediction
* unix_time_utc
Expand All @@ -55,11 +52,11 @@ def main(inputfile, chunksize, n_jobs, yes):
inputfile,
key='events',
columns=[
'az_tracking',
'zd_tracking',
'pointing_position_az',
'pointing_position_zd',
'source_x_prediction',
'source_y_prediction',
'unix_time_utc',
'timestamp',
],
chunksize=chunksize
)
Expand Down
41 changes: 19 additions & 22 deletions fact/analysis/scripts/theta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
from fact.analysis import calc_theta_camera, calc_theta_offs_camera
from fact.io import read_h5py_chunked
from fact.io import create_empty_h5py_dataset, append_to_h5py_dataset
from fact.coordinates.utils import array_to_time
from fact.instrument.constants import LOCATION
from astropy.coordinates import SkyCoord, AltAz
from astropy.time import Time
from joblib import Parallel, delayed
import h5py
import click


def calc_theta_source(df, source):

obstime = array_to_time(pd.to_datetime(
df.unix_time_utc_0 * 1e6 + df.unix_time_utc_1, unit='us'
))
obstime = Time(pd.to_datetime(df['timestamp']).dt.to_pydatetime())

altaz = AltAz(location=LOCATION, obstime=obstime)
source_altaz = source.transform_to(altaz)
Expand All @@ -25,16 +22,16 @@ def calc_theta_source(df, source):
df.source_y_prediction,
source_altaz.zen.deg,
source_altaz.az.deg,
zd_pointing=df.zd_tracking,
az_pointing=df.az_tracking,
zd_pointing=df['pointing_position_zd'],
az_pointing=df['pointing_position_az'],
)
theta_offs = calc_theta_offs_camera(
df.source_x_prediction,
df.source_y_prediction,
source_altaz.zen.deg,
source_altaz.az.deg,
zd_pointing=df.zd_tracking,
az_pointing=df.az_tracking,
zd_pointing=df['pointing_position_zd'],
az_pointing=df['pointing_position_az'],
n_off=5,
)

Expand All @@ -51,16 +48,16 @@ def calc_theta_coordinates(df):
df.source_y_prediction,
df.zd_source_calc,
df.az_source_calc,
zd_pointing=df.zd_tracking,
az_pointing=df.az_tracking,
zd_pointing=df['pointing_position_zd'],
az_pointing=df['pointing_position_az'],
)
theta_offs = calc_theta_offs_camera(
df.source_x_prediction,
df.source_y_prediction,
df.zd_source_calc,
df.az_source_calc,
zd_pointing=df.zd_tracking,
az_pointing=df.az_tracking,
zd_pointing=df['pointing_position_zd'],
az_pointing=df['pointing_position_az'],
n_off=5,
)

Expand Down Expand Up @@ -106,12 +103,12 @@ def main(inputfile, source, chunksize, yes):
inputfile,
key='events',
columns=[
'az_tracking',
'zd_tracking',
'pointing_position_az',
'pointing_position_zd',
'source_x_prediction',
'source_y_prediction',
'az_source_calc',
'zd_source_calc',
'source_position_az',
'source_position_zd',
],
chunksize=chunksize
)
Expand All @@ -122,25 +119,25 @@ def main(inputfile, source, chunksize, yes):
for df, start, stop in df_it
)
else:
crab = SkyCoord.from_name(source)
source = SkyCoord.from_name(source)

df_it = read_h5py_chunked(
inputfile,
key='events',
columns=[
'az_tracking',
'zd_tracking',
'pointing_position_az',
'pointing_position_zd',
'source_x_prediction',
'source_y_prediction',
'unix_time_utc',
'timestamp',
],
chunksize=chunksize
)

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

dfs = pool(
delayed(calc_theta_source)(df, crab)
delayed(calc_theta_source)(df, source)
for df, start, stop in df_it
)

Expand Down
50 changes: 38 additions & 12 deletions fact/coordinates/camera_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
FunctionTransform,
)

try:
from astropy.coordinates import CoordinateAttribute, TimeAttribute, EarthLocationAttribute
except ImportError:
# for astropy <= 2.0.0
from astropy.coordinates import (
CoordinateAttribute,
TimeFrameAttribute as TimeAttribute,
EarthLocationAttribute,
)
from astropy.coordinates import (
CoordinateAttribute,
TimeAttribute,
EarthLocationAttribute,
Attribute
)

from astropy.coordinates.matrix_utilities import rotation_matrix
from astropy.coordinates.representation import CartesianRepresentation
Expand All @@ -27,11 +24,31 @@


class CameraFrame(BaseCoordinateFrame):
'''Astropy CoordinateFrame representing coordinates in the CameraPlane'''
'''
Astropy CoordinateFrame representing coordinates in the CameraPlane
Attributes
----------
pointing_direction: astropy.coordinates.AltAz
The pointing direction of the telescope
obstime: astropy.Time
The timestamp of the observation, only needed to directly transform
to Equatorial coordinates, transforming to AltAz does not need this.
location: astropy.coordinates.EarthLocation
The location of the observer, only needed to directly transform
to Equatorial coordinates, transforming to AltAz does not need this,
default is FACT's location
rotated: bool
True means x points right and y points up when looking on the camera
from the dish, which is the efinition of FACT-Tools >= 1.0 and Mars.
False means x points up and y points left,
which is definition in the original FACTPixelMap file.
'''
default_representation = PlanarRepresentation
pointing_direction = CoordinateAttribute(frame=AltAz, default=None)
obstime = TimeAttribute(default=None)
location = EarthLocationAttribute(default=LOCATION)
rotated = Attribute(default=True)


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

if camera.rotated is True:
x, y = y, -x

z = 1 / np.sqrt(1 + (x / focal_length)**2 + (y / focal_length)**2)
x *= z / focal_length
y *= z / focal_length
Expand Down Expand Up @@ -78,9 +98,15 @@ def altaz_to_camera(altaz, camera):
cartesian = cartesian.transform(rot_z_az)
cartesian = cartesian.transform(rot_y_zd)

x = (cartesian.x * focal_length / cartesian.z).copy()
y = (cartesian.y * focal_length / cartesian.z).copy()

if camera.rotated is True:
x, y = -y, x

return CameraFrame(
x=cartesian.x * focal_length / cartesian.z,
y=cartesian.y * focal_length / cartesian.z,
x=x,
y=y,
pointing_direction=camera.pointing_direction,
obstime=altaz.obstime,
location=camera.location,
Expand Down
Loading

0 comments on commit f64d6fb

Please sign in to comment.