<commit_before>#!/usr/bin/env python
'''
@file ion/services/sa/product/test/test_data_product_management_service_integration.py
@brief Data Product Management Service Integration Tests
'''
from pyon.core.exception import NotFound
from pyon.public import  IonObject
from pyon.public import RT, PRED
from pyon.util.int_test import IonIntegrationTestCase
from pyon.util.log import log
from pyon.util.context import LocalContextMixin
from pyon.util.containers import DotDict
from pyon.datastore.datastore import DataStore

from ion.processes.data.last_update_cache import CACHE_DATASTORE_NAME
from ion.services.dm.utility.granule_utils import time_series_domain
from ion.util.parameter_yaml_IO import get_param_dict
from interface.services.dm.iuser_notification_service import UserNotificationServiceClient
from interface.services.cei.iprocess_dispatcher_service import ProcessDispatcherServiceClient
from interface.services.coi.iresource_registry_service import ResourceRegistryServiceClient
from interface.services.dm.idataset_management_service import DatasetManagementServiceClient
from interface.services.dm.iingestion_management_service import IngestionManagementServiceClient
from interface.services.dm.ipubsub_management_service import PubsubManagementServiceClient
from interface.services.sa.idata_acquisition_management_service import DataAcquisitionManagementServiceClient
from interface.services.sa.idata_product_management_service import  DataProductManagementServiceClient
from interface.objects import LastUpdate, ComputedValueAvailability

from nose.plugins.attrib import attr
from interface.objects import ProcessDefinition

from coverage_model.basic_types import AxisTypeEnum, MutabilityEnum
from coverage_model.coverage import CRS, GridDomain, GridShape

import unittest

class FakeProcess(LocalContextMixin):
    name = ''

@attr('INT', group='sa')
#@unittest.skip('not working')
class TestDataProductManagementServiceIntegration(IonIntegrationTestCase):

    def setUp(self):
        # Start container
        #print 'instantiating container'
        self._start_container()

        self.container.start_rel_from_url('res/deploy/r2deploy.yml')

        self.dpsc_cli = DataProductManagementServiceClient(node=self.container.node)
        self.rrclient = ResourceRegistryServiceClient(node=self.container.node)
        self.damsclient = DataAcquisitionManagementServiceClient(node=self.container.node)
        self.pubsubcli =  PubsubManagementServiceClient(node=self.container.node)
        self.ingestclient = IngestionManagementServiceClient(node=self.container.node)
        self.process_dispatcher   = ProcessDispatcherServiceClient()
        self.dataset_management = DatasetManagementServiceClient()
        self.unsc = UserNotificationServiceClient()

        #------------------------------------------
        # Create the environment
        #------------------------------------------

        datastore_name = CACHE_DATASTORE_NAME
        self.db = self.container.datastore_manager.get_datastore(datastore_name)
        self.stream_def_id = self.pubsubcli.create_stream_definition(name='SBE37_CDM')

        self.process_definitions  = {}
        ingestion_worker_definition = ProcessDefinition(name='ingestion worker')
        ingestion_worker_definition.executable = {
            'module':'ion.processes.data.ingestion.science_granule_ingestion_worker',
            'class' :'ScienceGranuleIngestionWorker'
        }
        process_definition_id = self.process_dispatcher.create_process_definition(process_definition=ingestion_worker_definition)
        self.process_definitions['ingestion_worker'] = process_definition_id

        #------------------------------------------------------------------------------------------------
        # First launch the ingestors
        #------------------------------------------------------------------------------------------------
        self.exchange_space       = 'science_granule_ingestion'
        self.exchange_point       = 'science_data'
        config = DotDict()
        config.process.datastore_name = 'datasets'
        config.process.queue_name = self.exchange_space

        self.process_dispatcher.schedule_process(self.process_definitions['ingestion_worker'],configuration=config)

    def get_datastore(self, dataset_id):
        dataset = self.dataset_management.read_dataset(dataset_id)
        datastore_name = dataset.datastore_name
        datastore = self.container.datastore_manager.get_datastore(datastore_name, DataStore.DS_PROFILE.SCIDATA)
        return datastore

    @unittest.skip('OBE')
    def test_get_last_update(self):

        # Construct temporal and spatial Coordinate Reference System objects
        tcrs = CRS([AxisTypeEnum.TIME])
        scrs = CRS([AxisTypeEnum.LON, AxisTypeEnum.LAT])

        # Construct temporal and spatial Domain objects
        tdom = GridDomain(GridShape('temporal', [0]), tcrs, MutabilityEnum.EXTENSIBLE) # 1d (timeline)
        sdom = GridDomain(GridShape('spatial', [0]), scrs, MutabilityEnum.IMMUTABLE) # 1d spatial topology (station/trajectory)

        sdom = sdom.dump()
        tdom = tdom.dump()

        #@TODO: DO NOT DO THIS, WHEN THIS TEST IS REWRITTEN GET RID OF THIS, IT WILL FAIL, thanks -Luke
        parameter_dictionary = get_param_dict('ctd_parsed_param_dict')
        parameter_dictionary = parameter_dictionary.dump()

        dp_obj = IonObject(RT.DataProduct,
            name='DP1',
            description='some new dp',
            temporal_domain = tdom,
            spatial_domain = sdom)

        data_product_id = self.dpsc_cli.create_data_product(data_product=dp_obj, stream_definition_id=self.stream_def_id, parameter_dictionary=parameter_dictionary)
        stream_ids, garbage = self.rrclient.find_objects(data_product_id, PRED.hasStream, id_only=True)
        stream_id = stream_ids[0]

        fake_lu = LastUpdate()
        fake_lu_doc = self.db._ion_object_to_persistence_dict(fake_lu)
        self.db.create_doc(fake_lu_doc, object_id=stream_id)

        #------------------------------------------
        # Now execute
        #------------------------------------------
        res = self.dpsc_cli.get_last_update(data_product_id=data_product_id)
        self.assertTrue(isinstance(res[stream_id], LastUpdate), 'retrieving documents failed')

    def test_create_data_product(self):

        #------------------------------------------------------------------------------------------------
        # create a stream definition for the data from the ctd simulator
        #------------------------------------------------------------------------------------------------
        parameter_dictionary_id = self.dataset_management.read_parameter_dictionary_by_name('ctd_parsed_param_dict')
        ctd_stream_def_id = self.pubsubcli.create_stream_definition(name='Simulated CTD data', parameter_dictionary_id=parameter_dictionary_id)
        log.debug("Created stream def id %s" % ctd_stream_def_id)

        #------------------------------------------------------------------------------------------------
        # test creating a new data product w/o a stream definition
        #------------------------------------------------------------------------------------------------
        log.debug('test_createDataProduct: Creating new data product w/o a stream definition (L4-CI-SA-RQ-308)')

        # Generic time-series data domain creation
        tdom, sdom = time_series_domain()

        dp_obj = IonObject(RT.DataProduct,
            name='DP1',
            description='some new dp',
            temporal_domain = tdom.dump(),
            spatial_domain = sdom.dump())

        log.debug("Created an IonObject for a data product: %s" % dp_obj)

        #------------------------------------------------------------------------------------------------
        # Create a set of ParameterContext objects to define the parameters in the coverage, add each to the ParameterDictionary
        #------------------------------------------------------------------------------------------------

        dp_id = self.dpsc_cli.create_data_product( data_product= dp_obj,
                                            stream_definition_id=ctd_stream_def_id)

        dp_obj = self.dpsc_cli.read_data_product(dp_id)

        log.debug('new dp_id = %s' % dp_id)
        log.debug("test_createDataProduct: Data product info from registry %s (L4-CI-SA-RQ-308)", str(dp_obj))

        #------------------------------------------------------------------------------------------------
        # test creating a new data product with  a stream definition
        #------------------------------------------------------------------------------------------------
        log.debug('Creating new data product with a stream definition')
        dp_obj = IonObject(RT.DataProduct,
            name='DP2',
            description='some new dp',
            temporal_domain = tdom.dump(),
            spatial_domain = sdom.dump())

        dp_id2 = self.dpsc_cli.create_data_product(dp_obj, ctd_stream_def_id)
        log.debug('new dp_id = %s' % dp_id2)

        #------------------------------------------------------------------------------------------------
        #make sure data product is associated with stream def
        #------------------------------------------------------------------------------------------------
        streamdefs = []
        streams, _ = self.rrclient.find_objects(dp_id2, PRED.hasStream, RT.Stream, True)
        for s in streams:
            log.debug("Checking stream %s" % s)
            sdefs, _ = self.rrclient.find_objects(s, PRED.hasStreamDefinition, RT.StreamDefinition, True)
            for sd in sdefs:
                log.debug("Checking streamdef %s" % sd)
                streamdefs.append(sd)
        self.assertIn(ctd_stream_def_id, streamdefs)

        # test reading a non-existent data product
        log.debug('reading non-existent data product')

        with self.assertRaises(NotFound):
            dp_obj = self.dpsc_cli.read_data_product('some_fake_id')

        # update a data product (tests read also)
        log.debug('Updating data product')
        # first get the existing dp object
        dp_obj = self.dpsc_cli.read_data_product(dp_id)

        # now tweak the object
        dp_obj.description = 'the very first dp'
        # now write the dp back to the registry
        update_result = self.dpsc_cli.update_data_product(dp_obj)

        # now get the dp back to see if it was updated
        dp_obj = self.dpsc_cli.read_data_product(dp_id)
        self.assertEquals(dp_obj.description,'the very first dp')

        #test extension
        extended_product = self.dpsc_cli.get_data_product_extension(dp_id)
        self.assertEqual(dp_id, extended_product._id)
        self.assertEqual(ComputedValueAvailability.PROVIDED,
                         extended_product.computed.product_download_size_estimated.status)
        self.assertEqual(0, extended_product.computed.product_download_size_estimated.value)

        self.assertEqual(ComputedValueAvailability.PROVIDED,
                         extended_product.computed.parameters.status)
        #log.debug("test_create_data_product: parameters %s" % extended_product.computed.parameters.value)

        # now 'delete' the data product
        log.debug("deleting data product: %s" % dp_id)
        self.dpsc_cli.delete_data_product(dp_id)

        self.dpsc_cli.force_delete_data_product(dp_id)

        # now try to get the deleted dp object
        try:
            dp_obj = self.dpsc_cli.read_data_product(dp_id)
        except NotFound as ex:
            pass
        else:
            self.fail("force deleted data product was found during read")

        # Get the events corresponding to the data product
        ret = self.unsc.get_recent_events(resource_id=dp_id)
        events = ret.value

        for event in events:
            log.debug("event time: %s" % event.ts_created)

        # now try to get the deleted dp object

        #todo: the RR should perhaps not return retired data products
    #            dp_obj = self.dpsc_cli.read_data_product(dp_id)

    # now try to delete the already deleted dp object
    #        log.debug( "deleting non-existing data product")
    #            self.dpsc_cli.delete_data_product(dp_id)

    # Shut down container
    #container.stop()

    def test_activate_suspend_data_product(self):

        #------------------------------------------------------------------------------------------------
        # create a stream definition for the data from the ctd simulator
        #------------------------------------------------------------------------------------------------
        pdict_id = self.dataset_management.read_parameter_dictionary_by_name('ctd_parsed_param_dict', id_only=True)
        ctd_stream_def_id = self.pubsubcli.create_stream_definition(name='Simulated CTD data', parameter_dictionary_id=pdict_id)
        log.debug("Created stream def id %s" % ctd_stream_def_id)

        #------------------------------------------------------------------------------------------------
        # test creating a new data product w/o a stream definition
        #------------------------------------------------------------------------------------------------
        log.debug('test_createDataProduct: Creating new data product w/o a stream definition (L4-CI-SA-RQ-308)')

        # Construct temporal and spatial Coordinate Reference System objects
        tdom, sdom = time_series_domain()

        sdom = sdom.dump()
        tdom = tdom.dump()

        dp_obj = IonObject(RT.DataProduct,
            name='DP1',
            description='some new dp',
            temporal_domain = tdom,
            spatial_domain = sdom)

        log.debug("Created an IonObject for a data product: %s" % dp_obj)

        #------------------------------------------------------------------------------------------------
        # Create a set of ParameterContext objects to define the parameters in the coverage, add each to the ParameterDictionary
        #------------------------------------------------------------------------------------------------

        dp_id = self.dpsc_cli.create_data_product(data_product= dp_obj,
            stream_definition_id=ctd_stream_def_id)

        dp_obj = self.dpsc_cli.read_data_product(dp_id)

        log.debug('new dp_id = %s' % dp_id)
        log.debug("test_createDataProduct: Data product info from registry %s (L4-CI-SA-RQ-308)", str(dp_obj))

        dataset_ids, _ = self.rrclient.find_objects(subject=dp_id, predicate=PRED.hasDataset, id_only=True)
        if not dataset_ids:
            raise NotFound("Data Product %s dataset  does not exist" % str(dp_id))
        self.get_datastore(dataset_ids[0])

        #------------------------------------------------------------------------------------------------
        # test activate and suspend data product persistence
        #------------------------------------------------------------------------------------------------
        self.dpsc_cli.activate_data_product_persistence(dp_id)

        #------------------------------------------------------------------------------------------------
        # test suspend data product persistence
        #------------------------------------------------------------------------------------------------
        self.dpsc_cli.suspend_data_product_persistence(dp_id)

