<commit_before>from django.views.generic import list_detail
from django.shortcuts import get_object_or_404
from enhydris.contourplot.models import ChartPage, CPoint, OtherPlot
from django.http import (HttpResponse, HttpResponseRedirect,
                            HttpResponseForbidden, Http404)
from enhydris.hcore.views import inc_month, timeseries_data
from enhydris.hcore.models import TimeStep as TTimeStep
from pthelma.cplot import plot_contours
from datetime import datetime,timedelta
from django.core.servers.basehttp import FileWrapper
from django.conf import settings
import mimetypes
import os
try:
    import json
except ImportError:
    import simplejson as json
from pthelma.timeseries import TimeStep
from datetime import datetime, timedelta


def last_update(request, urlcode, **kwargs):
    page = get_object_or_404(ChartPage, url_name = urlcode)
    return HttpResponse(page.get_concurent_timestamp_str(),
                        mimetype='text/plain')


class DummyRequest:
    def __init__(self, method, GET):
        self.method = method
        self.GET = GET


def fetch_value(point, date, secondary=False):
    timeseries = getattr(point, ('timeseries',
                                  'secondary_timeseries')[secondary])
    dummyrequest = DummyRequest('GET', {'object_id': timeseries.id,
                                'date': date.strftime('%Y-%m-%d'),
                                'time': date.strftime('%H:%M'),
                                'last': 'moment', 'exact_datetime': 'true'})
    try:
        v = json.loads(timeseries_data(dummyrequest).content)[u'data'][0][1]
        if v!=u'null':
            v = float(v)
        else:
            v = None
    except:
        v = None
    return v


