Index: /trunk/DataCheck/DataCheck/plot_ratescan.py
===================================================================
--- /trunk/DataCheck/DataCheck/plot_ratescan.py	(revision 18202)
+++ /trunk/DataCheck/DataCheck/plot_ratescan.py	(revision 18203)
@@ -14,11 +14,14 @@
 
 Usage:
-  plot_ratescan.py [-d DATE]
+  plot_ratescan.py [-d DATE] [--cronjob | --verbose]
   plot_ratescan.py (-h | --help) 
   plot_ratescan.py --version
+  plot_ratescan.py all [--verbose]
 
 Options:
-  -h --help     Show this screen.
+  -h --help       Show this screen.
   -d --date DATE  date in typical fact format: YYYYMMDD
+  --cronjob       Suppress most output for humans, only create output in case of real malfunctions.
+  --verbose       Create even more output
 """
 from docopt import docopt
@@ -27,4 +30,5 @@
 from astropy.io import fits
 import numpy as np
+import glob
 
 # import matplotlib, without a running X server.
@@ -35,4 +39,5 @@
 outpath = '/loc_data/analysis/ratescan_plots/{y:04d}/{m:02d}/{d:02d}/{y:04d}{m:02d}{d:02d}.png'
 infile_path = '/loc_data/aux/{y:04d}/{m:02d}/{d:02d}/{y:04d}{m:02d}{d:02d}.RATE_SCAN_DATA.fits'
+pointing_file_path_template = '/loc_data/aux/{y:04d}/{m:02d}/{d:02d}/{y:04d}{m:02d}{d:02d}.DRIVE_CONTROL_POINTING_POSITION.fits'
 
 def make_outpath(file_path):
@@ -41,18 +46,47 @@
     os.makedirs(directory)
 
-def plot_ratescan(datetime_instance):
+def plot_ratescan(datetime_instance, arguments):
     dt = datetime_instance
     year, month, day = dt.year, dt.month, dt.day
 
     file_path = infile_path.format(y=year, m=month, d=day)
-    #print "opening file_path:", file_path
+    pointing_file_path = pointing_file_path_template.format(y=year, m=month, d=day)
+    if arguments['--verbose']:
+        print "opening file_path:", file_path
+
     try:
         f = fits.open(file_path)
-    
+        p_file = fits.open(pointing_file_path)
         d = f[1].data
-
+        p_data = p_file[1].data
+        
+        ids = d['Id']
+        threshold = d['Threshold']
+        trigger_rate = d['TriggerRate']
+        id_set = sorted(list(set(ids)))
+        
         plt.figure(figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
-        plt.semilogy( d['Threshold'], d['TriggerRate'], 'o:')
+        for _id in id_set:
+            starttime = _id / (24. * 3600)
+            stoptime = (_id+30) / (24. * 3600)
+            idx = np.where((p_data['Time'] >= starttime) * (p_data['Time'] < stoptime))[0]
+            zd = p_data['Zd'][idx].mean()
+            az = p_data['Az'][idx].mean()
+            # _id is starttime of ratescan in unix timestamp.
+            dt = datetime.datetime.utcfromtimestamp(_id)
+            try:
+                tresh = threshold[ids==_id]
+                rate = trigger_rate[ids==_id]
+                plt.semilogy(
+                    tresh[rate > 0.], 
+                    rate[rate > 0.], 
+                    'o:', 
+                    label=dt.strftime("%H:%M:%S")+" Zd:{0:.1f}".format(zd)
+                    )
+            except Exception as e:
+                print e
+                print 'trigger_rate', trigger_rate[ids==_id], (trigger_rate[ids==_id] < 0.).any()
         plt.grid()
+        plt.legend()
 
         plt.xlabel('Threshold [DAC]')
@@ -64,12 +98,36 @@
         make_outpath(file_path)
         plt.savefig(file_path, dpi=80)
-        print file_path, "has been written"
+        if not arguments['--cronjob']:
+            # the cronjob does not like too much output
+            print file_path, "has been written"
 
     except IOError as e: 
-        print e
+        if not arguments['--cronjob']:
+            # The cronjob might regularly try to open ratescans
+            # which have not yet been taken. So ... well, we don't care.
+            print e
 
 if __name__ == '__main__':
-    arguments = docopt(__doc__, version='plot_ratescan 0.9')
-    
+    arguments = docopt(__doc__, version='plot_ratescan 0.9.1')
+    if arguments['--verbose']:
+        print arguments
+
+    if arguments['all']:
+        my_list = sorted(glob.glob('/loc_data/aux/*/*/*/*.RATE_SCAN_DATA.fits'))
+        file_names = []
+        for x in my_list:
+            if 'bad' in x:
+                continue
+            if '_' in x.split('/')[-1].split('.')[0]:
+                continue
+            file_names.append(x)
+        
+            ymd = map(int, x.split('/')[3:6])
+            date = datetime.datetime(*ymd)
+            try:
+                plot_ratescan(date, arguments)
+            except Exception as e:
+                print e
+
     date = arguments['--date']
     now = datetime.datetime.utcnow()
@@ -78,5 +136,5 @@
         # need to get y,m,d of yesterday.
         yesterday = now - datetime.timedelta(hours=24)
-        plot_ratescan(yesterday)
+        plot_ratescan(yesterday, arguments)
     else:
         # The user supplied a date-string, which we need to parse
@@ -92,5 +150,5 @@
                 print "Please choose a better date"
         
-            plot_ratescan(date)
+            plot_ratescan(date, arguments)
         except ValueError as e:
             print e