#        pid = self.container.spawn_process(name='dummy_process_for_test',
#            module='pyon.ion.process',
#            cls='SimpleProcess',
#            config={})
#        dummy_process = self.container.proc_manager.procs[pid]

        self.dpsc_cli.force_delete_data_product(dp_id)
        # now try to get the deleted dp object
        try:
            dp_obj = self.rrclient.read(dp_id)
        except NotFound as ex:
            pass
        else:
            self.fail("force_deleted data product was found during read")<commit_msg>Added check to assert that sa req L4-CI-SA-RQ-308 is satisfied<commit_after>#!/usr/bin/env python
'''
@file ion/services/sa/product/test/test_data_product_management_service_integration.py
@brief Data Product Management Service Integration Tests
'''
from pyon.core.exception import NotFound
from pyon.public import  IonObject
from pyon.public import RT, PRED
from pyon.util.int_test import IonIntegrationTestCase
from pyon.util.log import log
from pyon.util.context import LocalContextMixin
from pyon.util.containers import DotDict
from pyon.datastore.datastore import DataStore

from ion.processes.data.last_update_cache import CACHE_DATASTORE_NAME
from ion.services.dm.utility.granule_utils import time_series_domain
from ion.util.parameter_yaml_IO import get_param_dict
from interface.services.dm.iuser_notification_service import UserNotificationServiceClient
from interface.services.cei.iprocess_dispatcher_service import ProcessDispatcherServiceClient
from interface.services.coi.iresource_registry_service import ResourceRegistryServiceClient
from interface.services.dm.idataset_management_service import DatasetManagementServiceClient
from interface.services.dm.iingestion_management_service import IngestionManagementServiceClient
from interface.services.dm.ipubsub_management_service import PubsubManagementServiceClient
from interface.services.sa.idata_acquisition_management_service import DataAcquisitionManagementServiceClient
from interface.services.sa.idata_product_management_service import  DataProductManagementServiceClient
from interface.objects import LastUpdate, ComputedValueAvailability

from nose.plugins.attrib import attr
from interface.objects import ProcessDefinition

from coverage_model.basic_types import AxisTypeEnum, MutabilityEnum
from coverage_model.coverage import CRS, GridDomain, GridShape

import unittest

class FakeProcess(LocalContextMixin):
    name = ''

