Example #1
0
def main():
    parser = ACMEParser()
    args = parser.view_args()

    if args.parameters and not args.other_parameters:  # -p only
        cmdline_parameter = parser.get_cmdline_parameters()
        # If just a -p with no command line parameters, check the py for errors
        # Otherwise don't check for errors, the command line args might have some missing ones
        check_values = True if not cmdline_parameter else False
        original_parameter = parser.get_orig_parameters(check_values)

        # Special case, until selector parameter is added.
        if hasattr(cmdline_parameter, 'sets'):
            original_parameter.sets = cmdline_parameter.sets
        elif not hasattr(original_parameter, 'sets'):
            original_parameter.sets = SET_NAMES

        # load the default cfg files
        default_diags_paths = []
        for set_num in original_parameter.sets:
            run_type = 'model_vs_obs'
            dataset = ''
            if hasattr(original_parameter, 'dataset'):
                dataset = original_parameter.dataset
            if hasattr(original_parameter, 'run_type'):
                run_type = original_parameter.run_type
            default_diags_paths.append(
                _get_default_diags(set_num, dataset, run_type))

        other_parameters = parser.get_other_parameters(
            files_to_open=default_diags_paths)

        # Don't put the sets from the Python parameters to each of the parameters.
        # Ex. if sets=[5, 7] in the Python parameters, don't change sets in the
        # default jsons like lat_lon_ACME_default.json
        vars_to_ignore = ['sets']
        parameters = parser.get_parameters(
            cmdline_parameters=cmdline_parameter,
            orig_parameters=original_parameter,
            other_parameters=other_parameters,
            vars_to_ignore=vars_to_ignore,
            cmd_default_vars=False)

    elif not args.parameters and args.other_parameters:  # -d only
        cmdline_parameter = parser.get_cmdline_parameters()
        other_parameters = parser.get_other_parameters()
        parameters = parser.get_parameters(
            cmdline_parameters=cmdline_parameter,
            other_parameters=other_parameters,
            cmd_default_vars=False)

    elif args.parameters and args.other_parameters:  # -p and -d
        parameters = parser.get_parameters(cmd_default_vars=False)

    else:  # command line args without -p or -d
        parameters = parser.get_parameters(cmd_default_vars=False)

    dt = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    for p in parameters:
        # needed for distributed running
        # chown of all generated files to the user who ran the diags
        p.user = getpass.getuser()

        if not hasattr(p, 'results_dir'):
            p.results_dir = '{}-{}'.format('e3sm_diags_results', dt)

    if not os.path.exists(parameters[0].results_dir):
        os.makedirs(parameters[0].results_dir, 0o775)
    if parameters[0].multiprocessing:
        parameters = cdp.cdp_run.multiprocess(run_diag, parameters)
    elif parameters[0].distributed:
        parameters = cdp.cdp_run.distribute(run_diag, parameters)
    else:
        parameters = cdp.cdp_run.serial(run_diag, parameters)

    parameters = _collaspse_results(parameters)

    if not parameters:
        print(
            'There was not a single valid diagnostics run, no viewer created.')
    else:
        provenance(parameters[0].results_dir)

        if parameters[0].no_viewer:
            print(
                'Viewer not created because the no_viewer parameter is True.')
        else:
            pth = os.path.join(parameters[0].results_dir, 'viewer')
            if not os.path.exists(pth):
                os.makedirs(pth)
            create_viewer(pth, parameters, parameters[0].output_format[0])
        # load the default jsons
        default_jsons_paths = []

        for set_num in original_parameter.sets:
            datasets = ['ACME']
            if hasattr(original_parameter, 'datasets'):
                datasets = original_parameter.datasets
            for ds in datasets:
                default_jsons_paths.append(_get_default_diags(set_num, ds))
        other_parameters = parser.get_other_parameters(
            files_to_open=default_jsons_paths, check_values=False)
        # Don't put the sets from the Python parameters to the default.
        # Ex. if sets=[5, 7] in the Python parameters, don't change sets in the default jsons like lat_lon_AMWG_default.json
        vars_to_ignore = ['sets']
        parameters = parser.get_parameters(orig_parameters=original_parameter,
                                           other_parameters=other_parameters,
                                           vars_to_ignore=vars_to_ignore)

    elif not args.parameters and args.other_parameters:  # -d only
        other_parameters = parser.get_other_parameters(check_values=True)
        parameters = parser.get_parameters(other_parameters=other_parameters)

    elif args.parameters and args.other_parameters:  # -p and -d
        original_parameter = parser.get_orig_parameters(default_vars=False)
        other_parameters = parser.get_other_parameters(check_values=False)
        parameters = parser.get_parameters(orig_parameters=original_parameter,
                                           other_parameters=other_parameters)

    else:
        raise RuntimeError('You tried running the diags without -p and/or -d')