Example #1
0
    def _get_a_file(self, filename, local=None):
        ''' Retrieve a file served locally or remotely over the internet

        Will retrieve a local filename or grab the file
        contents remotely from the SAS

        Parameters:
            filename (str):
                Full filepath to load
            local (bool):
                If True, does a local system check

        Returns:
            A file object to be read in by Yanny
        '''

        if local:
            if not os.path.exists(filename):
                raise MarvinError(
                    'No {0} file found locally.'.format(filename))
            else:
                fileobj = filename
        else:
            r = requests.get(filename)
            if not r.ok:
                raise MarvinError(
                    'Could not retrieve {0} file remotely'.format(filename))
            else:
                fileobj = stringio(r.content.decode())

        return fileobj
Example #2
0
    def _getIdleProcesses(self):
        ''' Get a list of all idle processes on server

        This grabs a list of all processes in a state of
        idle, or idle in transaction using pg_stat_activity
        and returns the process id, the state, and the query

        '''
        if self.mode == 'local':
            sql = ("select p.pid,p.state,p.query from pg_stat_activity as p \
                   where p.state ilike '%idle%';")
            res = self.session.execute(sql)
            procs = res.fetchall()
        elif self.mode == 'remote':
            # Fail if no route map initialized
            if not config.urlmap:
                raise MarvinError('No URL Map found.  Cannot make remote call')

            # Get the query route
            url = config.urlmap['api']['cleanupqueries']['url']

            params = {'task': 'getprocs', 'release': self._release}

            try:
                ii = Interaction(route=url, params=params)
            except Exception as e:
                raise MarvinError('API Query call failed: {0}'.format(e))
            else:
                procs = ii.getData()
        return procs
Example #3
0
    def _check_file(header, data, objtype):
        ''' Check the file input to ensure correct tool '''

        # get/check various header keywords
        bininhdr = ('binkey' in header) or ('bintype' in header)
        dapinhdr = 'dapfrmt' in header
        dapfrmt = header['DAPFRMT'] if dapinhdr else None

        # check the file
        if objtype == 'Maps' or objtype == 'ModelCube':
            # get indices in daptype
            daptype = ['MAPS', 'LOGCUBE']
            dapindex = daptype.index(
                "MAPS") if objtype == 'Maps' else daptype.index("LOGCUBE")
            altdap = 1 - dapindex

            # check for emline_gflux extension
            gfluxindata = 'EMLINE_GFLUX' in data
            wronggflux = (gfluxindata and objtype == 'ModelCube') or \
                         (not gfluxindata and objtype == 'Maps')

            if not bininhdr:
                raise MarvinError(
                    'Trying to open a non DAP file with Marvin {0}'.format(
                        objtype))
            else:
                if (dapfrmt and dapfrmt != daptype[dapindex]) or (wronggflux):
                    raise MarvinError(
                        'Trying to open a DAP {0} with Marvin {1}'.format(
                            daptype[altdap], objtype))
        elif objtype == 'Cube':
            if bininhdr or dapinhdr:
                raise MarvinError('Trying to open a DAP file with Marvin Cube')
Example #4
0
File: mma.py Project: rgcl/marvin
    def _doLocal(self):
        """Tests if it's possible to load the data locally."""

        if self.filename:

            if os.path.exists(self.filename):
                self.mode = 'local'
                self.data_origin = 'file'
            else:
                raise MarvinError('input file {0} not found'.format(
                    self.filename))

        elif self.plateifu:

            testDbConnection(marvindb.session)

            if marvindb.db and not self._ignore_db:
                self.mode = 'local'
                self.data_origin = 'db'
            else:
                fullpath = self._getFullPath()

                if fullpath and os.path.exists(fullpath):
                    self.mode = 'local'
                    self.filename = fullpath
                    self.data_origin = 'file'
                else:
                    if self._forcedownload:
                        self.download()
                        self.data_origin = 'file'
                    else:
                        raise MarvinError('this is the end of the road. '
                                          'Try using some reasonable inputs.')
Example #5
0
    def _get_dapall_from_db(self):
        """Uses the DB to retrieve the DAPAll data."""

        dapall_data = {}

        daptype = self.bintype.name + '-' + self.template.name

        mdb = marvin.marvindb

        if not mdb.isdbconnected:
            raise MarvinError('No DB connected')

        datadb = mdb.datadb
        dapdb = mdb.dapdb

        dapall_row = mdb.session.query(dapdb.DapAll).join(
            dapdb.File, datadb.PipelineInfo, datadb.PipelineVersion).filter(
                mdb.datadb.PipelineVersion.version == self._dapver,
                dapdb.DapAll.plateifu == self.plateifu,
                dapdb.DapAll.daptype == daptype).first()

        if dapall_row is None:
            raise MarvinError('cannot find a DAPall match for this target in the DB.')

        for col in dapall_row.__table__.columns.keys():
            if col != 'pk' and '_pk' not in col:
                dapall_data[col] = getattr(dapall_row, col)

        return dapall_data
