def fetch_sensor(platformID: str, name: str, units: str = None, parser: str = None, tags: list = None, startime: int = None, endtime: int = None, truncate = True, db: str = DB, connection: MongoClient = CONN ) -> Sensor: """ Fetches a specified Sensor from MongoDB, truncated by startime and endtime Args: platformID (str): name of the target collection, typically a platformID name (str): Sensor name, eg 'm_depth' or 'salinity' units (str=None): sensor units, eg 'rad' or 'deg' or 'nodim' parser (str=None): parser-tag, eg mima or xbd or suflog tags (list=None): sensor's tags. #TODO, currently UNIMPLIMENTED startime (int=None): epoch query start time endtime (int=None): epoch query end time truncate (bool=True): True -> Sensor data range is exactly the requested range False -> Sensor data returned in 24hr chunks, as saved in database db (str=<default>): name of the target database connection (MongoClient=<default>): specifies host and port of target mongodb server Returns: a Sensor """ collection = connection[db][platformID] doc = {'name': name} if units: doc['units'] = units if parser: doc['parser'] = parser if startime: startday = MSensor.epoch2epochday(startime) else: startday = 0 startime = -inf if endtime: enday = endtime else: enday = time() #no future data endtime = inf doc['epochday'] = {'$gte':startday, '$lt':enday} cursor = collection.find(doc).sort('epochday',1) msensors = [] for doc in cursor: msensors.append(CLASSON.decode(doc)) # sensor.xy.extend(doc['xy']) sensor = MSensor.to_sensor(msensors) if truncate: sensor.xy = list(filter(lambda xy_tup: startime <= xy_tup[0] < endtime, sensor.xy)) return sensor