Beispiel #1
0
def run(config_file, override_file, save_netcdf, save_csv, save_plots,
        save_logs, debug, pdb, profile, profile_filename):
    """Execute the given model."""
    if debug:
        print(_get_version())
    logging.captureWarnings(True)
    start_time = datetime.datetime.now()
    with format_exceptions(debug, pdb, profile, profile_filename, start_time):
        if save_csv is None and save_netcdf is None:
            print(
                '!!!\nWARNING: Neither save_csv nor save_netcdf have been '
                'specified. Model will run without saving results!\n!!!\n'
            )
        tstart = start_time.strftime(_time_format)
        print('Calliope run starting at {}\n'.format(tstart))
        override_dict = {
            'run.save_logs': save_logs
        }
        model = Model(
            config_file, override_file=override_file, override_dict=override_dict
        )
        model_name = model._model_run.get_key('model.name', default='None')
        print('Model name:   {}'.format(model_name))
        msize = '{locs} locations, {techs} technologies, {times} timesteps'.format(
            locs=len(model._model_run.sets['locs']),
            techs=(
                len(model._model_run.sets['techs_non_transmission']) +
                len(model._model_run.sets['techs_transmission_names'])
            ),
            times=len(model._model_run.sets['timesteps']))
        print('Model size:   {}\n'.format(msize))
        print('Starting model run...')
        model.run()
        if save_csv:
            print('Saving CSV results to directory: {}'.format(save_csv))
            model.to_csv(save_csv)
        if save_netcdf:
            print('Saving NetCDF results to file: {}'.format(save_netcdf))
            model.to_netcdf(save_netcdf)
        if save_plots:
            print('Saving HTML file with plots to: {}'.format(save_plots))
            model.plot.summary(out_file=save_plots)
        print_end_time(start_time)
Beispiel #2
0
def run(model_file, override_file, save_netcdf, save_csv, save_plots,
        save_logs, model_format, debug, quiet, pdb, profile, profile_filename):
    """
    Execute the given model. Tries to guess from the file extension whether
    ``model_file`` is a YAML file or a pre-built model saved to NetCDF.
    This can also explicitly be set with the --model_format=yaml or
    --model_format=netcdf option.

    """
    if debug:
        print(_get_version())

    set_quietness_level(quiet)

    logging.captureWarnings(True)
    pywarning_logger = logging.getLogger('py.warnings')
    pywarning_logger.addHandler(console)

    start_time = datetime.datetime.now()
    with format_exceptions(debug, pdb, profile, profile_filename, start_time):
        if save_csv is None and save_netcdf is None:
            click.secho(
                '\n!!!\nWARNING: No options to save results have been '
                'specified.\nModel will run without saving results!\n!!!\n',
                fg='red',
                bold=True)
        tstart = start_time.strftime(_time_format)
        print('Calliope {} starting at {}\n'.format(__version__, tstart))

        # Try to determine model file type if not given explicitly
        if model_format is None:
            if model_file.split('.')[-1] in ['yaml', 'yml']:
                model_format = 'yaml'
            elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']:
                model_format = 'netcdf'
            else:
                raise ValueError(
                    'Cannot determine model file format based on file '
                    'extension for "{}". Set format explicitly with '
                    '--model_format.'.format(model_file))

        if model_format == 'yaml':
            override_dict = {'run.save_logs': save_logs}
            model = Model(model_file,
                          override_file=override_file,
                          override_dict=override_dict)
        elif model_format == 'netcdf':
            if override_file is not None:
                raise ValueError(
                    'Overrides cannot be applied when loading a pre-built '
                    'model from NetCDF. Please run without --override options.'
                )
            model = read_netcdf(model_file)
            if save_logs is not None:
                model._model_data.attrs['run.save_logs'] = save_logs
        else:
            raise ValueError('Invalid model format: {}'.format(model_format))

        print(model.info() + '\n')
        print('Starting model run...')
        model.run()

        termination = model._model_data.attrs.get('termination_condition',
                                                  'unknown')
        if save_csv:
            print('Saving CSV results to directory: {}'.format(save_csv))
            model.to_csv(save_csv)
        if save_netcdf:
            print('Saving NetCDF results to file: {}'.format(save_netcdf))
            model.to_netcdf(save_netcdf)
        if save_plots:
            if termination == 'optimal':
                print('Saving HTML file with plots to: {}'.format(save_plots))
                model.plot.summary(to_file=save_plots)
            else:
                click.secho(
                    'Model termination condition non-optimal. Not saving plots',
                    fg='red',
                    bold=True)
        print_end_time(start_time)