@attr('INT', group='sa')
#@unittest.skip('not working')
class TestDataProductManagementServiceIntegration(IonIntegrationTestCase):

    def setUp(self):
        # Start container
        #print 'instantiating container'
        self._start_container()

        self.container.start_rel_from_url('res/deploy/r2deploy.yml')

        self.dpsc_cli = DataProductManagementServiceClient(node=self.container.node)
        self.rrclient = ResourceRegistryServiceClient(node=self.container.node)
        self.damsclient = DataAcquisitionManagementServiceClient(node=self.container.node)
        self.pubsubcli =  PubsubManagementServiceClient(node=self.container.node)
        self.ingestclient = IngestionManagementServiceClient(node=self.container.node)
        self.process_dispatcher   = ProcessDispatcherServiceClient()
        self.dataset_management = DatasetManagementServiceClient()
        self.unsc = UserNotificationServiceClient()

        #------------------------------------------
        # Create the environment
        #------------------------------------------

        datastore_name = CACHE_DATASTORE_NAME
        self.db = self.container.datastore_manager.get_datastore(datastore_name)
        self.stream_def_id = self.pubsubcli.create_stream_definition(name='SBE37_CDM')

        self.process_definitions  = {}
        ingestion_worker_definition = ProcessDefinition(name='ingestion worker')
        ingestion_worker_definition.executable = {
            'module':'ion.processes.data.ingestion.science_granule_ingestion_worker',
            'class' :'ScienceGranuleIngestionWorker'
        }
        process_definition_id = self.process_dispatcher.create_process_definition(process_definition=ingestion_worker_definition)
        self.process_definitions['ingestion_worker'] = process_definition_id

        #------------------------------------------------------------------------------------------------
        # First launch the ingestors
        #------------------------------------------------------------------------------------------------
        self.exchange_space       = 'science_granule_ingestion'
        self.exchange_point       = 'science_data'
        config = DotDict()
        config.process.datastore_name = 'datasets'
        config.process.queue_name = self.exchange_space

        self.process_dispatcher.schedule_process(self.process_definitions['ingestion_worker'],configuration=config)

    def get_datastore(self, dataset_id):
        dataset = self.dataset_management.read_dataset(dataset_id)
        datastore_name = dataset.datastore_name
        datastore = self.container.datastore_manager.get_datastore(datastore_name, DataStore.DS_PROFILE.SCIDATA)
        return datastore

    @unittest.skip('OBE')
    def test_get_last_update(self):

        # Construct temporal and spatial Coordinate Reference System objects
        tcrs = CRS([AxisTypeEnum.TIME])
        scrs = CRS([AxisTypeEnum.LON, AxisTypeEnum.LAT])

        # Construct temporal and spatial Domain objects
        tdom = GridDomain(GridShape('temporal', [0]), tcrs, MutabilityEnum.EXTENSIBLE) # 1d (timeline)
        sdom = GridDomain(GridShape('spatial', [0]), scrs, MutabilityEnum.IMMUTABLE) # 1d spatial topology (station/trajectory)

        sdom = sdom.dump()
        tdom = tdom.dump()

        #@TODO: DO NOT DO THIS, WHEN THIS TEST IS REWRITTEN GET RID OF THIS, IT WILL FAIL, thanks -Luke
        parameter_dictionary = get_param_dict('ctd_parsed_param_dict')
        parameter_dictionary = parameter_dictionary.dump()

        dp_obj = IonObject(RT.DataProduct,
            name='DP1',
            description='some new dp',
            temporal_domain = tdom,
            spatial_domain = sdom)

        data_product_id = self.dpsc_cli.create_data_product(data_product=dp_obj, stream_definition_id=self.stream_def_id, parameter_dictionary=parameter_dictionary)
        stream_ids, garbage = self.rrclient.find_objects(data_product_id, PRED.hasStream, id_only=True)
        stream_id = stream_ids[0]

        fake_lu = LastUpdate()
        fake_lu_doc = self.db._ion_object_to_persistence_dict(fake_lu)
        self.db.create_doc(fake_lu_doc, object_id=stream_id)

        #------------------------------------------
        # Now execute
        #------------------------------------------
        res = self.dpsc_cli.get_last_update(data_product_id=data_product_id)
        self.assertTrue(isinstance(res[stream_id], LastUpdate), 'retrieving documents failed')

    def test_create_data_product(self):

        #------------------------------------------------------------------------------------------------
        # create a stream definition for the data from the ctd simulator
        #------------------------------------------------------------------------------------------------
        parameter_dictionary_id = self.dataset_management.read_parameter_dictionary_by_name('ctd_parsed_param_dict')
        ctd_stream_def_id = self.pubsubcli.create_stream_definition(name='Simulated CTD data', parameter_dictionary_id=parameter_dictionary_id)
        log.debug("Created stream def id %s" % ctd_stream_def_id)

        #------------------------------------------------------------------------------------------------
        # test creating a new data product w/o a stream definition
        #------------------------------------------------------------------------------------------------
        log.debug('test_createDataProduct: Creating new data product w/o a stream definition (L4-CI-SA-RQ-308)')

        # Generic time-series data domain creation
        tdom, sdom = time_series_domain()

        dp_obj = IonObject(RT.DataProduct,
            name='DP1',
            description='some new dp',
            temporal_domain = tdom.dump(),
            spatial_domain = sdom.dump())

        log.debug("Created an IonObject for a data product: %s" % dp_obj)

        #------------------------------------------------------------------------------------------------
        # Create a set of ParameterContext objects to define the parameters in the coverage, add each to the ParameterDictionary
        #------------------------------------------------------------------------------------------------

        dp_id = self.dpsc_cli.create_data_product( data_product= dp_obj,
                                            stream_definition_id=ctd_stream_def_id)

        dp_obj = self.dpsc_cli.read_data_product(dp_id)

        log.debug('new dp_id = %s' % dp_id)
        log.debug("test_createDataProduct: Data product info from registry %s (L4-CI-SA-RQ-308)", str(dp_obj))

        #------------------------------------------------------------------------------------------------
        # test creating a new data product with  a stream definition
        #------------------------------------------------------------------------------------------------
        log.debug('Creating new data product with a stream definition')
        dp_obj = IonObject(RT.DataProduct,
            name='DP2',
            description='some new dp',
            temporal_domain = tdom.dump(),
            spatial_domain = sdom.dump())

        dp_id2 = self.dpsc_cli.create_data_product(dp_obj, ctd_stream_def_id)
        log.debug('new dp_id = %s' % dp_id2)

        #------------------------------------------------------------------------------------------------
        #make sure data product is associated with stream def
        #------------------------------------------------------------------------------------------------
        streamdefs = []
        streams, _ = self.rrclient.find_objects(dp_id2, PRED.hasStream, RT.Stream, True)
        for s in streams:
            log.debug("Checking stream %s" % s)
            sdefs, _ = self.rrclient.find_objects(s, PRED.hasStreamDefinition, RT.StreamDefinition, True)
            for sd in sdefs:
                log.debug("Checking streamdef %s" % sd)
                streamdefs.append(sd)
        self.assertIn(ctd_stream_def_id, streamdefs)

        # test reading a non-existent data product
        log.debug('reading non-existent data product')

        with self.assertRaises(NotFound):
            dp_obj = self.dpsc_cli.read_data_product('some_fake_id')

        # update a data product (tests read also)
        log.debug('Updating data product')
        # first get the existing dp object
        dp_obj = self.dpsc_cli.read_data_product(dp_id)

        # now tweak the object
        dp_obj.description = 'the very first dp'
        # now write the dp back to the registry
        update_result = self.dpsc_cli.update_data_product(dp_obj)

        # now get the dp back to see if it was updated
        dp_obj = self.dpsc_cli.read_data_product(dp_id)
        self.assertEquals(dp_obj.description,'the very first dp')

        #test extension
        extended_product = self.dpsc_cli.get_data_product_extension(dp_id)
        self.assertEqual(dp_id, extended_product._id)
        self.assertEqual(ComputedValueAvailability.PROVIDED,
                         extended_product.computed.product_download_size_estimated.status)
        self.assertEqual(0, extended_product.computed.product_download_size_estimated.value)

        self.assertEqual(ComputedValueAvailability.PROVIDED,
                         extended_product.computed.parameters.status)
        #log.debug("test_create_data_product: parameters %s" % extended_product.computed.parameters.value)

        # now 'delete' the data product
        log.debug("deleting data product: %s" % dp_id)
        self.dpsc_cli.delete_data_product(dp_id)

        self.dpsc_cli.force_delete_data_product(dp_id)

        # now try to get the deleted dp object
        try:
            dp_obj = self.dpsc_cli.read_data_product(dp_id)
        except NotFound as ex:
            pass
        else:
            self.fail("force deleted data product was found during read")

        # Get the events corresponding to the data product
        ret = self.unsc.get_recent_events(resource_id=dp_id)
        events = ret.value

        for event in events:
            log.debug("event time: %s" % event.ts_created)

        # now try to get the deleted dp object

        #todo: the RR should perhaps not return retired data products
    #            dp_obj = self.dpsc_cli.read_data_product(dp_id)

    # now try to delete the already deleted dp object
    #        log.debug( "deleting non-existing data product")
    #            self.dpsc_cli.delete_data_product(dp_id)

    # Shut down container
    #container.stop()

    def test_activate_suspend_data_product(self):

        #------------------------------------------------------------------------------------------------
        # create a stream definition for the data from the ctd simulator
        #------------------------------------------------------------------------------------------------
        pdict_id = self.dataset_management.read_parameter_dictionary_by_name('ctd_parsed_param_dict', id_only=True)
        ctd_stream_def_id = self.pubsubcli.create_stream_definition(name='Simulated CTD data', parameter_dictionary_id=pdict_id)
        log.debug("Created stream def id %s" % ctd_stream_def_id)

        #------------------------------------------------------------------------------------------------
        # test creating a new data product w/o a stream definition
        #------------------------------------------------------------------------------------------------
        log.debug('test_createDataProduct: Creating new data product w/o a stream definition (L4-CI-SA-RQ-308)')

        # Construct temporal and spatial Coordinate Reference System objects
        tdom, sdom = time_series_domain()

        sdom = sdom.dump()
        tdom = tdom.dump()

        dp_obj = IonObject(RT.DataProduct,
            name='DP1',
            description='some new dp',
            temporal_domain = tdom,
            spatial_domain = sdom)

        log.debug("Created an IonObject for a data product: %s" % dp_obj)

        #------------------------------------------------------------------------------------------------
        # Create a set of ParameterContext objects to define the parameters in the coverage, add each to the ParameterDictionary
        #------------------------------------------------------------------------------------------------

        dp_id = self.dpsc_cli.create_data_product(data_product= dp_obj,
            stream_definition_id=ctd_stream_def_id)

        dp_obj = self.dpsc_cli.read_data_product(dp_id)

        log.debug('new dp_id = %s' % dp_id)
        log.debug("test_createDataProduct: Data product info from registry %s (L4-CI-SA-RQ-308)", str(dp_obj))

        dataset_ids, _ = self.rrclient.find_objects(subject=dp_id, predicate=PRED.hasDataset, id_only=True)
        if not dataset_ids:
            raise NotFound("Data Product %s dataset  does not exist" % str(dp_id))
        self.get_datastore(dataset_ids[0])

        #------------------------------------------------------------------------------------------------
        # test activate and suspend data product persistence
        #------------------------------------------------------------------------------------------------
        self.dpsc_cli.activate_data_product_persistence(dp_id)

        # Check that the streams associated with the data product are persisted with
        stream_ids, _ =  self.rrclient.find_objects(dp_id,PRED.hasStream,RT.Stream,True)
        for stream_id in stream_ids:
            self.assertTrue(self.ingestclient.is_persisted(stream_id))

        log.debug("Satisfies L4-CI-SA-RQ-308: 'Data product management shall persist data product metadata'")

        #------------------------------------------------------------------------------------------------
        # test suspend data product persistence
        #------------------------------------------------------------------------------------------------
        self.dpsc_cli.suspend_data_product_persistence(dp_id)

#        pid = self.container.spawn_process(name='dummy_process_for_test',
#            module='pyon.ion.process',
#            cls='SimpleProcess',
#            config={})
#        dummy_process = self.container.proc_manager.procs[pid]

        self.dpsc_cli.force_delete_data_product(dp_id)
        # now try to get the deleted dp object
        try:
            dp_obj = self.rrclient.read(dp_id)
        except NotFound as ex:
            pass
        else:
            self.fail("force_deleted data product was found during read")<|endoftext|>


<commit_before>'Algorithmia Algorithm API Client (python)'

import base64
import re
from Algorithmia.async_response import AsyncResponse
from Algorithmia.algo_response import AlgoResponse
from Algorithmia.errors import ApiError, ApiInternalError
from enum import Enum
from algorithmia_api_client.rest import ApiException
from algorithmia_api_client import CreateRequest, UpdateRequest, VersionRequest, Details, Settings, SettingsMandatory, SettingsPublish, \
    CreateRequestVersionInfo, VersionInfo, VersionInfoPublish

OutputType = Enum('OutputType','default raw void')

