def xlsx():
    '''Prompt for a macro building spreadsheet for any instrument. Use the
    content of cell B1 to direct this spreadsheet to the correct builder.

    if cell B1 is "Glancing angle" --> build a glancing angle macro

    if cell B1 is "Sample wheel" --> build a sample wheel macro

    if cell B1 is empty --> build a sample wheel macro

    '''
    spreadsheet = present_options('xlsx')
    if spreadsheet is None:
        print(error_msg('No spreadsheet specified!'))
        return None
    #spreadsheet = os.path.join(BMMuser.folder, spreadsheet)
    wb = load_workbook(os.path.join(BMMuser.folder, spreadsheet),
                       read_only=True)
    ws = wb.active
    instrument = str(ws['B1'].value).lower()
    if instrument == 'glancing angle':
        print(bold_msg('This is a glancing angle spreadsheet'))
        pinwheel.spreadsheet(spreadsheet)
    else:
        print(bold_msg('This is a sample wheel spreadsheet'))
        wmb.spreadsheet(spreadsheet)
Beispiel #2
0
 def do_RunMacro(self):
     print(go_msg('You would like to run a measurement macro...\n'))
     macro = present_options('py')
     if macro is None:
         return
     ipython = get_ipython()
     fullpath = os.path.join(user_ns['BMMuser'].folder, macro)
     ipython.magic(f'run -i \'{fullpath}\'')
     print(disconnected_msg(f'yield from {macro[:-3]}()'))
     yield from null()
    def spreadsheet(self, spreadsheet=None, energy=False):
        '''Convert a wheel macro spreadsheet to a BlueSky plan.

        Examples
        --------
        To create a macro from a spreadsheet called "MySamples.xlsx"

        >>> xlsx('MySamples')

        To specify a change_edge() command at the beginning of the macro:

        >>> xlsx('MySamples', energy=True)

        '''
        if spreadsheet is None:
            spreadsheet = present_options('xlsx')
        if spreadsheet is None:
            print(error_msg('No spreadsheet specified!'))
            return None
        if spreadsheet[-5:] != '.xlsx':
            spreadsheet = spreadsheet + '.xlsx'
        self.source = os.path.join(self.folder, spreadsheet)
        self.basename = os.path.splitext(spreadsheet)[0]
        self.basename = re.sub('[ -]+', '_', self.basename)
        self.wb = load_workbook(self.source, read_only=True)
        self.ws = self.wb.active
        self.ini = os.path.join(self.folder, self.basename + '.ini')
        self.macro = os.path.join(self.folder, self.basename + '_macro.py')
        self.measurements = list()
        #self.do_first_change = False
        #self.close_shutters  = True
        if energy is True:
            self.do_first_change = True

        if self.ws['H5'].value.lower(
        ) == 'e0':  # accommodate older xlsx files which have e0 values in column H
            self.has_e0_column = True

        self.do_first_change = self.truefalse(self.ws['G2'].value)
        self.close_shutters = self.truefalse(self.ws['J2'].value)
        self.append_element = str(self.ws['L2'].value)

        self.instrument = str(self.ws['B1'].value).lower()

        isok, explanation = self.read_spreadsheet()
        if isok is False:
            print(error_msg(explanation))
            return None
        self.write_macro()
        return 0
