Retrieve an HTML land cover analysis table report from a multi exposure
    analysis.
961213
def land_cover_analysis_summary_report(feature, parent):
    
    _ = feature, parent  # NOQA
    analysis_dir = get_analysis_dir(exposure_land_cover['key'])
    if analysis_dir:
        return get_impact_report_as_string(analysis_dir)
    return None
935429
def exposure_summary_layer():
    
    project_context_scope = QgsExpressionContextUtils.projectScope(
        QgsProject.instance())
    project = QgsProject.instance()

    key = provenance_layer_analysis_impacted_id['provenance_key']
    analysis_summary_layer = project.mapLayer(
        project_context_scope.variable(key))
    if not analysis_summary_layer:
        key = provenance_layer_analysis_impacted['provenance_key']
        if project_context_scope.hasVariable(key):
            analysis_summary_layer = load_layer(
                project_context_scope.variable(key))[0]

    if not analysis_summary_layer:
        return None

    keywords = KeywordIO.read_keywords(analysis_summary_layer)
    extra_keywords = keywords.get(property_extra_keywords['key'], {})
    is_multi_exposure = extra_keywords.get(extra_keyword_analysis_type['key'])

    key = provenance_layer_exposure_summary_id['provenance_key']
    if is_multi_exposure:
        key = ('{provenance}__{exposure}').format(
            provenance=provenance_multi_exposure_summary_layers_id[
                'provenance_key'],
            exposure=exposure_place['key'])
    if not project_context_scope.hasVariable(key):
        return None

    exposure_summary_layer = project.mapLayer(
        project_context_scope.variable(key))
    if not exposure_summary_layer:
        key = provenance_layer_exposure_summary['provenance_key']
        if is_multi_exposure:
            key = ('{provenance}__{exposure}').format(
                provenance=provenance_multi_exposure_summary_layers[
                    'provenance_key'],
                exposure=exposure_place['key'])
        if project_context_scope.hasVariable(key):
            exposure_summary_layer = load_layer(
                project_context_scope.variable(key))[0]
        else:
            return None

    return exposure_summary_layer
1265861
def analysis_question_report(feature, parent):
    
    _ = feature, parent  # NOQA
    project_context_scope = QgsExpressionContextUtils.projectScope()
    key = provenance_layer_analysis_impacted['provenance_key']
    if not project_context_scope.hasVariable(key):
        return None

    analysis_dir = dirname(project_context_scope.variable(key))
    complete_html_report = get_impact_report_as_string(analysis_dir)

    requested_html_report = get_report_section(
        complete_html_report, component_id=analysis_question_component['key'])

    return requested_html_report
51706
def fetch_plos_images(article_doi, output_dir, document):
    
    log.info('Processing images for {0}...'.format(article_doi))

    #A dict of URLs for PLoS subjournals
    journal_urls = {'pgen': 'http://www.plosgenetics.org/article/{0}',
                    'pcbi': 'http://www.ploscompbiol.org/article/{0}',
                    'ppat': 'http://www.plospathogens.org/article/{0}',
                    'pntd': 'http://www.plosntds.org/article/{0}',
                    'pmed': 'http://www.plosmedicine.org/article/{0}',
                    'pbio': 'http://www.plosbiology.org/article/{0}',
                    'pone': 'http://www.plosone.org/article/{0}',
                    'pctr': 'http://clinicaltrials.ploshubs.org/article/{0}'}

    #Identify subjournal name for base URL
    subjournal_name = article_doi.split('.')[1]
    base_url = journal_urls[subjournal_name]

    #Acquire <graphic> and <inline-graphic> xml elements
    graphics = document.document.getroot().findall('.//graphic')
    graphics += document.document.getroot().findall('.//inline-graphic')

    #Begin to download
    log.info('Downloading images, this may take some time...')
    for graphic in graphics:
        nsmap = document.document.getroot().nsmap
        xlink_href = graphic.attrib['{' + nsmap['xlink'] + '}' + 'href']

        #Equations are handled a bit differently than the others
        #Here we decide that an image name starting with "e" is an equation
        if xlink_href.split('.')[-1].startswith('e'):
            resource = 'fetchObject.action?uri=' + xlink_href + '&representation=PNG'
        else:
            resource = xlink_href + '/largerimage'
        full_url = base_url.format(resource)
        try:
            image = urllib.request.urlopen(full_url)
        except urllib.error.HTTPError as e:
            if e.code == 503:  # Server overload error
                time.sleep(1)  # Wait a second
                try:
                    image = urllib.request.urlopen(full_url)
                except:
                    return False  # Happened twice, give up
            else:
                log.error('urllib.error.HTTPError {0}'.format(e.code))
                return False
        else:
            img_name = xlink_href.split('.')[-1] + '.png'
            img_path = os.path.join(output_dir, img_name)
            with open(img_path, 'wb') as output:
                output.write(image.read())
            log.info('Downloaded image {0}'.format(img_name))
    log.info('Done downloading images')
    return True