Beispiel #3
0
def run(model_file, override_file, save_netcdf, save_csv, save_plots,
        save_logs, model_format,
        debug, pdb, profile, profile_filename):
    """
    Execute the given model. Tries to guess from the file extension whether
    ``model_file`` is a YAML file or a pre-built model saved to NetCDF.
    This can also explicitly be set with the --model_format=yaml or
    --model_format=netcdf option.

    """
    if debug:
        print(_get_version())
    logging.captureWarnings(True)
    start_time = datetime.datetime.now()
    with format_exceptions(debug, pdb, profile, profile_filename, start_time):
        if save_csv is None and save_netcdf is None:
            print(
                '!!!\nWARNING: Neither save_csv nor save_netcdf have been '
                'specified. Model will run without saving results!\n!!!\n'
            )
        tstart = start_time.strftime(_time_format)
        print('Calliope run starting at {}\n'.format(tstart))

        # Try to determine model file type if not given explicitly
        if model_format is None:
            if model_file.split('.')[-1] in ['yaml', 'yml']:
                model_format = 'yaml'
            elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']:
                model_format = 'netcdf'
            else:
                raise ValueError(
                    'Cannot determine model file format based on file '
                    'extension for "{}". Set format explicitly with '
                    '--model_format.'.format(model_file)
                )

        if model_format == 'yaml':
            override_dict = {
                'run.save_logs': save_logs
            }
            model = Model(
                model_file, override_file=override_file, override_dict=override_dict
            )
        elif model_format == 'netcdf':
            if override_file is not None:
                raise ValueError(
                    'Overrides cannot be applied when loading a pre-built '
                    'model from NetCDF. Please run without --override options.'
                )
            model = read_netcdf(model_file)
            if save_logs is not None:
                model._model_data.attrs['run.save_logs'] = save_logs
        else:
            raise ValueError('Invalid model format: {}'.format(model_format))

        # FIXME: get this from model._model_data rather than _model_run
        model_name = model._model_data.attrs.get('model.name', 'None')
        print('Model name:   {}'.format(model_name))
        msize = '{locs} locations, {techs} technologies, {times} timesteps'.format(
            locs=len(model._model_data.coords['locs'].values),
            techs=(
                len(model._model_data.coords['techs_non_transmission'].values) +
                len(model._model_data.coords['techs_transmission_names'].values)
            ),
            times=len(model._model_data.coords['timesteps'].values))
        print('Model size:   {}\n'.format(msize))
        print('Starting model run...')
        model.run()
        if save_csv:
            print('Saving CSV results to directory: {}'.format(save_csv))
            model.to_csv(save_csv)
        if save_netcdf:
            print('Saving NetCDF results to file: {}'.format(save_netcdf))
            model.to_netcdf(save_netcdf)
        if save_plots:
            print('Saving HTML file with plots to: {}'.format(save_plots))
            model.plot.summary(out_file=save_plots)
        print_end_time(start_time)
Beispiel #4
0
def run(model_file, scenario, save_netcdf, save_csv, save_plots,
        save_logs, model_format, override_dict,
        debug, quiet, pdb, profile, profile_filename):
    """
    Execute the given model. Tries to guess from the file extension whether
    ``model_file`` is a YAML file or a pre-built model saved to NetCDF.
    This can also explicitly be set with the --model_format=yaml or
    --model_format=netcdf option.

    """
    if debug:
        print(_get_version())

    set_quietness_level(quiet)

    logging.captureWarnings(True)
    pywarning_logger = logging.getLogger('py.warnings')
    pywarning_logger.addHandler(console)

    start_time = datetime.datetime.now()
    with format_exceptions(debug, pdb, profile, profile_filename, start_time):
        if save_csv is None and save_netcdf is None:
            click.secho(
                '\n!!!\nWARNING: No options to save results have been '
                'specified.\nModel will run without saving results!\n!!!\n',
                fg='red', bold=True
            )
        tstart = start_time.strftime(_time_format)
        print('Calliope {} starting at {}\n'.format(__version__, tstart))

        # Try to determine model file type if not given explicitly
        if model_format is None:
            if model_file.split('.')[-1] in ['yaml', 'yml']:
                model_format = 'yaml'
            elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']:
                model_format = 'netcdf'
            else:
                raise ValueError(
                    'Cannot determine model file format based on file '
                    'extension for "{}". Set format explicitly with '
                    '--model_format.'.format(model_file)
                )

        if model_format == 'yaml':
            model = Model(
                model_file, scenario=scenario, override_dict=override_dict
            )
        elif model_format == 'netcdf':
            if scenario is not None or override_dict is not None:
                raise ValueError(
                    'When loading a pre-built model from NetCDF, the '
                    '--scenario and --override_dict options are not available.'
                )
            model = read_netcdf(model_file)
        else:
            raise ValueError('Invalid model format: {}'.format(model_format))

        if save_logs:
            model._model_data.attrs['run.save_logs'] = save_logs

        print(model.info() + '\n')
        print('Starting model run...')
        model.run()

        termination = model._model_data.attrs.get(
            'termination_condition', 'unknown')
        if save_csv:
            print('Saving CSV results to directory: {}'.format(save_csv))
            model.to_csv(save_csv)
        if save_netcdf:
            print('Saving NetCDF results to file: {}'.format(save_netcdf))
            model.to_netcdf(save_netcdf)
        if save_plots:
            if termination == 'optimal':
                print('Saving HTML file with plots to: {}'.format(save_plots))
                model.plot.summary(to_file=save_plots)
            else:
                click.secho(
                    'Model termination condition non-optimal. Not saving plots',
                    fg='red', bold=True
                )
        print_end_time(start_time)