<commit_before>"""The endpoints server."""
# https://docs.google.com/drawings/d/1DnJy1rjOXMD7PLMm0Yr1MbEKpViYM2HvmnrgWycSwZU/edit?usp=sharing
# pylint: disable=W0232, E0401, E1101, E0611, R0201, C0103

import urllib2
import json

import datetime
import endpoints
from protorpc import message_types, remote, messages
from google.appengine.api import urlfetch
import google.appengine.api.users
import api_key
import noaa_stations
from math import cos, asin, sqrt
import models
import pytz
# import crops
from messages import (DataMessage, ScheduleResponse, ScheduledWater, Valve,
                      StatusResponse, Status, SetupRequest, ScheduleAdd, ValveDataResponse)

API_EXPLORER = '292824132082.apps.googleusercontent.com'
CLIENT_IDS = ['651504877594-9qh2hc91udrhht8gv1h69qarfa90hnt3.apps.googleusercontent.com', API_EXPLORER]

__author__ = '<NAME>'
__copyright__ = 'Copyright (C) 2015 SB Technology Holdings International'


def earth_distance(lat1, lon1, lat2, lon2):
  p = 0.017453292519943295
  a = 0.5 - cos((lat2 - lat1) * p) / 2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
  return 12742 * asin(sqrt(a))


def find_noaa_station(lat, lng):
  distances = []
  for s in noaa_stations.stations:
    distances.append(earth_distance(lat, lng, s['latitude'], s['longitude']))
  return noaa_stations.stations[distances.index(min(distances))]['id']


def load_eto(lat, lng, date_value):
  '''Load from CIMIS servers'''
  date_string = date_value.strftime("%Y-%m-%d")
  base_url = 'http://et.water.ca.gov/api/data?appKey=' + api_key.cimis_key
  targets = '&targets=lat=' + str(lat) + ',lng=' + str(lng)
  start_date = '&startDate=' + date_string
  end_date = '&endDate=' + date_string
  data_req = '&dataItems=day-asce-eto'
  units = '&unitOfMeasure=E'
  url = base_url + targets + start_date + end_date + data_req + units
  req = urllib2.Request(url, None, {'accept': 'application/json'})
  response = urllib2.urlopen(req, None, 15)
  json_data = response.read()
  data = json.loads(json_data)
  return float(data['Data']['Providers'][0]['Records'][0]['DayAsceEto']['Value'])


def load_precip(station_id, date_value):
  date_string = date_value.strftime("%Y-%m-%d")
  base_url = 'http://www.ncdc.noaa.gov/cdo-/api/v2/data?datasetid=GHCND&datatypeid=PRCP'
  start_date = '&startdate=' + date_string
  end_date = '&enddate=' + date_string
  station = '&stationid=' + station_id
  url = base_url + start_date + end_date + station
  req = urllib2.Request(url, None, {'token': api_key.noaa_key})
  response = urllib2.urlopen(req, None, 15)
  json_data = response.read()
  data = json.loads(json_data)
  if 'results' in data:
    return data['results'][0]['value'] * (1 / 254.0)
  else:
    return 0


def yesterday_local_date():
  tz = pytz.timezone('America/Los_Angeles')  # Only works in California
  utc_date = datetime.date.today() - datetime.timedelta(1)
  utc_time = datetime.datetime.combine(utc_date, datetime.time.min)
  local = pytz.utc.localize(utc_time, is_dst=None).astimezone(tz).date()
  return local


def today_local_datetime():
  tz = pytz.timezone('America/Los_Angeles')
  utc_time = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
  return pytz.utc.localize(utc_time, is_dst=None).astimezone(tz).replace(tzinfo=None)


def find_schedule(device, device_key):
  responses = []<commit_msg>Fix annoying bug that took forever to fix<commit_after>"""The endpoints server."""
# https://docs.google.com/drawings/d/1DnJy1rjOXMD7PLMm0Yr1MbEKpViYM2HvmnrgWycSwZU/edit?usp=sharing
# pylint: disable=W0232, E0401, E1101, E0611, R0201, C0103

import urllib2
import json

import datetime
import endpoints
from protorpc import message_types, remote, messages
from google.appengine.api import urlfetch
import google.appengine.api.users
import api_key
import noaa_stations
from math import cos, asin, sqrt
import models
import pytz
# import crops
from messages import (DataMessage, ScheduleResponse, ScheduledWater, Valve,
                      StatusResponse, Status, SetupRequest, ScheduleAdd, ValveDataResponse)

API_EXPLORER = '292824132082.apps.googleusercontent.com'
CLIENT_IDS = ['651504877594-9qh2hc91udrhht8gv1h69qarfa90hnt3.apps.googleusercontent.com', API_EXPLORER]

__author__ = '<NAME>'
__copyright__ = 'Copyright (C) 2015 SB Technology Holdings International'


def earth_distance(lat1, lon1, lat2, lon2):
  p = 0.017453292519943295
  a = 0.5 - cos((lat2 - lat1) * p) / 2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
  return 12742 * asin(sqrt(a))


def find_noaa_station(lat, lng):
  distances = []
  for s in noaa_stations.stations:
    distances.append(earth_distance(lat, lng, s['latitude'], s['longitude']))
  return noaa_stations.stations[distances.index(min(distances))]['id']


def load_eto(lat, lng, date_value):
  '''Load from CIMIS servers'''
  date_string = date_value.strftime("%Y-%m-%d")
  base_url = 'http://et.water.ca.gov/api/data?appKey=' + api_key.cimis_key
  targets = '&targets=lat=' + str(lat) + ',lng=' + str(lng)
  start_date = '&startDate=' + date_string
  end_date = '&endDate=' + date_string
  data_req = '&dataItems=day-asce-eto'
  units = '&unitOfMeasure=E'
  url = base_url + targets + start_date + end_date + data_req + units
  req = urllib2.Request(url, None, {'accept': 'application/json'})
  response = urllib2.urlopen(req, None, 15)
  json_data = response.read()
  data = json.loads(json_data)
  return float(data['Data']['Providers'][0]['Records'][0]['DayAsceEto']['Value'])


def load_precip(station_id, date_value):
  date_string = date_value.strftime("%Y-%m-%d")
  base_url = 'http://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&datatypeid=PRCP'
  start_date = '&startdate=' + date_string
  end_date = '&enddate=' + date_string
  station = '&stationid=' + station_id
  url = base_url + start_date + end_date + station
  req = urllib2.Request(url, None, {'token': api_key.noaa_key})
  response = urllib2.urlopen(req, None, 15)
  json_data = response.read()
  data = json.loads(json_data)
  if 'results' in data:
    return data['results'][0]['value'] * (1 / 254.0)
  else:
    return 0


def yesterday_local_date():
  tz = pytz.timezone('America/Los_Angeles')  # Only works in California
  utc_date = datetime.date.today() - datetime.timedelta(1)
  utc_time = datetime.datetime.combine(utc_date, datetime.time.min)
  local = pytz.utc.localize(utc_time, is_dst=None).astimezone(tz).date()
  return local


def today_local_datetime():
  tz = pytz.timezone('America/Los_Angeles')
  utc_time = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
  return pytz.utc.localize(utc_time, is_dst=None).astimezone(tz).replace(tzinfo=None)


def find_schedule(device, device_key):
  responses = []
{'fraction_non_alphanumeric': 0.14151925078043703, 'fraction_numerical': 0.03805559684852088, 'mean_word_length': 5.094202898550725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4468209', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2518, 'n_words': 489}