"""
PENBBScan2D()
Performs a scan moving in x-y axis
...
Function to perform an automate scan in 2D (x,y axis)
This function requires as input the starting point (x_start,y_start), and the end point (x_ends,y_ends) as well 
as the step sizes in both axis(step_x,step_y). All values are in mm. The range specified muss be in the interval x[0.,10.], y[0.,10.]
In addition you can specify the name of the holder or sample and time of data taking for each position
PENBBScan2D(x_start, y_start, step_x, step_y, x_ends, y_ends, HolderName::String, time_per_point)
Example: PENBBScan2D(0.0,0.0,20.0,20.0,40.0,40.0,"small",2)
...
# Arguments
- x_start: intial point in x
- y_start: intial point in y
- step_x: step size in x
- step_y: step size in y
- x_ends: final point in x
- y_ends: final point in y
- HolderName::String: name of the holder of piece you are scanning 
- time_per_point: time of data taking in each point
...
"""
function PENBBScan2D(x_start, y_start, step_x, step_y, x_ends, y_ends, HolderName::String, time_per_point)
    if x_start < 0.0 || y_start < 0.0 || x_ends > 100.0 || y_ends > 100.
        @info("Error: value out of range: you muss use values in the range x[0.,10.], y[0.,10.]")
    else
	motor = mymotor()
	Calibrate(motor)
        for i in  collect(x_start:step_x:x_ends)
            XMoveMM(i,motor)
            for j in collect(y_start:step_y:y_ends)
                @info("position: ",i,j)
                YMoveMM(j,motor)
                pos_x = string(PosX(motor))
                pos_y = string(PosY(motor))
                name_file = string("2D_PEN_Scan_Holder_",HolderName,"_x_",pos_x,"_y_",pos_y)
                settings = (fadc = "gelab-fadc08",
                    output_basename = name_file,
                    data_dir = "./",
                    conv_data_dir = "./",
                    measurement_time = time_per_point,
                    number_of_measurements = 1,
                    channels = [1,2,3,4,5,6,7],
                    trigger_threshold = 45,
                    trigger_pmt = 5,
                    peakTime = 2,
                    gapTime = 2, 
                    nPreTrig = 70,
                    nSamples = 128,
                    saveEnergy = false,
                    delete_dat = true,
                    h5_filesize_limit = 1000,
                    filter_faulty_events = false,
                    coincidence_interval = 4e-9
                );
                println(name_file)
                take_struck_data(settings, calibration_data=false)
                sleep(2)
            end
        end
        @info("PEN BB 2D scan completed, see you soon!")
    end
    
end 

"""
PENBBScan1DY()
Performs a scan moving in y axis
...
Function to perform an automate scan in 1D (y axis) keeping x constant
This function requires as input the starting point (x_start,y_start), and the end point (x_start,y_ends) as well 
as the step size(step_y). All values are in mm. The range specified muss be in the interval x[0.,10.], y[0.,10.]
In addition you can specify the name of the holder or sample and time of data taking for each position
...
# Arguments
- x_start: intial point in x
- y_start: intial point in y
- step_y: step size in y
- y_ends: final point in y
- HolderName::String: name of the holder of piece you are scanning 
- time_per_point: time of data taking in each point
...
"""
function PENBBScan1DY(x_start, y_start, step_y, y_ends, HolderName::String, time_per_point)
    if x_start < 0.0 || y_start < 0.0 || x_start > 100.0 || y_ends > 100.
        @info("Error: value out of range: you muss use soem value in the range x[0.,10.], y[0.,10.]")
    else
	motor = mymotor()
	Calibrate(motor)
        XMoveMM(x_start,motor)
        for j in collect(y_start:step_y:y_ends)
            @info("position: ",x_start,j)
            YMoveMM(j,motor)
            pos_x = string(PosX(motor))
            pos_y = string(PosY(motor))
            name_file = string("2D_PEN_Scan_Holder_",HolderName,"_x_",pos_x,"_y_",pos_y)
            settings = (fadc = "gelab-fadc08",
                    output_basename = name_file,
                    data_dir = "./",
                    conv_data_dir = "./",
                    measurement_time = time_per_point,
                    number_of_measurements = 1,
                    channels = [1,2,3,4,5,6,7],
                    trigger_threshold = 45,
                    trigger_pmt = 5,
                    peakTime = 2,
                    gapTime = 2,
                    nPreTrig = 70,
                    nSamples = 128,
                    saveEnergy = false,
                    delete_dat = true,
                    h5_filesize_limit = 1000,
                    filter_faulty_events = false,
                    coincidence_interval = 4e-9
            );
            println(name_file)
            take_struck_data(settings, calibration_data=false)
            sleep(2)
        end
        @info("PEN 1D BB scan completed, see you soon!")
    end

end

"""
PENBBScan1DX()
Performs a scan moving in x axis
...
Function to perform an automate scan in 1D (x axis) keeping y constant
This function requires as input the starting point (y_start,x_start), and the end point (y_start,x_ends) as well 
as the step size(step_x). All values are in mm. The range specified muss be in the interval x[0.,10.], y[0.,10.]
In addition you can specify the name of the holder or sample and time of data taking for each position
...
# Arguments
- x_start: intial point in x
- y_start: intial point in y
- step_x: step size in y
- x_ends: final point in y
- HolderName::String: name of the holder of piece you are scanning 
- time_per_point: time of data taking in each point
...
"""

function PENBBScan1DX(y_start, x_start, step_x, x_ends, HolderName::String, time_per_point)
    if x_start < 0.0 || y_start < 0.0 || x_start > 100.0 || y_start > 100.
        @info("Error: value out of range: you muss use soem value in the range x[0.,10.], y[0.,10.]")
    else
	motor = mymotor()
	Calibrate(motor)
        YMoveMM(y_start,motor)
        for i in collect(x_start:step_x:x_ends)
            @info("position: ",i,y_start)
            XMoveMM(i,motor)
            pos_x = string(PosX(motor))
            pos_y = string(PosY(motor))
            name_file = string("2D_PEN_Scan_Holder_",HolderName,"_x_",pos_x,"_y_",pos_y)
            settings = (fadc = "gelab-fadc08",
                    output_basename = name_file,
                    data_dir = "./",
                    conv_data_dir = "./",
                    measurement_time = time_per_point,
                    number_of_measurements = 1,
                    channels = [1,2,3,4,5,6,7],
                    trigger_threshold = 45,
                    trigger_pmt = 5,
                    peakTime = 2,
                    gapTime = 2,
                    nPreTrig = 70,
                    nSamples = 128,
                    saveEnergy = false,
                    delete_dat = true,
                    h5_filesize_limit = 1000,
                    filter_faulty_events = false,
                    coincidence_interval = 4e-9
            );
            println(name_file)
            take_struck_data(settings, calibration_data=false)
            sleep(2)
        end
        @info("PEN BB 1D scan completed, see you soon!")
    end

end

