Exemplo n.º 1
0
def create_viewer(root_dir, parameters, ext):
    """Based of the parameters, find the files with
    extension ext and create the viewer in root_dir."""

    viewer = OutputViewer(path=root_dir, index_name='ACME Diagnostics')
    add_pages_and_top_row(viewer, parameters)

    for parameter in parameters:
        for set_num in parameter.sets:
            viewer.set_page("Set-{}".format(set_num))
            try:
                viewer.set_group(parameter.case_id)
            except RuntimeError:
                viewer.add_group(parameter.case_id)

            # Add all of the files with extension ext from the case_id/ folder'
            pth = get_output_dir(set_num, parameter)
            files = glob.glob(
                pth + '/*.' + ext
            )  # ex: files[0] = myresults/set5/set5_SST_HadISST/HadISST_CL-SST-SON-global.png

            for ext_fnm in files:
                fnm = ext_fnm.replace('.' + ext, '')
                fnm = fnm.split('/')[-1]  # ex: HadISST_CL-SST-SON-global
                keywords = fnm.split('-')

                # 2d vars, format is [ref_name, var, season, region]
                # 3d vars, format is [ref_name, var, plev, season, region]
                # ref_name and/or var can be something_like_this, so we always use negative indices
                ref_name = keywords[0]
                region = keywords[-1]
                season = keywords[-2]
                if keywords[-3].isdigit(
                ):  # for when we have a 3d variable, ex: keywords[-3] = 880
                    plev = keywords[-3]
                    var = keywords[-4]
                else:
                    plev = None
                    var = keywords[-3]

                if plev is None:  # 2d variable
                    #row_name = '%s %s %s' % (ref_name, var, region)
                    row_name = '%s %s' % (var, region)
                else:  # 3d variable
                    #row_name = '%s %s %s %s' % (ref_name, var, plev + ' mb ', region)
                    row_name = '%s %s %s' % (var, plev + ' mb ', region)

                try:
                    viewer.set_row(row_name)
                except RuntimeError:
                    # row of row_name wasn't in the viewer, so add it
                    viewer.add_row(row_name)
                    viewer.add_col(var)  # the description

                if parameter.case_id not in ROW_INFO[set_num]:
                    ROW_INFO[set_num][parameter.case_id] = {}
                if row_name not in ROW_INFO[set_num][parameter.case_id]:
                    ROW_INFO[set_num][parameter.case_id][row_name] = {}
                # format fnm to support relative paths
                ROW_INFO[set_num][
                    parameter.case_id][row_name][season] = os.path.join(
                        'set{}'.format(set_num), parameter.case_id, fnm)

    # add all of the files in from the case_id/ folder in ANN, DJF, MAM, JJA, SON order
    for set_num in ROW_INFO:
        viewer.set_page("Set-{}".format(set_num))
        for group in ROW_INFO[set_num]:
            viewer.set_group(group)
            for row_name in ROW_INFO[set_num][group]:
                viewer.set_row(row_name)
                for col_season in viewer.page.columns[
                        1:]:  # [1:] is to ignore 'Description' col
                    if col_season in ROW_INFO[set_num][group][row_name]:
                        fnm = ROW_INFO[set_num][group][row_name][col_season]
                        nc_files = [
                            fnm + nc_ext
                            for nc_ext in ['_test.nc', '_ref.nc', '_diff.nc']
                        ]
                        formatted_files = [{
                            'url': f,
                            'title': f
                        } for f in nc_files]
                        viewer.add_col(fnm + '.' + ext,
                                       is_file=True,
                                       title=col_season,
                                       other_files=formatted_files)
                    else:
                        # insert a blank value
                        # is_file must be True, otherwise OutputViewer indexes incorrectly
                        viewer.add_col('-----', is_file=True)

    viewer.generate_viewer(prompt_user=False)
Exemplo n.º 2
0
def create_viewer(root_dir, parameters):
    """
    Given a set of parameters for a certain set of diagnostics,
    create a single page.

    Return the title and url for this page.
    """
    viewer = OutputViewer(path=root_dir)

    set_name = parameters[0].sets[0]
    # The name that's displayed on the viewer.
    if set_name == 'zonal_mean_2d':
        display_name = 'Pressure-Latitude zonal mean contour plots'
    elif set_name == 'meridional_mean_2d':
        display_name = 'Pressure-Longitude meridional mean contour plots'

    cols = ['Description'] + seasons_used(parameters)
    viewer.add_page(display_name, short_name=set_name, columns=cols)

    # Sort the parameters so that the viewer is created in the correct order.
    # Using SEASONS.index(), we make sure we get the parameters in
    # ['ANN', 'DJF', ..., 'SON'] order instead of alphabetical.
    parameters.sort(
        key=lambda x: (x.case_id, x.variables[0], SEASONS.index(x.seasons[0])))

    for param in parameters:
        ref_name = getattr(param, 'ref_name', '')
        for var in param.variables:
            for season in param.seasons:
                for region in param.regions:

                    try:
                        viewer.set_group(param.case_id)
                    except RuntimeError:
                        viewer.add_group(param.case_id)

                    if param.run_type == 'model_vs_model':
                        row_name = '{} {}'.format(var, region)
                    else:
                        row_name = '{} {} {}'.format(var, region, ref_name)

                    try:
                        viewer.set_row(row_name)
                    except RuntimeError:
                        # A row of row_name wasn't in the viewer, so add it.
                        viewer.add_row(row_name)
                        # Adding the description for the row.
                        viewer.add_col(param.viewer_descr[var])

                    ext = param.output_format[0]
                    fnm = '{}-{}-{}-{}'.format(ref_name, var, season, region)
                    file_name = os.path.join('..', set_name, param.case_id,
                                             '{}.{}'.format(fnm, ext))
                    viewer.add_col(file_name,
                                   is_file=True,
                                   title=season,
                                   meta=create_metadata(param))

    url = viewer.generate_page()
    add_header(root_dir, os.path.join(root_dir, url), parameters)
    h1_to_h3(os.path.join(root_dir, url))

    return display_name, url
