示例#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)
示例#2
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)
示例#3
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.
    """
    # Information for each row in the viewer.
    # The order is:
    # {
    #  case_id: {
    #       row_name: {
    #             season: filename
    #       }
    #   }
    # }
    ROW_INFO = collections.OrderedDict()
    # A similar dict, but for creating the lat-lon tables.
    LAT_LON_TABLE_INFO = collections.OrderedDict()

    # Since we're only having one set for each
    # create_viewer() call, this works.
    set_name = parameters[0].sets[0]

    viewer = OutputViewer(path=root_dir)
    cols = ['Description'] + seasons_used(parameters)
    viewer.add_page(SET_TO_NAME[set_name], short_name=set_name, columns=cols)

    for parameter in parameters:
        results_dir = parameter.results_dir

        # Filenames are like one in the two formats:
        #   ref_name-variable-season-region
        #   ref_name-variable-plev'mb'-season-region
        ref_name = getattr(parameter, 'ref_name', '')
        for var in parameter.variables:
            for season in parameter.seasons:
                for region in parameter.regions:
                    # Since some parameters have plevs, there might be
                    # more than one row_name, filename pair.
                    # Hence, this is why we have a list.
                    row_name_and_filename = []

                    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_filename.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_filename.append((row_name, fnm))

                    if set_name == 'lat_lon':
                        metrics_path = os.path.join(results_dir, '{}'.format(set_name), parameter.case_id, fnm)
                        if os.path.exists(metrics_path + '.json'):
                            _add_to_lat_lon_metrics_table(LAT_LON_TABLE_INFO, metrics_path, season, row_name)
                        else:
                            print(('JSON does not exist: {}'.format(metrics_path + '.json')))
                            continue
                    for row_name, fnm in row_name_and_filename:
                        if set_name not in ROW_INFO:
                            ROW_INFO[set_name] = collections.OrderedDict()
                        if parameter.case_id not in ROW_INFO[set_name]:
                            ROW_INFO[set_name][parameter.case_id] = collections.OrderedDict(
                            )
                        if row_name not in ROW_INFO[set_name][parameter.case_id]:
                            ROW_INFO[set_name][parameter.case_id][row_name] = collections.OrderedDict(
                            )
                            ROW_INFO[set_name][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_name][parameter.case_id][row_name][season] = {}
                        # Format the filename to support relative paths.
                        ROW_INFO[set_name][parameter.case_id][row_name][season]['image_path'] = os.path.join(
                            '..', '{}'.format(set_name), parameter.case_id, fnm)
                        ROW_INFO[set_name][parameter.case_id][row_name][season]['metadata'] = create_metadata(parameter)

    save_netcdf = parameters[0].save_netcdf
    ext = parameters[0].output_format[0]
    _add_information_to_viewer(viewer, ROW_INFO, set_name, save_netcdf, ext)

    viewer.set_page(SET_TO_NAME[set_name])
    # Return the name and the url of the current page of the viewer.
    name, url = SET_TO_NAME[set_name], viewer.generate_page()

    # Edit the final URL to add in the header.
    utils.add_header(root_dir, os.path.join(root_dir, url), parameters)
    utils.h1_to_h3(os.path.join(root_dir, url))

    if set_name == 'lat_lon':
        table_tuple = lat_lon_viewer.generate_lat_lon_metrics_table(
            LAT_LON_TABLE_INFO, SEASONS, viewer, root_dir, parameters)
        taylor_diag_tuple = lat_lon_viewer.generate_lat_lon_taylor_diag(
            LAT_LON_TABLE_INFO, SEASONS, viewer, root_dir, parameters)

        return [(name, url), table_tuple, taylor_diag_tuple]

    return (name, url)
示例#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)