def contourpage_detail(request, urlcode, **kwargs):
    kwargs["queryset"] = ChartPage.objects.all()
    page = get_object_or_404(ChartPage, url_name = urlcode)
    object_id = page.id
    large_dimension = page.default_dimension
    (x0, y0, x1, y1) = [getattr(page, x) for x in ('chart_bounds_bl_x', 
                               'chart_bounds_bl_y','chart_bounds_tr_x', 
                                                   'chart_bounds_tr_y')]
    width, height = x1-x0, y1-y0
    small_dimension = int(round(large_dimension*min(height, width)/max(
                                                        height, width)))
    if width>height:
        x_dim, y_dim = large_dimension, small_dimension
    else:
        x_dim, y_dim = small_dimension, large_dimension
    x_thumb = 140
    y_thumb = x_thumb*y_dim/x_dim
    last_update = page.get_concurent_timestamp_str()
    last_update_tstmp = page.get_concurent_timestamp()
    if page.display_station_values:
        mean_value = 0.0
        count = 0
        points = CPoint.objects.filter(chart_page__id=page.id)
        for point in points:
            point.old_values=[]
            point.value= fetch_value(point, last_update_tstmp)
            if point.value is not None:
                mean_value+=point.value*point.weight
                count+=point.weight
        if count>0:
            mean_value/=count
        atstmp = last_update_tstmp
        old_values_meta=[]
        secondary_timeseries_exist = False
        for it in range(page.display_station_old_values):
            mean_value1 = 0.0
            mean_value2 = 0.0
            count1 = 0
            count2 = 0
            atstmp+= timedelta(minutes=-page.old_values_step_minutes)
            atstmp = inc_month(atstmp, -page.old_values_step_months)
            for point in points:
                if point.secondary_timeseries:
                    v2 = fetch_value(point, atstmp, True)
                    secondary_timeseries_exist = True
                else:
                    v2 = None
                v1 = fetch_value(point, atstmp)
                point.old_values.append({'v1':v1, 'v2':v2})
                if v1 is not None:
                    mean_value1+=v1*point.weight
                    count1+=point.weight
                if v2 is not None:
                    mean_value2+=v2*point.weight
                    count2+=point.weight
            if count>0:
                mean_value1/=count1
                mean_value2/=count2
            old_values_meta.append({'ts':atstmp, 'mv1':mean_value1, 
                                    'mv2':mean_value2})
    other_pages = None
    try:
        other_pages = OtherPlot.objects.filter(page__id=page.id)
    except OtherPlot.DoesNotExist:
        pass
    kwargs["template_name"] = "contours_detail.html"
    kwargs["template_object_name"] = "page"
    kwargs["extra_context"] = {'last_update': last_update,
                               'other_pages': other_pages,<commit_msg>Minor fix in contour plots<commit_after>from django.views.generic import list_detail
from django.shortcuts import get_object_or_404
from enhydris.contourplot.models import ChartPage, CPoint, OtherPlot
from django.http import (HttpResponse, HttpResponseRedirect,
                            HttpResponseForbidden, Http404)
from enhydris.hcore.views import inc_month, timeseries_data
from enhydris.hcore.models import TimeStep as TTimeStep
from pthelma.cplot import plot_contours
from datetime import datetime,timedelta
from django.core.servers.basehttp import FileWrapper
from django.conf import settings
import mimetypes
import os
try:
    import json
except ImportError:
    import simplejson as json
from pthelma.timeseries import TimeStep
from datetime import datetime, timedelta


def last_update(request, urlcode, **kwargs):
    page = get_object_or_404(ChartPage, url_name = urlcode)
    return HttpResponse(page.get_concurent_timestamp_str(),
                        mimetype='text/plain')


class DummyRequest:
    def __init__(self, method, GET):
        self.method = method
        self.GET = GET


def fetch_value(point, date, secondary=False):
    timeseries = getattr(point, ('timeseries',
                                  'secondary_timeseries')[secondary])
    dummyrequest = DummyRequest('GET', {'object_id': timeseries.id,
                                'date': date.strftime('%Y-%m-%d'),
                                'time': date.strftime('%H:%M'),
                                'last': 'moment', 'exact_datetime': 'true'})
    try:
        v = json.loads(timeseries_data(dummyrequest).content)[u'data'][0][1]
        if v!=u'null':
            v = float(v)
        else:
            v = None
    except:
        v = None
    return v


def contourpage_detail(request, urlcode, **kwargs):
    kwargs["queryset"] = ChartPage.objects.all()
    page = get_object_or_404(ChartPage, url_name = urlcode)
    object_id = page.id
    large_dimension = page.default_dimension
    (x0, y0, x1, y1) = [getattr(page, x) for x in ('chart_bounds_bl_x', 
                               'chart_bounds_bl_y','chart_bounds_tr_x', 
                                                   'chart_bounds_tr_y')]
    width, height = x1-x0, y1-y0
    small_dimension = int(round(large_dimension*min(height, width)/max(
                                                        height, width)))
    if width>height:
        x_dim, y_dim = large_dimension, small_dimension
    else:
        x_dim, y_dim = small_dimension, large_dimension
    x_thumb = 140
    y_thumb = x_thumb*y_dim/x_dim
    last_update = page.get_concurent_timestamp_str()
    last_update_tstmp = page.get_concurent_timestamp()
    if page.display_station_values:
        mean_value = 0.0
        count = 0
        points = CPoint.objects.filter(chart_page__id=page.id)
        for point in points:
            point.old_values=[]
            point.value= fetch_value(point, last_update_tstmp)
            if point.value is not None:
                mean_value+=point.value*point.weight
                count+=point.weight
        if count>0:
            mean_value/=count
        atstmp = last_update_tstmp
        old_values_meta=[]
        secondary_timeseries_exist = False
        for it in range(page.display_station_old_values):
            mean_value1 = 0.0
            mean_value2 = 0.0
            count1 = 0
            count2 = 0
            atstmp+= timedelta(minutes=-page.old_values_step_minutes)
            atstmp = inc_month(atstmp, -page.old_values_step_months)
            for point in points:
                if point.secondary_timeseries:
                    v2 = fetch_value(point, atstmp, True)
                    secondary_timeseries_exist = True
                else:
                    v2 = None
                v1 = fetch_value(point, atstmp)
                point.old_values.append({'v1':v1, 'v2':v2})
                if v1 is not None:
                    mean_value1+=v1*point.weight
                    count1+=point.weight
                if v2 is not None:
                    mean_value2+=v2*point.weight
                    count2+=point.weight
            if count1>0:
                mean_value1/=count1
            if count2>0:
                mean_value2/=count2
            old_values_meta.append({'ts':atstmp, 'mv1':mean_value1, 
                                    'mv2':mean_value2})
    other_pages = None
    try:
        other_pages = OtherPlot.objects.filter(page__id=page.id)
    except OtherPlot.DoesNotExist:
        pass
    kwargs["template_name"] = "contours_detail.html"
    kwargs["template_object_name"] = "page"
    kwargs["extra_context"] = {'last_update': last_update,
                               'other_pages': other_pages,
{'fraction_non_alphanumeric': 0.08849931354947724, 'fraction_numerical': 0.013623402682437427, 'mean_word_length': 2.038177735001604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6918474', 'n_tokens_mistral': 2861, 'n_tokens_neox': 2730, 'n_words': 577}