Exemplo n.º 3
0
def create_viewer(root_dir, parameters, ext):
    """Based of the parameters, find the files with
    extension ext and create the viewer in root_dir."""

    viewer = OutputViewer(path=root_dir, index_name='E3SM Diagnostics')
    _add_pages_and_top_row(viewer, parameters)

    for parameter in parameters:
        results_dir = parameter.results_dir
        for set_num in parameter.sets:
            set_num = get_set_name(set_num)

            # Filenames are like:
            # ref_name-variable-season-region
            # or
            # ref_name-variable-plev'mb'-season-region
            ref_name = parameter.ref_name
            for var in parameter.variables:
                for season in parameter.seasons:
                    for region in parameter.regions:
                        # because some parameters have plevs, there might be
                        # more than one row_name, fnm pair
                        row_name_and_fnm = []

                        if parameter.plevs == []:  # 2d variables
                            if parameter.run_type == 'model_vs_model':
                                row_name = '{} {}'.format(var, region)
                            else:
                                row_name = '{} {} {}'.format(
                                    var, region, ref_name)
                            fnm = '{}-{}-{}-{}'.format(ref_name, var, season,
                                                       region)
                            row_name_and_fnm.append((row_name, fnm))
                        else:  # 3d variables
                            for plev in parameter.plevs:
                                if parameter.run_type == 'model_vs_model':
                                    row_name = '{}-{} {}'.format(
                                        var,
                                        str(int(plev)) + 'mb', region)
                                else:
                                    row_name = '{}-{} {} {}'.format(
                                        var,
                                        str(int(plev)) + 'mb', region,
                                        ref_name)
                                fnm = '{}-{}-{}-{}-{}'.format(
                                    ref_name, var, int(plev), season, region)
                                row_name_and_fnm.append((row_name, fnm))

                        if set_num in ['lat_lon', '5']:
                            metrics_path = os.path.join(
                                results_dir, '{}'.format(set_num),
                                parameter.case_id, fnm)
                            if os.path.exists(metrics_path + '.json'):
                                _add_to_lat_lon_metrics_table(
                                    metrics_path, season, row_name)
                            else:
                                print(('JSON does not exist: {}'.format(
                                    metrics_path + '.json')))
                                continue
                        for row_name, fnm in row_name_and_fnm:
                            if parameter.case_id not in ROW_INFO[set_num]:
                                ROW_INFO[set_num][
                                    parameter.
                                    case_id] = collections.OrderedDict()
                            if row_name not in ROW_INFO[set_num][
                                    parameter.case_id]:
                                ROW_INFO[set_num][parameter.case_id][
                                    row_name] = collections.OrderedDict()
                                ROW_INFO[set_num][parameter.case_id][row_name][
                                    'descr'] = _get_description(
                                        var, parameter)
                            # each season has a image_path and metadata linked to it, thus we use a dict
                            ROW_INFO[set_num][
                                parameter.case_id][row_name][season] = {}
                            # format fnm to support relative paths
                            ROW_INFO[set_num][parameter.case_id][row_name][
                                season]['image_path'] = os.path.join(
                                    '..', '{}'.format(set_num),
                                    parameter.case_id, fnm)
                            # If ran in a container, create_metadata() will modify *_data_path and results_dir to their original value.
                            ROW_INFO[set_num][parameter.case_id][row_name][
                                season]['metadata'] = create_metadata(
                                    parameter)

    # add all of the files in from the case_id/ folder in ANN, DJF, MAM, JJA,
    # SON order
    for set_num in ROW_INFO:
        viewer.set_page("{}".format(_better_page_name(set_num)))

        for group in ROW_INFO[set_num]:
            try:
                viewer.set_group(group)
            except RuntimeError:
                viewer.add_group(group)

            for row_name in ROW_INFO[set_num][group]:
                try:
                    viewer.set_row(row_name)
                except RuntimeError:
                    # row of row_name wasn't in the viewer, so add it
                    viewer.add_row(row_name)
                    # the description, currently the var
                    viewer.add_col(ROW_INFO[set_num][group][row_name]['descr'])

                # [1:] is to ignore 'Description' col
                for col_season in viewer.page.columns[1:]:
                    if col_season not in ROW_INFO[set_num][group][row_name]:
                        viewer.add_col('-----', is_file=True, title='-----')
                    else:
                        metadata = ROW_INFO[set_num][group][row_name][
                            col_season]['metadata']
                        fnm = ROW_INFO[set_num][group][row_name][col_season][
                            'image_path']
                        formatted_files = []
                        if parameters[0].save_netcdf:
                            nc_files = [
                                fnm + nc_ext for nc_ext in
                                ['_test.nc', '_ref.nc', '_diff.nc']
                            ]
                            formatted_files = [{
                                'url': f,
                                'title': f
                            } for f in nc_files]
                        viewer.add_col(fnm + '.' + ext,
                                       is_file=True,
                                       title=col_season,
                                       other_files=formatted_files,
                                       meta=metadata)

    generate_lat_lon_metrics_table(viewer, root_dir, parameters)
    generate_lat_lon_taylor_diag(viewer, root_dir, parameters)
    viewer.generate_viewer(prompt_user=False)
    _extras(root_dir, parameters)
