def clicpix_radsource_test_pulse():
    # Set number and thresholds
    SETNumber = 16
    sets =        [9,      10,      11,      12,      13,      14,     15,      16]
    thresholds =  [914.55, 1092.57, 1105.83, 1017.87, 1012.36, 938.13, 1052.02, 980]
    thresholdForSample = thresholds[sets.index(SETNumber)]

    # Test pulse variables
    # Number of frames to take
    number_of_frames = 100
#    number_of_frames = 10
    # Number of test pulses
    tp_pulses = 1  
    # Amplitude of pulse
    tp_amplitudes_list = [round((x+1)*0.002,3) for x in range(90)]
    # Shutter time of CLICpix
    tp_shutter_time = 600e-6
    # Spacing of masked pixels. (2 is spacing between unmasked pixels.  So 2 covers 1 in 4 pixels.)
    mask_spacing = 4

    # Logging info
    logging.config.fileConfig('logging.conf')
    cc=ccdaq()
    cc.check_bias()
    logging.info( "Chipboard ID : %s"%cc.chipid)
    logging.info( "Temperature  : %.2f [deg C]"% cc.temperature_get())

    # Setup of CLICpix settings
    cc.clicpix.loadConfig(os.path.join(cc.path,"equalization","last","clicpix.cpc"))

    print os.path.join(cc.path,"equalization","last","clicpix.cpc")

    cc.clicpix.write_register(clicpix.REG_GCR,  0x00) # set negative polarity 0x00 
    cc.check_bias()
    cc.testpulses.set_source(5)
    cc.timing.set_shutter_time(tp_shutter_time)
    cc.testpulses.set_period(tp_shutter_time/tp_pulses)
    cc.clicpix.write_register(clicpix.REG_IKRUM, 25) #discharge current on pixel (ikrum)
    cc.clicpix.setAcqClk(9) # acquisition clock frequency (200/(value+1)) MHz
    cc.clicpix.extthreshold(thresholdForSample) 
    ROWS = 64
    COLUMNS = 64 
 
    try:
        for tp_amplitude in tp_amplitudes_list:
            cc.voltage_set(clicpix.VTEST,tp_amplitude)
            # Set the mask for the CLICpix.  Spacing variable tells you information about what pixels are covered up in the mask.
            for seq in range(mask_spacing*mask_spacing):
                storage_directory = "/storage/pixel/data/TestPulse/SET" + str(SETNumber) + "/AllResultsRaw"
                fbase=storage_directory #os.path.join(storage_directory,time_dir())
                mkdir(fbase)
                if mask_spacing>1: 
                    # There is a mask to apply to the sample
                    cc.clicpix.cnfpixel_set_softmask(1)
                    cc.clicpix.cnfpixel_set_tp_enable(0)
                    for py in range(ROWS):
                        for px in range(COLUMNS):
                            if px%mask_spacing==seq%mask_spacing and py%mask_spacing==seq/mask_spacing:
                                #print seq,px,py
                                cc.clicpix.cnfpixel_set_softmask(0,px,py)
                                cc.clicpix.cnfpixel_set_tp_enable(1,px,py)
                else: 
                    # No mask and set the parameters globally
                    cc.clicpix.cnfpixel_set_softmask(0)
                    cc.clicpix.cnfpixel_set_tp_enable(1)

                # Configure the matrix to apply the mask settings.
                cc.clicpix.configure_matrix(log=False)
  
                frame_no=0
                intframe=CLICpixFrame()
                noiseframe=CLICpixFrame()
                noisefrms=0
                start=time.time()
                cc.timing.display()
                for mf in range(number_of_frames):
                    timenow=time.time()
                    cc.timing.trg()
                    frame=cc.clicpix.get_frame()
                    _sum=np.sum(frame.cnt)
                    time.sleep(0.001)
                    frame_no+=1
                    if _sum>0:
                        plotSuffix = 'SET' + str(SETNumber) + '_Seq' + str(seq) + '_VoltagePulse' + str(int(tp_amplitude * 1000)) + 'mV_TestPulse'
                        frameFileName = plotSuffix + "_%04d.txt"%(frame_no)
                        fn = os.path.join(fbase,frameFileName)
                        frame.save_tot(fn)
                        dt=timenow-start
                        print "[%6.2f][%03d] %5d -> %s (maxpix:%d)"%(dt,frame_no,_sum,fn,np.max(frame.cnt))# frame.nonZero()
                        with open(fn, 'r') as original: data = original.read()
                        headerString = "HV-CMOS CLICpix\n"
                        headerString += "SET: %s\n"%(SETNumber)
                        headerString += "ChipID: %s\n"%(cc.chipid)
                        headerString += "Test_Pulse_Amplitude(mV): %s\n"%(tp_amplitude)
                        headerString += "Mask_Spacing: %s\n"%(mask_spacing)
                        headerString += "Sequence: %s\n"%(seq)
