1 | #!/usr/bin/python -i
|
---|
2 | #
|
---|
3 | # Dominik Neise
|
---|
4 | #
|
---|
5 | # copy of the /tools/cleaning/avea_vs_size.py script which was originally meant
|
---|
6 | # as an example for the AmplitudeCleaner.
|
---|
7 | #
|
---|
8 | # This script is an example how one can use the python dict, for
|
---|
9 | # book keeping.
|
---|
10 | # Note, this script is not yet runnable, since one needs to change
|
---|
11 | # quite some things in the classes...
|
---|
12 | # this is just meant to be entertaining
|
---|
13 |
|
---|
14 | from pyfact_rename import *
|
---|
15 | import os.path
|
---|
16 | import matplotlib.pyplot as plt
|
---|
17 | import numpy as np
|
---|
18 | from fir_filter import *
|
---|
19 | from extractor import *
|
---|
20 | from drs_spikes import *
|
---|
21 | from plotters import *
|
---|
22 | import time as t
|
---|
23 | from cleaners import AmplitudeCleaner
|
---|
24 | confirm_next_step = False# this is for user interaction
|
---|
25 |
|
---|
26 | import sys
|
---|
27 | import pickle
|
---|
28 |
|
---|
29 | # this little can will hold
|
---|
30 | # * all the settings,
|
---|
31 | # * all results and intermediate results
|
---|
32 | # * as well as this script itself
|
---|
33 | # and then it will be written, to an output file
|
---|
34 | can = {} # the FACT can
|
---|
35 | can['settings'] = {} # a subspace for holdind all settings
|
---|
36 | can['results'] = {} # a subspace for holding all results... or similar stuff
|
---|
37 | can['logbook'] = [] # a list of statements ... human readable logbook
|
---|
38 | can['src'] = [] # this script and other sources
|
---|
39 |
|
---|
40 | # store this scipt in the list of sources.
|
---|
41 | can['src'].append( open(sys.argv[0], 'r').read() )
|
---|
42 |
|
---|
43 | # declare filenames to work with:
|
---|
44 | can['setting']['data_file_names'] = ['/media/daten_platte/FACT/data/20120229_144.fits.gz']
|
---|
45 | can['setting']['calib_file_names'] = ['/media/daten_platte/FACT/data/20120229_132.drs.fits.gz']
|
---|
46 | # we even tell the can, where we are going to store it
|
---|
47 | can['setting']['outfile_name'] = ['./can.pkl']
|
---|
48 |
|
---|
49 | # FileChecker checks if all files exist, and appends this action to the logbook
|
---|
50 | # if one File does not exist, it will cause this script to end. (...well ... maybe)
|
---|
51 | FileChecker(can)
|
---|
52 |
|
---|
53 | # put the settings for all the extractors and similar data processors
|
---|
54 | # into the settings
|
---|
55 | can['settings']['SlidingAverage'] = [8]
|
---|
56 | can['settings']['DRSSpikes'] = []
|
---|
57 | can['settings']['GlobalMaxFinder'] = [40,200]
|
---|
58 | can['settings']['AmplitudeCleaner'] = [45,18,'return_bool_mask=False']
|
---|
59 | # then call the __init__s of the processors and give them the can...
|
---|
60 | # they will see if they can find their own init parameters ... if not they take
|
---|
61 | # the default
|
---|
62 | # in addition, they will add some lines to the logbook telling the reader
|
---|
63 | # that they were created, and what parameters they found/used.
|
---|
64 | event = RawDataFeeder(can)
|
---|
65 | despike = DRSSpikes(can)
|
---|
66 | smooth = SlidingAverage(can)
|
---|
67 | extract = GlobalMaxFinder(can)
|
---|
68 | cleaner = AmplitudeCleaner(can)
|
---|
69 |
|
---|
70 | # prepare some space in the can, for the results of this script
|
---|
71 | can['results']['areas'] = []
|
---|
72 | can['results']['sizes'] = []
|
---|
73 | # and make some shortcuts
|
---|
74 | areas = can['results']['areas']
|
---|
75 | sizes = can['results']['sizes']
|
---|
76 |
|
---|
77 | # this loop will loop over all events, out of all data files in the can,
|
---|
78 | # so the analysis loop doesn't even know, there are more files involved.
|
---|
79 | # of course in the logbook the opening and closing will be stored
|
---|
80 | for data,startcell,tt in event:
|
---|
81 | if tt==4:
|
---|
82 | data = despike(data)
|
---|
83 | data = smooth(data)
|
---|
84 | amplitude, time_of_max = extract(data)
|
---|
85 | survivors = cleaner(amplitude)
|
---|
86 |
|
---|
87 | size = 0
|
---|
88 | for pixel in survivors:
|
---|
89 | size += amplitude[pixel]
|
---|
90 |
|
---|
91 | if len(survivors) > 0:
|
---|
92 | areas.append( len(survivors) )
|
---|
93 | sizes.append( size )
|
---|
94 | # we suddenly realize, that we would like to store
|
---|
95 | # the event IDs of events, which are have no survivors
|
---|
96 | # we should scroll up ... before the loop and declare an empty list
|
---|
97 | # like: can['results']['no_survivor_ids'] = []
|
---|
98 | # but imagine we are writing a function, which is beeing called inside the loop
|
---|
99 | # and this function has no possibility to declare that ...
|
---|
100 | # well then this function can still put new things inside the can!
|
---|
101 | else:
|
---|
102 | if 'no_survivor_ids' not in can['results']:
|
---|
103 | can['results']['no_survivor_ids'] = [event.event_id.value]
|
---|
104 | else:
|
---|
105 | can['results']['no_survivor_ids'].append(event.event_id.value)
|
---|
106 |
|
---|
107 | if confirm_next_step:
|
---|
108 | user_input = raw_input("'q'-quit, 'r'-run, anything else goes one step")
|
---|
109 | number=None
|
---|
110 | try:
|
---|
111 | number=int(user_input)
|
---|
112 | except:
|
---|
113 | number=None
|
---|
114 | if user_input.find('q') != -1:
|
---|
115 | sys.exit(0)
|
---|
116 | elif user_input.find('r') != -1:
|
---|
117 | confirm_next_step = False
|
---|
118 | elif number!=None:
|
---|
119 | run += number
|
---|
120 |
|
---|
121 | # Now our analysis is done and we save the can
|
---|
122 | output = open(can['settings']['outfile_name'], 'wb')
|
---|
123 | pickle.dump(can, output)
|
---|
124 | output.close() |
---|