class Algorithm(object):
    def __init__(self, client, algoRef):
        # Parse algoRef
        algoRegex = re.compile(r"(?:algo://|/|)(\w+/.+)")
        m = algoRegex.match(algoRef)
        if m is not None:
            self.client = client
            self.path = m.group(1)
            self.username = self.path.split("/")[0]
            self.algoname = self.path.split("/")[1]
            if len(self.path.split("/")) > 2:
                self.version = self.path.split("/")[2]
            self.url = '/v1/algo/' + self.path
            self.query_parameters = {}
            self.output_type = OutputType.default
        else:
            raise ValueError('Invalid algorithm URI: ' + algoRef)

    def set_options(self, timeout=300, stdout=False, output=OutputType.default, **query_parameters):
        self.query_parameters = {'timeout':timeout, 'stdout':stdout}
        self.output_type = output
        self.query_parameters.update(query_parameters)
        return self

    # Create a new algorithm
    def create(self, details, settings, version_info):
        detailsObj = Details(**details)
        settingsObj = SettingsMandatory(**settings)
        createRequestVersionInfoObj = CreateRequestVersionInfo(**version_info)
        create_parameters = {"name": self.algoname, "details": detailsObj, "settings": settingsObj, "version_info": createRequestVersionInfoObj}
        create_request = CreateRequest(**create_parameters)
        try:
            # Create Algorithm
            api_response = self.client.manageApi.create_algorithm(self.username, create_request)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->create_algorithm: %s\n" % e)

    # Update the settings in an algorithm
    def update(self, details, settings, version_info):
        detailsObj = Details(**details)
        settingsObj = Settings(**settings)
        createRequestVersionInfoObj = CreateRequestVersionInfo(**version_info)
        update_parameters = {"details": detailsObj, "settings": settingsObj, "version_info": createRequestVersionInfoObj}
        update_request = UpdateRequest(**update_parameters)
        try:
            # Update Algorithm
            api_response = self.client.manageApi.update_algorithm(self.username, self.algoname, update_request)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->update_algorithm: %s\n" % e)

    # Publish an algorithm
    def publish(self, details={}, settings={}, version_info={}):
        detailsObj = Details(**details)
        settingsObj = SettingsPublish(**settings)
        versionRequestObj = VersionInfoPublish(**version_info)
        publish_parameters = {"details": detailsObj, "settings": settingsObj, "version_info": versionRequestObj}
        version_request = VersionRequest(**publish_parameters) # VersionRequest | Publish Version Request
        try:
            # Publish Algorithm
            api_response = self.client.manageApi.publish_algorithm(self.username, self.algoname, version_request)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->publish_algorithm: %s\n" % e)

    # Get info on an algorithm
    def getInfo(self):
        try:
            # Get Algorithm
            api_response = self.client.manageApi.get_algorithm(self.username, self.algoname)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->get_algorithm: %s\n" % e)

    # Compile an algorithm
    def compile(self):
        try:
            # Compile algorithm
            api_response = self.client.manageApi.algorithms_username_algoname_compile_post(self.username, self.algoname)
            return api_response
        except ApiException as e:
            print("Exception when calling DefaultApi->algorithms_username_algoname_compile_post: %s\n" % e)

    # Pipe an input into this algorithm
    def pipe(self, input1):

        if self.output_type == OutputType.raw:
            return self._postRawOutput(input1)
        elif self.output_type == OutputType.void:
            return self._postVoidOutput(input1)
        else:
            return AlgoResponse.create_algo_response(self.client.postJsonHelper(self.url, input1, **self.query_parameters))

    def _postRawOutput(self, input1):
            # Don't parse response as json
            self.query_parameters['output'] = 'raw'
            response = self.client.postJsonHelper(self.url, input1, parse_response_as_json=False, **self.query_parameters)
            # Check HTTP code and throw error as needed
            if response.status_code == 400:
                # Bad request
                raise ApiError(response.text)
            elif response.status_code == 500:
                raise ApiInternalError(response.text)
            else:
                return response.text

    def _postVoidOutput(self, input1):
            self.query_parameters['output'] = 'void'
            responseJson = self.client.postJsonHelper(self.url, input1, **self.query_parameters)
            if 'error' in responseJson:
                raise ApiError(responseJson['error']['message'])
            else:
                return AsyncResponse(responseJson)
<commit_msg>Make all parameters optional to algo management api's since the server response w/ appropiate error anyway<commit_after>'Algorithmia Algorithm API Client (python)'

import base64
import re
from Algorithmia.async_response import AsyncResponse
from Algorithmia.algo_response import AlgoResponse
from Algorithmia.errors import ApiError, ApiInternalError
from enum import Enum
from algorithmia_api_client.rest import ApiException
from algorithmia_api_client import CreateRequest, UpdateRequest, VersionRequest, Details, Settings, SettingsMandatory, SettingsPublish, \
    CreateRequestVersionInfo, VersionInfo, VersionInfoPublish

OutputType = Enum('OutputType','default raw void')

class Algorithm(object):
    def __init__(self, client, algoRef):
        # Parse algoRef
        algoRegex = re.compile(r"(?:algo://|/|)(\w+/.+)")
        m = algoRegex.match(algoRef)
        if m is not None:
            self.client = client
            self.path = m.group(1)
            self.username = self.path.split("/")[0]
            self.algoname = self.path.split("/")[1]
            if len(self.path.split("/")) > 2:
                self.version = self.path.split("/")[2]
            self.url = '/v1/algo/' + self.path
            self.query_parameters = {}
            self.output_type = OutputType.default
        else:
            raise ValueError('Invalid algorithm URI: ' + algoRef)

    def set_options(self, timeout=300, stdout=False, output=OutputType.default, **query_parameters):
        self.query_parameters = {'timeout':timeout, 'stdout':stdout}
        self.output_type = output
        self.query_parameters.update(query_parameters)
        return self

    # Create a new algorithm
    def create(self, details={}, settings={}, version_info={}):
        detailsObj = Details(**details)
        settingsObj = SettingsMandatory(**settings)
        createRequestVersionInfoObj = CreateRequestVersionInfo(**version_info)
        create_parameters = {"name": self.algoname, "details": detailsObj, "settings": settingsObj, "version_info": createRequestVersionInfoObj}
        create_request = CreateRequest(**create_parameters)
        try:
            # Create Algorithm
            api_response = self.client.manageApi.create_algorithm(self.username, create_request)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->create_algorithm: %s\n" % e)

    # Update the settings in an algorithm
    def update(self, details={}, settings={}, version_info={}):
        detailsObj = Details(**details)
        settingsObj = Settings(**settings)
        createRequestVersionInfoObj = CreateRequestVersionInfo(**version_info)
        update_parameters = {"details": detailsObj, "settings": settingsObj, "version_info": createRequestVersionInfoObj}
        update_request = UpdateRequest(**update_parameters)
        try:
            # Update Algorithm
            api_response = self.client.manageApi.update_algorithm(self.username, self.algoname, update_request)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->update_algorithm: %s\n" % e)

    # Publish an algorithm
    def publish(self, details={}, settings={}, version_info={}):
        detailsObj = Details(**details)
        settingsObj = SettingsPublish(**settings)
        versionRequestObj = VersionInfoPublish(**version_info)
        publish_parameters = {"details": detailsObj, "settings": settingsObj, "version_info": versionRequestObj}
        version_request = VersionRequest(**publish_parameters) # VersionRequest | Publish Version Request
        try:
            # Publish Algorithm
            api_response = self.client.manageApi.publish_algorithm(self.username, self.algoname, version_request)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->publish_algorithm: %s\n" % e)

    # Get info on an algorithm
    def getInfo(self):
        try:
            # Get Algorithm
            api_response = self.client.manageApi.get_algorithm(self.username, self.algoname)
            return api_response
        except ApiException as e:
            raise ApiError("Exception when calling DefaultApi->get_algorithm: %s\n" % e)

    # Compile an algorithm
    def compile(self):
        try:
            # Compile algorithm
            api_response = self.client.manageApi.algorithms_username_algoname_compile_post(self.username, self.algoname)
            return api_response
        except ApiException as e:
            print("Exception when calling DefaultApi->algorithms_username_algoname_compile_post: %s\n" % e)

    # Pipe an input into this algorithm
    def pipe(self, input1):

        if self.output_type == OutputType.raw:
            return self._postRawOutput(input1)
        elif self.output_type == OutputType.void:
            return self._postVoidOutput(input1)
        else:
            return AlgoResponse.create_algo_response(self.client.postJsonHelper(self.url, input1, **self.query_parameters))

    def _postRawOutput(self, input1):
            # Don't parse response as json
            self.query_parameters['output'] = 'raw'
            response = self.client.postJsonHelper(self.url, input1, parse_response_as_json=False, **self.query_parameters)
            # Check HTTP code and throw error as needed
            if response.status_code == 400:
                # Bad request
                raise ApiError(response.text)
            elif response.status_code == 500:
                raise ApiInternalError(response.text)
            else:
                return response.text

    def _postVoidOutput(self, input1):
            self.query_parameters['output'] = 'void'
            responseJson = self.client.postJsonHelper(self.url, input1, **self.query_parameters)
            if 'error' in responseJson:
                raise ApiError(responseJson['error']['message'])
            else:
                return AsyncResponse(responseJson)
<|endoftext|>


<commit_before># Copyright (c) 2003-2005 Maxim Sobolev. All rights reserved.
# Copyright (c) 2006-2007 Sippy Software, Inc. All rights reserved.
#
# This file is part of SIPPY, a free RFC3261 SIP stack and B2BUA.
#
# SIPPY is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# For a license to use the SIPPY software under conditions
# other than those described here, or to purchase support for this
# software, please contact Sippy Software, Inc. by e-mail at the
# following addresses: sales@sippysoft.com.
#
# SIPPY is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
#
# $Id: Radius_client.py,v 1.8 2009/11/19 02:09:30 sobomax Exp $

from External_command import External_command

