Exemple #1
0
def getObservations(stations,datetimes):
    ''' Returns a list of Observations. These do *not* contain data, but are essentially metadata for the measurements for a 
        particular station-datetime: whether it exists, when it was loaded into the database, etc. Takes a station
        (as Station or station id) or list of stations, and a datetime or tuple of datetimes. Note that as of now
        it is possible to get multiple datetimes for one station or multiple stations for one datetime but *not*
        multiple stations for multiple datetimes.
    '''

    freshenGlobals()
    stationIds = List([s.stationId for s in findStations(stations)])
    (begin,end) = _parseDatetimeParam(datetimes)

    if len(stationIds) > 1 and begin != end:
        raise Exception("Can get observations for multiple stations or multiple datetimes, but NOT both simultaneously.")
    elif len(stationIds) > 1 and begin == end:
        return list(observationDao.getObservations(begin,stationIds).values())
    else:
        return list(observationDao.getObservations(begin,end,stationIds.get(0)).values())
Exemple #2
0
def getData(stations,datetimes,elements):
    '''Get facts (as a Collection of Fact domain objects). Pass a station or list of stations (as Station, string or int), a 
        datetime (as Datetime, string, or int) or a tuple containing a beginDatetime and endDatetime (as Datetime, 
        string, or int), and an element or list of elements (as Element, string or int), and get back a list of Facts. 
        That might sound complicated, but the upshot is that you can pass stations, datetimes, and elements in almost 
        any way that happens to be convenient at the time. One handy additional form for dates is to use "+n" for 
        the endDatetime, with n the number of hours' data you'd like, eg ("2009010100","+24")
        
        Limited to getting 120,000 pieces of data at once. If you're SURE that you need more, and you're aware of the
        impact on the database, and you can't break it into several separate queries, call the allowQuerySizeOverride()
        method before each large query. 
        
        The returned Facts have the following properties: station, datetime, element, value, flag, stationId, datetimeId,
        and elementId. The ids are sometimes useful as arguments to DAO methods. 
    '''
    global querySizeOverrideAllowed
    freshenGlobals()
    
    stations = [s.stationId for s in findStations(stations)]
    (begin,end) = _parseDatetimeParam(datetimes)

    # Ensure query is not too big
    if (querySizeOverrideAllowed):
        querySizeOverrideAllowed = False
    else:
        if _queryTooLarge(stations,begin,end,elements):
            raise Exception("Query size too large. Aborting. ("+str(_length(stations))+" stations, "+
                            str(end+1-begin)+" datetimes, "+str(_length(elements))+" elements).")

    # Create a map of parameters in the form that ElementDao wants
    elementIds = [s.elementId for s in findElements(elements)]
    params = HashMap()
    params["stationIds"] = stations
    params["begin"] = begin
    params["end"]   = end
    params["elementIds"] = elementIds
    
    # Done handling input parameters. Go ahead and make the query.
    queryResults = elementDao.getElementValues(params)
    
    factList = _asFactList(queryResults)
    return FactCollection(factList)