Example #6
0
def _get_column(results, colname, format_type=None):
    ''' Gets a column from a Query

    Parameters:
        results (obj):
            A set of Marvin Results
        colname (str):
            The name of the column to extract
        format_type (str):
            The format of the dictionary

    Returns:
        A list of data for that column
    '''

    column = None
    if format_type == 'list':
        try:
            column = results.getListOf(colname)
        except MarvinError as e:
            raise MarvinError('Cannot get list for column {0}: {1}'.format(
                colname, e))
    elif format_type in ['listdict', 'dictlist']:
        try:
            if colname == 'None':
                column = results.getDictOf(format_type=format_type)
            else:
                column = results.getDictOf(colname, format_type=format_type)
        except MarvinError as e:
            raise MarvinError(
                'Cannot get dictionary for column {0}: {1}'.format(colname, e))

    return column
Example #7
0
    def _load_modelcube_from_db(self):
        """Initialises a model cube from the DB."""

        mdb = marvin.marvindb
        plate, ifu = self.plateifu.split('-')

        if not mdb.isdbconnected:
            raise MarvinError('No db connected')

        else:

            datadb = mdb.datadb
            dapdb = mdb.dapdb

            if self.data:
                assert isinstance(self.data, dapdb.ModelCube), \
                    'data is not an instance of marvindb.dapdb.ModelCube.'
            else:
                # Initial query for version
                version_query = mdb.session.query(dapdb.ModelCube).join(
                    dapdb.File, datadb.PipelineInfo,
                    datadb.PipelineVersion).filter(
                        datadb.PipelineVersion.version ==
                        self._dapver).from_self()

                # Query for model cube parameters
                db_modelcube = version_query.join(
                    dapdb.File, datadb.Cube, datadb.IFUDesign).filter(
                        datadb.Cube.plate == plate,
                        datadb.IFUDesign.name == str(ifu)).from_self().join(
                            dapdb.File, dapdb.FileType
                        ).filter(dapdb.FileType.value == 'LOGCUBE').join(
                            dapdb.Structure, dapdb.BinType).join(
                                dapdb.Template, dapdb.Structure.template_kin_pk
                                == dapdb.Template.pk).filter(
                                    dapdb.BinType.name == self.bintype.name,
                                    dapdb.Template.name ==
                                    self.template.name).all()

                if len(db_modelcube) > 1:
                    raise MarvinError('more than one ModelCube found for '
                                      'this combination of parameters.')

                elif len(db_modelcube) == 0:
                    raise MarvinError(
                        'no ModelCube found for this combination of parameters.'
                    )

                self.data = db_modelcube[0]

            self.header = self.data.file.primary_header
            self.wcs = WCS(self.data.file.cube.wcs.makeHeader())
            self._wavelength = np.array(
                self.data.file.cube.wavelength.wavelength, dtype=np.float)
            self._redcorr = np.array(self.data.redcorr[0].value,
                                     dtype=np.float)
            self._shape = self.data.file.cube.shape.shape

            self.plateifu = str(self.header['PLATEIFU'].strip())
            self.mangaid = str(self.header['MANGAID'].strip())