589195
def default_inasafe_html_resources(feature, parent):
    
    _ = feature, parent  # NOQA
    project_context_scope = QgsExpressionContextUtils.projectScope()
    key = provenance_layer_analysis_impacted['provenance_key']
    if not project_context_scope.hasVariable(key):
        return None

    analysis_dir = dirname(project_context_scope.variable(key))
    complete_html_report = get_impact_report_as_string(analysis_dir)

    requested_html_report = get_report_section(
        complete_html_report,
        component_id='inasafe-html-resources',
        container_wrapper_format=u'{section_content}')

    return requested_html_report
1290945
def make_report(plots, path):
    '''
    
    '''
    logging.info("Writing html report.")
    html_head = """<!DOCTYPE html>
    <html>
        <head>
        <meta charset="UTF-8">
            <style>
            table, th, td {
                text-align: left;
                padding: 2px;
                /* border: 1px solid black;
                border-collapse: collapse; */
            }
            h2 {
                line-height: 0pt;
            }
            </style>
            <title>NanoComp Report</title>
        </head>"""
    html_content = ["\n<body>\n<h1>NanoComp report</h1>"]
    html_content.append("<h2>Summary statistics</h2>")
    html_content.append(utils.stats2html(path + "NanoStats.txt"))
    html_content.append('\n<br>\n<br>\n<br>\n<br>')
    html_content.append("<h2>Plots</h2>")
    for plot in plots:
        html_content.append("\n<h3>" + plot.title + "</h3>\n" + plot.encode())
        html_content.append('\n<br>\n<br>\n<br>\n<br>')
    html_body = '\n'.join(html_content) + "</body></html>"
    html_str = html_head + html_body
    with open(path + "NanoComp-report.html", "w") as html_file:
        html_file.write(html_str)
    return path + "NanoComp-report.html"
