<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import

import logging
import requests
import json

from ast import literal_eval as make_tuple

from StringIO import StringIO

from celery import shared_task

from django.conf import settings

from apps.modeling.tr55.utils import (aoi_resolution,
                                      precipitation,
                                      apply_modifications_to_census,
                                      )

from tr55.model import simulate_day
from gwlfe import gwlfe, parser

logger = logging.getLogger(__name__)

KG_PER_POUND = 0.453592
CM_PER_INCH = 2.54


def format_quality(model_output):
    measures = ['Total Suspended Solids',
                'Total Nitrogen',
                'Total Phosphorus']
    codes = ['tss', 'tn', 'tp']

    quality = {}
    for key in model_output:
        def map_and_convert_units(input):
            measure, code = input
            return {
                'measure': measure,
                'load': model_output[key][code] * KG_PER_POUND,
                'runoff': model_output[key]['runoff']  # Already CM
            }

        quality[key] = map(map_and_convert_units, zip(measures, codes))

    return quality


def format_runoff(model_output):
    """
    Convert model output values that are in inches into centimeters.
    """
    inch_keys = ['runoff', 'inf', 'et']
    for key in model_output:
        for item in inch_keys:
            model_output[key][item] = model_output[key][item] * CM_PER_INCH

        distribution = model_output[key]['distribution']
        for k in distribution:
            for item in inch_keys:
                model_output[key]['distribution'][k][item] = \
                    model_output[key]['distribution'][k][item] * CM_PER_INCH

    return model_output


def format_for_srat(huc12_id, model_output):
    formatted = {'huc12': huc12_id,
                 # Tile Drain may be calculated by future versions of
                 # Mapshed. The SRAT API requires a placeholder
                 'tpload_tiledrain': 0,
                 'tnload_tiledrain': 0,
                 'tssload_tiledrain': 0,
                 }

    for load in model_output['Loads']:
        source_key = settings.SRAT_KEYS.get(load['Source'], None)

        if source_key is None:
            continue

        formatted['tpload_' + source_key] = load['TotalP']
        formatted['tnload_' + source_key] = load['TotalN']

        if source_key not in ['farman',
                              'subsurface',
                              'septics',
                              'pointsource']:
            formatted['tssload_' + source_key] = load['Sediment']

    return formatted