Example #8
0
    def getSubset(self, start, limit=10):
        ''' Extracts a subset of results

            Parameters:
                start (int):
                    The starting index of your subset extraction
                limit (int):
                    The limiting number of results to return.

            Returns:
                results (list):
                    A list of query results

            Example:
                >>> r = q.run()
                >>> r.getSubset(0, 10)
                >>> [(u'14-12', u'1901', -9999.0),
                >>> (u'14-13', u'1902', -9999.0),
                >>> (u'27-134', u'1901', -9999.0),
                >>> (u'27-100', u'1902', -9999.0),
                >>> (u'27-762', u'1901', -9999.0),
                >>> (u'27-759', u'1902', -9999.0),
                >>> (u'27-827', u'1901', -9999.0),
                >>> (u'27-828', u'1902', -9999.0),
                >>> (u'27-1170', u'1901', -9999.0),
                >>> (u'27-1167', u'1902', -9999.0)]
        '''
        start = 0 if int(start) < 0 else int(start)
        end = start + int(limit)
        # if end > self.count:
        #     end = self.count
        #     start = end - int(limit)
        self.start = start
        self.end = end
        self.chunk = limit
        if self.mode == 'local':
            self.results = self.query.slice(start, end).all()
        elif self.mode == 'remote':
            # Fail if no route map initialized
            if not config.urlmap:
                raise MarvinError('No URL Map found.  Cannot make remote call')

            # Get the query route
            url = config.urlmap['api']['getsubset']['url']

            params = {'searchfilter': self.searchfilter, 'params': self.returnparams,
                      'start': start, 'end': end, 'limit': self.limit,
                      'sort': self.sortcol, 'order': self.order}
            try:
                ii = Interaction(route=url, params=params)
            except MarvinError as e:
                raise MarvinError('API Query GetNext call failed: {0}'.format(e))
            else:
                self.results = ii.getData()
                self._makeNamedTuple()

        if self.returntype:
            self.convertToTool()

        return self.results
Example #9
0
File: rss.py Project: zpace/marvin
    def _load_rss_from_api(self):
        """Initialises the RSS object using the remote API."""

        # Checks that the RSS exists.
        routeparams = {'name': self.plateifu}
        url = marvin.config.urlmap['api']['getRSS']['url'].format(**routeparams)

        try:
            response = self._toolInteraction(url.format(name=self.plateifu))
        except Exception as ee:
            raise MarvinError('found a problem when checking if remote RSS '
                              'exists: {0}'.format(str(ee)))

        data = response.getData()

        self.header = fits.Header.fromstring(data['header'])
        self.wcs = astropy.wcs.WCS(fits.Header.fromstring(data['wcs_header']))
        self._wavelength = data['wavelength']
        self._nfibers = data['nfibers']

        self.obsinfo = astropy.io.ascii.read(data['obsinfo'])

        if self.plateifu != data['plateifu']:
            raise MarvinError('remote RSS has a different plateifu!')

        return
Example #10
0
    def _load_rss_from_db(self):
        """Initialises the RSS object from the DB."""

        import sqlalchemy

        mdb = marvin.marvindb

        if not mdb.isdbconnected:
            raise MarvinError('No db connected')

        plate, ifudesign = [item.strip() for item in self.plateifu.split('-')]

        try:
            self.data = mdb.session.query(mdb.datadb.RssFiber).join(
                mdb.datadb.Cube, mdb.datadb.PipelineInfo,
                mdb.datadb.PipelineVersion, mdb.datadb.IFUDesign).filter(
                    mdb.datadb.PipelineVersion.version == self._drpver,
                    mdb.datadb.Cube.plate == plate,
                    mdb.datadb.IFUDesign.name == ifudesign).all()

        except sqlalchemy.orm.exc.NoResultFound as ee:
            raise MarvinError('Could not retrieve RSS for plate-ifu {0}: '
                              'No Results Found: {1}'.format(
                                  self.plateifu, ee))

        except Exception as ee:
            raise MarvinError('Could not retrieve RSS for plate-ifu {0}: '
                              'Unknown exception: {1}'.format(
                                  self.plateifu, ee))

        if not self.data:
            raise MarvinError('Could not retrieve RSS for plate-ifu {0}: '
                              'Unknown error.'.format(self.plateifu))
Example #11
0
def restore(path, delete=False):
    """Restores a MarvinToolsClass object from a pickled file.

    If ``delete=True``, the pickled file will be removed after it has been
    unplickled. Note that, for objects with ``data_origin='file'``, the
    original file must exists and be in the same path as when the object
    was first created.

    """

    assert isinstance(path, string_types), 'path must be a string.'

    path = os.path.realpath(os.path.expanduser(path))

    if os.path.isdir(path):
        raise MarvinError('path must be a full route, including the filename.')

    if not os.path.exists(path):
        raise MarvinError('the path does not exists.')

    try:
        with open(path, 'rb') as fin:
            obj = pickle.load(fin)
    except Exception as ee:
        raise MarvinError(
            'something went wrong unplicking the object: {0}'.format(str(ee)))

    if delete is True:
        os.remove(path)

    return obj
