import pandas as pd
import numpy as np
import io
import requests


def importdata(filename):
    if "https" in filename:  # likely a url
        s = requests.get(filename).content
        df = pd.read_csv(io.StringIO(s.decode('utf-8')))
    else:  # likely a local file
        df = pd.read_csv(filename)

    conversionfactor = 2**10 / 9.81  # Converts to meters / seconds / seconds
    a_x = df["x-axis"].to_numpy() / conversionfactor
    a_y = df["y-axis"].to_numpy() / conversionfactor
    a_z = df["z-axis"].to_numpy() / conversionfactor

    # This computes the timestamp approximately, assuming that
    # the sampling rate is *always* 100 Hertz
    samplingrate = 100  # in Hertz
    nsamples = len(a_x)  # = len(a_y) = len(a_z)
    seconds = np.linspace(0, nsamples / samplingrate, nsamples)

    return seconds, a_x, a_y, a_z
