Houston, we have a trunk.
[debian/gnuradio] / gr-radio-astronomy / src / python / local_calibrator.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2003,2004,2005 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
21
22
23 import Numeric
24 import math
25 import ephem
26 import time
27
28 #
29 # Simple class for allowing local definition of a calibration function
30 #  for raw samples coming from the RA detector chain.  Each observatory
31 #  is different, and rather than hacking up the main code in usrp_ra_receiver
32 #  we define the appropriate function here.
33 #
34 # For example, one could calibrate the output in Janskys, rather than
35 #  dB.
36 #
37 #
38
39 def calib_default_total_power(data):
40     r = 10.0*math.log10(data)
41     return(r)
42
43 def calib_numogate_ridge_observatory_total_power(data):
44
45     me = ephem.Observer()
46
47     #
48     # PyEphem wants lat/long as strings, rather than floats--took me quite
49     #  a long time to figure that out.  If they don't arrive as strings,
50     #  the calculations for sidereal time are complete garbage
51     #
52     me.long = str(-76.043)
53     me.lat = str(44.967)
54
55     me.date = ephem.now()
56     sidtime = me.sidereal_time()
57
58     foo = time.localtime()
59     if not "calib_prefix" in globals():
60         pfx = "./"
61     else:
62         pfx = globals()["calib_prefix"]
63     filenamestr = "%s/%04d%02d%02d%02d" % (pfx, foo.tm_year, 
64        foo.tm_mon, foo.tm_mday, foo.tm_hour)
65
66     rainbow_file = open (filenamestr+".tpdat","a")
67   
68     r = (math.sqrt(data) / 2048) * 1000.0
69     #r = calib_default_total_power(data)
70     inter = globals()["calib_decln"]
71     rainbow_file.write(str(ephem.hours(sidtime))+" "+str(r)+" "+str(inter)+"\n")
72     rainbow_file.close()
73     return(r)
74
75 def calib_numogate_ridge_observatory_fft(data,l):
76
77     me = ephem.Observer()
78
79     #
80     # PyEphem wants lat/long as strings, rather than floats--took me quite
81     #  a long time to figure that out.  If they don't arrive as strings,
82     #  the calculations for sidereal time are complete garbage
83     #
84     me.long = str(-76.043)
85     me.lat = str(44.967)
86
87     me.date = ephem.now()
88     sidtime = me.sidereal_time()
89
90     foo = time.localtime()
91     
92     if not "calib_prefix" in globals():
93         pfx = "./"
94     else:
95         pfx = globals()["calib_prefix"]
96     filenamestr = "%s/%04d%02d%02d%02d" % (pfx, foo.tm_year, 
97        foo.tm_mon, foo.tm_mday, foo.tm_hour)
98
99     now = time.time()
100
101     if not "calib_then" in globals():
102         globals()["calib_then"] = now
103
104     delta = 5
105                 
106     if (now - globals()["calib_then"]) >= delta:
107
108         globals()["calib_then"] = now
109         rainbow_file = open (filenamestr+".sdat","a")
110   
111         r = calib_default_fft(data,l)
112         inter = globals()["calib_decln"]
113         rainbow_file.write("data:"+str(ephem.hours(sidtime))+" "+str(inter)+" "+str(r)+"\n")
114         rainbow_file.close()
115         return(r)
116
117     return(data)
118
119 def calib_default_fft(db,l):
120     return(db)
121
122 #
123 # We capture various parameters from the receive chain here, because
124 #  they can affect the calibration equations.
125 #
126 #
127 def calib_set_gain(gain):
128     globals()["calib_gain_setting"] = gain
129
130 def calib_set_integ(integ):
131     globals()["calib_integ_setting"] = integ
132
133 def calib_set_bw(bw):
134     globals()["calib_bw_setting"] = bw
135
136 def calib_set_freq(freq):
137     globals()["calib_freq_setting"] = freq
138
139 def calib_set_avg_alpha(alpha):
140     globals()["calib_avg_alpha"] = alpha
141
142 def calib_set_interesting(inter):
143     globals()["calib_is_interesting"] = inter
144
145 def calib_set_decln(dec):
146     globals()["calib_decln"] = dec
147
148 def calib_set_prefix(pfx):
149     globals()["calib_prefix"] = pfx