Example #12
0
    def get_binid(self, model=None):
        """Returns the 2D array for the binid map associated with ``model``."""

        assert model is None or isinstance(model, Model), 'invalid model type.'

        if model is not None:
            binid_prop = model.binid
        else:
            binid_prop = self.datamodel.parent.default_binid

        # Before MPL-6, the modelcube does not include the binid extension,
        # so we need to get the binid map from the associated MAPS.
        if (distutils.version.StrictVersion(self._dapver) <
                distutils.version.StrictVersion('2.1')):
            return self.getMaps().get_binid()

        if self.data_origin == 'file':

            if binid_prop.channel is None:
                return self.data[binid_prop.name].data[:, :]
            else:
                return self.data[binid_prop.name].data[binid_prop.channel.idx, :, :]

        elif self.data_origin == 'db':

            mdb = marvin.marvindb

            table = mdb.dapdb.ModelSpaxel
            column = getattr(table, binid_prop.db_column())

            binid_list = mdb.session.query(column).filter(
                table.modelcube_pk == self.data.pk).order_by(table.x, table.y).all()

            nx = ny = int(np.sqrt(len(binid_list)))
            binid_array = np.array(binid_list)

            return binid_array.transpose().reshape((ny, nx)).transpose(1, 0)

        elif self.data_origin == 'api':

            params = {'release': self._release}
            url = marvin.config.urlmap['api']['getModelCubeBinid']['url']

            extension = model.fits_extension().lower() if model is not None else 'flux'

            try:
                response = self._toolInteraction(
                    url.format(name=self.plateifu,
                               modelcube_extension=extension,
                               bintype=self.bintype.name,
                               template=self.template.name), params=params)
            except Exception as ee:
                raise MarvinError('found a problem when checking if remote '
                                  'modelcube exists: {0}'.format(str(ee)))

            if response.results['error'] is not None:
                raise MarvinError('found a problem while getting the binid from API: {}'
                                  .format(str(response.results['error'])))

            return np.array(response.getData()['binid'])
Example #13
0
    def _doLocal(self):
        """Tests if it's possible to load the data locally."""

        if self.filename:

            if os.path.exists(self.filename):
                self.mode = 'local'
                self.data_origin = 'file'
            else:
                raise MarvinError('input file {0} not found'.format(
                    self.filename))

        elif self.plateifu:

            from marvin import marvindb
            if marvindb:
                testDbConnection(marvindb.session)

            if marvindb and marvindb.db and not self._ignore_db:
                self.mode = 'local'
                self.data_origin = 'db'
            else:
                fullpath = self._getFullPath()

                if fullpath and os.path.exists(fullpath):
                    self.mode = 'local'
                    self.filename = fullpath
                    self.data_origin = 'file'
                else:
                    if self._forcedownload:
                        self.download()
                        self.data_origin = 'file'
                    else:
                        raise MarvinError('failed to retrieve data using '
                                          'input parameters.')
Example #14
0
    def save(self, path=None, overwrite=False):
        ''' Save the results as a pickle object

        Parameters:
            path (str):
                Filepath and name of the pickled object
            overwrite (bool):
                Set this to overwrite an existing pickled file

        Returns:
            path (str):
                The filepath and name of the pickled object

        '''

        # set Marvin query object to None, in theory this could be pickled as well
        self._queryobj = None
        try:
            isnotstr = type(self.query) != unicode
        except NameError as e:
            isnotstr = type(self.query) != str
        if isnotstr:
            self.query = None

        sf = self.searchfilter.replace(' ', '') if self.searchfilter else 'anon'
        # set the path
        if not path:
            path = os.path.expanduser('~/marvin_results_{0}.mpf'.format(sf))

        # check for file extension
        if not os.path.splitext(path)[1]:
            path = os.path.join(path+'.mpf')

        path = os.path.realpath(path)

        if os.path.isdir(path):
            raise MarvinError('path must be a full route, including the filename.')

        if os.path.exists(path) and not overwrite:
            warnings.warn('file already exists. Not overwriting.', MarvinUserWarning)
            return

        dirname = os.path.dirname(path)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        try:
            pickle.dump(self, open(path, 'wb'), protocol=-1)
        except Exception as ee:
            if os.path.exists(path):
                os.remove(path)
            raise MarvinError('Error found while pickling: {0}'.format(str(ee)))

        return path