Exemplo n.º 4
0
def create_viewer(root_dir, parameters, ext):
    """Based of the parameters, find the files with
    extension ext and create the viewer in root_dir."""

    viewer = OutputViewer(path=root_dir, index_name='ACME Diagnostics')
    _add_pages_and_top_row(viewer, parameters)

    for parameter in parameters:
        for set_num in parameter.sets:
            set_num = get_set_name(set_num)

            # Add all of the files with extension ext from the case_id/ folder'
            pth = get_output_dir(set_num, parameter)  # ex: myresults/lat_lon

            # Filenames are like:
            # ref_name-variable-season-region
            # or
            # ref_name-variable-plev'mb'-season-region
            ref_name = parameter.ref_name
            for var in parameter.variables:
                for season in parameter.seasons:
                    for region in parameter.regions:
                        row_name_and_fnm = [
                        ]  # because some parameters have plevs, there might be more than one row_name, fnm pair

                        if parameter.plevs == []:  # 2d variables
                            row_name = '{} {}'.format(var, region)
                            fnm = '{}-{}-{}-{}'.format(ref_name, var, season,
                                                       region)
                            row_name_and_fnm.append((row_name, fnm))
                        else:  # 3d variables
                            for plev in parameter.plevs:
                                row_name = '{} {} {}'.format(
                                    var,
                                    str(int(plev)) + ' mb', region)
                                fnm = '{}-{}-{}-{}-{}'.format(
                                    ref_name, var, int(plev), season, region)
                                row_name_and_fnm.append((row_name, fnm))

                        for row_name, fnm in row_name_and_fnm:
                            if parameter.case_id not in ROW_INFO[set_num]:
                                ROW_INFO[set_num][
                                    parameter.
                                    case_id] = collections.OrderedDict()
                            if row_name not in ROW_INFO[set_num][
                                    parameter.case_id]:
                                ROW_INFO[set_num][parameter.case_id][
                                    row_name] = collections.OrderedDict()
                                ROW_INFO[set_num][parameter.case_id][row_name][
                                    'descr'] = _get_description(
                                        var, parameter)
                            # format fnm to support relative paths
                            ROW_INFO[set_num][parameter.case_id][row_name][
                                season] = os.path.join('..',
                                                       '{}'.format(set_num),
                                                       parameter.case_id, fnm)

    # add all of the files in from the case_id/ folder in ANN, DJF, MAM, JJA, SON order
    for set_num in ROW_INFO:
        viewer.set_page("{}".format(_better_page_name(set_num)))

        for group in ROW_INFO[set_num]:
            try:
                viewer.set_group(group)
            except RuntimeError:
                viewer.add_group(group)

            for row_name in ROW_INFO[set_num][group]:
                try:
                    viewer.set_row(row_name)
                except RuntimeError:
                    # row of row_name wasn't in the viewer, so add it
                    viewer.add_row(row_name)
                    viewer.add_col(
                        ROW_INFO[set_num][group][row_name]
                        ['descr'])  # the description, currently the var

                for col_season in viewer.page.columns[
                        1:]:  # [1:] is to ignore 'Description' col
                    fnm = ROW_INFO[set_num][group][row_name][col_season]
                    formatted_files = []
                    if parameters[0].save_netcdf:
                        nc_files = [
                            fnm + nc_ext
                            for nc_ext in ['_test.nc', '_ref.nc', '_diff.nc']
                        ]
                        formatted_files = [{
                            'url': f,
                            'title': f
                        } for f in nc_files]
                    viewer.add_col(fnm + '.' + ext,
                                   is_file=True,
                                   title=col_season,
                                   other_files=formatted_files)

    viewer.generate_viewer(prompt_user=False)
    _extras(root_dir, parameters)