Пример #1
0
    def __init__(self,
                 input=None,
                 filename=None,
                 mangaid=None,
                 plateifu=None,
                 mode=None,
                 data=None,
                 release=None,
                 drpall=None,
                 download=None,
                 ignore_db=False):
        self.data = data
        self.data_origin = None
        self._ignore_db = ignore_db

        self.filename = filename
        self.mangaid = mangaid
        self.plateifu = plateifu

        self.mode = mode if mode is not None else config.mode

        self._release = release if release is not None else config.release

        self._drpver, self._dapver = config.lookUpVersions(
            release=self._release)
        self._drpall = config._getDrpAllPath(
            self._drpver) if drpall is None else drpall

        self._forcedownload = download if download is not None else config.download

        self._determine_inputs(input)

        assert self.mode in ['auto', 'local', 'remote']
        assert self.filename is not None or self.plateifu is not None, 'no inputs set.'

        self.datamodel = None
        self._set_datamodel()

        if self.mode == 'local':
            self._doLocal()
        elif self.mode == 'remote':
            self._doRemote()
        elif self.mode == 'auto':
            try:
                self._doLocal()
            except Exception as ee:

                if self.filename:
                    # If the input contains a filename we don't want to go into remote mode.
                    raise (ee)
                else:
                    log.debug('local mode failed. Trying remote now.')
                    self._doRemote()

        # Sanity check to make sure data_origin has been properly set.
        assert self.data_origin in ['file', 'db',
                                    'api'], 'data_origin is not properly set.'
Пример #2
0
 def _setupDB(self):
     ''' Try to import the database '''
     try:
         from marvin.db.database import db
     except RuntimeError as e:
         log.debug('RuntimeError raised: Problem importing db: {0}'.format(e))
         self.db = None
     except ImportError as e:
         log.debug('ImportError raised: Problem importing db: {0}'.format(e))
         self.db = None
     else:
         self.db = db
Пример #3
0
    def _importModels(self):
        ''' Try to import the sql alchemy model classes '''

        try:
            import marvin.db.models.SampleModelClasses as sampledb
        except Exception as e:
            log.debug(
                'Exception raised: Problem importing mangadb SampleModelClasses: {0}'
                .format(e))
            self.sampledb = None
        else:
            self.sampledb = sampledb

        try:
            import marvin.db.models.DataModelClasses as datadb
        except Exception as e:
            log.debug(
                'Exception raised: Problem importing mangadb DataModelClasses: {0}'
                .format(e))
            self.datadb = None
        else:
            self.datadb = datadb

        try:
            import marvin.db.models.DapModelClasses as dapdb
        except Exception as e:
            log.debug(
                'Exception raised: Problem importing mangadb DapModelClasses: {0}'
                .format(e))
            self.dapdb = None
            self.spaxelpropdict = None
        else:
            self.dapdb = dapdb
            self.spaxelpropdict = self._setSpaxelPropDict()
