# stream.detrend('demean')
    # stream.resample(samp_rate)
    # stream.write('scripts/brightness_test_daylong.ms',format='MSEED')
    stream=obsread('scripts/brightness_test_daylong.ms')
    stream.trim(starttime=UTCDateTime('2011-09-04 17:05:00'),\
                endtime=UTCDateTime('2011-09-04 17:15:00'))#, pad=True,\
               # fill_value=0)
    # for tr in stream:
        # if tr.stats.station=='WVZ':
            # stream.remove(tr)
stream.filter('bandpass',freqmin=4.0, freqmax=8.0)
# stream.trim(stream[0].stats.starttime+90, stream[0].stats.endtime)
stream.trim(stream[0].stats.starttime, stream[0].stats.endtime, pad=True, fill_value=0)
stream.plot(size=(800,600),equal_scale=False)

instance=0

# Cut the nodes...
cutnodes=[nodes[0]]+[nodes[116]]
cutlags=np.array([lags[:,0]]+[lags[:,116]]).T
detect_templates, detect_nodes=bright_lights.brightness(stations, \
                        nodes, lags, stream,
                        brightdef.threshold, brightdef.thresh_type,\
                        brightdef.coherance, instance, matchdef, templatedef,\
                        brightdef)
plotting.threeD_gridplot(detect_nodes)
# detect_templates, detect_nodes=bright_lights.brightness(stations, \
                        # cutnodes, cutlags, stream,
                        # brightdef.threshold, brightdef.thresh_type,\
                        # brightdef.coherance, instance, matchdef, templatedef)
            stream=Stream(stream)
            print stream
    if not Prep:
        #stream_copy=stream.copy() # Keep the stream safe
        print "Running the detection routine"
        # Check that the data are okay
        detect_templates, detect_nodes=bright_lights.brightness(stations, \
                        nodes, lags, stream,
                        brightdef.threshold, brightdef.thresh_type,\
                        brightdef.coherance, instance, matchdef, templatedef,\
                        brightdef)
        del detect_templates#, stream # Delete templates from memory to conserve RAM!
        #stream=stream_copy
        nodesout+=detect_nodes
        if Split:
            plotting.threeD_gridplot(nodesout, save=brightdef.plotsave,\
                                 savefile='Detected_nodes_'+str(instance)+'.png')
        else:
            plotting.threeD_gridplot(nodesout, save=brightdef.plotsave,\
                                 savefile='Detected_nodes.png')

    else:
        for tr in stream:
            print "Writing data as: test_data/"+tr.stats.station+'-'+tr.stats.channel+\
                    '-'+str(tr.stats.starttime.year)+\
                    '-'+str(tr.stats.starttime.month).zfill(2)+\
                    '-'+str(tr.stats.starttime.day).zfill(2)+\
                    '-processed.ms'
            tr.write('test_data/'+tr.stats.station+'-'+tr.stats.channel+\
                    '-'+str(tr.stats.starttime.year)+\
                    '-'+str(tr.stats.starttime.month).zfill(2)+\
                    '-'+str(tr.stats.starttime.day).zfill(2)+\
    stream = obsread('scripts/brightness_test_daylong.ms')
    stream.trim(starttime=UTCDateTime('2011-09-04 17:05:00'),\
                endtime=UTCDateTime('2011-09-04 17:15:00'))#, pad=True,\
    # fill_value=0)
    # for tr in stream:
    # if tr.stats.station=='WVZ':
    # stream.remove(tr)
stream.filter('bandpass', freqmin=4.0, freqmax=8.0)
# stream.trim(stream[0].stats.starttime+90, stream[0].stats.endtime)
stream.trim(stream[0].stats.starttime,
            stream[0].stats.endtime,
            pad=True,
            fill_value=0)
stream.plot(size=(800, 600), equal_scale=False)

instance = 0

# Cut the nodes...
cutnodes = [nodes[0]] + [nodes[116]]
cutlags = np.array([lags[:, 0]] + [lags[:, 116]]).T
detect_templates, detect_nodes=bright_lights.brightness(stations, \
                        nodes, lags, stream,
                        brightdef.threshold, brightdef.thresh_type,\
                        brightdef.coherance, instance, matchdef, templatedef,\
                        brightdef)
plotting.threeD_gridplot(detect_nodes)
# detect_templates, detect_nodes=bright_lights.brightness(stations, \
# cutnodes, cutlags, stream,
# brightdef.threshold, brightdef.thresh_type,\
# brightdef.coherance, instance, matchdef, templatedef)