Example #15
0
    def __init__(self, *args, **kwargs):

        valid_kwargs = [
            'data', 'cube', 'maps', 'filename', 'mangaid', 'plateifu', 'mode',
            'release', 'bintype', 'template_kin', 'template_pop', 'nsa_source'
        ]

        assert len(args) == 0, 'Maps does not accept arguments, only keywords.'
        for kw in kwargs:
            assert kw in valid_kwargs, 'keyword {0} is not valid'.format(kw)

        if kwargs.pop('template_pop', None):
            warnings.warn('template_pop is not yet in use. Ignoring value.',
                          MarvinUserWarning)

        self.bintype = kwargs.pop('bintype',
                                  marvin.tools.maps.__BINTYPES_UNBINNED__)
        self.template_kin = kwargs.pop(
            'template_kin', marvin.tools.maps.__TEMPLATES_KIN_DEFAULT__)
        self.template_pop = None

        super(ModelCube, self).__init__(*args, **kwargs)

        # Checks that DAP is at least MPL-5
        MPL5 = distutils.version.StrictVersion('2.0.2')
        if self.filename is None and distutils.version.StrictVersion(
                self._dapver) < MPL5:
            raise MarvinError('ModelCube requires at least dapver=\'2.0.2\'')

        self._cube = kwargs.pop('cube', None)
        self._maps = kwargs.pop('maps', None)

        assert self.bintype in marvin.tools.maps.__BINTYPES__, \
            'bintype must be on of {0}'.format(marvin.tools.maps.__BINTYPES__)
        assert self.template_kin in marvin.tools.maps.__TEMPLATES_KIN__, \
            'template_kin must be on of {0}'.format(marvin.tools.maps.__TEMPLATES_KIN__)

        self.header = None
        self.wcs = None
        self.shape = None
        self.wavelength = None

        if self.data_origin == 'file':
            self._load_modelcube_from_file()
        elif self.data_origin == 'db':
            self._load_modelcube_from_db()
        elif self.data_origin == 'api':
            self._load_modelcube_from_api()
        else:
            raise MarvinError('data_origin={0} is not valid'.format(
                self.data_origin))

        # Confirm that drpver and dapver match the ones from the header.
        marvin.tools.maps.Maps._check_versions(self)
