-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoktlogger_json_to_df.py
executable file
·198 lines (158 loc) · 6.89 KB
/
toktlogger_json_to_df.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 3 09:54:15 2020
@author: lukem
"""
import pandas as pd
import datetime
from datetime import datetime as dt
import numpy as np
import requests
def flattenjson( b, delim ):
'''
Parameters
----------
b : TYPE: dict
DESCRIPTION: json dictionary
delim : TYPE: string
DESCRIPTION: When dictionary is flattened, child key is appended to the parent key separated by this delimiter.
Returns
-------
val : TYPE: dict
DESCRIPTION: dictionary with one tier flattened (put on same level as parent)
'''
val = {}
for i in b.keys():
if isinstance( b[i], dict ):
get = flattenjson( b[i], delim )
for j in get.keys():
val[ i + delim + j ] = get[j]
else:
val[i] = b[i]
return val
def date_only(timestamp):
'''
Reads string of timestamp and returns string of only date
'''
return dt.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%Y-%m-%d')
def time_only(timestamp):
'''
Reads string of timestamp and returns string of only time
'''
return dt.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%H:%M:%S')
def json_to_df(toktlogger):
'''
Provide IP or DNS of toktlogger to access IMR API
Returns single dataframe that can be included in the gear log
'''
#Pull data from IMR API in json format. URL should match IMR API host.
url = "http://"+toktlogger+"/api/activities/inCurrentCruise?format=json"
response = requests.get(url)
json_activities = response.json()
json_activities = list(map( lambda x: flattenjson( x, "__" ), json_activities ))
key_map = {
'eventID': 'id',
'description': 'name',
'gearType': '',
'eventDate': 'startTime',
'eventTime': 'startTime',
'stationName': 'fields',
'decimalLatitude': 'startPosition__coordinates',
'decimalLongitude': 'startPosition__coordinates',
'bottomDepthInMeters': 'fields',
'statID': 'localstationNumber',
'sampleDepthInMeters': '',#
'maximumDepthInMeters': '',#
'minimumDepthInMeters': '',#
'start_date': 'startTime',
'end_date': 'endTime',
'end_time': 'endTime',
'endDecimalLatitude': 'endPosition__coordinates',
'endDecimalLongitude': 'endPosition__coordinates',
'eventRemarks': 'comment',
'samplingProtocol': '',
'recordedBy': '',
'pi_name': '',
'pi_email': '',
'pi_institution': ''
}
gear_df = pd.read_csv('list_gear_types.csv')
data = pd.DataFrame(columns=key_map.keys())
for idx, activity in enumerate(json_activities):
if type(activity['endTime']) == str and type(activity['endPosition__coordinates'][0]) == float:
# Creating dictionary where key is column header and value is value to be written for that column and activity
dic = {}
for key, val in key_map.items():
if key in ['eventDate', 'end_date', 'start_date']:
dic[key] = [dt.strptime(activity[val], '%Y-%m-%dT%H:%M:%S.%fZ').date()]
elif key in ['eventTime', 'end_time', 'start_time']:
dic[key] = [dt.strptime(activity[val], '%Y-%m-%dT%H:%M:%S.%fZ').time()]
elif key in ['decimalLatitude','endDecimalLatitude']:
dic[key] = [activity[val][1]]
elif key in ['decimalLongitude','endDecimalLongitude']:
dic[key] = [activity[val][0]]
elif key in ['bottomDepthInMeters']:
# Taking bottom depth for 2 seconds from start time of activity
# Previously took as medium between start and end time, but was taking too long for long activities.
start_dt = dt.strptime(activity['startTime'], '%Y-%m-%dT%H:%M:%S.%fZ')
end_dt = start_dt + datetime.timedelta(seconds=2)
start_date = start_dt.strftime('%Y-%m-%d')
sthh = "{:02d}".format(start_dt.hour)
stmm = "{:02d}".format(start_dt.minute)
stss = start_dt.strftime('%S.%f')[:-3]
end_date = end_dt.strftime('%Y-%m-%d')
ethh = "{:02d}".format(end_dt.hour)
etmm = "{:02d}".format(end_dt.minute)
etss = end_dt.strftime('%S.%f')[:-3]
url = "http://"+toktlogger+"/api/instrumentData/inPeriod?after="+start_date+"T"+sthh+"%3A"+stmm+"%3A"+stss+"Z&before="+end_date+"T"+ethh+"%3A"+etmm+"%3A"+etss+"Z&mappingIds=depth&format=json"
response = requests.get(url)
json_bd = response.json()
if len(json_bd) >= 1:
bd = []
for i, t in enumerate(json_bd):
bd.append(t['numericValue'])
dic[key] = [np.median([bd])]
else:
dic[key] = ['']
elif key == 'stationName':
numFields = len(activity['fields'])
for fld in range(numFields):
if "station" in activity['fields'][fld]['name'].lower():
dic[key] = [activity['fields'][fld]['value']]
else:
dic[key] = ['']
if numFields == 0:
dic[key] = ['']
# Getting gear type from IMR activities list if possible by using the mapping in the list_gear_types.csv file
elif key == 'gearType':
if activity['activityTypeName'] in gear_df['IMR name'].values:
dic[key] = [gear_df.loc[gear_df['IMR name'] == activity['activityTypeName'], 'Gear type'].item()]
else:
dic[key] = ['']
elif val == '':
dic[key] = ['']
else:
dic[key] = [activity[val]]
new_activity = pd.DataFrame.from_dict(dic)
print(new_activity)
data = pd.concat([data, new_activity], ignore_index=True)
return data
def pull_metadata(toktlogger):
url = "http://"+toktlogger+"/api/cruises/current?format=json"
response = requests.get(url)
json_cruise = response.json()
metadata_dic = {
'title': [''],
'abstract': [''],
'pi_name': [''],
'pi_email': [''],
'pi_institution': [''],
'pi_address': [''],
'recordedBy': [''],
'projectID': [''],
'cruiseNumber': [int(json_cruise['cruiseNumber'])],
'vesselName': [json_cruise['vesselName']]
}
metadata_df = pd.DataFrame.from_dict(metadata_dic)
return metadata_df