<commit_before>#!/usr/bin/env python
"""JSON service that emits RAOB profiles in JSON format

{'profiles': [
  {
  'station': 'OAX',
  'valid': '2013-08-21T12:00:00Z',
  'profile': [
    {'tmpc':99, 'pres': 99, 'dwpc': 99, 'sknt': 99, 'drct': 99, 'hght': 99},
    {'tmpc':99, 'pres': 99, 'dwpc': 99, 'sknt': 99, 'drct': 99, 'hght': 99},
    {...}
              ]
  },
  {...}]
}
"""
import cgi
import sys
import datetime
import json
# http://stackoverflow.com/questions/1447287
from json import encoder

import pytz
import memcache
from pyiem.util import get_dbconn
encoder.FLOAT_REPR = lambda o: format(o, '.2f')


def safe(val):
    """Be careful """
    if val is None:
        return None
    return float(val)


def run(ts, sid):
    """ Actually do some work! """
    dbconn = get_dbconn('postgis')
    cursor = dbconn.cursor()

    res = {'profiles': []}
    table = 'raob_profile_%s' % (ts.year,)
    if ts.year > datetime.datetime.utcnow().year:
        return json.dumps(res)
 
    cursor.execute("""
        SELECT p.pressure, p.height,
        round(p.tmpc::numeric,1),
        round(p.dwpc::numeric,1), p.drct,
        round((p.smps * 1.94384)::numeric,0) from """ + table + """
        p JOIN raob_flights f
        on (p.fid = f.fid) WHERE
        f.station = %s and f.valid = %s ORDER by p.pressure DESC
        """, (sid, ts))
    profile = []
    for row in cursor:
        profile.append(dict(pres=safe(row[0]),
                            hght=safe(row[1]),
                            tmpc=safe(row[2]),
                            dwpc=safe(row[3]),
                            drct=safe(row[4]),
                            sknt=safe(row[5])))
    res['profiles'].append(dict(station=sid,
                                valid=ts.strftime("%Y-%m-%dT%H:%M:%SZ"),
                                profile=profile))
    cursor.close()
    dbconn.close()
    return json.dumps(res)<commit_msg>also do lower bounds check on year for RAOB service<commit_after>#!/usr/bin/env python
"""JSON service that emits RAOB profiles in JSON format

{'profiles': [
  {
  'station': 'OAX',
  'valid': '2013-08-21T12:00:00Z',
  'profile': [
    {'tmpc':99, 'pres': 99, 'dwpc': 99, 'sknt': 99, 'drct': 99, 'hght': 99},
    {'tmpc':99, 'pres': 99, 'dwpc': 99, 'sknt': 99, 'drct': 99, 'hght': 99},
    {...}
              ]
  },
  {...}]
}
"""
import cgi
import sys
import datetime
import json
# http://stackoverflow.com/questions/1447287
from json import encoder

import pytz
import memcache
from pyiem.util import get_dbconn
encoder.FLOAT_REPR = lambda o: format(o, '.2f')


def safe(val):
    """Be careful """
    if val is None:
        return None
    return float(val)


def run(ts, sid):
    """ Actually do some work! """
    dbconn = get_dbconn('postgis')
    cursor = dbconn.cursor()

    res = {'profiles': []}
    table = 'raob_profile_%s' % (ts.year,)
    if ts.year > datetime.datetime.utcnow().year or ts.year < 1946:
        return json.dumps(res)

    cursor.execute("""
        SELECT p.pressure, p.height,
        round(p.tmpc::numeric,1),
        round(p.dwpc::numeric,1), p.drct,
        round((p.smps * 1.94384)::numeric,0) from """ + table + """
        p JOIN raob_flights f
        on (p.fid = f.fid) WHERE
        f.station = %s and f.valid = %s ORDER by p.pressure DESC
        """, (sid, ts))
    profile = []
    for row in cursor:
        profile.append(dict(pres=safe(row[0]),
                            hght=safe(row[1]),
                            tmpc=safe(row[2]),
                            dwpc=safe(row[3]),
                            drct=safe(row[4]),
                            sknt=safe(row[5])))
    res['profiles'].append(dict(station=sid,
                                valid=ts.strftime("%Y-%m-%dT%H:%M:%SZ"),
                                profile=profile))
    cursor.close()
    dbconn.close()
    return json.dumps(res)
{'fraction_non_alphanumeric': 0.17023471756512767, 'fraction_numerical': 0.03249935517152437, 'mean_word_length': 2.058359621451104, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4397363', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1325, 'n_words': 303}