# Greateyes scan 

#******** SCAN Settings ********
Note="DESY - "

Exp_Time= 1

X0=4200
Xstep=450
XNum_points=3

Y0=5960
Ystep=-40
YNum_points=10

GE_ROI_Line = 3

GE_AreaDet = det

GE_ROI_Image= det.getDataMatrix()
GE_Raw_Image= det.getDataMatrix()
GE_BG_Image = det.getDataMatrix()
GE_Spectrum_Conv =  det.getDataArray()
GE_Spectrum =  det.getDataArray()
GE_BG_Line = det.getArraySize2()
GE_BG_SizeX  = det.getArraySize0()
GE_BG_SizeY  = det.getArraySize1()
GE_ROI_Line = det.getArraySize2()
GE_ROI_SizeX  = det.getArraySize0()
GE_ROI_SizeY  = det.getArraySize1()
IZero_Profile = det.getDataArray()
TFY_Profile = det.getDataArray()
IZero=sin
TFY = sin
GE_Sensor_Temp = sin
GE_FrameID = sin
Press_Diag_PV= sin
Press_Spec_PV= sin
Press_Spec_HV =sin
Press_Sample_Ch=sin
Press_Diag_HV = sin
GE_Spectrum_Sum = det.getDataArray()

#******** Functions Definitions ********
def Save_Pre_Scan_Data():
    save_dataset("RAW/GE_BG_Image", GE_BG_Image.read())
    save_dataset("Processed/GE_Spectrum_Convertion", GE_Spectrum_Conv.read())
    save_dataset("Detector/GE_ROI_Line", GE_ROI_Line.read())
    save_dataset("Detector/GE_ROI_SizeX", GE_ROI_SizeX.read())
    save_dataset("Detector/GE_ROI_SizeY", GE_ROI_SizeY.read())
    save_dataset("Detector/Exposure_Time", GE_AreaDet.getExposure())
    save_dataset("Detector/GE_Open_Delay", 1)
    save_dataset("Detector/GE_Close_Delay", 1)
    save_dataset("Detector/GE_Num_Images", GE_AreaDet.getNumImages())
    save_dataset("Detector/Gain_Type", 1)
    save_dataset("Scan/Scan_Start_Time", time.ctime())
    save_dataset("Scan/Note", Note)
    #save_dataset("Scan/Note", Note)

def Create_Scan_Dataset():
    create_dataset("RAW/GE_Raw_Image", 'd', False, (Num_Images, int(GE_BG_SizeY.take()), int(GE_BG_SizeX.take())))
    create_dataset("RAW/IZero_Profile", 'd', False, (Num_Images, 100))
    create_dataset("RAW/TFY_Profile", 'd', False, (Num_Images, 100))
    create_dataset("Processed/GE_ROI_Image", 'd', False, (Num_Images, int(GE_ROI_SizeY.take()), int(GE_ROI_SizeX.take())))
    create_dataset("Processed/GE_Spectrum", 'd', False, (Num_Images, int(GE_BG_SizeX.take())))
    create_dataset("Processed/Izero", 'd', False)
    create_dataset("Processed/TFY", 'd', False)
    create_dataset("Detector/GE_Sensor_Temp", 'd', False)
    create_dataset("Scan/GE_FrameID", 'i', False)
    create_dataset("Scan/Timestamps", 'l', False)
    create_dataset("Pressure/Diagnostic_PV", 'd', False)
    create_dataset("Pressure/Diagnostic_HV", 'd', False)
    create_dataset("Pressure/Spectrometer_PV", 'd', False)
    create_dataset("Pressure/Spectrometer_HV", 'd', False)
    create_dataset("Pressure/Sample_Chamber", 'd', False)
    create_dataset("RAW/Xposition", 'd', False)
    create_dataset("RAW/Yposition", 'd', False)
    

    
def Save_Scan_Data():
    #sleep(0.2)
    append_dataset("RAW/GE_Raw_Image", GE_Raw_Image.read())
    append_dataset("RAW/IZero_Profile", IZero_Profile.take())
    append_dataset("RAW/TFY_Profile", TFY_Profile.take())
    append_dataset("Processed/GE_ROI_Image", GE_ROI_Image.read())
    append_dataset("Processed/GE_Spectrum", GE_Spectrum.take())
    append_dataset("Processed/Izero", IZero.take())
    append_dataset("Processed/TFY", TFY.take())
    append_dataset("Detector/GE_Sensor_Temp", GE_Sensor_Temp.take())
    append_dataset("Scan/GE_FrameID", GE_FrameID.take())
    append_dataset("Scan/Timestamps", GE_FrameID.getTimestampNanos())
    append_dataset("Pressure/Diagnostic_PV", Press_Diag_PV.take())
    append_dataset("Pressure/Diagnostic_HV", Press_Diag_HV.take())
    append_dataset("Pressure/Spectrometer_PV", Press_Spec_PV.take())
    append_dataset("Pressure/Spectrometer_HV", Press_Spec_HV.take())
    append_dataset("Pressure/Sample_Chamber", Press_Sample_Ch.take())
    #Scan_Progress = (100*calcprog(GE_FrameID.take(),GE_start_frame,Num_Images))
    
def Save_Pos_Scan_Data():
    sleep(1)
    save_dataset("Processed/GE_Spectrum_Sum", GE_Spectrum_Sum.read())
    save_dataset("Scan/Scan_Finish_Time", time.ctime())

#******** Test limits on sample vertical and horizontal motor ********
X0=float(X0)
Xstep=float(Xstep)
Y0=float(Y0)
Ystep=float(Ystep)

X1=X0+(XNum_points*Xstep)
Y1=Y0+(YNum_points*Ystep)
Num_Images= XNum_points*YNum_points

#******** Setting up Caenels ********
values_p_reading=1000*Exp_Time
Scan_Progress = 1

############### Scan ###############

    
#********  Scan Script Begins here  ********

# Set Nr of images to NofImages
#******** Saving Pre Scan data ********
Save_Pre_Scan_Data()

#******** Pre Scan Setup ********
print("Scan starting: " + time.ctime())
GE_start_frame =1
Create_Scan_Dataset()
#Sample_Horiz.move(X0)


#******** Main Scan Function ********

for j in range(XNum_points):
    #Sample_Horiz.move(X0+(j*Xstep))
    for i in range(YNum_points):
        #Sample_Vert.move(Y0+(i*Ystep))
        print  GE_start_frame ,  " / ", (XNum_points*YNum_points)
        time.sleep(0.001)
        Save_Scan_Data()
        GE_start_frame = GE_start_frame+1

#Save_Scan_Data()
#Save_Scan_Data()
#******** Saving Pos Scan data ********
Save_Pos_Scan_Data()

#******** Post Scan ********
print("Scan Finished: " + time.ctime())


