-
Notifications
You must be signed in to change notification settings - Fork 0
/
grouperPlotM72.py
123 lines (95 loc) · 3.5 KB
/
grouperPlotM72.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import simplekml
# user changeable variables
# import files
gpsFile = 'M72_Jun2017/M72gps.csv'
dataFile = 'M72_Jun2017/M72_RedGrouper.csv'
tagFile = 'M72_Jun2017/M72_VMT.csv'
# output files
mergeFile = 'M72_Jun2017/M72_merge.csv'
kmlFile = 'M72_Jun2017/M72_RedGrouper.kml'
kmlGlider = 'M72_Jun2017/M72gps.kml'
# Time zone offset for red grouper file
# if time in UTC, offsetHours = 0
offsetHours = 0
# read in and format GPS csv file
df = pd.read_csv(gpsFile, header=0, sep=',', names=['Date_Time', 'Latitude', 'Longitude'], dtype = {'Date_Time': str, 'Latitude': np.float64, 'Longitude': np.float64})
df = df.dropna()
df.index = pd.to_datetime(df.Date_Time)
# df = df.drop('Date', 1)
# df = df.drop('Time', 1)
df = df.drop('Date_Time', 1)
df = df.dropna()
df = df.sort_index()
# resample location to 1 minute intervals averaging lat and lon values
df = df.resample('1T').mean()
df = df.interpolate()
# read in grouper data
df2 = pd.read_csv(dataFile, sep=',', usecols=[1, 2])
df2.index = pd.to_datetime(df2[df2.columns[0]])
df2.index = df2.index + pd.DateOffset(hours=offsetHours)
df2 = df2.drop(df2.columns[0], axis=1)
df2 = df2.sort_index()
# resample 1 minute bins
df2 = df2.resample('1T').sum()
df2.columns = ['sounds']
# read in VMT
df3 = pd.read_csv(tagFile, sep=',')
df3.index = pd.to_datetime(df3[df3.columns[0]])
df3 = df3.drop(df3.columns[0], axis=1)
df3 = df3.sort_index()
# bin VMT data into pings per minute (not unique)
df4 = df3.resample('1T').count()
df4.columns = ['tagCount']
# join dataframes
df_join = df.join(df2, how='outer')
df_join = df_join.join(df4, how='outer')
# df_join = df_join.dropna()
# Summary stats
print("Sounds: ")
print(df2.sum())
# # Glider and sound plot
# plt.scatter(df_join.Longitude, df_join.Latitude, c='c')
# plt.scatter(df_join.Longitude, df_join.Latitude, c='r', s=df_join['sounds'] * 50)
# plt.show()
# plot with tag data
# limit time range
df5 = df_join.loc['2017-05-19 15:53':'2017-06-05 14:36']
plt.scatter(df5.Longitude, df5.Latitude, c='c')
plt.scatter(df5.Longitude, df5.Latitude, marker='v', c='b', s=df5['tagCount'] * 20)
plt.scatter(df5.Longitude, df5.Latitude, c='r', s=df5['sounds'] * 20)
plt.show()
# export to csv
df_join.to_csv(mergeFile)
# export to kml
kml = simplekml.Kml()
style = simplekml.Style()
style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png'
# i is index ; row.Name row.lat row.lon row.count
for i, row in df_join.iterrows():
pnt = kml.newpoint(name = "", description = "", coords=[(row.Longitude, row.Latitude)])
pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png'
pnt.style.iconstyle.scale = str(np.float(row[2])/1.4)
kml.save(kmlFile)
latlon = []
for i,row in df.iterrows():
latlon.append((row.Longitude, row.Latitude, 0.0))
kml = simplekml.Kml()
ls = kml.newlinestring(name = "glider", description = "", coords=latlon)
ls.extrude = 1
ls.altitudemode = simplekml.AltitudeMode.relativetoground
kml.save(kmlGlider)
start_date = '2017-06-03 03:45:58'
end_date = '2017-06-03 05:03:48'
print(df2.dropna()[start_date : end_date])
print(sum(df2.dropna()[start_date : end_date].sounds))
start_date = '2017-06-04 18:23:13'
end_date = '2017-06-04 18:36:48'
print(df2.dropna()[start_date : end_date])
print(sum(df2.dropna()[start_date : end_date].sounds))
start_date = '2017-06-04 21:26:54'
end_date = '2017-06-04 22:10:26'
print(df2.dropna()[start_date : end_date])
print(sum(df2.dropna()[start_date : end_date].sounds))