Skip to content

Instantly share code, notes, and snippets.

@mgmarino
Created June 5, 2015 14:17
Show Gist options
  • Save mgmarino/0f8e7e81422d0a39b48e to your computer and use it in GitHub Desktop.
Save mgmarino/0f8e7e81422d0a39b48e to your computer and use it in GitHub Desktop.
import cloudant
import json
import urllib3.request as p
import requests
import struct
def interpret_file(url):
"""
File structure is:
bytes 0..3: length of json header N (excluding header word)
bytes 4..4+N: json header (ASCII data)
bytes 4+N+1..EOF: binary data of channels
The binary data format depends on what's in the json header:
header["channel_list"] ---> ordered list of channels
header["byte_depth"] ---> size of binary word
header["bit_shift"] ---> amount to shift right
Every channel is listed one after another for each time point (fully
interlaced)
"""
bits = requests.get(file_url, headers={'Range': 'bytes=0-3'}).content
header_length = struct.unpack("<L", bits)[0]
bits = requests.get(file_url, headers={'Range': 'bytes=4-{}'.format(4+header_length)}).content
hdr = json.loads(bits)
try:
bit_depth = hdr["bit_depth"]
except:
bit_depth = hdr["byte_depth"]
bit_shift = hdr["bit_shift"]
print(json.dumps(hdr, indent=4))
def build_url(doc):
cl = doc["file_servers"]["cluster-local"]
return cl + "?" + p.urlencode(dict(
id = doc["_id"],
filename=doc["external_docs"].values()[0]["ondiskname"],
db="nedm/measurements"
))
# Authentication
acct = cloudant.Account(uri="http://10.155.59.15:5984")
res = acct.login("nedm_user", """pw""")
assert res.status_code == 200
# Grab the correct database
db = acct["nedm%2Fmeasurements"]
# Reads all data from a certain time
document = db.document("c85a276eac9ceb54174368c97915f25c")
res = document.get().json()
#print(json.dumps(res, indent=4))
file_url = build_url(res)
interpret_file(file_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment