source: trunk/DataCheck/Tools/prepare_db.sh@ 20046

Last change on this file since 20046 was 18895, checked in by Daniela Dorner, 7 years ago
added (tools to get data/fluxes from DB)
  • Property svn:executable set to *
File size: 4.8 KB
Line 
1#!/bin/bash
2
3# setup
4sqlpw=/home/$USER/.mysql.pw
5dbname=factdata20170804
6
7# DB files
8dbfile=/home/dorner/fact_db/factdata_2017-08-04.sql
9dbfileold=/home/dorner/fact_db/factdata_cp20150811.sql
10
11# cu-function (QLA)
12cuqla="CREATE FUNCTION CUQLA(n int) RETURNS double DETERMINISTIC RETURN
13 IF (n<20140520, 16.4, IF (n<20150131, 25.2, IF (n<20150716, 12.5,
14 IF (n<20160218, 20.0, IF (n<20160901, 15.1, 16.8)))))"
15# define CUISDC accordingly
16
17# mjd-function
18mjd="CREATE FUNCTION MJD(t datetime) RETURNS double DETERMINISTIC RETURN
19 ROUND(UNIX_TIMESTAMP(CONVERT_TZ(t, '+00:00', 'SYSTEM'))/86400.+40587,9) "
20
21lima="CREATE FUNCTION LiMa(sig double, bg double) RETURNS double DETERMINISTIC
22 RETURN IF (sig>0 AND bg*5>0 AND (sig*Log(sig/(sig+bg*5)*1.2/0.2)+bg*5*Log(bg*5/(sig+bg*5)*1.2))> 0,
23 Sqrt((sig*Log(sig/(sig+bg*5)*1.2/0.2)+bg*5*Log(bg*5/(sig+bg*5)*1.2))*2), 0) "
24
25excerr="CREATE FUNCTION ExcErr(sig double, bg double) RETURNS double DETERMINISTIC
26 RETURN IF ((sig + 0.2*0.2*bg*5)<0, 0, Sqrt(sig + 0.2*0.2*bg*5)) "
27
28combinedtable="create table AnalysisResultsAllQLA like AnalysisResultsRunLP"
29
30# set up DB
31echo "read-in DB-copy $dbfile to DB $dbname..."
32cat $dbfile | sed "s/factdata/$dbname/g" | mysql --defaults-file=$sqlpw -u root
33# create functions
34# (not needed if same computer and DB only updated)
35echo "Creating funciton CUQLA..."
36mysql --defaults-file=$sqlpw -u root $dbname -s -e "$cuqla"
37echo "Creating funciton Mjd..."
38mysql --defaults-file=$sqlpw -u root $dbname -s -e "$mjd"
39echo "Creating funciton LiMa..."
40mysql --defaults-file=$sqlpw -u root $dbname -s -e "$lima"
41echo "Creating funciton ExcErr..."
42mysql --defaults-file=$sqlpw -u root $dbname -s -e "$excerr"
43
44# create table with full QLA content
45echo "create and populate table with full QLA results..."
46drop="drop table if exists AnalysisResultsAllQLA"
47create="create table AnalysisResultsAllQLA like AnalysisResultsRunLP"
48copyqla="insert into AnalysisResultsAllQLA select * from AnalysisResultsRunLP"
49copyold="insert into AnalysisResultsAllQLA select fRunID, fNight, fNumEvtsAfterCleaning, "
50copyold=$copyold"fNumEvtsAfterQualCuts, fNumEvtsAfterBgCuts, fNumBgEvts, fNumSigEvts, fNumExcEvts, "
51copyold=$copyold"fNumIslandsMean, fOnTimeAfterCuts, Now() from factdata_cp20150811.AnalysisResultsRunISDC fcp "
52copyold=$copyold"where fNight<20121213"
53mysql --defaults-file=$sqlpw -u root $dbname -s -e "$drop"
54mysql --defaults-file=$sqlpw -u root $dbname -s -e "$create"
55mysql --defaults-file=$sqlpw -u root $dbname -s -e "$copyqla"
56mysql --defaults-file=$sqlpw -u root $dbname -s -e "$copyold"
57
58echo "done. Please check if table AnalysisResultsAllQLA is complete."
59
60checkdb="SELECT MIN(fNight), MAX(fNight), COUNT(*), COUNT(DISTINCT fNight) FROM "$dbname".AnalysisResultsAllQLA"
61mysql --defaults-file=$sqlpw -u root $dbname -e "$checkdb"
62
63# create functions
64exit
65
66# alternative way for CU-Function
67
68# creat table CU:
69createcu="CREATE TABLE CU (fNight int(10) unsigned DEFAULT NULL,
70 fCU double(3,1) DEFAULT NULL, fLastUpdate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
71 fValid tinyint(3) unsigned DEFAULT NULL, fAnalysis tinyint(3) unsigned DEFAULT '1')"
72
73# from 9.6.2017
74#mysql> select * from CU;
75#+----------+------+---------------------+--------+-----------+
76#| fNight | fCU | fLastUpdate | fValid | fAnalysis |
77#+----------+------+---------------------+--------+-----------+
78# --- first iteration of CU:
79#| 20111115 | 16.5 | 2017-05-30 20:50:15 | NULL | 1 |
80#| 20140520 | 24.0 | 2017-05-30 20:50:15 | NULL | 1 |
81#| 20150131 | 16.0 | 2017-05-30 20:50:15 | NULL | 1 |
82#| 20150716 | 20.0 | 2017-05-30 20:50:15 | NULL | 1 |
83#| 20160218 | 15.0 | 2017-05-30 20:50:15 | NULL | 1 |
84#| 20160901 | 16.5 | 2017-05-30 20:50:15 | NULL | 1 |
85# --- next iteration of CU:
86#| 20121212 | 16.4 | 2017-05-30 21:07:26 | 1 | 1 |
87#| 20140526 | 25.2 | 2017-05-30 21:08:28 | 1 | 1 |
88#| 20150131 | 12.5 | 2017-05-30 21:08:51 | 1 | 1 |
89#| 20150716 | 20.0 | 2017-05-30 21:09:09 | 1 | 1 |
90#| 20160218 | 15.1 | 2017-05-30 21:09:42 | 1 | 1 |
91#| 20160901 | 16.8 | 2017-05-30 21:10:15 | 1 | 1 |
92#+----------+------+---------------------+--------+-----------+
93#12 rows in set (0,00 sec)
94
95cuinsert="INSERT CU VALUES
96 (20121212, 16.4, '2017-05-30 21:07:26', 1, 1)
97 (20140526, 25.2, '2017-05-30 21:07:26', 1, 1)
98 (20150131, 12.5, '2017-05-30 21:07:26', 1, 1)
99 (20150716, 20.0, '2017-05-30 21:07:26', 1, 1)
100 (20160218, 15.1, '2017-05-30 21:07:26', 1, 1)
101 (20160901, 16.8, '2017-05-30 21:07:26', 1, 1) "
102
103delstart=" delimiter $$ "
104delend=" delimiter ; "
105cufkt="create function CU(night int, ana tinyint) returns double(4,1) deterministic begin declare cu double; set cu=0; select fCU into cu from CU where fValid=1 and fAnalysis=ana and fNight<night order by fNight desc limit 0,1; return cu; end $$ "
106
Note: See TracBrowser for help on using the repository browser.