442053
def parse(self, ssos_result_filename_or_lines, mpc_observations=None):
        
        table_reader = ascii.get_reader(Reader=ascii.Basic)
        table_reader.inconsistent_handler = self._skip_missing_data
        table_reader.header.splitter.delimiter = '\t'
        table_reader.data.splitter.delimiter = '\t'
        ssos_table = table_reader.read(ssos_result_filename_or_lines)

        dbimage_list = storage.list_dbimages(dbimages=storage.DBIMAGES)
        logger.debug("Comparing to {} observations in dbimages: {}".format(len(dbimage_list), storage.DBIMAGES))
        sources = []
        observations = []
        source_readings = []

        if mpc_observations is not None and isinstance(mpc_observations[0], mpc.Observation):
            orbit = Orbfit(mpc_observations)
        else:
            from mp_ephem import horizons
            start_time = Time(min(ssos_table['MJD']), format='mjd')
            stop_time = Time(max(ssos_table['MJD']), format='mjd')
            step_size = 5.0 * units.hour
            orbit = horizons.Body(self.provisional_name, start_time, stop_time, step_size)

        warnings.filterwarnings('ignore')
        logger.info("Loading {} observations\n".format(len(ssos_table)))
        expnums_examined = []
        for row in ssos_table:
            # Trim down to OSSOS-specific images

            logger.debug("Checking row: {}".format(row))
            if (row['Filter'] not in parameters.OSSOS_FILTERS) or row['Image_target'].startswith('WP'):
                logger.debug("Failed filter / target name check")
                continue

            # check if a dbimages object exists
            # For CFHT/MegaCam strip off the trailing character to get the exposure number.
            ftype = row['Image'][-1]
            expnum = row['Image'][:-1]
            if str(expnum) not in dbimage_list:
                logger.debug("Expnum: {} Failed dbimage list check".format(expnum))
                continue
            logger.debug("Expnum: {} Passed dbimage list check".format(expnum))
            # The file extension is the ccd number + 1 , or the first extension.
            ccd = int(row['Ext'])-1
            if 39 < ccd < 0 or ccd < 0:
                ccd = None
            x = row['X'] * units.pix
            y = row['Y'] * units.pix
            ra = row['Object_RA'] * units.degree
            dec = row['Object_Dec'] * units.degree
            ssois_coordinate = SkyCoord(ra, dec)
            mjd = row['MJD'] * units.day

            # if not 0 < x.value < 2060 or not 0 < y.value < 4700:
            #    continue

            obs_date = Time(mjd, format='mjd', scale='utc')
            logger.info("Calling predict")
            orbit.predict(obs_date)
            logger.info("Done calling predict")
            if orbit.dra > 4 * units.arcminute or orbit.ddec > 4.0 * units.arcminute:
                print "Skipping entry as orbit uncertainty at date {} is large.".format(obs_date)
                continue
            if expnum in expnums_examined:
                logger.debug("Already checked this exposure.")
                continue
            expnums_examined.append(expnum)

            logger.debug(("SSOIS Prediction: exposure:{} ext:{} "
                          "ra:{} dec:{} x:{} y:{}").format(expnum, ccd, ra, dec, x, y))

            logger.debug(("Orbfit Prediction: "
                          "ra:{} dec:{} ").format(orbit.coordinate.ra.to(units.degree),
                                                  orbit.coordinate.dec.to(units.degree)))
            logger.info("Building Observation")
            observation = SSOSParser.build_source_reading(expnum, ccd, ftype=ftype)
            observation.mjd = mjd
            from_input_file = observation.rawname in self.input_rawnames
            # compare to input observation list.
            previous = False
            mpc_observation = None
            if from_input_file:
                for mpc_observation in mpc_observations:
                    try:
                        if mpc_observation.comment.frame.strip() == observation.rawname:
                            # only skip previous obseravtions if not discovery.
                            previous = not mpc_observation.discovery
                            break
                    except Exception as e:
                        logger.debug(str(e))
                        pass
                    mpc_observation = None

            # skip previously measured observations if requested.
            if self.skip_previous and ( previous or observation.rawname in self.null_observations):
                continue

            logger.info('built observation {}'.format(observation))
            observations.append(observation)
            null_observation = observation.rawname in self.null_observations

            ddec = orbit.ddec + abs(orbit.coordinate.dec - ssois_coordinate.dec)
            dra = orbit.dra + abs(orbit.coordinate.ra - ssois_coordinate.ra)

            logger.info(" Building SourceReading .... \n")
            source_reading = astrom.SourceReading(x=x, y=y, x0=x, y0=y,
                                                  ra=orbit.coordinate.ra.to(units.degree).value,
                                                  dec=orbit.coordinate.dec.to(units.degree).value,
                                                  xref=x, yref=y, obs=observation,
                                                  ssos=True, from_input_file=from_input_file,
                                                  dx=dra, dy=ddec, pa=orbit.pa,
                                                  null_observation=null_observation)
            source_reading.mpc_observation = mpc_observation
            source_readings.append(source_reading)
            logger.info("Source Reading Built")

        # build our array of SourceReading objects
        sources.append(source_readings)

        warnings.filterwarnings('once')

        return SSOSData(observations, sources, self.provisional_name)
1248706
def plots_html_page(query_module):
    

    # page template
    template = jenv.get_template("analysis.html")

    # container for template context
    context = dict(extended=config.EXTENDED)

    # a database client/session to run queries in
    cl = client.get_client()
    session = cl.create_session()

    # general styling
    seaborn.set_style('whitegrid')

    #
    #  plot: painting area by decade, with linear regression
    #

    decade_df = query_module.decade_query()

    pix_size = pixels_to_inches((600, 400))
    ax = seaborn.lmplot(x='decade', y='area', data=decade_df,
                        size=pix_size[1], aspect=pix_size[0] / pix_size[1],
                        scatter_kws={"s": 30, "alpha": 0.3})
    ax.set(xlabel='Decade', ylabel='Area, m^2')
    context['area_by_decade_svg'] = fig_to_svg(plt.gcf())
    plt.close('all')

    #
    #  plot: painting area by gender, with logistic regression
    #

    if config.EXTENDED:
        gender_df = query_module.gender_query()

        pix_size = pixels_to_inches((600, 400))
        g = seaborn.FacetGrid(gender_df, hue="gender", margin_titles=True,
                              size=pix_size[1], aspect=pix_size[0] / pix_size[1])
        bins = np.linspace(0, 5, 30)
        g.map(plt.hist, "area", bins=bins, lw=0, alpha=0.5, normed=True)
        g.axes[0, 0].set_xlabel('Area, m^2')
        g.axes[0, 0].set_ylabel('Percentage of paintings')
        context['area_by_gender_svg'] = fig_to_svg(plt.gcf())
        plt.close('all')

    #
    # render template
    #

    out_file = path.join(out_dir, "analysis.html")
    html_content = template.render(**context)
    with open(out_file, 'w') as f:
        f.write(html_content)

    # done, clean up
    plt.close('all')
    session.close()