def format_subbasin(huc12_gwlfe_results, srat_catchment_results):
    def empty_source(s):
        return {'Source': s, 'TotalN': 0, 'TotalP': 0, 'Sediment': 0}

    def add_huc12_source(source, srat_huc12):
        source_name = source['Source']
        source_key = settings.SRAT_KEYS[source_name]
        total_n = srat_huc12.get('tnload_' + source_key, 0)
        total_p = srat_huc12.get('tpload_' + source_key, 0)
        sediment = srat_huc12.get('tssload_' + source_key, 0)

        source['TotalN'] += total_n
        source['TotalP'] += total_p
        source['Sediment'] += sediment

        return {
            'Source': source_name,
            'TotalN': total_n,
            'TotalP': total_p,
            'Sediment': sediment
        }

    def format_catchment(srat_catchment):
        return {
            'TotalLoadingRates': {
                'TotalN': srat_catchment['tnloadrate_total'],
                'TotalP': srat_catchment['tploadrate_total'],
                'Sediment': srat_catchment['tssloadrate_total'],
            },
            'LoadingRateConcentrations': {
                'TotalN': srat_catchment['tnloadrate_conc'],
                'TotalP': srat_catchment['tploadate_conc'],
                'Sediment': srat_catchment['tssloadrate_conc'],
            },
        }

    def add_huc12(srat_huc12, aggregate):
        area = huc12_gwlfe_results[srat_huc12['huc12']]['AreaTotal']
        aggregate['AreaTotal'] += area
        return {
            'Loads': [add_huc12_source(s, srat_huc12)
                      for s in aggregate['Loads']],
            'AreaTotal': area,
            'Catchments': {comid: format_catchment(result)
                           for comid, result
                           in srat_huc12['catchments'].iteritems()}
        }

    aggregate = {
        'Loads': [empty_source(source_name)
                  for source_name in settings.SRAT_KEYS.keys()],
        'AreaTotal': 0,
        # All gwlf-e results should have the same inputmod hash,
        # so grab any of them
        'inputmod_hash': huc12_gwlfe_results.itervalues()
                                            .next()['inputmod_hash'],
    }

    aggregate['HUC-12s'] = {huc12_id: add_huc12(result, aggregate)<commit_msg>Update SRAT Catchment Loading Rate Key<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import

import logging
import requests
import json

from ast import literal_eval as make_tuple

from StringIO import StringIO

from celery import shared_task

from django.conf import settings

from apps.modeling.tr55.utils import (aoi_resolution,
                                      precipitation,
                                      apply_modifications_to_census,
                                      )

from tr55.model import simulate_day
from gwlfe import gwlfe, parser

logger = logging.getLogger(__name__)

KG_PER_POUND = 0.453592
CM_PER_INCH = 2.54


def format_quality(model_output):
    measures = ['Total Suspended Solids',
                'Total Nitrogen',
                'Total Phosphorus']
    codes = ['tss', 'tn', 'tp']

    quality = {}
    for key in model_output:
        def map_and_convert_units(input):
            measure, code = input
            return {
                'measure': measure,
                'load': model_output[key][code] * KG_PER_POUND,
                'runoff': model_output[key]['runoff']  # Already CM
            }

        quality[key] = map(map_and_convert_units, zip(measures, codes))

    return quality


def format_runoff(model_output):
    """
    Convert model output values that are in inches into centimeters.
    """
    inch_keys = ['runoff', 'inf', 'et']
    for key in model_output:
        for item in inch_keys:
            model_output[key][item] = model_output[key][item] * CM_PER_INCH

        distribution = model_output[key]['distribution']
        for k in distribution:
            for item in inch_keys:
                model_output[key]['distribution'][k][item] = \
                    model_output[key]['distribution'][k][item] * CM_PER_INCH

    return model_output


def format_for_srat(huc12_id, model_output):
    formatted = {'huc12': huc12_id,
                 # Tile Drain may be calculated by future versions of
                 # Mapshed. The SRAT API requires a placeholder
                 'tpload_tiledrain': 0,
                 'tnload_tiledrain': 0,
                 'tssload_tiledrain': 0,
                 }

    for load in model_output['Loads']:
        source_key = settings.SRAT_KEYS.get(load['Source'], None)

        if source_key is None:
            continue

        formatted['tpload_' + source_key] = load['TotalP']
        formatted['tnload_' + source_key] = load['TotalN']

        if source_key not in ['farman',
                              'subsurface',
                              'septics',
                              'pointsource']:
            formatted['tssload_' + source_key] = load['Sediment']

    return formatted


def format_subbasin(huc12_gwlfe_results, srat_catchment_results):
    def empty_source(s):
        return {'Source': s, 'TotalN': 0, 'TotalP': 0, 'Sediment': 0}

    def add_huc12_source(source, srat_huc12):
        source_name = source['Source']
        source_key = settings.SRAT_KEYS[source_name]
        total_n = srat_huc12.get('tnload_' + source_key, 0)
        total_p = srat_huc12.get('tpload_' + source_key, 0)
        sediment = srat_huc12.get('tssload_' + source_key, 0)

        source['TotalN'] += total_n
        source['TotalP'] += total_p
        source['Sediment'] += sediment

        return {
            'Source': source_name,
            'TotalN': total_n,
            'TotalP': total_p,
            'Sediment': sediment
        }

    def format_catchment(srat_catchment):
        return {
            'TotalLoadingRates': {
                'TotalN': srat_catchment['tnloadrate_total'],
                'TotalP': srat_catchment['tploadrate_total'],
                'Sediment': srat_catchment['tssloadrate_total'],
            },
            'LoadingRateConcentrations': {
                'TotalN': srat_catchment['tnloadrate_conc'],
                'TotalP': srat_catchment['tploadrate_conc'],
                'Sediment': srat_catchment['tssloadrate_conc'],
            },
        }

    def add_huc12(srat_huc12, aggregate):
        area = huc12_gwlfe_results[srat_huc12['huc12']]['AreaTotal']
        aggregate['AreaTotal'] += area
        return {
            'Loads': [add_huc12_source(s, srat_huc12)
                      for s in aggregate['Loads']],
            'AreaTotal': area,
            'Catchments': {comid: format_catchment(result)
                           for comid, result
                           in srat_huc12['catchments'].iteritems()}
        }

    aggregate = {
        'Loads': [empty_source(source_name)
                  for source_name in settings.SRAT_KEYS.keys()],
        'AreaTotal': 0,
        # All gwlf-e results should have the same inputmod hash,
        # so grab any of them
        'inputmod_hash': huc12_gwlfe_results.itervalues()
                                            .next()['inputmod_hash'],
    }

    aggregate['HUC-12s'] = {huc12_id: add_huc12(result, aggregate)
{'fraction_non_alphanumeric': 0.10189948555599525, 'fraction_numerical': 0.013256826276216858, 'mean_word_length': 2.0375600961538463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3285784', 'n_tokens_mistral': 3166, 'n_tokens_neox': 2971, 'n_words': 646}