/
randomnumber.py
207 lines (166 loc) · 5.38 KB
/
randomnumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from datetime import timedelta, datetime, date
import datetime
import random
import scipy.stats as stats
import thinkplot as tp
import calcLatLong as cll
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import cProfile
import calcLatLong
from math import radians, cos, sin, asin, sqrt
class MyStuff(object):
def __init__(self):
self.datetimelist = [(datetime.datetime(2000,1,1,0,0,0,0),datetime.datetime(2000,1,1,5,0,0,0)),
(datetime.datetime(2000,1,2,10,0,0,0),datetime.datetime(2000,1,2,15,0,0,0)),
(datetime.datetime(2000,1,3,20,0,0,0),datetime.datetime(2000,1,3,23,0,0,0)),
(datetime.datetime(2000,1,4,10,0,0,0),datetime.datetime(2000,1,4,15,0,0,0)),
(datetime.datetime(2000,1,5,0,0,0,0),datetime.datetime(2000,1,5,15,0,0,0))
]
self.integers = []
def calcDistances(self):
haversine(42,-71,thing1,thing2)
def haversine(self, lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# 6367 km is the radius of the Earth
km = 6367 * c
return km
def makeLocation(self, latlon=(42.3417,70.9661)):
self.latlon = latlon
def datespan(self):
"create a list of date objects that iterate through a start and end date"
self.things=[]
def tmpdatespan(startDate, endDate, delta=timedelta(days=1)):
currentDate = startDate
while currentDate < endDate:
yield currentDate
currentDate += delta
for day in tmpdatespan(date(2007, 1, 1), date(2008, 1, 1), delta=timedelta(days=1)):
self.things.append(day)
print self.things
return self.things
def calcR(self, lat, lon, dates):
self.datetimelist=[]
for day in dates:
risesettime = calcLatLong.calcRiseSet(lat,lon,day)
self.datetimelist.append(risesettime)
print self.datetimelist
def profile(self):
cProfile.runctx('self.fixedsunset(5,15)',globals(),locals())
def parseList(self):
sunrise=[]
sunset=[]
for x in self.datetimelist:
sunrise.append(x[0])
sunset.append(x[1])
print [sunrise,sunset]
return [sunrise,sunset]
def getIntegers(self):
return self.integers
def takeSTD(self,listofthings):
return np.std(listofthings)
def addErrors(self,datetime,number,length):
#returns list of datetimes with error added to them
#there are number amounts of these datetimes
#the datetimes are within 'length' of the estimated given datetime
self.addedErrors = []
integers = []
for i in range(number):
integer = random.randint(-length,length)
d=timedelta(minutes=integer)
error = datetime + d
self.addedErrors.append(error)
integers.append(integer)
return self.addedErrors, integers, datetime
# def fixedsunrise(self,number,length, approx=(42,-71)):
# self.latlonlist =[]
# #a list of fixed sunrises
# #added errors of the sunset times
# listoftimes=parseList(times)
# for risetime,settime in self.datetimelist:
# [settimes,integers]=self.addErrors(settime,number,length)
# for errorsettime in settimes:
# latlon = cll.calcLatLon(risetime,errorsettime,approx)
# self.latlonlist.append(latlon)
# print risetime.date()
# print latlon
# print 'a'
# print self.latlonlist
# print 'b'
# return self.latlonlist
def fixedsunset(self,number,length, approx=(42,-71)):
self.datelatlonlist=[]
# self.integerlist = []
#a list of fixed sunsets
#added errors of the sunrise times
#listoftimes=self.parseList(times)
for risetime,settime in self.datetimelist:
[risetimes,integers,datetime]=self.addErrors(risetime,number,length)
self.latlonlist=[]
for errorrisetime in risetimes:
latlon = cll.calcLatLon(errorrisetime,settime,approx)
#print latlon
self.latlonlist.append(latlon)
self.datelatlonlist.append([settime.date(),self.latlonlist,integers])
print self.datelatlonlist
print self.datelatlonlist
return self.latlonlist #this should be erased
def dictParse(self):
self.lats = []
self.lons = []
self.dates = []
for k,v,y in self.datelatlonlist: #date,(lon,lat),error
londate = []
latdate = []
for i in range(len(v)):
londate.append(v[i][0]) #lon
latdate.append(v[i][1]) #lat
lonstd = np.std(londate)
latstd = np.std(latdate)
gsstd= np.std(y)
self.dates.append(k)
self.lons.append(lonstd/gsstd)
self.lats.append(latstd/gsstd)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
g1, = plt.plot(self.dates,self.lats)
g2, = plt.plot(self.dates,self.lons)
plt.xlabel('Date')
plt.ylabel('STD/Gaussian STD')
plt.gcf().autofmt_xdate()
plt.legend([g1, g2], ["lat", "lon"])
plt.show()
def notfixed():
#skip for now
pass
def lookatLat(self,listoflatlons):
lats = []
for latlon in listoflatlons:
lats.append(latlon[0])
return lats
def lookatLon(self,listoflatlons):
lons = []
for latlon in listoflatlons:
lons.append(latlon[1])
return lons
def main():
hello = MyStuff()
#hello.profile()
dates = hello.datespan()
hello.calcR(42.358,-71.064,dates)
hello.fixedsunset(10,5)
#hello.fixedsunset(5,5)
hello.dictParse()
if __name__ == '__main__':
main()