class Radius_client(External_command):
    global_config = None
    _avpair_names = ('call-id', 'h323-session-protocol', 'h323-ivr-out', 'h323-incoming-conf-id', \
      'release-source')
    _cisco_vsa_names = ('h323-remote-address', 'h323-conf-id', 'h323-setup-time', 'h323-call-origin', \
      'h323-call-type', 'h323-connect-time', 'h323-disconnect-time', 'h323-disconnect-cause', \
      'h323-voice-quality', 'h323-credit-time', 'h323-return-code', 'h323-redirect-number', \
      'h323-preferred-lang', 'h323-billing-model', 'h323-currency')

    def __init__(self, global_config = {}):
        self.global_config = global_config
        command = global_config.getdefault('radiusclient', '/usr/local/sbin/radiusclient')
        config = global_config.getdefault('radiusclient.conf', None)
        max_workers = global_config.getdefault('max_radiusclients', 20)
        if config != None:
            External_command.__init__(self, (command, '-f', config, '-s'), max_workers = max_workers)
        else:
            External_command.__init__(self, (command, '-s'), max_workers = max_workers)

    def _prepare_attributes(self, type, attributes):
        data = [type]
        for a, v in attributes:
            if a in self._avpair_names:
                v = '%s=%s' % (str(a), str(v))
                a = 'Cisco-AVPair'
            elif a in self._cisco_vsa_names:
                v = '%s=%s' % (str(a), str(v))
            data.append('%s="%s"' % (str(a), str(v)))
        return data

    def do_auth(self, attributes, result_callback, *callback_parameters):
        return External_command.process_command(self, self._prepare_attributes('AUTH', attributes), result_callback, *callback_parameters)

    def do_acct(self, attributes, result_callback = None, *callback_parameters):
        External_command.process_command(self, self._prepare_attributes('ACCT', attributes), result_callback, *callback_parameters)

    def process_result(self, result_callback, result, *callback_parameters):
        if result_callback == None:
            return
        nav = []
        for av in result[:-1]:
            a, v = [x.strip() for x in av.split(' = ', 1)]
            v = v.strip('\'')
            if (a == 'Cisco-AVPair' or a in self._cisco_vsa_names):
                t = v.split('=', 1)
                if len(t) > 1:
                    a, v = t
            elif v.startswith(a + '='):
                v = v[len(a) + 1:]
            nav.append((a, v))
        External_command.process_result(self, result_callback, (tuple(nav), int(result[-1])), *callback_parameters)
<commit_msg>Add alert-timepoint and provisional-timepoint into list of VSAs.<commit_after># Copyright (c) 2003-2005 Maxim Sobolev. All rights reserved.
# Copyright (c) 2006-2007 Sippy Software, Inc. All rights reserved.
#
# This file is part of SIPPY, a free RFC3261 SIP stack and B2BUA.
#
# SIPPY is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# For a license to use the SIPPY software under conditions
# other than those described here, or to purchase support for this
# software, please contact Sippy Software, Inc. by e-mail at the
# following addresses: sales@sippysoft.com.
#
# SIPPY is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
#
# $Id: Radius_client.py,v 1.9 2009/11/20 07:26:12 sobomax Exp $

from External_command import External_command

class Radius_client(External_command):
    global_config = None
    _avpair_names = ('call-id', 'h323-session-protocol', 'h323-ivr-out', 'h323-incoming-conf-id', \
      'release-source', 'alert-timepoint', 'provisional-timepoint')
    _cisco_vsa_names = ('h323-remote-address', 'h323-conf-id', 'h323-setup-time', 'h323-call-origin', \
      'h323-call-type', 'h323-connect-time', 'h323-disconnect-time', 'h323-disconnect-cause', \
      'h323-voice-quality', 'h323-credit-time', 'h323-return-code', 'h323-redirect-number', \
      'h323-preferred-lang', 'h323-billing-model', 'h323-currency')

    def __init__(self, global_config = {}):
        self.global_config = global_config
        command = global_config.getdefault('radiusclient', '/usr/local/sbin/radiusclient')
        config = global_config.getdefault('radiusclient.conf', None)
        max_workers = global_config.getdefault('max_radiusclients', 20)
        if config != None:
            External_command.__init__(self, (command, '-f', config, '-s'), max_workers = max_workers)
        else:
            External_command.__init__(self, (command, '-s'), max_workers = max_workers)

    def _prepare_attributes(self, type, attributes):
        data = [type]
        for a, v in attributes:
            if a in self._avpair_names:
                v = '%s=%s' % (str(a), str(v))
                a = 'Cisco-AVPair'
            elif a in self._cisco_vsa_names:
                v = '%s=%s' % (str(a), str(v))
            data.append('%s="%s"' % (str(a), str(v)))
        return data

    def do_auth(self, attributes, result_callback, *callback_parameters):
        return External_command.process_command(self, self._prepare_attributes('AUTH', attributes), result_callback, *callback_parameters)

    def do_acct(self, attributes, result_callback = None, *callback_parameters):
        External_command.process_command(self, self._prepare_attributes('ACCT', attributes), result_callback, *callback_parameters)

    def process_result(self, result_callback, result, *callback_parameters):
        if result_callback == None:
            return
        nav = []
        for av in result[:-1]:
            a, v = [x.strip() for x in av.split(' = ', 1)]
            v = v.strip('\'')
            if (a == 'Cisco-AVPair' or a in self._cisco_vsa_names):
                t = v.split('=', 1)
                if len(t) > 1:
                    a, v = t
            elif v.startswith(a + '='):
                v = v[len(a) + 1:]
            nav.append((a, v))
        External_command.process_result(self, result_callback, (tuple(nav), int(result[-1])), *callback_parameters)
<|endoftext|>


<commit_before>import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
import numpy as np

class RadioNRF:

    GPIO.setmode(GPIO.BCM)

    def __init__(self, timeOut=5, trys=20):
        self.pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1],
        [0xE8, 0xE8, 0xF0, 0xF0, 0xE2]]
        self.radio = NRF24(GPIO, spidev.SpiDev())
        self.radio.begin(0, 25)
        self.radio.setPayloadSize(32)

        self.radio.setChannel(0x76)
        self.radio.setDataRate(NRF24.BR_1MBPS)
        self.radio.setPALevel(NRF24.PA_MIN)
        self.radio.setAutoAck(True)
        self.radio.enableDynamicPayloads()
        self.radio.enableAckPayload()
        self.radio.openReadingPipe(1, self.pipes[1])
        self.radio.openWritingPipe(self.pipes[0])
        self.radio.printDetails()
        self.radio.stopListening()
        self.timeOut = timeOut
        self.trys = trys

    def ForcePingPong(self):
        while True:
            receivedMessage = []
            self.radio.stopListening()
            print "Ping"
            self.radio.write("Ping")
            self.radio.startListening()
            time.sleep(1)
            if(self.radio.available(0)):
                self.radio.read(receivedMessage, 4)
                print "Rec: ", receivedMessage

    def createPackage(self, ID, quant, temp, sound, vel):
        package = ''
        package = package + hex(ID).replace("0x", "")
        package += hex(quant).replace("0x", "")
        package += hex(temp).replace("0x", "")
        package += hex(sound).replace("0x", "")
        package += hex(vel).replace("0x", "")
        self.radio.stopListening()
        self.radio.write(package)
        print package
        return package

    def SendTo(self, verifyByte, msg):
        for i in range(0, self.trys):  # Numero Maximo de tentativas de envio
            reciveAck = []             # Rxbuffer para o Ack
            self.radio.stopListening()
            send = '*' + verifyByte + msg
            # Byte de verificacao Fixo, e variavel
            self.radio.write(send)
            time.sleep(0.1)
            self.radio.startListening()
            for i in range(0, self.timeOut):
                if self.radio.available(0):
                    self.radio.read(reciveAck, 1)
                    print "Ack Recive: ", reciveAck[0], unichr(reciveAck[0])
                    if unichr(reciveAck[0]) == verifyByte:
                        return True
                    else:
                        print "Error: ", verifyByte, reciveAck[0]
                time.sleep(0.5)
        print "Error: retorno sem sucesso"
        return False

    def SendToKL43Z(self, ID, msg):
        self.radio.stopListening()
        for i in range(0, self.trys):
            print "Seend the ID", ID
            self.radio.write(ID)
            time.sleep(0.5)
            if self.getArk(ID) is True:
                self.radio.stopListening()
                self.radio.write(msg)
                if self.getArk(ID):
                    return True
            time.sleep(1 / 2)
        return False

    def getArk(self, ID):
        receivedMessage = []
        for i in range(0, self.timeOut):
            self.radio.startListening()
            if(self.radio.available(0)):
                self.radio.read(receivedMessage, 1)
                self.radio.stopListening()
                if(receivedMessage == ID):
                    return True
                print "ID Error !!!", receivedMessage
            time.sleep(0.50)
        print "Time OUT!"
        return False

    def ForceSendID(self, ID):
        receivedMessage = []
        while True:
            print "Send: ", ID
            self.radio.write(ID)
            time.sleep(1)
            self.radio.startListening()
            if(self.radio.available(0)):
                self.radio.read(receivedMessage, 1)
                print "ID: ", ID, "Recive: ", receivedMessage
            self.radio.stopListening()

    def ForceLiten(self):
        receivedMessage = []
        self.radio.startListening()
        while(True):
            if(self.radio.available(0)):
                self.radio.read(receivedMessage, 1)
                print "Recive: ", receivedMessage
            time.sleep(0.5)

    def exit(self):
        self.radio.end()
        GPIO.cleanup()
        print ("lean GPIO")  # Clean the GPIO

if __name__ == "__main__":
    radio = RadioNRF()
    radio.createPackage(124, 20, 25, 2, 8)

#
#    try:
#        while(radio.SendTo("B", "CRISMEAJUDA1")):
#            pass
#        #radio.ForceSendID("3")
#    except KeyboardInterrupt:
#        radio.exit()
#    radio.exit()<commit_msg>Adding verify byte, and clean the code<commit_after>import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