1127365
public static Document getResultSetAsXML(ResultSet rs, String tableName) throws Exception {
        return getResultSetAsXML(rs, tableName, "");
    }
412414
def get(self, slug, xdata, ydatasets, label, opts, style, ctype):
        
        xdataset = self._format_list(xdata)
        width = "100%"
        height = "300px"
        if opts is not None:
            if "width" in opts:
                width = str(opts["width"])
            if "height" in opts:
                height = str(opts["height"])
        stylestr = '<style>#container_' + slug + \
            ' { width:' + width + ' !important; height:' + \
            height + ' !important}</style>\n'
        html = stylestr
        html += '<div id="container_' + slug + \
            '"><canvas id="canvas_' + slug + '"></canvas></div>\n'
        html += '<script>\n'
        html += 'var data = {\n'
        html += 'labels: ' + xdataset + ',\n'
        html += 'datasets:[\n'
        colors = None
        if "color" in style:
            colors = style["color"]
        i = 0
        for dataset in ydatasets:
            name = dataset["name"]
            data = dataset["data"]
            html += self._get_dataset(data, name, colors)
            if i < len(ydatasets) - 1:
                html += ","
            i += 1
        html += ']\n'
        html += '}\n'

        html += 'window.onload = function() {'
        html += 'var ctx = document.getElementById("canvas_' + \
                slug + '").getContext("2d");'
        html += 'window.myChart = new Chart(ctx, {'
        html += 'type: "' + ctype + '",'
        html += 'data: data,'
        html += 'options: {'
        html += 'spanGaps: false,'
        html += 'responsive: true,'
        html += 'maintainAspectRatio: false,'
        if "legend" in opts:
            html += 'legend: {'
            html += 'position: "' + opts["legend"] + '",'
            html += '},'
        else:
            html += 'legend: {'
            html += 'display: false,'
            html += '},'
        if "title" in opts:
            html += 'title: {'
            html += 'display: true,'
            html += 'text: "' + opts["title"] + '"'
            html += '}'
        html += '}'
        html += '});'
        html += '};'
        html += '</script>\n'
        return html
662816
def structure_analysis_summary_report(feature, parent):
    
    _ = feature, parent  # NOQA
    analysis_dir = get_analysis_dir(exposure_structure['key'])
    if analysis_dir:
        return get_impact_report_as_string(analysis_dir)
    return None
723050
def render_analyses_table(self, table="lab"):
        
        if table not in ["lab", "field", "qc"]:
            raise KeyError("Table '{}' does not exist".format(table))
        view_name = "table_{}_analyses".format(table)
        view = api.get_view(
            view_name, context=self.context, request=self.request)
        # Call listing hooks
        view.update()
        view.before_render()
        return view.ajax_contents_table()
1043980
def get_report_html(self):
        
        res = '<h2>Project:' + self.nme  + '</h2>'
        res += '<p>' + self.desc + '</p>'
        res += '<p>' + self.fldr + '</p>'
        
        res += '<BR><h3>TABLES</h3>'
        
        for t in self.datatables:
            res += '<b>' + t.name + '<b><BR>'
            res += '<p>' + str(t) + '</p>'
        return res
