Perform polyfit once regions selected

    Globals: cal_fname, data (read-only, so no declaration)

def callback_save_poly():
    '''Perform polyfit once regions selected

    Globals: cal_fname, data (read-only, so no declaration)
    '''
    import datetime
    import pylleo
    import yamlord
    import itertools

    def _check_param_regions(param, regions, cal_dict):
        msg = '''
              <b>{}</b> was not found in the calibration dictionary.

              Process that parameter and then try saving the polyfit again.
              '''.format(param)

        params_present = True
        if param not in cal_dict['parameters']:
            params_present = False
            msg.format(param)
        else:
            for region in regions:
                if region not in cal_dict['parameters'][param]:
                    params_present = False
                    msg.format('{}/{}'.format(param, region))
                    output_window.text = output_template.format(msg)

        return params_present


    def _check_index_order(param, regions, cal_dict):
        '''Check that index positions exist for each calibration region'''

        indices_present = True
        for region in regions:
            start = cal_dict['parameters'][param][region]['start']
            end = cal_dict['parameters'][param][region]['end']
            # Check if start comes after end
            if int(start) > int(end):
                indices_present = False
                msg = '''
                      The start index ({}) comes after the end index ({}).

                      Please set new start/end indexes for <b>{}/{}</b>
                      '''.format(start, end, param, region)
                msg.format(start, end, param, region)
                output_window.text = output_template.format(msg)

        return indices_present


    if datadirs_select.value != 'None':
        path_dir = os.path.join(parent_input.value, datadirs_select.value)
        cal_yaml_path = os.path.join(path_dir, cal_fname)
        cal_dict = pylleo.lleocal.read_cal(cal_yaml_path)

        # Get currently selected parameter
        param = param_select.value
        regions = region_select.options

        # Check that index positions have been recorded in `cal.yml`
        if not _check_index_order(param, regions, cal_dict):
            return None

        # Check that index positions are in sequence
        if not _check_index_order(param, regions, cal_dict):
            return None

        param = (param_select.value).lower().replace('-','_')

        try:
            msg = '''
                  Saved polyfit for <b>{}</b> to <b>{}</b>.
                  '''.format(param, cal_fname)
            output_window.text = output_template.format(msg)

            lower, upper = pylleo.lleocal.get_cal_data(data, cal_dict, param)
            poly = list(pylleo.lleocal.fit1d(lower, upper))
            poly = [float(str(i)) for i in poly]

            cal_dict['parameters'][param]['poly'] = poly
            yamlord.write_yaml(cal_dict, cal_yaml_path)
        except Exception as e:
            msg = 'Problem saving polyfit: {}'.format(e)
            output_window.text = output_template.format(msg)
    else:
        msg = '''
              You must first load data and select indices for calibration
              regions before you can save to polyfit to `cal.yml`
              '''
        output_window.text = output_template.format(msg)

    return None