#                        headerString += "Frame_Number: %s\n"%(frame_no)
                        headerString += "EqualizationFile: %s\n"%(os.path.join(cc.path,"equalization","last","clicpix.cpc"))
                        with open(fn, 'w') as modified: modified.write(headerString + data)
                        intframe.cnt+=frame.cnt
                        plotSuffix = 'SET' + str(SETNumber) + '_Seq' + str(seq) + '_VoltagePulse' + str(int(tp_amplitude * 1000)) + 'mV_TestPulse'
                        filename = os.path.join(fbase,"chipid_%s_%s.txt"%(cc.chipid, plotSuffix))
                        intframe.save(filename)
                    else:
                        print "[%03d] %5d "%(frame_no,_sum)
                        filename = os.path.join(fbase,"noise.txt")
                        noiseframe.cnt+=frame.cnt
                        noiseframe.save(filename)
                        noisefrms+=1
                        f=open(filename,"a")
                        f.write("#%d\n"%noisefrms)
                        f.close()
                with open(filename, 'r') as original: data = original.read()
                headerString = "HV-CMOS CLICpix\n"
                headerString += "SET: %s\n"%(SETNumber)
                headerString += "ChipID: %s\n"%(cc.chipid)
                headerString += "Test_Pulse_Amplitude(mV): %s\n"%(tp_amplitude)
                headerString += "Mask_Spacing: %s\n"%(mask_spacing)
                headerString += "Sequence: %s\n"%(seq)
                headerString += "EqualizationFile: %s\n"%(os.path.join(cc.path,"equalization","last","clicpix.cpc"))
                with open(filename, 'w') as modified: modified.write(headerString + data)
        logging.info("Done")
    except KeyboardInterrupt:
        print "Exiting ..."
def clicpix_dac_scan():
    logging.config.fileConfig('logging.conf')
    cc=ccdaq(CLICpix=False)
#    scope=dso9254a.dso('192.168.222.2')
    fbase=os.path.join(cc.path,"scope",time_dir())
    mkdir(fbase)
    print "Chipboard ID:",cc.get_id()
    print "Temperature ", cc.temperature_get()

#    dac_list=("BLBIAS",    "VNNEW",   "BLRES",    "NU",
#           "VNCLIC",    "VN",      "VNFB",     "VNFOLL",
#           "VNLOAD",    "VNDAC",   "VPUP",     "VPCOMP",
#           "VNCOMPLD2", "VNCOMP",  "VNCOMPLD", "VNCOUT1",
#           "VNCOUT2",   "VNCOUT3", "VNBUFFER", "VPFOLL",
#           "VNBIAS",    "SOUT")
# Scan VPFOLL too!

    dac = []
    meanVoltage = []

    try:
        cc.ccpd.dac_defaults()
        cc.ccpd.set_dacs()
        cc.voltage_set(ccpd.TP,0.5)
        cc.voltage_set(ccpd.BL,0.0)
        time.sleep(0.5)

        dac_name = 'dac_scan'

        current1 = []

        for bl in np.arange(0,0.6,0.005):
            cc.voltage_set(ccpd.BL,bl)

            runningVoltage = 0
            count = 0

            for pixel in [3]: #,7,11,15,19,23,27,31,35,39,43,47,51,55,59]:
                cc.ccpd.sendConfig(mux_chn=pixel)
                