Beispiel #4
0
def xlsx():
    '''Prompt for a macro building spreadsheet for any instrument. Use the
    content of cell B1 to direct this spreadsheet to the correct builder.

    if cell B1 is "Glancing angle" --> build a glancing angle macro

    if cell B1 is "Sample wheel" --> build a sample wheel macro

    if cell B1 is empty --> build a sample wheel macro

    Then prompt for the sheet, if there are more than 1 sheet in the
    spreadsheet file.
    
    '''
    spreadsheet = present_options('xlsx')
    if spreadsheet is None:
        print(error_msg('No spreadsheet specified!'))
        return None

    wb = load_workbook(os.path.join(BMMuser.folder, spreadsheet),
                       read_only=True)
    #ws = wb.active
    sheets = wb.sheetnames
    if len(sheets) == 1:
        sheet = sheets[0]
    elif len(sheets) == 2 and 'Version history' in sheets:
        sheet = sheets[0]
    else:
        print(f'Select a sheet from {spreadsheet}:\n')
        actual = []
        for i, x in enumerate(sheets):
            if x == 'Version history':
                continue
            print(f' {i+1:2}: {x}')
            actual.append(x)

        print('\n  r: return')
        choice = input("\nSelect a sheet > ")
        try:
            if int(choice) > 0 and int(choice) <= len(actual):
                sheet = actual[int(choice) - 1]
            else:
                print('No sheet specified')
                return
        except Exception as E:
            print(E)
            print('No sheet specified')
            return
    instrument = str(wb[sheet]['B1'].value).lower()

    if instrument.lower() == 'glancing angle':
        print(bold_msg('This is a glancing angle spreadsheet'))
        gawheel.spreadsheet(spreadsheet, sheet)
        BMMuser.instrument = 'glancing angle stage'
    elif instrument.lower() == 'double wheel':
        print(bold_msg('This is a double sample wheel spreadsheet'))
        wmb.spreadsheet(spreadsheet, sheet, double=True)
        BMMuser.instrument = 'double wheel'
    elif instrument.lower() == 'linkam':
        print(bold_msg('This is a Linkam spreadsheet'))
        lmb.spreadsheet(spreadsheet, sheet)
        BMMuser.instrument = 'Linkam stage'
    elif instrument.lower() == 'lakeshore':
        print(bold_msg('This is a LakeShore spreadsheet'))
        lsmb.spreadsheet(spreadsheet, sheet)
        BMMuser.instrument = 'LakeShore 331'
    elif instrument.lower() == 'grid':
        print(bold_msg('This is a motor grid spreadsheet'))
        gmb.spreadsheet(spreadsheet, sheet)
        BMMuser.instrument = 'motor grid'
    else:
        print(bold_msg('This is a sample wheel spreadsheet'))
        wmb.spreadsheet(spreadsheet, sheet, double=False)
        BMMuser.instrument = 'sample wheel'
    rkvs.set('BMM:automation:type', instrument)
    def spreadsheet(self, spreadsheet=None, sheet=None, double=False):
        '''Convert an experiment description spreadsheet to a BlueSky plan.

        Usually called with no arguments, in which case the user will
        be prompted in the bsui shell for a .xlsx file in the data
        directory.  If the .xlsx file has more that one sheet, the
        user will also be prompted for the sheet name.

        Arguments
        =========
        spreadsheet : str
           The fully resolved path to the .xlsx file
        sheet : int/str/None
           The sheet to read from the spreadsheet file. If int, 
           interpret as the index of the sheetnames list, if str 
           interpret as the name from the sheetnames list, if None 
           use the active sheet as determined by openpyxl.

        '''
        if spreadsheet is None:
            spreadsheet = present_options('xlsx')
        if spreadsheet is None:
            print(error_msg('No spreadsheet specified!'))
            return None
        if spreadsheet[-5:] != '.xlsx':
            spreadsheet = spreadsheet+'.xlsx'
        self.folder   = BMMuser.folder
        self.source   = os.path.join(self.folder, spreadsheet)
        self.basename = os.path.splitext(spreadsheet)[0]
        self.wb       = load_workbook(self.source, data_only=True, read_only=True);
        self.measurements = list()
        # self.do_first_change = False
        # self.close_shutters  = True

        self.double = double

        #print(f'-----{sheet}')
        if sheet is None:
            self.ws = self.wb.active
        elif type(sheet) is int:
            this = self.wb.sheetnames[sheet-1]
            self.ws = self.wb[this]
            self.basename = this
        elif sheet in self.wb.sheetnames:
            self.ws = self.wb[sheet]
            self.basename = sheet
        else:
            self.ws = self.wb.active
        self.basename = re.sub('[ -]+', '_', self.basename)
        self.ini      = os.path.join(self.folder, self.basename+'.ini')
        self.macro    = os.path.join(self.folder, self.basename+'_macro.py')

        if self.ws['H5'].value.lower() == 'e0':  # accommodate older xlsx files which have e0 values in column H
            self.has_e0_column = True

        if double is True:
            self.do_first_change = self.truefalse(self.ws['H2'].value)
            self.close_shutters  = self.truefalse(self.ws['K2'].value)
            self.append_element  = str(self.ws['M2'].value)
        else:
            self.do_first_change = self.truefalse(self.ws['G2'].value)
            self.close_shutters  = self.truefalse(self.ws['J2'].value)
            self.append_element  = str(self.ws['L2'].value)

        self.instrument = str(self.ws['B1'].value).lower()
        self.version = str(self.ws['B2'].value).lower()

        isok, explanation, reference = self.read_spreadsheet()
        if isok is False:
            print('\n' + error_msg(explanation))
            print(f'See: {reference}')
            return None
        self.write_macro()
        rsync_to_gdrive()
        #copy_to_gdrive(spreadsheet)
        return 0