# The umetrics module contains all functionality that SIMCA exports to Python.
import umetrics
# The os module is a standard Python module that contains operating system functionality.
import os
import tempfile

def run() :
    (files, type) = umetrics.impdata.open_file_dialog(initialfile = 'Foods_predset.xls')
    if files == None :
        print('No file was selected')
        return
    if os.path.basename(files[0]) != 'Foods_predset.xls':
        print("This example only works with the file 'Foods_predset.xls'.")
        print("The file is installed with SIMCA and can normally be found in")
        print(r"C:\Program Files\Umetrics\SIMCA 14\Program\scriptexamples\data")
        return
    data=umetrics.impdata.read_file(files, type)
    data.importspec.set_col_type(0, umetrics.impdata.ImportSpecification.columntype.primaryobsid)
    data.importspec.set_col_type(1, umetrics.impdata.ImportSpecification.columntype.secondaryobsid)
    data.importspec.set_row_type(0, umetrics.impdata.ImportSpecification.rowtype.primaryvarid)
    umetrics.simca.ProjectHandler.close_all_projects()
    project_file_name = os.path.join(tempfile.gettempdir(), "import_example.usp")
    if os.access(project_file_name, os.F_OK):
        os.unlink(project_file_name)
    project = umetrics.simca.ProjectHandler.create_project(project_file_name)
    project.create_dataset(data, "import example")
    project.save()
    umetrics.SimcaApp.set_active_project(project)

if __name__ == "__main__":
    run()