#                for code in range(64):
#                    cc.ccpd.dacs['VPFOLL']=code
#                    cc.ccpd.set_dacs()
#                for acq in range(1):
#                    fn=os.path.join(fbase,"chn%02d_%03d.dat"%(pixel,acq))
#                    print " ->",fn
#                    scope.storeSingleChnToFile(1,fn)
#                    with open(fn) as f:
#                        for line in f:
#                            data = line.split()
#                            currentTime = float(data[0])
#                            currentVoltage = float(data[1]) * 1000
#                            runningVoltage += currentVoltage

                #for chn in range(8):
                for chn in [2]:
                    l= "%d %.4f [V] %6.2f [mA] "%(chn,cc.volreg_measure_voltage(chn), 1000.0*cc.volreg_measure_current(chn))
                    current1.append(1000.0*cc.volreg_measure_current(chn))
                    print l

#                    count += 1
            dac.append(bl)
#            meanVoltage.append(runningVoltage / count)

        fig = plt.figure()
#        plt.plot(dac,meanVoltage)
        plt.plot(dac,current1)
#        fig.suptitle('DAC Scan For SET14')
        plt.xlabel('DAC (BL)')
        plt.ylabel('Current 1 [mA]')
        plt.show()
#        plt.savefig('bldac_scan_SET14.png')

    except KeyboardInterrupt:
        print "Exiting ..."
