def initialize_optics(tel_id, filename, file_closed = 1):
    if 'simtel.gz' in filename:
        if file_closed:
            ld.clear_lists_optics()
            load_hessio(filename)
            nextevent_hessio()
        else:
            pass
    
        ld.mirror_area.append(h.get_mirror_area(tel_id))
        ld.mirror_number.append(h.get_mirror_number(tel_id))

        if file_closed:
            close_hessio()        

    elif 'fits' in filename:
        hdulist = file_closed
        if file_closed == 1:
            ld.clear_lists_optics()
            ld.clear_lists_telescope()
            hdulist = load_fits(filename)
            teles = hdulist[1].data
            ld.telescope = teles["TelID"].tolist()  
        else:
            pass
        index = ld.telescope_id.index(tel_id)
        ld.mirror_area.append(hdulist[1].data[index]["MirrorArea"])

        if file_closed == 1:
            close_fits(hdulist)
def initialize_optics(tel_id, filename, file_closed=1):
    if 'simtel.gz' in filename:
        if file_closed:
            ld.clear_lists_optics()
            load_hessio(filename)
            nextevent_hessio()
        else:
            pass

        ld.mirror_area.append(h.get_mirror_area(tel_id))
        ld.mirror_number.append(h.get_mirror_number(tel_id))

        if file_closed:
            close_hessio()

    elif 'fits' in filename:
        hdulist = file_closed
        if file_closed == 1:
            ld.clear_lists_optics()
            ld.clear_lists_telescope()
            hdulist = load_fits(filename)
            teles = hdulist[1].data
            ld.telescope = teles["TelID"].tolist()
        else:
            pass
        index = ld.telescope_id.index(tel_id)
        ld.mirror_area.append(hdulist[1].data[index]["MirrorArea"])

        if file_closed == 1:
            close_fits(hdulist)
def initialize_optics(filename, tel_id,file_closed = True):
    if file_closed:
        ld.clear_lists_optics()
        ld.clear_lists_telescope()
    else:
        pass

    if 'simtel.gz' in filename:
        OD.initialize_hessio(filename,tel_id,file_closed)
    elif 'fits' in filename:
        OD.initialize_fits(filename,tel_id,file_closed)
def initialize_optics(filename, tel_id, file_closed=True):
    if file_closed:
        ld.clear_lists_optics()
        ld.clear_lists_telescope()
    else:
        pass

    if 'simtel.gz' in filename:
        OD.initialize_hessio(filename, tel_id, file_closed)
    elif 'fits' in filename:
        OD.initialize_fits(filename, tel_id, file_closed)
def initialize_camera(tel_id, filename, file_closed=1):
    if 'simtel.gz' in filename:
        if file_closed:
            ld.clear_lists_camera()
            load_hessio(filename)
            nextevent_hessio()
        else:
            pass

        px = h.get_pixel_position(tel_id)[0]
        py = h.get_pixel_position(tel_id)[1]
        ld.pixel_posX.append(px)
        ld.pixel_posY.append(py)
        ld.camera_class.append(
            io.guess_camera_geometry(px * u.m, py * u.m).cam_id)

        #to use this, one has to go through every event of the run...
        n_channel = h.get_num_channel(tel_id)
        ld.channel_num = n_channel
        for chan in range(n_channel):
            ld.adc_sampels.append(h.get_adc_sample(tel_id, chan).tolist())

        if file_closed:
            close_hessio()

    elif 'fits' in filename:
        hdulist = file_closed
        if file_closed == 1:
            ld.clear_lists_camera()
            ld.clear_lists_telescope()
            hdulist = load_fits(filename)
            teles = hdulist[1].data
            ld.telescope = teles["TelID"].tolist()
        else:
            pass

        index = ld.telescope_id.index(tel_id)
        ld.camera_fov.append(hdulist[1].data[index]["FOV"])

        pixel_id_cam = []
        index2 = np.where(hdulist[2].data['L0ID'] == index)[0]
        for i in index2:
            pixel_id_cam.append(hdulist[2].data[i]['PixelID'])
        ld.pixel_id.append(pixel_id_cam)

        if file_closed == 1:
            close_fits(hdulist)
