Exemplo n.º 1
0
def get_inventory(segment, session=None, **kwargs):
    """raises tons of exceptions (see main). FIXME: write doc
    :param session: if **not** None but a valid sqlalchemy session object, then
    the inventory, if downloaded because not present, will be saveed to the db (compressed)
    """
    data = segment.channel.station.inventory_xml
    if not data:
        query_url = get_inventory_query(segment.channel.station)
        data = url_read(query_url, **kwargs)
        if session and data:
            segment.channel.station.inventory_xml = dumps_inv(data)
            session.commit()
        elif not data:
            raise ValueError("No data from server")
    return loads_inv(data)
Exemplo n.º 2
0
    def test_inventory_io(self):
        from obspy.core.inventory.inventory import Inventory
        e = models.Station(network='abcwerwre', station='gwdgafsf',
                           datacenter_id=self.session.query(models.DataCenter)[0].id,
                           latitude=3,
                           longitude=3)

        parentdir = os.path.dirname(os.path.dirname(__file__))
        invname = os.path.join(parentdir, "data", "inventory_GE.APE.xml")
        with open(invname, 'rb') as opn:
            data = opn.read()

        dumped_inv = dumps_inv(data,  compression='gzip', compresslevel=9)

        assert len(dumped_inv) < len(data)
        e.inventory_xml = dumped_inv

        self.session.add(e)
        self.session.commit()
        inv_xml = loads_inv(e.inventory_xml)

        assert isinstance(inv_xml, Inventory)
        
        inv_count = self.session.query(models.Station).filter(models.Station.inventory_xml != None).count()
        stationsc = self.session.query(models.Station).count()
        
        # test what happens deleting it:
        ret = self.session.query(models.Station).\
            filter(models.Station.inventory_xml!=None).\
            update({models.Station.inventory_xml: None})
        assert ret == inv_count
        
        self.session.commit()
        # assert we did not delete stations, but also their inventories:
        assert self.session.query(models.Station).count() == stationsc
        
        # test what happens deleting it (DANGER: WE ARE NOT DELETING ONLY invenotry_xml, SEE BELOW):
        ret = self.session.query(models.Station.inventory_xml).delete()
        assert ret == stationsc
        self.session.commit()
        
        # SHIT< WE DELETED ALL STATIONS IN THE COMMAND ABOVE, NOT ONLY inventory_xml!!
        # now delete only nonnull, should return zero:
        assert self.session.query(models.Station).count() == 0