def clicpix_tot_scan():
    SETNumber = 14

    sets =        [9,      10,      11,      12,      13,      14,     15,      16]
    thresholds =  [914.55, 1092.57, 1105.83, 1017.87, 1012.36, 938.13, 1052.02, 980]
    thresholdForSample = thresholds[sets.index(SETNumber)]

    numberOfFramesToTake = 12000 # Rough calc.  90 events in a minute.  1.5 hours per pixel (= 1.5 * 15 = 22.5hrs). 8000 ~= 90 * 1.5 * 60.

    logging_start()
    cc=ccdaq()
    cc.check_bias()
    logging.info( "Chipboard ID : %s"%cc.chipid)
    logging.info( "Temperature  : %.2f [deg C]"% cc.temperature_get())
    set_storage_directory = "/storage/pixel/data/scope/calibration/SET" + str(SETNumber) 
    mkdir(set_storage_directory)

    scope=dso9254a.dso('192.168.222.2')  
    scope.conf(1)
    cc.clicpix.loadConfig(os.path.join(cc.path,"equalization","last","clicpix.cpc"))
    cc.check_bias()
    cc.clicpix.extthreshold(thresholdForSample)
    cc.clicpix.configure_matrix(verify=True)

    cc.timing._timing_ctrl.getNode("shutter_src").write(cc.timing.SHUTTER_SRC_ADV)
    cc.timing._timing_ctrl.getNode("power_src").write(cc.timing.POWER_SRC_ADV)
    cc.timing._timing_power_rise_conf.getNode("invert").write(0)
    cc.timing._timing_power_rise_conf.getNode("input_sel").write(cc.timing.ADV_INPUT_USR_PWR)
    cc.timing._timing_power_rise_delay.write(1)

    cc.timing._timing_shutter_rise_conf.getNode("invert").write(0)
    cc.timing._timing_shutter_rise_conf.getNode("input_sel").write(cc.timing.ADV_INPUT_POWER)
    cc.timing._timing_shutter_rise_delay.write(50000)

    cc.timing._timing_shutter_fall_conf.getNode("invert").write(1)
    cc.timing._timing_shutter_fall_conf.getNode("input_sel").write(cc.timing.ADV_INPUT_USR_PWR)
    cc.timing._timing_shutter_fall_delay.write(10)

    cc.timing._timing_power_fall_conf.getNode("invert").write(1)
    cc.timing._timing_power_fall_conf.getNode("input_sel").write(cc.timing.ADV_INPUT_SHUTTER)
    cc.timing._timing_power_fall_delay.write(1000)
    cc.timing.readout_ready()

    for pixel in [3,7,11,15,19,23,27,31,35,39,43,47,51,55,59]:
        cc.ccpd.sendConfig(mux_chn=pixel)
        pixel_storage_directory = "/storage/pixel/data/scope/calibration/SET" + str(SETNumber) + "/Pixel" + str(pixel)
        fbase=os.path.join(pixel_storage_directory,time_dir())
        mkdir(fbase)
        logging.info( "Data output  : %s"%fbase)

        try:
            start=time.time()
            frame_no=0
            acq=0
            ikrum=25
            clkdiv=9
            cc.clicpix.write_register(clicpix.REG_IKRUM, ikrum)  
            frame_no=0
            fn=os.path.join(fbase,"clkdiv%d_ikrum%d.dat"%(clkdiv,ikrum))
            logging.info( "Output file : %s"%fn)
            f=open(fn,"w")
            cc.clicpix.setAcqClk(clkdiv)
            # set negative polarity            
            cc.clicpix.write_register(clicpix.REG_GCR, 0x00)	
            for hits in range(numberOfFramesToTake):
                timenow=time.time()
                cc.timing._timing_ctrl.getNode("sequence_finished").write(0)
                cc.timing._timing_ctrl.getNode("sequence_finished").write(1)
                cc.timing._timing_ctrl.getNode("sequence_finished").write(0)
                cc.timing._hw.dispatch() 

                cc.timing._timing_ctrl.getNode("user_power_bit").write(0)
                cc.timing._timing_ctrl.getNode("user_power_bit").write(1)
                cc.timing._hw.dispatch()
                scope.single_seq()

                scope.wait_for_triger()
                cc.timing._timing_ctrl.getNode("user_power_bit").write(0)
                cc.timing._hw.dispatch()

                fn=os.path.join(fbase,"ev%03d.dat"%(acq))
                r=scope.get_single(1)
                bl=np.mean(r["data"][0:500])
                scope_amp=bl-min(r["data"])
         
                fn=os.path.join(fbase,"frame%05d.dat"%(frame_no))
                fw=open(fn,"w")
                for x,y in enumerate(r["data"]):
                    t=r["XIN"]*x
                    fw.write("%.4e %.4e\n"%(t,y))
                fw.close()

                frame=cc.clicpix.get_frame()
                _sum=np.sum(frame.tot)
                dt=timenow-start
                px,py=pixel,0
                print "%.3f %d %d Target_Pixel %d %d +1x_Pixel %d %d +1x+1y_Pixel %d %d +1y_Pixel %d %d -1x+1y_Pixel %d %d -1x_Pixel %s"%(scope_amp,frame.tot[px,py],frame.cnt[px,py],frame.tot[px+1,py],frame.cnt[px+1,py],frame.tot[px+1,py+1],frame.cnt[px+1,py+1],frame.tot[px,py+1],frame.cnt[px,py+1],frame.tot[px-1,py+1],frame.cnt[px-1,py+1],frame.tot[px-1,py],frame.cnt[px-1,py],fn)
                f.write("%.3f %d %d Target_Pixel %d %d +1x_Pixel %d %d +1x+1y_Pixel %d %d +1y_Pixel %d %d -1x+1y_Pixel %d %d -1x_Pixel Event_Number %d\n"%(scope_amp,frame.tot[px,py],frame.cnt[px,py],frame.tot[px+1,py],frame.cnt[px+1,py],frame.tot[px+1,py+1],frame.cnt[px+1,py+1],frame.tot[px,py+1],frame.cnt[px,py+1],frame.tot[px-1,py+1],frame.cnt[px-1,py+1],frame.tot[px-1,py],frame.cnt[px-1,py],frame_no ))
                f.flush()
                frame_no+=1
            f.close()
            logging.info("Done")
        except KeyboardInterrupt:
            print "Exiting ..."