def initialize_camera(tel_id, filename, file_closed = 1):
    if 'simtel.gz' in filename:
        if file_closed:
            ld.clear_lists_camera()
            load_hessio(filename)
            nextevent_hessio()
        else:
            pass
    
        px = h.get_pixel_position(tel_id)[0]
        py = h.get_pixel_position(tel_id)[1]
        ld.pixel_posX.append(px)
        ld.pixel_posY.append(py)
        ld.camera_class.append(io.guess_camera_geometry(px*u.m,py*u.m).cam_id)
        
        #to use this, one has to go through every event of the run...
        n_channel = h.get_num_channel(tel_id)
        ld.channel_num = n_channel
        for chan in range(n_channel):
            ld.adc_sampels.append(h.get_adc_sample(tel_id,chan).tolist())

        if file_closed:
            close_hessio()
        
    elif 'fits' in filename:
        hdulist = file_closed
        if file_closed == 1:
            ld.clear_lists_camera()
            ld.clear_lists_telescope()
            hdulist = load_fits(filename)
            teles = hdulist[1].data
            ld.telescope = teles["TelID"].tolist()
        else:
            pass
        
        index = ld.telescope_id.index(tel_id)
        ld.camera_fov.append(hdulist[1].data[index]["FOV"])
        
        pixel_id_cam = []
        index2 = np.where(hdulist[2].data['L0ID'] == index)[0]
        for i in index2:
            pixel_id_cam.append(hdulist[2].data[i]['PixelID'])
        ld.pixel_id.append(pixel_id_cam)
        
        if file_closed == 1:
            close_fits(hdulist)
def initialize_telescope(filename, file_closed = True):
    ld.clear_lists_telescope()
    ld.clear_lists_camera()
    ld.clear_lists_optics()

    if 'simtel.gz' in filename:
        file_closed = TD.initialize_hessio(filename,file_closed)
        for tel in ld.telescope_id:
            CD.initialize_hessio(filename,tel,file_closed)
            OD.initialize_hessio(filename,tel,file_closed)
        util_functions.close_hessio()

    elif 'fits' in filename:
        file_closed = TD.initialize_fits(filename,file_closed)
        for tel in ld.telescope_id:
            CD.initialize_fits(filename,tel,file_closed)
            OD.initialize_fits(filename,tel,file_closed)
        util_functions.close_fits(file_closed)
def initialize_telescope(filename, file_closed=True):
    ld.clear_lists_telescope()
    ld.clear_lists_camera()
    ld.clear_lists_optics()

    if 'simtel.gz' in filename:
        file_closed = TD.initialize_hessio(filename, file_closed)
        for tel in ld.telescope_id:
            CD.initialize_hessio(filename, tel, file_closed)
            OD.initialize_hessio(filename, tel, file_closed)
        util_functions.close_hessio()

    elif 'fits' in filename:
        file_closed = TD.initialize_fits(filename, file_closed)
        for tel in ld.telescope_id:
            CD.initialize_fits(filename, tel, file_closed)
            OD.initialize_fits(filename, tel, file_closed)
        util_functions.close_fits(file_closed)
def initialize_telescope(filename, file_closed = True):
    ld.clear_lists_telescope()
    ld.clear_lists_camera()

    if 'simtel.gz' in filename:
        if file_closed:
            file_closed = load_hessio(filename)
            nextevent_hessio()
        else:
            pass

        #ld.telescope_id = h.get_telescope_ids().tolist() #--> this function only can be used if the according python wrapper hase been added to pyhessio.c and hessio.py
        ld.telescope_id = h.get_teldata_list().tolist()
        ld.telescope_num = h.get_num_telescope()

    elif 'fits' in filename:
        if file_closed:
            hdulist = load_fits(filename)
            file_closed = hdulist
        else:
            pass
        teles = hdulist[1].data
        ld.telescope_id = teles["TelID"].tolist()
        ld.telescope_posX = teles["TelX"].tolist()
        ld.telescope_posY = teles["TelY"].tolist()
        ld.telescope_posZ = teles["TelZ"].tolist()
        ld.telescope_num = len(ld.telescope_id)

    for tel in ld.telescope_id:
        initialize_camera(tel,filename,file_closed)
        initialize_optics(tel,filename,file_closed)

    if 'simtel.gz' in filename:   
        close_hessio()
    elif 'fits' in filename:
        close_fits(hdulist)
def initialize_telescope(filename, file_closed=True):
    ld.clear_lists_telescope()
    ld.clear_lists_camera()

    if 'simtel.gz' in filename:
        if file_closed:
            file_closed = load_hessio(filename)
            nextevent_hessio()
        else:
            pass

        #ld.telescope_id = h.get_telescope_ids().tolist() #--> this function only can be used if the according python wrapper hase been added to pyhessio.c and hessio.py
        ld.telescope_id = h.get_teldata_list().tolist()
        ld.telescope_num = h.get_num_telescope()

    elif 'fits' in filename:
        if file_closed:
            hdulist = load_fits(filename)
            file_closed = hdulist
        else:
            pass
        teles = hdulist[1].data
        ld.telescope_id = teles["TelID"].tolist()
        ld.telescope_posX = teles["TelX"].tolist()
        ld.telescope_posY = teles["TelY"].tolist()
        ld.telescope_posZ = teles["TelZ"].tolist()
        ld.telescope_num = len(ld.telescope_id)

    for tel in ld.telescope_id:
        initialize_camera(tel, filename, file_closed)
        initialize_optics(tel, filename, file_closed)

    if 'simtel.gz' in filename:
        close_hessio()
    elif 'fits' in filename:
        close_fits(hdulist)