Imported Upstream version 3.2.2
[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 3, 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., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 import Numeric
24 import math
25 import ephem
26 import time
27
28 #
29 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30 # NO LONGER USED
31 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32 #
33 #
34
35
36 #
37 # Simple class for allowing local definition of a calibration function
38 #  for raw samples coming from the RA detector chain.  Each observatory
39 #  is different, and rather than hacking up the main code in usrp_ra_receiver
40 #  we define the appropriate function here.
41 #
42 # For example, one could calibrate the output in Janskys, rather than
43 #  dB.
44 #
45 #
46
47 def calib_default_total_power(data):
48     r = 10.0*math.log10(data)
49     return(r)
50
51 def calib_numogate_ridge_observatory_total_power(data):
52
53     me = ephem.Observer()
54
55     #
56     # PyEphem wants lat/long as strings, rather than floats--took me quite
57     #  a long time to figure that out.  If they don't arrive as strings,
58     #  the calculations for sidereal time are complete garbage
59     #
60     me.long = globals()["calib_long"]
61     me.lat = globals()["calib_lat"]
62
63     me.date = ephem.now()
64     sidtime = me.sidereal_time()
65
66     foo = time.localtime()
67     if not "calib_prefix" in globals():
68         pfx = "./"
69     else:
70         pfx = globals()["calib_prefix"]
71     filenamestr = "%s/%04d%02d%02d%02d" % (pfx, foo.tm_year, 
72        foo.tm_mon, foo.tm_mday, foo.tm_hour)
73
74     numogate_file = open (filenamestr+".tpdat","a")
75   
76     r = (data / 409.6)
77     flt = "%6.3f" % r
78     #r = calib_default_total_power(data)
79     inter = globals()["calib_decln"]
80     integ = globals()["calib_integ_setting"]
81     fc = globals()["calib_freq_setting"]
82     fc = fc / 1000000
83     bw = globals()["calib_bw_setting"]
84     bw = bw / 1000000
85     ga = globals()["calib_gain_setting"]
86
87     now = time.time()
88
89     if not "calib_then_tpdat" in globals():
90         globals()["calib_then_tpdat"] = now
91
92     if (now - globals()["calib_then_tpdat"]) >= 20:
93         globals()["calib_then_tpdat"] = now
94     
95         numogate_file.write(str(ephem.hours(sidtime))+" "+flt+" Dn="+str(inter)+",")
96         numogate_file.write("Ti="+str(integ)+",Fc="+str(fc)+",Bw="+str(bw))
97         numogate_file.write(",Ga="+str(ga)+"\n")
98     else:
99         numogate_file.write(str(ephem.hours(sidtime))+" "+flt+"\n")
100
101     numogate_file.close()
102     return(r)
103
104 def calib_numogate_ridge_observatory_fft(data,l):
105
106     me = ephem.Observer()
107
108     #
109     # PyEphem wants lat/long as strings, rather than floats--took me quite
110     #  a long time to figure that out.  If they don't arrive as strings,
111     #  the calculations for sidereal time are complete garbage
112     #
113     me.long = globals()["calib_long"]
114     me.lat = globals()["calib_lat"]
115
116     me.date = ephem.now()
117     sidtime = me.sidereal_time()
118
119     foo = time.localtime()
120     
121     if not "calib_prefix" in globals():
122         pfx = "./"
123     else:
124         pfx = globals()["calib_prefix"]
125     filenamestr = "%s/%04d%02d%02d%02d" % (pfx, foo.tm_year, 
126        foo.tm_mon, foo.tm_mday, foo.tm_hour)
127
128     now = time.time()
129
130     if not "calib_then" in globals():
131         globals()["calib_then"] = now
132
133     delta = (l/1024)*5
134                 
135     if (now - globals()["calib_then"]) >= delta:
136
137         globals()["calib_then"] = now
138         numogate_file = open (filenamestr+".sdat","a")
139   
140         r = data
141         inter = globals()["calib_decln"]
142         fc = globals()["calib_freq_setting"]
143         fc = fc / 1000000
144         bw = globals()["calib_bw_setting"]
145         bw = bw / 1000000
146         av = globals()["calib_avg_alpha"]
147         numogate_file.write("data:"+str(ephem.hours(sidtime))+" Dn="+str(inter)+",Fc="+str(fc)+",Bw="+str(bw)+",Av="+str(av))
148         numogate_file.write(" "+str(r)+"\n")
149         numogate_file.close()
150         return(r)
151
152     return(data)
153
154 def calib_default_fft(db,l):
155     return(db)
156
157 #
158 # We capture various parameters from the receive chain here, because
159 #  they can affect the calibration equations.
160 #
161 #
162 def calib_set_gain(gain):
163     globals()["calib_gain_setting"] = gain
164     globals()["calib_then_tpdat"] = time.time() - 50
165
166 def calib_set_integ(integ):
167     globals()["calib_integ_setting"] = integ
168     globals()["calib_then_tpdat"] = time.time() - 50
169
170 def calib_set_bw(bw):
171     globals()["calib_bw_setting"] = bw
172     globals()["calib_then_tpdat"] = time.time() - 50
173
174 def calib_set_freq(freq):
175     globals()["calib_freq_setting"] = freq
176     globals()["calib_then_tpdat"] = time.time() - 50
177
178 def calib_set_avg_alpha(alpha):
179     globals()["calib_avg_alpha"] = alpha
180
181 def calib_set_interesting(inter):
182     globals()["calib_is_interesting"] = inter
183
184 def calib_set_decln(dec):
185     globals()["calib_decln"] = dec
186     globals()["calib_then_tpdat"] = time.time() - 50
187
188 def calib_set_prefix(pfx):
189     globals()["calib_prefix"] = pfx
190
191 def calib_set_long(long):
192     globals()["calib_long"] = long
193
194 def calib_set_lat(lat):
195     globals()["calib_lat"] = lat