Пример #4
0
def get_nsa_data(mangaid, source='nsa', mode='auto', drpver=None, drpall=None):
    """Returns a dictionary of NSA data from the DB or from the drpall file.

    Parameters:
        mangaid (str):
            The mangaid of the target for which the NSA information will be returned.
        source ({'nsa', 'drpall'}):
            The data source. If ``source='nsa'``, the full NSA catalogue from the DB will
            be used. If ``source='drpall'``, the subset of NSA columns included in the drpall
            file will be returned.
        mode ({'auto', 'local', 'remote'}):
            See :ref:`mode-decision-tree`.
        drpver (str or None):
            The version of the DRP to use, if ``source='drpall'``. If ``None``, uses the
            version set by ``marvin.config.release``.
        drpall (str or None):
            A path to the drpall file to use if ``source='drpall'``. If not defined, the
            default drpall file matching ``drpver`` will be used.

    Returns:
        nsa_data (dict):
            A dictionary containing the columns and values from the NSA catalogue for
            ``mangaid``.

    """

    from marvin import config, marvindb
    from marvin.core.core import DotableCaseInsensitive

    valid_modes = ['auto', 'local', 'remote']
    assert mode in valid_modes, 'mode must be one of {0}'.format(valid_modes)

    valid_sources = ['nsa', 'drpall']
    assert source in valid_sources, 'source must be one of {0}'.format(
        valid_sources)

    log.debug(
        'get_nsa_data: getting NSA data for mangaid=%r with source=%r, mode=%r',
        mangaid, source, mode)

    if mode == 'auto':
        log.debug('get_nsa_data: running auto mode mode.')
        try:
            nsa_data = get_nsa_data(mangaid,
                                    mode='local',
                                    source=source,
                                    drpver=drpver,
                                    drpall=drpall)
            return nsa_data
        except MarvinError as ee:
            log.debug('get_nsa_data: local mode failed with error %s', str(ee))
            try:
                nsa_data = get_nsa_data(mangaid,
                                        mode='remote',
                                        source=source,
                                        drpver=drpver,
                                        drpall=drpall)
                return nsa_data
            except MarvinError as ee:
                raise MarvinError(
                    'get_nsa_data: failed to get NSA data for mangaid=%r in '
                    'auto mode with with error: %s', mangaid, str(ee))

    elif mode == 'local':

        if source == 'nsa':

            if config.db is not None:

                session = marvindb.session
                sampledb = marvindb.sampledb

                nsa_row = session.query(sampledb.NSA).join(
                    sampledb.MangaTargetToNSA, sampledb.MangaTarget).filter(
                        sampledb.MangaTarget.mangaid == mangaid).all()

                if len(nsa_row) == 1:
                    return DotableCaseInsensitive(
                        _db_row_to_dict(nsa_row[0],
                                        remove_columns=['pk', 'catalogue_pk']))
                elif len(nsa_row) > 1:
                    warnings.warn(
                        'get_nsa_data: multiple NSA rows found for mangaid={0}. '
                        'Using the first one.'.format(mangaid),
                        MarvinUserWarning)
                    return DotableCaseInsensitive(
                        _db_row_to_dict(nsa_row[0],
                                        remove_columns=['pk', 'catalogue_pk']))
                elif len(nsa_row) == 0:
                    raise MarvinError(
                        'get_nsa_data: cannot find NSA row for mangaid={0}'.
                        format(mangaid))

            else:

                raise MarvinError(
                    'get_nsa_data: cannot find a valid DB connection.')

        elif source == 'drpall':

            plateifu = mangaid2plateifu(mangaid,
                                        drpver=drpver,
                                        drpall=drpall,
                                        mode='drpall')
            log.debug('get_nsa_data: found plateifu=%r for mangaid=%r',
                      plateifu, mangaid)

            drpall_row = get_drpall_row(plateifu, drpall=drpall, drpver=drpver)

            nsa_data = collections.OrderedDict()
            for col in drpall_row.colnames:
                if col.startswith('nsa_'):
                    value = drpall_row[col]
                    if isinstance(value, np.ndarray):
                        value = value.tolist()
                    else:
                        value = np.asscalar(value)
                    nsa_data[col[4:]] = value

            return DotableCaseInsensitive(nsa_data)

    elif mode == 'remote':

        from marvin.api.api import Interaction

        try:
            if source == 'nsa':
                request_name = 'nsa_full'
            else:
                request_name = 'nsa_drpall'
            url = marvin.config.urlmap['api'][request_name]['url']
            response = Interaction(url.format(mangaid=mangaid))
        except MarvinError as ee:
            raise MarvinError('API call to {0} failed: {1}'.format(
                request_name, str(ee)))
        else:
            if response.results['status'] == 1:
                return DotableCaseInsensitive(
                    collections.OrderedDict(response.getData()))
            else:
                raise MarvinError('get_nsa_data: %s', response['error'])