def clicpix_dac_scan():
    logging.config.fileConfig("logging.conf")
    cc = ccdaq(CLICpix=False)
    #    scope=dso9254a.dso('192.168.222.2')
    fbase = os.path.join(cc.path, "scope", time_dir())
    mkdir(fbase)
    print "Chipboard ID:", cc.get_id()
    print "Temperature ", cc.temperature_get()

    #    dac_list=("BLBIAS",    "VNNEW",   "BLRES",    "NU",
    #           "VNCLIC",    "VN",      "VNFB",     "VNFOLL",
    #           "VNLOAD",    "VNDAC",   "VPUP",     "VPCOMP",
    #           "VNCOMPLD2", "VNCOMP",  "VNCOMPLD", "VNCOUT1",
    #           "VNCOUT2",   "VNCOUT3", "VNBUFFER", "VPFOLL",
    #           "VNBIAS",    "SOUT")
    # Scan VPFOLL too!

    setNumber = str(sys.argv[1])
    dac = []
    meanVoltageList = []
    meanCurrentList = []

    try:
        cc.ccpd.dac_defaults()
        cc.ccpd.set_dacs()
        cc.voltage_set(ccpd.TP, 0.5)
        cc.voltage_set(ccpd.BL, 0.0)
        time.sleep(0.5)

        dac_name = "dac_scan"

        currentList = []
        voltageList = []
        blList = []

        for chn in range(8):
            activeCurrentList = []
            activeVoltageList = []
            activeBLList = []

            for bl in np.arange(0, 0.6, 0.1):
                cc.voltage_set(ccpd.BL, bl)
                runningCurrent = 0
                runningVoltage = 0
                count = 0

                for pixel in [3]:  # ,7,11,15,19,23,27,31,35,39,43,47,51,55,59]:
                    cc.ccpd.sendConfig(mux_chn=pixel)

                    current = 1000.0 * cc.volreg_measure_current(chn)
                    runningCurrent += current

                    voltage = cc.volreg_measure_voltage(chn)
                    runningVoltage += voltage

                    count += 1

                meanVoltage = runningVoltage / count
                meanCurrent = runningCurrent / count

                activeCurrentList.append(meanCurrent)
                activeVoltageList.append(meanVoltage)

                activeBLList.append(bl)

            currentList.append(activeCurrentList)
            voltageList.append(activeVoltageList)
            blList.append(activeBLList)

        printString = "BLDAC0 : Current0 : Voltage0 : BLDAC1 : Current1 : Voltage1 : BLDAC2 : Current2 : Voltage2 : BLDAC3 : Current3 : Voltage3 : BLDAC4 : Current4 : Voltage4 : BLDAC5 : Current5 : Voltage5 : BLDAC6 : Current6 : Voltage6 : BLDAC7 : Current7 : Voltage7 \n"

        for idx, i in enumerate(blList[0]):
            printString += str(blList[0][idx]) + " " + str(currentList[0][idx]) + " " + str(voltageList[0][idx]) + " "
            printString += str(blList[1][idx]) + " " + str(currentList[1][idx]) + " " + str(voltageList[1][idx]) + " "
            printString += str(blList[2][idx]) + " " + str(currentList[2][idx]) + " " + str(voltageList[2][idx]) + " "
            printString += str(blList[3][idx]) + " " + str(currentList[3][idx]) + " " + str(voltageList[3][idx]) + " "
            printString += str(blList[4][idx]) + " " + str(currentList[4][idx]) + " " + str(voltageList[4][idx]) + " "
            printString += str(blList[5][idx]) + " " + str(currentList[5][idx]) + " " + str(voltageList[5][idx]) + " "
            printString += str(blList[6][idx]) + " " + str(currentList[6][idx]) + " " + str(voltageList[6][idx]) + " "
            printString += str(blList[7][idx]) + " " + str(currentList[7][idx]) + " " + str(voltageList[7][idx]) + "\n"

        results_file = open("BLDacScan_Voltage_Current_SET" + setNumber + ".txt", "w")
        results_file.write(printString)
        results_file.close()

    #        fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6), (ax7, ax8)) = plt.subplots(4, 2, sharex=True, sharey=False)
    #        fig.set_size_inches(16, 12)
    #        ax1.scatter(blList[0], currentList[0])
    #        ax1.set_title('Power Supply 0 Current SET ' + setNumber)
    #        ax1.set_ylabel('Current [mV]')
    #        ax1.ticklabel_format(useOffset=False)
    #        ax2.scatter(blList[1], currentList[1])
    #        ax2.set_title('Power Supply 1 Current SET ' + setNumber)
    #        ax2.ticklabel_format(useOffset=False)
    #        ax2.set_ylabel('Current [mA]')
    #        ax3.scatter(blList[2], currentList[2])
    #        ax3.set_title('Power Supply 2 Current SET ' + setNumber)
    #        ax3.ticklabel_format(useOffset=False)
    #        ax3.set_ylabel('Current [mA]')
    #        ax4.scatter(blList[3], currentList[3])
    #        ax4.set_title('Power Supply 3 Current SET ' + setNumber)
    #        ax4.ticklabel_format(useOffset=False)
    #        ax4.set_ylabel('Current [mA]')
    #        ax5.scatter(blList[4], currentList[4])
    #        ax5.set_title('Power Supply 4 Current SET ' + setNumber)
    #        ax5.ticklabel_format(useOffset=False)
    #        ax5.set_ylabel('Current [mA]')
    #        ax6.scatter(blList[5], currentList[5])
    #        ax6.set_title('Power Supply 5 Current SET ' + setNumber)
    #        ax6.ticklabel_format(useOffset=False)
    #        ax6.set_ylabel('Current [mA]')
    #        ax7.scatter(blList[6], currentList[6])
    #        ax7.set_title('Power Supply 6 Current SET ' + setNumber)
    #        ax7.ticklabel_format(useOffset=False)
    #        ax7.set_xlabel('DAC [BL]')
    #        ax7.set_ylabel('Current [mA]')
    #        ax8.scatter(blList[7], currentList[7])
    #        ax8.set_title('Power Supply 7 Current SET ' + setNumber)
    #        ax8.ticklabel_format(useOffset=False)
    #        ax8.set_ylabel('Current [mA')
    #        ax8.set_xlabel('DAC [BL]')
    #        plt.savefig('bldac_scan_powersupplycurrents_SET' + setNumber + '.png')
    #        plt.clf()

    #        fig2, ((ax21, ax22), (ax23, ax24), (ax25, ax26), (ax27, ax28)) = plt.subplots(4, 2, sharex=True, sharey=False)
    #        fig2.set_size_inches(16, 12)
    #        ax21.scatter(blList[0], voltageList[0])
    #        ax21.set_title('Power Supply 0 Voltage SET ' + setNumber)
    #        ax21.set_ylabel('Voltage [V]')
    #        ax21.ticklabel_format(useOffset=False)
    #        ax22.scatter(blList[1], voltageList[1])
    #        ax22.set_title('Power Supply 1 Voltage SET ' + setNumber)
    #        ax22.ticklabel_format(useOffset=False)
    #        ax22.set_ylabel('Voltage [V]')
    #        ax23.scatter(blList[2], voltageList[2])
    #        ax23.set_title('Power Supply 2 Voltage SET ' + setNumber)
    #        ax23.ticklabel_format(useOffset=False)
    #        ax23.set_ylabel('Voltage [V]')
    #        ax24.scatter(blList[3], voltageList[3])
    #        ax24.set_title('Power Supply 3 Voltage SET ' + setNumber)
    #        ax24.ticklabel_format(useOffset=False)
    #        ax24.set_ylabel('Voltage [V]')
    #        ax25.scatter(blList[4], voltageList[4])
    #        ax25.set_title('Power Supply 4 Voltage SET ' + setNumber)
    #        ax25.ticklabel_format(useOffset=False)
    #        ax25.set_ylabel('Voltage [V]')
    #        ax26.scatter(blList[5], voltageList[5])
    #        ax26.set_title('Power Supply 5 Voltage SET ' + setNumber)
    #        ax26.ticklabel_format(useOffset=False)
    #        ax26.set_ylabel('Voltage [V]')
    #        ax27.scatter(blList[6], voltageList[6])
    #        ax27.set_title('Power Supply 6 Voltage SET ' + setNumber)
    #        ax27.ticklabel_format(useOffset=False)
    #        ax27.set_xlabel('DAC [BL]')
    #        ax27.set_ylabel('Voltage [V]')
    #        ax28.scatter(blList[7], voltageList[7])
    #        ax28.set_title('Power Supply 7 Voltage SET ' + setNumber)
    #        ax28.ticklabel_format(useOffset=False)
    #        ax28.set_ylabel('Voltage [V]')
    #        ax28.set_xlabel('DAC [BL]')
    #        plt.savefig('bldac_scan_powersupplyvoltages_SET' + setNumber + '.png')

    except KeyboardInterrupt:
        print "Exiting ..."