source: branches/trigger_burst_research/sketch.py@ 18292

Last change on this file since 18292 was 18291, checked in by dneise, 9 years ago
some mods, so the stuff can maybe also be executed by somebody else :-|
File size: 3.6 KB
Line 
1# coding: utf-8
2import calendar
3import numpy as np
4from astropy.io import fits
5import pandas as pd
6from sqlalchemy import create_engine
7
8night_int = 20150721
9
10
11def fjd(datetime_inst):
12 """ convert datetime instance to FJD
13 """
14 return calendar.timegm(datetime_inst.utctimetuple()) / (24.*3600.)
15
16def create_DB_connection():
17 from ConfigParser import SafeConfigParser
18 config = SafeConfigParser()
19 config.optionxform = str # this make the parsing case sensitive
20 config.read('config.ini')
21
22 factdb = create_engine(
23 "mysql+mysqldb://{user}:{pw}@{host}/{db}".format(
24 user=config.get('database', 'user'),
25 pw=config.get('database', 'password'),
26 host=config.get('database', 'host'),
27 db=config.get('database', 'database'),
28 )
29 )
30
31 return factdb
32
33
34def get_data_runs_from_run_db(night_int, factdb):
35 keys = [
36 'fRunID',
37 'fNight',
38 'fRunStart',
39 'fRunStop',
40 ]
41
42 sql_query = """SELECT {comma_sep_keys}
43 FROM RunInfo WHERE fRunTypeKey=1 AND fNight={night:d};
44 """
45 sql_query = sql_query.format(
46 comma_sep_keys=', '.join(keys),
47 night=night_int,
48 )
49
50 data_runs = pd.read_sql_query(
51 sql_query,
52 factdb,
53 parse_dates=['fRunStart', 'fRunStop'],
54 )
55
56 return data_runs
57
58
59def get_trigger_rates(night_int, base_path='/fact/aux/'):
60 night_string = str(night_int)
61 fits_file = fits.open(
62 base_path+'{y}/{m}/{d}/{n}.FTM_CONTROL_TRIGGER_RATES.fits'.format(
63 ni=night_string,
64 y=night_string[0:4],
65 m=night_string[4:6],
66 d=night_string[6:8],
67 )
68 )
69 trigger_rates = fits_file[1].data
70 return trigger_rates
71
72
73def add_ratio_and_more_to_dataframe(data_runs, trigger_rates):
74
75 # create new columns and pre-assign some hopefully good NULL-like-values
76 data_runs['number_of_measurements'] = 0
77 data_runs['median_of_board_rates'] = np.nan
78 data_runs['std_dev_of_board_rates'] = np.nan
79 data_runs['fBoardTriggerRateRatioAboveThreshold'] = np.nan
80
81 for dataframe_index in data_runs.index:
82 this_run = data_runs.ix[dataframe_index]
83 mask = (
84 (trigger_rates['Time'] > fjd(this_run['fRunStart'])) *
85 (trigger_rates['Time'] < fjd(this_run['fRunStop']))
86 )
87 this_board_rates = trigger_rates['BoardRate'][mask]
88
89 N = this_board_rates.shape[0]
90 data_runs.ix[dataframe_index, 'number_of_measurements'] = N
91
92 left, med, right = np.percentile(
93 this_board_rates,
94 [50-20, 50, 50+20])
95 something_like_width = (right - left)/2.
96 # 40% of the area of the normal distrubution
97 # fall between -0.52*sigma and +0.52*sigma.
98 # The estimation of sigma in a distorted CDF
99 # is less affected near the median. (but of course less accurate as well.)
100 std_dev = something_like_width / 0.52
101
102 median_board_rates = np.median(this_board_rates, axis=1)
103 over = (median_board_rates > med+3*std_dev).sum()
104
105 data_runs.ix[dataframe_index, 'median_of_board_rates'] = med
106 data_runs.ix[dataframe_index, 'std_dev_of_board_rates'] = std_dev
107 data_runs.ix[dataframe_index, 'fBoardTriggerRateRatioAboveThreshold'] = float(over)/N
108
109def main(night_int):
110 factdb = create_DB_connection()
111 data_runs = get_data_runs_from_run_db(night_int, factdb)
112 trigger_rates = get_trigger_rates(night_int)
113 add_ratio_and_more_to_dataframe(data_runs, trigger_rates)
114 return data_runs
115
116if __name__ == '__main__':
117 data_runs = main(night_int)
118 data_runs.to_csv('foo.bar', index=False)
Note: See TracBrowser for help on using the repository browser.