Example #16
0
def save(obj, path=None, overwrite=False):
    """Pickles the object.

    If ``path=None``, uses the default location of the file in the tree
    but changes the extension of the file to ``.mpf`` (MaNGA Pickle File).
    Returns the path of the saved pickle file.

    """

    from marvin.core.core import MarvinToolsClass

    if path is None:
        assert isinstance(
            obj,
            MarvinToolsClass), 'path=None is only allowed for core objects.'
        path = obj._getFullPath()
        assert isinstance(path, string_types), 'path must be a string.'
        if path is None:
            raise MarvinError('cannot determine the default path in the '
                              'tree for this file. You can overcome this '
                              'by calling save with a path keyword with '
                              'the path to which the file should be saved.')

        # Replaces the extension (normally fits) with mpf
        if '.fits.gz' in path:
            path = path.strip('.fits.gz')
        else:
            path = os.path.splitext(path)[0]

        path += '.mpf'

    path = os.path.realpath(os.path.expanduser(path))

    if os.path.isdir(path):
        raise MarvinError('path must be a full route, including the filename.')

    if os.path.exists(path) and not overwrite:
        warnings.warn('file already exists. Not overwriting.',
                      MarvinUserWarning)
        return

    dirname = os.path.dirname(path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    try:
        with open(path, 'wb') as fout:
            pickle.dump(obj, fout, protocol=-1)
    except Exception as ee:
        if os.path.exists(path):
            os.remove(path)
        raise MarvinError('error found while pickling: {0}'.format(str(ee)))

    return path
Example #17
0
def _getCube(name, use_file=False, release=None, **kwargs):
    ''' Retrieve a cube using marvin tools '''

    drpver, __ = config.lookUpVersions(release)

    cube = None
    results = {}

    # parse name into either mangaid or plateifu
    try:
        idtype = parseIdentifier(name)
    except Exception as ee:
        results['error'] = 'Failed to parse input name {0}: {1}'.format(name, str(ee))
        return cube, results

    filename = None
    plateifu = None
    mangaid = None

    try:
        if use_file:

            if idtype == 'mangaid':
                plate, ifu = mangaid2plateifu(name, drpver=drpver)
            elif idtype == 'plateifu':
                plate, ifu = name.split('-')

            if Path is not None:
                filename = Path(release=release).full('mangacube', ifu=ifu, plate=plate, drpver=drpver, wave='LOG')
                assert os.path.exists(filename), 'file not found.'
            else:
                raise MarvinError('cannot create path for MaNGA cube.')

        else:

            if idtype == 'plateifu':
                plateifu = name
            elif idtype == 'mangaid':
                mangaid = name
            else:
                raise MarvinError('invalid plateifu or mangaid: {0}'.format(idtype))

        cube = Cube(filename=filename, mangaid=mangaid, plateifu=plateifu,
                    mode='local', release=release)

        results['status'] = 1

    except Exception as ee:

        results['error'] = 'Failed to retrieve cube {0}: {1}'.format(name, str(ee))

    return cube, results
Example #18
0
File: rss.py Project: zpace/marvin
def _getRSS(name, use_file=True, release=None, **kwargs):
    """Retrieves a RSS Marvin object."""

    drpver, __ = marvin.config.lookUpVersions(release)

    rss = None
    results = {}

    # parse name into either mangaid or plateifu
    try:
        idtype = parseIdentifier(name)
    except Exception as ee:
        results['error'] = 'Failed to parse input name {0}: {1}'.format(name, str(ee))
        return rss, results

    filename = None
    plateifu = None
    mangaid = None

    try:
        if use_file:

            if idtype == 'mangaid':
                plate, ifu = mangaid2plateifu(name, drpver=drpver)
            elif idtype == 'plateifu':
                plate, ifu = name.split('-')

            if Path is not None:
                filename = Path().full('mangarss', ifu=ifu, plate=plate, drpver=drpver)
                assert os.path.exists(filename), 'file not found.'
            else:
                raise MarvinError('cannot create path for MaNGA rss.')

        else:

            if idtype == 'plateifu':
                plateifu = name
            elif idtype == 'mangaid':
                mangaid = name
            else:
                raise MarvinError('invalid plateifu or mangaid: {0}'.format(idtype))

        rss = marvin.tools.RSS(filename=filename, mangaid=mangaid, plateifu=plateifu,
                               mode='local', release=release)

        results['status'] = 1

    except Exception as ee:

        results['error'] = 'Failed to retrieve RSS {0}: {1}'.format(name, str(ee))

    return rss, results
Example #19
0
    def _determine_inputs(self, input):
        """Determines what inputs to use in the decision tree."""

        if input:

            assert self.filename is None and self.plateifu is None and self.mangaid is None, \
                'if input is set, filename, plateifu, and mangaid cannot be set.'

            assert isinstance(input,
                              six.string_types), 'input must be a string.'

            input_dict = self._parse_input(input)

            if input_dict['plate'] is not None and input_dict[
                    'ifu'] is not None:
                self.plateifu = input
            elif input_dict['plate'] is not None and input_dict['ifu'] is None:
                self._plate = input
            elif input_dict['mangaid'] is not None:
                self.mangaid = input
            else:
                # Assumes the input must be a filename
                self.filename = input

        if self.filename is None and self.mangaid is None and self.plateifu is None:
            raise MarvinError('no inputs defined.')

        if self.filename:
            self.mangaid = None
            self.plateifu = None

            if self.mode == 'remote':
                raise MarvinError('filename not allowed in remote mode.')

            assert os.path.exists(self.filename), \
                'filename {} does not exist.'.format(str(self.filename))

        elif self.plateifu:
            assert not self.filename, 'invalid set of inputs.'

        elif self.mangaid:
            assert not self.filename, 'invalid set of inputs.'
            self.plateifu = mangaid2plateifu(self.mangaid,
                                             drpall=self._drpall,
                                             drpver=self._drpver)

        elif self._plate:
            assert not self.filename, 'invalid set of inputs.'
Example #20
0
    def lookUpVersions(self, release=None):
        """Retrieve the DRP and DAP versions that make up a release version.

        Parameters:
            release (str or None):
                The release version. If ``None``, uses the currently set
                ``release`` value.

        Returns:
            drpver, dapver (tuple):
                A tuple of strings of the DRP and DAP versions according
                to the input MPL version

        """

        release = release or self.release

        try:
            drpver, dapver = self._mpldict[release]
        except KeyError:
            raise MarvinError(
                'MPL/DR version {0} not found in lookup table. '
                'No associated DRP/DAP versions. '
                'Should they be added?  Check for typos.'.format(release))

        return drpver, dapver
Example #21
0
    def _load_modelcube_from_api(self):
        """Initialises a model cube from the API."""

        url = marvin.config.urlmap['api']['getModelCube']['url']
        url_full = url.format(name=self.plateifu,
                              bintype=self.bintype.name,
                              template=self.template.name)

        try:
            response = self._toolInteraction(url_full)
        except Exception as ee:
            raise MarvinError(
                'found a problem when checking if remote model cube '
                'exists: {0}'.format(str(ee)))

        data = response.getData()

        self.header = fits.Header.fromstring(data['header'])
        self.wcs = WCS(fits.Header.fromstring(data['wcs_header']))
        self._wavelength = np.array(data['wavelength'])
        self._redcorr = np.array(data['redcorr'])
        self._shape = tuple(data['shape'])

        self.plateifu = str(self.header['PLATEIFU'].strip())
        self.mangaid = str(self.header['MANGAID'].strip())
Example #22
0
def getDir3d(inputid, mode=None, release=None):
    ''' Get the 3d redux Image directory from an input plate or plate-IFU '''

    idtype = parseIdentifier(inputid)
    if idtype == 'plate':
        plateid = inputid
    elif idtype == 'plateifu':
        plateid, __ = inputid.split('-')

    release = marvin.config.release if not release else release
    drpver, __ = marvin.config.lookUpVersions(release=release)

    if check_versions(drpver, 'v1_5_4'):
        from marvin.tools.plate import Plate
        try:
            plate = Plate(plate=plateid, nocubes=True, mode=mode, release=release)
        except Exception as e:
            raise MarvinError('Could not retrieve a remote plate.  If it is a mastar '
                              'plate you are after, Marvin currently does not handle those: {0}'.format(e))
        else:
            dir3d = plate.dir3d
    else:
        dir3d = 'stack'

    return dir3d
Example #23
0
    def get_available_params(self):
        ''' Retrieve the available parameters to query on

        Retrieves a list of all the available query parameters.
        Used primarily for the web autocompletion.

        Parameters:

        Returns:
            mykeys (list):
                a list of all of the available queryable parameters
        '''
        if self.mode == 'local':
            keys = list(self.marvinform._param_form_lookup.keys())
            keys.sort()
            rev = {v: k for k, v in self.marvinform._param_form_lookup._tableShortcuts.items()}
            # simplify the spaxelprop list down to one set
            mykeys = [k.split('.', 1)[-1] for k in keys if 'cleanspaxel' not in k]
            mykeys = [k.replace(k.split('.')[0], 'spaxelprop') if 'spaxelprop'
                      in k else k for k in mykeys]
            # replace table names with shortcut names
            newkeys = [k.replace(k.split('.')[0], rev[k.split('.')[0]]) if k.split('.')[0] in rev.keys() else k for k in mykeys]
            return newkeys
        elif self.mode == 'remote':
            # Get the query route
            url = config.urlmap['api']['getparams']['url']
            params = {'paramdisplay': 'all'}
            try:
                ii = Interaction(route=url, params=params)
            except MarvinError as e:
                raise MarvinError('API Query call to get params failed: {0}'.format(e))
            else:
                mykeys = ii.getData()
                return mykeys
Example #24
0
    def dapall(self):
        """Returns the contents of the DAPall data for this target."""

        if (not self._dapver or
                distutils.version.StrictVersion(self._dapver) < self.__min_dapall_version__):
            raise MarvinError('DAPall is not available for versions before MPL-6.')

        if hasattr(self, '_dapall') and self._dapall is not None:
            return self._dapall

        if self.data_origin == 'file':
            try:
                dapall_data = self._get_dapall_from_file()
            except IOError:
                warnings.warn('cannot find DAPall file. Trying remote request.',
                              MarvinUserWarning)
                dapall_data = self._get_from_api()
        elif self.data_origin == 'db':
            dapall_data = self._get_dapall_from_db()
        else:
            dapall_data = self._get_dapall_from_api()

        self._dapall = dapall_data

        return self._dapall
Example #25
0
    def _checkFilename(self):
        ''' Checks the filename for a proper FITS file '''

        # if filename is not FITS, then try to load one
        if 'fits' not in self.filename.lower():

            if not Path:
                raise MarvinError('sdss_access is not installed')
            else:
                # is_public = 'DR' in self._release
                # path_release = self._release.lower() if is_public else None
                sdss_path = Path(release=self._release)

            # try a cube
            full = sdss_path.full('mangacube', drpver=self._drpver, plate=self.plateid, ifu='*', wave='LOG')
            cubeexists = sdss_path.any('', full=full)
            if cubeexists:
                file = sdss_path.one('', full=full)
            else:
                # try an rss
                full = sdss_path.full('mangarss', drpver=self._drpver, plate=self.plateid, ifu='*', wave='LOG')
                rssexists = sdss_path.any('', full=full)
                if rssexists:
                    file = sdss_path.one('', full=full)
                else:
                    file = None
            # load the file
            if file:
                self.filename = file
            else:
                self.filename = None
Example #26
0
    def login(self, refresh=None):
        ''' Login with netrc credentials to receive an API token

        Copy this token into the "use_token" parameter in your
        custom marvin.yml file to ensure preserve authentication
        across iPython user sessions.

        Parameters:
            refresh (bool):
                Set to True to refresh a login to receive a new token

        '''

        assert config.access == 'collab', 'You must have collaboration access to login.'

        # do nothing if token already generated
        if self.token and not refresh:
            return

        valid_netrc = bconfig._check_netrc()
        if valid_netrc:
            # get login info for api.sdss.org
            user, password = bconfig._read_netrc('api.sdss.org')
            data = {'username': user, 'password': password}

            # send token request
            url = self.urlmap['api']['login']['url']
            try:
                resp = Interaction(url, params=data, auth='netrc')
            except Exception as e:
                raise MarvinError('Error getting login token. {0}'.format(e))
            else:
                self.token = resp.results['access_token']
Example #27
0
    def overlay_hexagon(self, ax, return_figure=True, **kwargs):
        """ Overlay the IFU hexagon on a plot

        Parameters:
            ax (Axis):
                The matplotlib axis object
            return_figure (bool):
                If True, returns the figure axis object.  Default is True
            kwargs:
                Any keyword arguments accepted by Matplotlib plot
        """

        if self.wcs is None:
            raise MarvinError('No WCS found.  Cannot overlay hexagon.')

        # get IFU hexagon pixel coordinates
        hexagon_pix = self.wcs.wcs_world2pix(self.bundle.hexagon, 1)
        # reconnect the last point to the first point.
        hexagon_pix = np.concatenate((hexagon_pix, [hexagon_pix[0]]), axis=0)

        # some matplotlib kwargs
        kwargs['color'] = kwargs.get('color', 'magenta')
        kwargs['linestyle'] = kwargs.get('linestyle', 'solid')
        kwargs['linewidth'] = kwargs.get('linewidth', 0.8)

        ax.plot(hexagon_pix[:, 0], hexagon_pix[:, 1], **kwargs)

        if return_figure:
            return ax
Example #28
0
    def _getExtensionData(self, extName):
        """Returns the data from an extension."""

        if self.data_origin == 'file':
            return self.data[extName.upper()].data

        elif self.data_origin == 'db':
            return self.data.get3DCube(extName.lower())

        elif self.data_origin == 'api':

            url = marvin.config.urlmap['api']['getCubeExtension']['url']

            try:
                response = self._toolInteraction(
                    url.format(name=self.plateifu,
                               cube_extension=extName.lower()))
            except Exception as ee:
                raise MarvinError(
                    'found a problem when checking if remote cube '
                    'exists: {0}'.format(str(ee)))

            data = response.getData()

            return np.array(data['cube_extension'])
Example #29
0
def getDapRedux(release=None):
    ''' Retrieve SAS url link to the DAP redux directory

    Parameters:
        release (str):
            The release version of the data to download.
            Defaults to Marvin config.release.

    Returns:
        dapredux (str):
            The full redux path to the DAP MAPS
    '''

    if not Path:
        raise MarvinError('sdss_access is not installed')
    else:
        sdss_path = Path()

    release = release or marvin.config.release
    drpver, dapver = marvin.config.lookUpVersions(release=release)
    # hack a url version of MANGA_SPECTRO_ANALYSIS
    dapdefault = sdss_path.dir('mangadefault',
                               drpver=drpver,
                               dapver=dapver,
                               plate=None,
                               ifu=None)
    dappath = dapdefault.rsplit('/', 2)[0]
    dapredux = sdss_path.url('', full=dappath)
    return dapredux
Example #30
0
    def __init__(self, ra, dec, width, height, scale=None, **kwargs):
        self.rawurl = (
            "http://skyserver.sdss.org/public/SkyServerWS/ImgCutout/getjpeg?"
            "ra={ra}&dec={dec}&scale={scale}&width={width_pix}&height={height_pix}&opt={opts}&query="
        )
        self.ra = ra
        self.dec = dec
        self.scale = scale or 0.198  # default arcsec/pixel
        self.image = None
        self.center = np.array([ra, dec])
        self.size = np.array([width, height], dtype=int)
        self.coords = {
            'ra': ra,
            'dec': dec,
            'width': width,
            'height': height,
            'scale': self.scale
        }
        self._get_pix_size()
        if max(self.size_pix) >= 2048:
            raise MarvinError(
                'Requested image size is too large. '
                'The Skyserver image cutout can only return a size up to 2048 pixels'
            )

        self._define_wcs()
        self._get_cutout(**kwargs)