import requests


def lcls_archiver_history(pvname, start='2018-08-11T10:40:00.000-07:00', end='2018-08-11T11:40:00.000-07:00', verbose=True):
    """
    Get time series data from a PV name pvname,
        with start and end times in ISO 8601 format, using the EPICS Archiver Appliance:
    
    https://slacmshankar.github.io/epicsarchiver_docs/userguide.html
    
    Returns tuple: 
        secs, vals
    where secs is the UNIX timestamp, seconds since January 1, 1970, and vals are the values at those times.
    
    Seconds can be converted to a datetime object using:
    import datetime
    datetime.datetime.utcfromtimestamp(secs[0])
    
    """
    url="http://lcls-archapp.slac.stanford.edu/retrieval/data/getData.json?"
    url += "pv="+pvname
    url += "&from="+start
    url += "&to="+end
    #url += "&donotchunk"
    #url="http://lcls-archapp.slac.stanford.edu/retrieval/data/getData.json?pv=VPIO:IN20:111:VRAW&donotchunk"
    print(url)
    r = requests.get(url)
    data =  r.json()
    secs = [x['secs'] for x in data[0]['data']]
    vals = [x['val'] for x in data[0]['data']]
    return secs, vals