Commit e6bcc19f authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Reformat, checked with pycodestyle

parent 312029bc
......@@ -15,18 +15,27 @@ class CamrasHpibDevice(object):
if not CamrasHpibDevice.command_thread:
serial_port = serial.Serial("/dev/ttyUSB0")
CamrasHpibDevice.command_thread = serial.threaded.ReaderThread(serial_port, hpib.GPIBProtocol)
CamrasHpibDevice.command_thread = serial.threaded.ReaderThread(serial_port,
hpib.GPIBProtocol)
CamrasHpibDevice.command_thread.start()
CamrasHpibDevice.command_thread._connection_made.wait()
CamrasHpibDevice.command_thread.protocol.init_hpib()
@property
def frequency(self):
"""Returns the frequency of the device as an astropy Quantity.
Throws a RuntimeError if no response"""
freq_str = CamrasHpibDevice.command_thread.protocol.command("freq?", address=self.hpib_address)
if not freq_str or len(freq_str)==0:
raise RuntimeError("Camras device at address {} is not responding".format(self.hpib_address))
"""Frequency of the device as an astropy Quantity.
Returns:
Quantity: frequency (in Hz) of the device
Raises:
RuntimeError: If the device does not respond
"""
freq_str = CamrasHpibDevice.command_thread.protocol.command("freq?",
address=self.hpib_address)
if not freq_str or len(freq_str) == 0:
raise RuntimeError("Camras device at address {} is not responding".format(
self.hpib_address))
return int(float(freq_str)) * u.Hz
@frequency.setter
......@@ -35,27 +44,35 @@ class CamrasHpibDevice(object):
Args:
freq (Union[float, Quantity]): new frequency. If no unit present, assume Hz
Raises:
RuntimeError: If the device sticks to a different frequency (e.g. if the specified
frequency is outside the accepted range for the device)
"""
if isinstance(freq, Quantity):
freq = freq.to(u.Hz).value
CamrasHpibDevice.command_thread.protocol.command("freq {:d} Hz".format(int(freq)), address=self.hpib_address)
CamrasHpibDevice.command_thread.protocol.command("freq {:d} Hz".format(int(freq)),
address=self.hpib_address)
new_freq = self.frequency
if new_freq.to(u.Hz).value != int(freq):
raise RuntimeError("Setting frequency failed: tried to set to {}, it is now {}".format(
freq, new_freq))
class Receiver(CamrasHpibDevice):
"""Wrapper around HPIB commands for the Rohde & Schwartz receiver"""
def __init__(self, **kwargs):
super(Receiver, self).__init__(1)
class LocalOscillator(CamrasHpibDevice):
"""Wrapper around HPIB commands for the local oscillator"""
def __init__(self, **kwargs):
super(LocalOscillator, self).__init__(28)
class ClockGenerator(CamrasHpibDevice):
"""Wrapper around HPIB commands for the clock generator (should be at 140MHz)"""
......
from typing import Any, Union
import astropy.units as u
from astropy.coordinates import SkyCoord, EarthLocation
from astropy.coordinates import EarthLocation
from astropy.time import Time
import numpy as np
import argparse
import threading
import subprocess
from vlsr import doppler_frequency
from telescope import telescope
from camrasdevices import Receiver, LocalOscillator
from camrasdevices import LocalOscillator
freq_hi = 1420.405751 * u.MHz
def doppler_harm(sky_coordinate, time, tracking_frequency, location, variant):
ra = sky_coordinate.ra.to(u.rad).value
dec = sky_coordinate.dec.to(u.rad).value
......@@ -24,19 +22,26 @@ def doppler_harm(sky_coordinate, time, tracking_frequency, location, variant):
argstring = "{ra} {dec} 2000 {time} {dt_lat} {dt_lon} {dt_alt}".format(
ra=ra, dec=dec, time=t, dt_lat=dt_lat, dt_lon=dt_lon, dt_alt=dt_alt)
doppler_executable = "/home/harm/bin/doppler_"+variant
doppler_executable = "/home/harm/bin/doppler_" + variant
doppler_cmd = doppler_executable + " " + argstring + " " + str(tracking_frequency.to(u.MHz).value)
freq_doppler = float(subprocess.Popen(doppler_cmd, stdout=subprocess.PIPE, shell=True).stdout.read())
doppler_cmd = doppler_executable + " " + argstring \
+ " " + str(tracking_frequency.to(u.MHz).value)
freq_doppler = float(subprocess.Popen(doppler_cmd,
stdout=subprocess.PIPE,
shell=True
).stdout.read())
return freq_doppler * u.MHz
def doppler_harm_bl(sky_coordinate, time, tracking_frequency, location):
return doppler_harm(sky_coordinate, time, tracking_frequency, location, 'bl')
def doppler_harm_mb(sky_coordinate, time, tracking_frequency, location):
return doppler_harm(sky_coordinate, time, tracking_frequency, location, 'mb')
def track_doppler(dt, lo,
tracking_frequency=freq_hi,
doppler_function=doppler_frequency,
......@@ -59,14 +64,14 @@ def track_doppler(dt, lo,
dt_lon = 6.396169 * u.deg
dt_alt = 25.0 * u.m
dt_loc = EarthLocation.from_geodetic(lat = dt_lat, lon = dt_lon, height = dt_alt)
dt_loc = EarthLocation.from_geodetic(lat=dt_lat, lon=dt_lon, height=dt_alt)
while not exit_event.is_set():
sky_coordinate = dt.radec
if sky_coordinate:
freq_doppler = doppler_function(sky_coordinate, Time.now(), tracking_frequency, dt_loc)
dfreq = 1*u.GHz + tracking_frequency - freq_doppler
dfreq = 1 * u.GHz + tracking_frequency - freq_doppler
lo.frequency = dfreq
exit_event.wait(timeout=timeout)
lo.frequency = 1*u.GHz
lo.frequency = 1 * u.GHz
exit_event.clear()
#!/usr/bin/env python3
from astropy.time import Time
from astropy.coordinates import SkyCoord, EarthLocation
import astropy.units as u
......@@ -9,32 +8,36 @@ import numpy as np
# Direction of motion of the Sun. Info from
# http://herschel.esac.esa.int/hcss-doc-15.0/load/hifi_um/html/hifi_ref_frame.html#hum_lsr_frame
psun = SkyCoord(ra = "18:03:50.29", dec = "+30:00:16.8",frame = "icrs",unit = (u.hourangle,u.deg))
vsun = -20.0*u.km/u.s
psun = SkyCoord(ra="18:03:50.29", dec="+30:00:16.8", frame="icrs", unit=(u.hourangle, u.deg))
vsun = -20.0 * u.km / u.s
# vlsr routine
def vlsr(t,loc,psrc,verbose=False):
def vlsr(t, loc, psrc, verbose=False):
"""Compute the line of sight radial velocity
Args:
psrc: SkyCoord object or source
loc: EarthLocation object of observer
t: Time object
Returns:
Line of sight radial velocity (in km/s) as an astropy Quantity
"""
# Radial velocity correction to solar system barycenter
vsrc = psrc.radial_velocity_correction(obstime = t, location = loc)
vsrc = psrc.radial_velocity_correction(obstime=t, location=loc)
# Projection of solar velocity towards the source
vsun_proj = psrc.cartesian.dot(psun.cartesian)*vsun
vsun_proj = psrc.cartesian.dot(psun.cartesian) * vsun
if verbose:
print("Barycentric radial velocity: {0:+8.3f}".format(vsrc.to(u.km/u.s)))
print("Projected solar velocity: {0:+8.3f}".format(vsun_proj.to(u.km/u.s)))
print("Barycentric radial velocity: {0:+8.3f}".format(vsrc.to(u.km / u.s)))
print("Projected solar velocity: {0:+8.3f}".format(vsun_proj.to(u.km / u.s)))
return vsun_proj - vsrc
return vsun_proj-vsrc
def doppler_frequency(psrc, t, rest_frequency, loc, verbose=False):
"""
Compute the Doppler corrected frequency, taking into account the line of sight radial velocity.
"""Doppler corrected frequency, taking into account the line of sight radial velocity.
Args:
psrc (SkyCoord): sky location for correction
......@@ -43,46 +46,51 @@ def doppler_frequency(psrc, t, rest_frequency, loc, verbose=False):
loc (EarthLocation): observer location
Returns:
Quantity: Observable frequency
Observable frequency in Hz as astropy Quantity
"""
v1 = vlsr(t, loc, psrc, verbose=verbose)
beta = v1/astropy.constants.c
return np.sqrt((1 + beta)/(1 - beta)) * rest_frequency
beta = v1 / astropy.constants.c
return np.sqrt((1 + beta) / (1 - beta)) * rest_frequency
if __name__=="__main__":
def test_vlsr():
freq_hi = 1420.405751
# Test 1: Source towards direction of solar motion, Earth moving prependicular
loc = EarthLocation.from_geodetic(lat = 52*u.deg, lon = 6*u.deg, height = 0*u.m)
psrc = SkyCoord(ra = 18*u.hourangle, dec = 30.0*u.deg, frame = "icrs")
t = Time("2018-06-21T12:00:00",scale = "utc",format = "isot")
v = vlsr(t,loc,psrc,verbose=True)
print("LSR velocity: {0:+8.3f}".format(v.to(u.km/u.s)))
loc = EarthLocation.from_geodetic(lat=52 * u.deg, lon=6 * u.deg, height=0 * u.m)
psrc = SkyCoord(ra=18 * u.hourangle, dec=30.0 * u.deg, frame="icrs")
t = Time("2018-06-21T12:00:00", scale="utc", format="isot")
v = vlsr(t, loc, psrc, verbose=True)
print("LSR velocity: {0:+8.3f}".format(v.to(u.km / u.s)))
f = doppler_frequency(t, loc, psrc, freq_hi)
print("Doppler HI: {}".format(f))
cmd = "~harm/bin/vlsr %f %f 2000 %s %f %f 0"%(psrc.ra.rad,psrc.dec.rad,t.unix,loc.lat.rad,loc.lon.rad)
cmd = "~harm/bin/vlsr %f %f 2000 %s %f %f 0" %\
(psrc.ra.rad, psrc.dec.rad, t.unix, loc.lat.rad, loc.lon.rad)
print(cmd)
cmd = "~harm/bin/doppler_bl %f %f 2000 %s %f %f 0 %f"%(psrc.ra.rad,psrc.dec.rad,t.unix,loc.lat.rad,loc.lon.rad, freq_hi)
print(cmd+"\n\n")
cmd = "~harm/bin/doppler_bl %f %f 2000 %s %f %f 0 %f" %\
(psrc.ra.rad, psrc.dec.rad, t.unix, loc.lat.rad, loc.lon.rad, freq_hi)
print(cmd + "\n\n")
# Test 2: Source perpendicular to direction of solar motion, Earth moving towards source
loc = EarthLocation.from_geodetic(lat = 52*u.deg, lon = 6*u.deg, height = 0*u.m)
psrc = SkyCoord(ra = 6*u.hourangle, dec = 0.0*u.deg, frame = "icrs")
t = Time("2018-06-21T12:00:00",scale = "utc",format = "isot")
v = vlsr(t,loc,psrc,verbose=True)
print("LSR velocity: {0:+8.3f}".format(v.to(u.km/u.s)))
loc = EarthLocation.from_geodetic(lat=52 * u.deg, lon=6 * u.deg, height=0 * u.m)
psrc = SkyCoord(ra=6 * u.hourangle, dec=0.0 * u.deg, frame="icrs")
t = Time("2018-06-21T12:00:00", scale="utc", format="isot")
v = vlsr(t, loc, psrc, verbose=True)
print("LSR velocity: {0:+8.3f}".format(v.to(u.km / u.s)))
f = doppler_frequency(t, loc, psrc, freq_hi)
print("Doppler HI: {}".format(f))
cmd = "~harm/bin/vlsr %f %f 2000 %s %f %f 0"%(psrc.ra.rad,psrc.dec.rad,t.unix,loc.lat.rad,loc.lon.rad)
cmd = "~harm/bin/vlsr %f %f 2000 %s %f %f 0" %\
(psrc.ra.rad, psrc.dec.rad, t.unix, loc.lat.rad, loc.lon.rad)
print(cmd)
cmd = "~harm/bin/doppler_bl %f %f 2000 %s %f %f 0 %f"%(psrc.ra.rad,psrc.dec.rad,t.unix,loc.lat.rad,loc.lon.rad, freq_hi)
print(cmd+"\n\n")
cmd = "~harm/bin/doppler_bl %f %f 2000 %s %f %f 0 %f" %\
(psrc.ra.rad, psrc.dec.rad, t.unix, loc.lat.rad, loc.lon.rad, freq_hi)
print(cmd + "\n\n")
# Test 3: Narrabri calculator
loc = EarthLocation.from_geodetic(lat = -30.3128*u.deg, lon = 149.5502*u.deg, height = 0*u.m)
psrc = SkyCoord(ra = 12*u.hourangle, dec = 0.0*u.deg, frame = "icrs")
t = Time("2018-04-16T12:00:00",scale = "utc",format = "isot")
v = vlsr(t,loc,psrc,verbose=True)
print("LSR velocity: {0:+8.3f}".format(v.to(u.km/u.s)))
loc = EarthLocation.from_geodetic(lat=-30.3128 * u.deg, lon=149.5502 * u.deg, height=0 * u.m)
psrc = SkyCoord(ra=12 * u.hourangle, dec=0.0 * u.deg, frame="icrs")
t = Time("2018-04-16T12:00:00", scale="utc", format="isot")
v = vlsr(t, loc, psrc, verbose=True)
print("LSR velocity: {0:+8.3f}".format(v.to(u.km / u.s)))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment