This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
forked from oceanobservatories/preload-database
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_preload.py
executable file
·76 lines (63 loc) · 2.7 KB
/
parse_preload.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
#!/usr/bin/env python
import json
import os
import gdata.spreadsheet.service as service
import pandas as pd
import config
CSV_OVERWRITE_WARNING = '\n\nThis will overwrite the Resource Data csv\n' \
'files with the data from the RTN Preloaded\n' \
'Resources Google Sheet.\n\n' +\
'Do you want to continue [y|n]? > '
key = config.SPREADSHEET_KEY
SHEET_COLUMNS = {
'ParameterDefs':
['scenario', 'confluence', 'name', 'id', 'hid', 'hidconflict',
'parametertype', 'dimensions', 'valueencoding', 'codeset',
'unitofmeasure', 'fillvalue', 'displayname', 'precision', 'visible',
'parameterfunctionid', 'parameterfunctionmap', 'lookupvalue',
'qcfunctions', 'standardname', 'dataproductidentifier',
'referenceurls', 'description', 'reviewstatus', 'reviewcomment',
'longname', 'skip', 'dataproducttype', 'datalevel'],
'ParameterFunctions':
['scenario', 'id', 'hid', 'name', 'instrumentclass',
'instrumentseries', 'functiontype', 'function', 'owner', 'args',
'kwargs', 'description', 'reference', 'skip', 'qcflag'],
'ParameterDictionary':
['scenario', 'id', 'confluence', 'name', 'parameterids',
'temporalparameter', 'streamdependency', 'streamtype',
'streamcontent', 'reviewstatus'],
'BinSizes':
['stream', 'binsize', 'estimatedrate', 'measuredrate', 'binsizeindays',
'particlesperbin', 'estimatedvsingested', 'dataratenotes'],
}
def sheet_generator(name):
rows = []
print 'fetching from google'
client = service.SpreadsheetsService()
for sheet in client.GetWorksheetsFeed(key, visibility='public',
projection='basic').entry:
title = sheet.title.text
rowid = sheet.id.text.split('/')[-1]
if title == name:
for x in client.GetListFeed(key, rowid, visibility='public',
projection='values').entry:
d = {}
for k, v in x.custom.items():
if v.text is not None:
d[k] = v.text.strip()
else:
d[k] = None
rows.append(d)
yield d
def create_csv_files():
for sheet in SHEET_COLUMNS:
df = pd.DataFrame(list(sheet_generator(sheet)),
columns=SHEET_COLUMNS[sheet])
df.to_csv(os.path.join('csv', '%s.csv' % sheet),
encoding='utf-8', index=False)
if __name__ == '__main__':
resp = ''
while resp not in ['y', 'n']:
resp = raw_input(CSV_OVERWRITE_WARNING)
if resp == 'y':
create_csv_files()