class RadioNRF:

    GPIO.setmode(GPIO.BCM)

    def __init__(self, timeOut=5, trys=20):
        self.pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1],
        [0xE8, 0xE8, 0xF0, 0xF0, 0xE2]]
        self.radio = NRF24(GPIO, spidev.SpiDev())
        self.radio.begin(0, 25)
        self.radio.setPayloadSize(32)

        self.radio.setChannel(0x76)
        self.radio.setDataRate(NRF24.BR_1MBPS)
        self.radio.setPALevel(NRF24.PA_MIN)
        self.radio.setAutoAck(True)
        self.radio.enableDynamicPayloads()
        self.radio.enableAckPayload()
        self.radio.openReadingPipe(1, self.pipes[1])
        self.radio.openWritingPipe(self.pipes[0])
        self.radio.printDetails()
        self.radio.stopListening()
        self.timeOut = timeOut
        self.trys = trys

    def ForcePingPong(self):
        while True:
            receivedMessage = []
            self.radio.stopListening()
            print "Ping"
            self.radio.write("Ping")
            self.radio.startListening()
            time.sleep(1)
            if(self.radio.available(0)):
                self.radio.read(receivedMessage, 4)
                print "Rec: ", receivedMessage

    def toHex(self, num):
        var = hex(num).replace("0x", "")
        if len(var) is 1:
            var = '00' + var
        if len(var) is 2:
            var = '0' + var
        if len(var) > 3:
            print "Size bigger than 2 bytes, var set to zero"
            var = '000'
        print var
        return var

    def createPackage(self, ID, quant, temp, sound, vel):
        print "Int value:", ID, quant, temp, sound, vel
        package = ''
        package += self.toHex(ID)
        package += self.toHex(quant)
        package += self.toHex(temp)
        package += self.toHex(sound)
        package += self.toHex(vel)
        self.radio.stopListening()
        return package

    def sendInt(self, num):
        package = []
        package.append(num)
        #package.append(num)
        print "pack: ", package
        alfa = self.radio.write(package)
        print alfa

    def sendTo(self, verifyByte, msg):
        send = '*' + verifyByte + msg
        print send, len(send)

        for i in range(0, self.trys):  # Numero Maximo de tentativas de envio
            reciveAck = []             # Rxbuffer para o Ack
            self.radio.stopListening()
            # Byte de verificacao Fixo, e variavel
            time.sleep(0.5)
            self.radio.write(send)
            self.radio.startListening()
            for i in range(0, self.timeOut):
                if self.radio.available(0):
                    self.radio.read(reciveAck, 1)
                    print "Ack Recive: ", reciveAck[0], unichr(reciveAck[0])
                    if unichr(reciveAck[0]) == verifyByte:
                        return True
                    else:
                        print "Error: ", verifyByte, reciveAck[0]
                time.sleep(0.2)
        print "Error: retorno sem sucesso"
        return False

    def exit(self):
        self.radio.end()
        GPIO.cleanup()
        print ("lean GPIO")  # Clean the GPIO

if __name__ == "__main__":
    radio = RadioNRF()
    msg = radio.createPackage(220, 111, 125, 112, 255)
    radio.sendTo('B', msg)
    radio.exit()

#
#    try:
#        while(radio.SendTo("B", "CRISMEAJUDA1")):
#            pass
#        #radio.ForceSendID("3")
#    except KeyboardInterrupt:
#        radio.exit()
#    radio.exit()<|endoftext|>


<commit_before># -*- coding: UTF-8 -*-
from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.view import view_config
from pyramid.response import Response
from c2cgeoportal_commons.models import DBSessions, DBSession
import logging
import owncloud
import shutil
import os
import smtplib
import urllib.request
from email.mime.text import MIMEText
import time
import datetime
import sys

_ = TranslationStringFactory("geoportailv3_geoportal-server")
log = logging.getLogger(__name__)