1327253
def getAsGrassAsciiRaster(self, tableName, rasterId=1, rasterIdFieldName='id', rasterFieldName='raster', newSRID=None):
        
        # Get raster in ArcInfo Grid format
        arcInfoGrid = self.getAsGdalRaster(rasterFieldName, tableName, rasterIdFieldName, rasterId, 'AAIGrid', newSRID).splitlines()

        ## Convert arcInfoGrid to GRASS ASCII format ##
        # Get values from header which look something this:
        # ncols        67
        # nrows        55
        # xllcorner    425802.32143212341
        # yllcorner    44091450.41551345213
        # cellsize     90.0000000
        # ...
        nCols = int(arcInfoGrid[0].split()[1])
        nRows = int(arcInfoGrid[1].split()[1])
        xLLCorner = float(arcInfoGrid[2].split()[1])
        yLLCorner = float(arcInfoGrid[3].split()[1])
        cellSize = float(arcInfoGrid[4].split()[1])

        # Remove old headers
        for i in range(0, 5):
            arcInfoGrid.pop(0)

        # Check for NODATA_value row and remove if it is there
        if 'NODATA_value' in arcInfoGrid[0]:
            arcInfoGrid.pop(0)

        ## Calculate values for GRASS ASCII headers ##
        # These should look like this:
        # north: 4501028.972140
        # south: 4494548.972140
        # east: 460348.288604
        # west: 454318.288604
        # rows: 72
        # cols: 67
        # ...

        # xLLCorner and yLLCorner represent the coordinates for the Lower Left corner of the raster
        north = yLLCorner + (cellSize * nRows)
        south = yLLCorner
        east = xLLCorner + (cellSize * nCols)
        west = xLLCorner

        # Create header Lines (the first shall be last and the last shall be first)
        grassHeader = ['cols: %s' % nCols,
                       'rows: %s' % nRows,
                       'west: %s' % west,
                       'east: %s' % east,
                       'south: %s' % south,
                       'north: %s' % north]

        # Insert grass headers into the grid
        for header in grassHeader:
            arcInfoGrid.insert(0, header)

        # Create string
        arcInfoGridString = '\n'.join(arcInfoGrid)
        return arcInfoGridString
905164
def inasafe_sub_analysis_summary_field_value(
        exposure_key, field, feature, parent):
    
    _ = feature, parent  # NOQA
    project_context_scope = QgsExpressionContextUtils.projectScope(
        QgsProject.instance())
    project = QgsProject.instance()

    key = ('{provenance}__{exposure}').format(
        provenance=provenance_multi_exposure_analysis_summary_layers_id[
            'provenance_key'],
        exposure=exposure_key)
    if not project_context_scope.hasVariable(key):
        return None

    analysis_summary_layer = project.mapLayer(
        project_context_scope.variable(key))
    if not analysis_summary_layer:
        return None

    index = analysis_summary_layer.fields().lookupField(field)
    if index < 0:
        return None

    feature = next(analysis_summary_layer.getFeatures())
    return feature[index]
864276
def __get_html(self, body=None):
        

        output = []
        output.append("<html>")
        output.append("<head>")
        for javascript in (self.__jquery_javascript,
                           self.__crittercism_javascript,
                           self.__reporter_javascript):
            output.append("<script type=\"text/javascript\">")
            output.append(javascript)
            output.append("</script>")
        output.append("<style type=\"text/css\">")
        output.append(self.__style)
        output.append("</style>")
        output.append("</head>")
        if body is not None:
            output.append(body)
        else:
            output.append("<body>")
            output.append("<div id=\"report\">")
            output.append("</div>")
            output.append("</body>")
        output.append("</html>")
        return "\n".join(output)
1070527
def getRastersAsPngs(self, session, tableName, rasterIds, postGisRampString, rasterField='raster', rasterIdField='id',  cellSize=None, resampleMethod='NearestNeighbour'):
        
        # Validate
        VALID_RESAMPLE_METHODS = ('NearestNeighbour', 'Bilinear', 'Cubic', 'CubicSpline', 'Lanczos')

        # Validate
        if resampleMethod not in VALID_RESAMPLE_METHODS:
            print('RASTER CONVERSION ERROR: {0} is not a valid resampleMethod.'
                  ' Please use either {1}'.format(resampleMethod,
                                                  ', '.join(VALID_RESAMPLE_METHODS)))

        if cellSize is not None:
            if not self.isNumber(cellSize):
                raise ValueError('RASTER CONVERSION ERROR: cellSize must be a number or None.')

        # Convert raster ids into formatted string
        rasterIdsString = '({0})'.format(', '.join(rasterIds))

        if cellSize is not None:
            statement = '''
                        SELECT ST_AsPNG(ST_Transform(ST_ColorMap(ST_Rescale({0}, {5}, '{6}'), 1, '{4}'), 4326, 'Bilinear')) As png
                        FROM {1}
                        WHERE {2} IN {3};
                        '''.format(rasterField, tableName, rasterIdField, rasterIdsString, postGisRampString, cellSize,
                                   resampleMethod)
        else:
            statement = '''
                        SELECT ST_AsPNG(ST_Transform(ST_ColorMap({0}, 1, '{4}'), 4326, 'Bilinear')) As png
                        FROM {1}
                        WHERE {2} IN {3};
                        '''.format(rasterField, tableName, rasterIdField, rasterIdsString, postGisRampString)
        result = session.execute(statement)
        return result