class Casipo(object):

    def __init__(self, request):
        self.request = request
        self.config = self.request.registry.settings
        self.localizer = get_localizer(self.request)

    def __download(self, num):
        if self.staging:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["staging_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        else:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["prod_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        db_ecadastre = DBSessions['ecadastre']
        cnt = 0
        try:
            sql = "select nextval_daily ('casipo_seq')"
            results = DBSession.execute(sql)
            for res in results:
                cnt = res[0]
        except Exception as e:
            log.exception(e)
        try:
            f = urllib.request.urlopen(url, None, 1800)
            data = f
            # YYYYMMJJ_Commune_Extrait_CASIPO_nn.pdf
            commune = ""
            sql = "select replace(commune_administrative , '/', '_') as commune_administrative FROM DIFFDATA.communes_adm_cad_sections WHERE code_commune = " + str(int(num[0:3])) + " GROUP BY commune_administrative"
            results = db_ecadastre.execute(sql)
            for res in results:
                commune = res['commune_administrative']

            self.filename = '/tmp/%s_%s_Extrait_CASIPO_%s.pdf' % (str(datetime.datetime.now().strftime("%Y%m%d")), commune, str(cnt))
            with open(self.filename, 'wb') as fp:
                shutil.copyfileobj(data, fp)
        except Exception as e:
            log.exception(e)
            data = None
            log.debug(url)
        return

    def __upload2owncloud(self):
        oc = owncloud.Client(self.config["casipo"]["owncloud_internal_url"])
        oc.login(self.config["casipo"]["owncloud_user"],
                 self.config["casipo"]["owncloud_password"])
        oc.put_file(os.path.basename(self.filename), self.filename)
        link_info = oc.share_file_with_link(os.path.basename(self.filename))
        self.link = link_info.get_link().replace(
            self.config["casipo"]["owncloud_internal_url"],
            self.config["casipo"]["owncloud_external_url"])
        self.link += "/download"
        os.remove(self.filename)
        return

    def __send_mail(self, email):
        if self.link == 'error':
            mailtext = _("CASIPO Error during report generation")
        else:
            mailtext = _("CASIPO Mail the report link ${link}",
                         mapping={'link': self.link})
        msg = MIMEText(self.localizer.translate(mailtext), 'html', 'utf-8')
        me = 'support@geoportail.lu'
        you = email
        mails = [you]
        if "bcc_address" in self.config["casipo"]:
            bcc = self.config["casipo"]["bcc_address"]
            msg['BCC'] = bcc
            mails.append(bcc)
        msg['Subject'] = 'Rapport CASIPO'
        msg['From'] = me
        msg['To'] = you

        s = smtplib.SMTP(self.config["casipo"]["smtp_server"])
        s.sendmail(me, mails, msg.as_string())
        s.quit()
        return

    def __log_download_stats(self, objectids, download_link):
        pass

    @view_config(route_name='casipo_report')
    def casipo_report(self):
        oid = self.request.matchdict.get('oid', None)
        email = self.request.params.get('email', None)
        self.staging =\
            self.request.params.get('staging', 'False').lower() == 'true'
        resp = _("CASIPO webservice response ${email}",
                 mapping={'email': email.encode('utf-8')})
        try:
            self.__download(oid)
            self.__upload2owncloud()
        except Exception as e:
            log.exception(e)
            self.link = 'error'
        self.__log_download_stats(oid, self.link)
        self.__send_mail(email)
        headers = {"Content-Type": 'text/html'}
        return Response(self.localizer.translate(resp), headers=headers)
<commit_msg>Owncloud https instead of http<commit_after># -*- coding: UTF-8 -*-
from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.view import view_config
from pyramid.response import Response
from c2cgeoportal_commons.models import DBSessions, DBSession
import logging
import owncloud
import shutil
import os
import smtplib
import urllib.request
from email.mime.text import MIMEText
import time
import datetime
import sys

_ = TranslationStringFactory("geoportailv3_geoportal-server")
log = logging.getLogger(__name__)

class Casipo(object):

    def __init__(self, request):
        self.request = request
        self.config = self.request.registry.settings
        self.localizer = get_localizer(self.request)

    def __download(self, num):
        if self.staging:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["staging_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        else:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["prod_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        db_ecadastre = DBSessions['ecadastre']
        cnt = 0
        try:
            sql = "select nextval_daily ('casipo_seq')"
            results = DBSession.execute(sql)
            for res in results:
                cnt = res[0]
        except Exception as e:
            log.exception(e)
        try:
            f = urllib.request.urlopen(url, None, 1800)
            data = f
            # YYYYMMJJ_Commune_Extrait_CASIPO_nn.pdf
            commune = ""
            sql = "select replace(commune_administrative , '/', '_') as commune_administrative FROM DIFFDATA.communes_adm_cad_sections WHERE code_commune = " + str(int(num[0:3])) + " GROUP BY commune_administrative"
            results = db_ecadastre.execute(sql)
            for res in results:
                commune = res['commune_administrative']

            self.filename = '/tmp/%s_%s_Extrait_CASIPO_%s.pdf' % (str(datetime.datetime.now().strftime("%Y%m%d")), commune, str(cnt))
            with open(self.filename, 'wb') as fp:
                shutil.copyfileobj(data, fp)
        except Exception as e:
            log.exception(e)
            data = None
            log.debug(url)
        return

    def __upload2owncloud(self):
        oc = owncloud.Client(self.config["casipo"]["owncloud_internal_url"])
        oc.login(self.config["casipo"]["owncloud_user"],
                 self.config["casipo"]["owncloud_password"])
        oc.put_file(os.path.basename(self.filename), self.filename)
        link_info = oc.share_file_with_link(os.path.basename(self.filename))
        self.link = link_info.get_link().replace(
            self.config["casipo"]["owncloud_internal_url"].replace('http://', 'https://'),
            self.config["casipo"]["owncloud_external_url"])
        self.link += "/download"
        os.remove(self.filename)
        return

    def __send_mail(self, email):
        if self.link == 'error':
            mailtext = _("CASIPO Error during report generation")
        else:
            mailtext = _("CASIPO Mail the report link ${link}",
                         mapping={'link': self.link})
        msg = MIMEText(self.localizer.translate(mailtext), 'html', 'utf-8')
        me = 'support@geoportail.lu'
        you = email
        mails = [you]
        if "bcc_address" in self.config["casipo"]:
            bcc = self.config["casipo"]["bcc_address"]
            msg['BCC'] = bcc
            mails.append(bcc)
        msg['Subject'] = 'Rapport CASIPO'
        msg['From'] = me
        msg['To'] = you

        s = smtplib.SMTP(self.config["casipo"]["smtp_server"])
        s.sendmail(me, mails, msg.as_string())
        s.quit()
        return

    def __log_download_stats(self, objectids, download_link):
        pass

    @view_config(route_name='casipo_report')
    def casipo_report(self):
        oid = self.request.matchdict.get('oid', None)
        email = self.request.params.get('email', None)
        self.staging =\
            self.request.params.get('staging', 'False').lower() == 'true'
        resp = _("CASIPO webservice response ${email}",
                 mapping={'email': email.encode('utf-8')})
        try:
            self.__download(oid)
            self.__upload2owncloud()
        except Exception as e:
            log.exception(e)
            self.link = 'error'
        self.__log_download_stats(oid, self.link)
        self.__send_mail(email)
        headers = {"Content-Type": 'text/html'}
        return Response(self.localizer.translate(resp), headers=headers)
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# sodtube1d.py
#
# Description:
#     1D Sod Tube Test
#
#     This program is implemented by OO style to be
#     a part of ipython notebook demo materials.
#
#     The derivation of the equations for the analytic solution
#     is based on the book,
#     Principles of Computational Fluid Dynamics,
#     written by Pieter Wesseling.
#     Or, someone could refer to the solvcon website
#     http://www.solvcon.net/en/latest/cese.html#sod-s-shock-tube-problem
#
#
# DEBUG: search string 'DEBUG'
# why: somewhere don't understand very much ...

import sys
import scipy.optimize as so
import matplotlib.pyplot as plt

# a number to claim two floating number value are equal.
delta_precision = 0.0000000000001

class DataManager():
    """
    Manage how to get extended information by input data.
    """
    def __init__(self):
        pass

    def get_errorNorm(self, solution_a, solution_b):
        return solution_errornorm

    def get_l2_norm(self, solution_a, solution_b, vi=1, filter=[]):
        """
        vi(vector_i)
            1:rho
            2:v
            3:p
        filter = [(x1,x2),(x3,x4),...], x1 < x2, x3 < x4
            when caculating l2 norm,
            data between x1 and x2, and x3 and x4,
            will be ignored.
            data on x1, x2, x3 and x4 will be adopted.
        """
        solution_deviation_square = self.get_deviation_square(solution_a, solution_b)
        l2_norm = 0
        sds = [] # deviation square of the solution

        # remove the deviation square value in the specific intervals
        for solution_dev_sqr_vector in solution_deviation_square:
            if len(filter) > 0:
                sdsv = solution_dev_sqr_vector[vi]
                for interval in filter:
                    if interval[0] < solution_dev_sqr_vector[0] < interval[1]:
                        sdsv = 0.
                sds.append(sdsv)
            else:
                sds.append(solution_dev_sqr_vector[vi])
        return sum(sds)

    def get_deviation(self, solution_a, solution_b):
        """
        Note:
            only the mesh grid points of solution_a
            will be adopted.
        """
        solution_deviation = []
        if len(solution_a) != len(solution_b):
            print("two solutions have different mesh point numbers!")
            solution_c = []
            for i in solution_a:
                for j in solution_b:
                    if abs(i[0] - j[0]) < delta_precision:
                        solution_c.append(j)
            solution_b = solution_c

        for i in range(len(solution_a)):
            if abs(solution_a[i][0] - solution_b[i][0]) < delta_precision:
                # 0.000000001 is a bad way
                # the mesh points are not the same
                # because they are not generated by the same
                # mesh generator,
                # and the float number will differ in the very small
                # order.
                x = solution_a[i][0]
                drho_abs = abs(solution_a[i][1] - solution_b[i][1])
                dv_abs = abs(solution_a[i][2] - solution_b[i][2])
                dp_abs = abs(solution_a[i][3] - solution_b[i][3])
                solution_deviation.append((x, drho_abs, dv_abs, dp_abs))
            else:
                print("two solutions have different mesh point!!")

        if len(solution_deviation) == len(solution_a):
            return solution_deviation
        else:
            print("sth. wrong when getting deviation!!")

    def get_deviation_percent(self, solution_a, solution_b):
        solution_deviation = self.get_deviation(solution_a, solution_b)
        solution_deviation_precent = []
        for i in range(len(solution_deviation)):
            solution_deviation_precent.append((solution_a[i][0],
                solution_deviation[i][1]/(solution_a[i][1]+1e-20),
                solution_deviation[i][2]/(solution_a[i][2]+1e-20),
                solution_deviation[i][3]/(solution_a[i][3]+1e-20)))
        return solution_deviation_precent

    def get_deviation_square(self, solution_a, solution_b):
        solution_deviation_square = []
        solution_deviation = self.get_deviation(solution_a, solution_b)
        for i in range(len(solution_deviation)):
            solution_deviation_square.append((
                solution_deviation[i][0],
                solution_deviation[i][1]*solution_deviation[i][1],
                solution_deviation[i][2]*solution_deviation[i][2],
                solution_deviation[i][3]*solution_deviation[i][3]))
        return solution_deviation_square

    def dump_solution(self, solution):
        print'x rho v p'
        for i in solution:
            print'%f %f %f %f' % (i[0], i[1], i[2], i[3])

<commit_msg>stop to use delta_precision as a global var, more comments<commit_after>#!/usr/bin/python
#
# sodtube1d.py
#
# Description:
#     1D Sod Tube Test
#
#     This program is implemented by OO style to be
#     a part of ipython notebook demo materials.
#
#     The derivation of the equations for the analytic solution
#     is based on the book,
#     Principles of Computational Fluid Dynamics,
#     written by Pieter Wesseling.
#     Or, someone could refer to the solvcon website
#     http://www.solvcon.net/en/latest/cese.html#sod-s-shock-tube-problem
#
#
# DEBUG: search string 'DEBUG'
# why: somewhere don't understand very much ...

import sys
import scipy.optimize as so
import matplotlib.pyplot as plt

class DataManager():
    """
    Manage how to get extended information by input data.
    """
    def __init__(self):
        pass

    def get_errorNorm(self, solution_a, solution_b):
        return solution_errornorm

    def get_l2_norm(self, solution_a, solution_b, vi=1, filter=[]):
        """
        vi(vector_i)
            1:rho
            2:v
            3:p
        filter = [(x1,x2),(x3,x4),...], x1 < x2, x3 < x4
            when caculating l2 norm,
            data between x1 and x2, and x3 and x4,
            will be ignored.
            data on x1, x2, x3 and x4 will be adopted.
        """
        solution_deviation_square = self.get_deviation_square(solution_a, solution_b)
        l2_norm = 0
        sds = [] # deviation square of the solution

        # remove the deviation square value in the specific intervals
        for solution_dev_sqr_vector in solution_deviation_square:
            if len(filter) > 0:
                sdsv = solution_dev_sqr_vector[vi]
                for interval in filter:
                    if interval[0] < solution_dev_sqr_vector[0] < interval[1]:
                        sdsv = 0.
                sds.append(sdsv)
            else:
                sds.append(solution_dev_sqr_vector[vi])
        return sum(sds)

    def get_deviation(self,
                      solution_a,
                      solution_b,
                      delta_precision=0.0000000000001):
        """
        delta_precision: float,
            a number to claim two floating number value are equal.

        Compare the value of the status on each mesh points of solution_a.

        If the deviation of the mesh point coordinate value
        is smaller than delta_precision, they will be regarded as
        the same location.

        Note:
            only the mesh grid points of solution_a
            will be adopted.
        """

        solution_deviation = []
        if len(solution_a) != len(solution_b):
            print("two solutions have different mesh point numbers!")
            solution_c = []
            for i in solution_a:
                for j in solution_b:
                    if abs(i[0] - j[0]) < delta_precision:
                        solution_c.append(j)
            solution_b = solution_c

        for i in range(len(solution_a)):
            if abs(solution_a[i][0] - solution_b[i][0]) < delta_precision:
                # 0.000000001 is a bad way
                # the mesh points are not the same
                # because they are not generated by the same
                # mesh generator,
                # and the float number will differ in the very small
                # order.
                x = solution_a[i][0]
                drho_abs = abs(solution_a[i][1] - solution_b[i][1])
                dv_abs = abs(solution_a[i][2] - solution_b[i][2])
                dp_abs = abs(solution_a[i][3] - solution_b[i][3])
                solution_deviation.append((x, drho_abs, dv_abs, dp_abs))
            else:
                print("two solutions have different mesh point!!")

        if len(solution_deviation) == len(solution_a):
            return solution_deviation
        else:
            print("sth. wrong when getting deviation!!")

    def get_deviation_percent(self, solution_a, solution_b):
        solution_deviation = self.get_deviation(solution_a, solution_b)
        solution_deviation_precent = []
        for i in range(len(solution_deviation)):
            solution_deviation_precent.append((solution_a[i][0],
                solution_deviation[i][1]/(solution_a[i][1]+1e-20),
                solution_deviation[i][2]/(solution_a[i][2]+1e-20),
                solution_deviation[i][3]/(solution_a[i][3]+1e-20)))
        return solution_deviation_precent

    def get_deviation_square(self, solution_a, solution_b):
        solution_deviation_square = []
        solution_deviation = self.get_deviation(solution_a, solution_b)
        for i in range(len(solution_deviation)):
            solution_deviation_square.append((
                solution_deviation[i][0],
                solution_deviation[i][1]*solution_deviation[i][1],
                solution_deviation[i][2]*solution_deviation[i][2],
                solution_deviation[i][3]*solution_deviation[i][3]))
        return solution_deviation_square

    def dump_solution(self, solution):
        print'x rho v p'
        for i in solution:
            print'%f %f %f %f' % (i[0], i[1], i[2], i[3])

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# This work was created by participants in the DataONE project, and is
# jointly copyrighted by participating institutions in DataONE. For
# more information on DataONE, see our web site at http://dataone.org.
#
#   Copyright 2009-2016 DataONE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Request handler middleware

This handler runs first in the middleware chain. If DEBUG_ECHO_REQUEST is True,
it will short circuit all other processing in GMN and return an echo of the
request. Also see the description in settings.py.
"""

import logging
import pprint

import d1_common
import d1_common.const
import d1_common.types.exceptions

import django.conf
import django.http

class RequestHandler:
  def __init__(self, next_in_chain_func):
    self.next_in_chain_func = next_in_chain_func

  def __call__(self, request):
    if django.conf.settings.DEBUG_GMN and django.conf.settings.DEBUG_ECHO_REQUEST:
      return self.create_http_echo_response(request)
    return self.next_in_chain_func(request)

  def create_http_echo_response(self, request):
    logging.warning(
      'Echoing request (django.conf.settings.DEBUG_ECHO_REQUEST=True)'
    )
    pp = pprint.PrettyPrinter(indent=2)
    return django.http.HttpResponse(
      pp.pformat(request.read()), d1_common.const.CONTENT_TYPE_TEXT
    )
<commit_msg>Add vendor extension for triggering echo in all APIs in GMN<commit_after># -*- coding: utf-8 -*-

# This work was created by participants in the DataONE project, and is
# jointly copyrighted by participating institutions in DataONE. For
# more information on DataONE, see our web site at http://dataone.org.
#
#   Copyright 2009-2016 DataONE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Request handler middleware

This handler runs first and last in the middleware "onion".

If settings.DEBUG_ECHO_REQUEST is True, it will short circuit all other
processing in GMN and return an echo of the request. Also see the description in
settings.py.

CORS headers are added here as a final step before passing the response to
Django, which sends it to the client. By adding the headers as a final step,
CORS headers are also added to responses created by the other middleware layers,
such as the exception handler.
"""

import d1_gmn.app.util
import d1_gmn.app.views.headers

import django.conf
import django.http

class RequestHandler:
  def __init__(self, next_in_chain_func):
    self.next_in_chain_func = next_in_chain_func

  def __call__(self, request):
    if django.conf.settings.DEBUG_GMN:
      if django.conf.settings.DEBUG_ECHO_REQUEST or 'HTTP_VENDOR_GMN_ECHO_REQUEST' in request.META:
        return d1_gmn.app.util.create_http_echo_response(request)

    response = self.next_in_chain_func(request)

    if hasattr(request, 'allowed_method_list'):
      d1_gmn.app.views.headers.add_cors_headers_to_response(
        response, request.allowed_method_list
      )

    return response
<|endoftext|>


<commit_before>

class Reference(object):
    def __init__(self, Mass, Ref_Concentration, Ref_Concentration_Error,
                 Conversion):
        self.Mass = Mass
        self.Ref_Concentration = Ref_Concentration
        self.Ref_Concentration_Error = Ref_Concentration_Error
        self.Conversion = Conversion

Dirt_Mass = 2.380
Dirt_K_40 = 2.57
Dirt_Bi_214 = 1.97
Dirt_Pb_214 = 1.97
Dirt_Th_234 = 2.26
Dirt_Tl_208 = 5.08
Dirt_Ac_228 = 5.43
Dirt_Pb_212 = 5.08

Dirt_K_40_Unc = 0.01
Dirt_Bi_214_Unc = 0.02
Dirt_Pb_214_Unc = 0.02
Dirt_Th_234_Unc = 0.07
Dirt_Tl_208_Unc = 0.05
Dirt_Ac_228_Unc = 0.07
Dirt_Pb_212_Unc = 0.05
Dirt_Concentrations = [Dirt_K_40, Dirt_Bi_214, Dirt_Pb_214, Dirt_Th_234,
                       Dirt_Tl_208, Dirt_Ac_228, Dirt_Pb_212]
Dirt_Concentrations_Uncertainty = [Dirt_K_40_Unc, Dirt_Bi_214_Unc,
                                   Dirt_Pb_214_Unc, Dirt_Th_234_Unc,
                                   Dirt_Tl_208_Unc, Dirt_Ac_228_Unc,
                                   Dirt_Pb_212_Unc]
Dirt_Conversions = [309.6, 12.3, 4.07]
Soil_Reference = Reference(Dirt_Mass, Dirt_Concentrations,
                           Dirt_Concentrations_Uncertainty, Dirt_Conversions)
<commit_msg>Reformatted class input variables<commit_after>

class Reference(object):
    """
    Generates a reference object that contains a reference mass and
    concentrations. Mass is in kg and concentrations are in percent for
    K-40 and ppm for the rest. Conversions are currently unknown in their
    origin and derivation.
    """
    def __init__(self, mass, ref_concentration, ref_concentration_error,
                 conversion):
        self.Mass = mass
        self.Ref_Concentration = ref_concentration
        self.Ref_Concentration_Error = ref_concentration_error
        self.Conversion = conversion

Dirt_Mass = 2.380
Dirt_K_40 = 2.57
Dirt_Bi_214 = 1.97
Dirt_Pb_214 = 1.97
Dirt_Th_234 = 2.26
Dirt_Tl_208 = 5.08
Dirt_Ac_228 = 5.43
Dirt_Pb_212 = 5.08

Dirt_K_40_Unc = 0.01
Dirt_Bi_214_Unc = 0.02
Dirt_Pb_214_Unc = 0.02
Dirt_Th_234_Unc = 0.07
Dirt_Tl_208_Unc = 0.05
Dirt_Ac_228_Unc = 0.07
Dirt_Pb_212_Unc = 0.05
Dirt_Concentrations = [Dirt_K_40, Dirt_Bi_214, Dirt_Pb_214, Dirt_Th_234,
                       Dirt_Tl_208, Dirt_Ac_228, Dirt_Pb_212]
Dirt_Concentrations_Uncertainty = [Dirt_K_40_Unc, Dirt_Bi_214_Unc,
                                   Dirt_Pb_214_Unc, Dirt_Th_234_Unc,
                                   Dirt_Tl_208_Unc, Dirt_Ac_228_Unc,
                                   Dirt_Pb_212_Unc]
Dirt_Conversions = [309.6, 12.3, 4.07]
Soil_Reference = Reference(Dirt_Mass, Dirt_Concentrations,
                           Dirt_Concentrations_Uncertainty, Dirt_Conversions)
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Created on Thu Jul 17 12:46:46 2014

@author: Bodangles
"""

import os
import time
import posixpath
import numpy as np
import scipy as sp
import tables

VARNAMES = ['data','datanames','coordnames','sensorloc']
class GeoData(object):
    # Variables
    # data - This is a dictionary with strings for keys only. The strings are the names of the
    def __init__(self,readmethod,inputs):
        (self.data,self.datanames,self.coordnames,self.dataloc,self.sensorloc,self.times) = readmethod(inputs)

    def write_h5(self,filename):
        h5file = tables.openFile(filename, mode = "w", title = "GeoData Out")
        # get the names of all the variables set in the init function
        varnames = self.__dict__.keys()
        vardict = self.__dict__

        # XXX only allow 1 level of dictionaries, do not allow for dictionary of dictionaries.
        # Make group for each dictionary
        for cvar in varnames:
            group = h5file.create_group(posixpath.sep, cvar)
            if type(vardict[cvar]) ==dict: # Check if dictionary
                dictkeys = vardict[cvar].keys()
                for ikeys in dictkeys:
                    group2 = h5file.create_group(os.path.join(cvar,ikeys))
                    h5file.createArray(group2,ikeys,vardict[cvar][ikeys],'Static array')
            h5file.createArray(group,cvar,vardict[cvar],'Static array')
        h5file.close()

    # TODO might want to make this private method
def read_h5(filename):
    h5file=tables.openFile(filename)
    output={}
    for group in h5file.walkGroups(posixpath.sep):
        output[group._v_pathname]={}
        for array in h5file.listNodes(group, classname = 'Array'):
            output[group._v_pathname][array.name]=array.read()
    h5file.close()
    for

def pathparts(path):
    components = []
    while True:
        (path,tail) = posixpath.split(path)
        if tail == "":
            components.reverse()
            return components
        components.append(tail)<commit_msg>Added read method for custom h5 files<commit_after>#!/usr/bin/env python
"""
Created on Thu Jul 17 12:46:46 2014

@author: Bodangles
"""

import os
import time
import posixpath
import numpy as np
import scipy as sp
import tables

VARNAMES = ['data','datanames','coordnames','dataloc','sensorloc','times']
class GeoData(object):
    '''This class will hold the information for geophysical data.'''
    # Variables
    # data - This is a dictionary with strings for keys only. The strings are the names of the
    def __init__(self,readmethod,inputs):
        '''This will create an instance of the GeoData class by giving it a read method and the inputs in a tuple'''
        (self.data,self.datanames,self.coordnames,self.dataloc,self.sensorloc,self.times) = readmethod(inputs)

    def write_h5(self,filename):
        h5file = tables.openFile(filename, mode = "w", title = "GeoData Out")
        # get the names of all the variables set in the init function
        varnames = self.__dict__.keys()
        vardict = self.__dict__

        # XXX only allow 1 level of dictionaries, do not allow for dictionary of dictionaries.
        # Make group for each dictionary
        for cvar in varnames:
            group = h5file.create_group(posixpath.sep, cvar)
            if type(vardict[cvar]) ==dict: # Check if dictionary
                dictkeys = vardict[cvar].keys()
                for ikeys in dictkeys:
                    group2 = h5file.create_group(os.path.join(cvar,ikeys))
                    h5file.createArray(group2,ikeys,vardict[cvar][ikeys],'Static array')
            h5file.createArray(group,cvar,vardict[cvar],'Static array')
        h5file.close()

# TODO might want to make this private method
# currently just give this to the init function and it will create a class instance.
def read_h5(filename):
    h5file=tables.openFile(filename)
    output={}
    for group in h5file.walkGroups(posixpath.sep):
        output[group._v_pathname]={}
        for array in h5file.listNodes(group, classname = 'Array'):
            output[group._v_pathname][array.name]=array.read()
    h5file.close()
    outarr = [pathparts(ipath) for ipath in output.keys()]
    outlist = []
    for ivar in VARNAMES:
        firsttime = True
        dictout = False
        for npath,ipath in enumerate(outarr):
            if ivar==ipath[0]:
                if len(ipath)==1:
                    outlist.append(output[output.keys()[npath]])
                elif len(ipath)>1 and firsttime:
                    firsttime = False
                    dictout=True
                    curdict = {ipath[1]:output[output.keys()[npath]]}
                else:
                    curdict[ipath[1]] = output[output.keys()[npath]]
        if dictout:
            outlist.append(dictout)

    return tuple(outlist)
def readSRI_h5(filenames,timelims,):
    '''This will read the SRI formated h5 files for RISR and PFISR'''

def pathparts(path):
    components = []
    while True:
        (path,tail) = posixpath.split(path)
        if tail == "":
            components.reverse()
            return components
        components.append(tail)<|endoftext|>