Ejemplo n.º 1
0
 def updateMaps(self):
     args = av.manual_parse(self, request, use_params='galaxy', required=['plateifu', 'bintemp', 'params[]'], makemulti=True)
     #self._drpver, self._dapver, self._release = parseSession()
     cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release}
     params = args.getlist('params[]', type=str)
     bintemp = args.get('bintemp', None, type=str)
     current_session['bintemp'] = bintemp
     # get cube (self.galaxy['cube'] does not work)
     try:
         cube = Cube(**cubeinputs)
     except Exception as e:
         cube = None
     # Try to make the web maps
     if not cube:
         output = {'mapmsg': 'No cube found', 'maps': None, 'status': -1}
     elif not params:
         output = {'mapmsg': 'No parameters selected', 'maps': None, 'status': -1}
     else:
         try:
             mapdict = buildMapDict(cube, params, self._dapver, bintemp=bintemp)
         except Exception as e:
             output = {'mapmsg': e.message, 'status': -1, 'maps': None}
         else:
             output = {'mapmsg': None, 'status': 1, 'maps': mapdict}
     return jsonify(result=output)
Ejemplo n.º 2
0
def galaxy_get_cache(*args, **kwargs):
    ''' Function used to generate the route cache key

    Cache key when using cache.memoize or cache.cached decorator.
    memoize remembers input methods arguments; cached does not.

    Parameters:
        args (list):
            a list of the fx/method route call and object instance (self)
        kwargs (dict):
            a dictonary of arguments passed into the method
    Returns:
        A string used for the cache key lookup
    '''

    # get the method and self instance
    fxn, inst = args

    # parse the form request to extract any parameters
    reqargs = av.manual_parse(inst, request, use_params='galaxy')

    galid = reqargs.get('galid').replace('-', '_')
    release = inst._release.lower().replace('-', '')

    # create unique cache key name
    key = 'getpage_{0}_{1}'.format(release, galid)
    # append if logged in
    if inst.galaxy['loggedin']:
        key = '{0}_loggedin'.format(key)

    return key
Ejemplo n.º 3
0
def update_maps_cache(*args, **kwargs):
    ''' Function used to generate the route cache key

    Cache key when using cache.memoize or cache.cached decorator.
    memoize remembers input methods arguments; cached does not.

    Parameters:
        args (list):
            a list of the fx/method route call and object instance (self)
        kwargs (dict):
            a dictonary of arguments passed into the method
    Returns:
        A string used for the cache key lookup
    '''
    # get the method and self instance
    fxn, inst = args

    # parse the form request to extract any parameters
    reqargs = av.manual_parse(inst, request, use_params='galaxy', required=[
                              'plateifu', 'bintemp', 'params[]'], makemulti=True)

    plateifu = reqargs.get('plateifu').replace('-', '_')
    release = inst._release.lower().replace('-', '')

    bintemp = reqargs.get('bintemp', None, type=str).replace('-', '_')
    params = reqargs.getlist('params[]', type=str)
    params.sort()
    param_string = '_'.join(params).replace(':', '_')

    # create unique cache key name
    key = 'updatemaps_{0}_{1}_{2}_{3}'.format(release, plateifu, bintemp, param_string)

    return key
Ejemplo n.º 4
0
    def getSpaxel(self):
        args = av.manual_parse(self, request, use_params='galaxy', required=['plateifu', 'type'], makemulti=True)
        cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release}
        maptype = args.get('type', None)

        if maptype == 'optical':
            # for now, do this, but TODO - general processRequest to handle lists and not lists
            try:
                mousecoords = args.getlist('mousecoords[]', type=float)
            except Exception as e:
                mousecoords = None

            if mousecoords:
                pixshape = (args.get('imwidth', type=int), args.get('imheight', type=int))
                if (mousecoords[0] < 0 or mousecoords[0] > pixshape[0]) or (mousecoords[1] < 0 or mousecoords[1] > pixshape[1]):
                    output = {'specmsg': 'Error: requested pixel coords are outside the image range.', 'status': -1}
                    self.galaxy['error'] = output['specmsg']
                else:
                    cube = Cube(**cubeinputs)

                    # TODO - generalize image file sas_url to filesystem switch, maybe in sdss_access
                    infile = get_manga_image(cube, local=True)
                    current_session['imagefile'] = infile
                    #infile = os.path.join(os.getenv('MANGA_SPECTRO_REDUX'), args.get('image').split('redux/')[1])
                    arrcoords = convertImgCoords(mousecoords, infile, to_radec=True)

                    webspec, specmsg, badspots = getWebSpectrum(cube, arrcoords[0], arrcoords[1], byradec=True)
                    if not webspec:
                        self.galaxy['error'] = 'Error: {0}'.format(specmsg)
                        status = -1
                    else:
                        status = 1
                    msg = 'gettin some spaxel at RA/Dec {0}'.format(arrcoords)
                    output = {'message': msg, 'specmsg': specmsg, 'spectra': webspec, 'status': status, 'badspots': badspots}
            else:
                output = {'specmsg': 'Error getting mouse coords', 'status': -1}
                self.galaxy['error'] = output['specmsg']
        elif maptype == 'heatmap':
            # grab spectrum based on (x, y) coordinates
            x = args.get('x', None, type=int)
            y = args.get('y', None, type=int)
            if all([x, y]):
                cube = Cube(**cubeinputs)
                webspec, specmsg, badspots = getWebSpectrum(cube, x, y, xyorig='lower')
                msg = 'gettin some spaxel with (x={0}, y={1})'.format(x, y)
                if not webspec:
                    self.galaxy['error'] = 'Error: {0}'.format(specmsg)
                    status = -1
                else:
                    status = 1
                output = {'message': msg, 'specmsg': specmsg, 'spectra': webspec, 'status': status, 'badspots': badspots}
            else:
                output = {'specmsg': 'Error: X or Y not specified for map', 'status': -1}
                self.galaxy['error'] = output['specmsg']
        else:
            output = {'specmsg': 'Error: No maptype specified in request', 'status': -1}
            self.galaxy['error'] = output['specmsg']

        return jsonify(result=output)
Ejemplo n.º 5
0
    def webtable(self):
        ''' Do a query for Bootstrap Table interaction in Marvin web '''

        form = processRequest(request=request)
        args = av.manual_parse(self, request, use_params='query')

        # remove args
        __tmp__ = args.pop('release', None)
        __tmp__ = args.pop('searchfilter', None)
        limit = args.get('limit')
        offset = args.get('offset')
        if 'sort' in args:
            current_session['query_sort'] = args['sort']

        # set parameters
        searchvalue = current_session.get('searchvalue', None)
        returnparams = current_session.get('returnparams', None)

        # exit if no searchvalue is found
        if not searchvalue:
            output = jsonify({'errmsg': 'No searchvalue found', 'status': -1})
            return output

        # this is to fix the brokeness with sorting on a table column using remote names
        print('rp', returnparams, args)

        # do query
        try:
            q, res = doQuery(searchfilter=searchvalue,
                             release=self._release,
                             returnparams=returnparams,
                             **args)
        except Exception as e:
            errmsg = 'Error generating webtable: {0}'.format(e)
            output = jsonify({'status': -1, 'errmsg': errmsg})
            return output

        # get subset on a given page
        try:
            __results__ = res.getSubset(offset, limit=limit)
        except Exception as e:
            errmsg = 'Error getting table page: {0}'.format(e)
            output = jsonify({'status': -1, 'errmsg': errmsg})
            return output
        else:
            # get keys
            cols = res.columns.remote
            # create output
            rows = res.getDictOf(format_type='listdict')
            output = {
                'total': res.totalcount,
                'rows': rows,
                'columns': cols,
                'limit': limit,
                'offset': offset
            }
            output = jsonify(output)
            return output
Ejemplo n.º 6
0
    def initDynamic(self):
        ''' Route to run when the dynamic toggle is initialized
            This creates the web spectrum and dap heatmaps
        '''

        # get the form parameters
        args = av.manual_parse(self, request, use_params='galaxy', required='plateifu')

        # datamodel
        dm = datamodel[self._dapver]

        # turning toggle on
        current_session['toggleon'] = args.get('toggleon')

        # get the cube
        cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release}
        cube = Cube(**cubeinputs)
        output = {'specstatus': -1, 'mapstatus': -1}

        # get web spectrum
        webspec, specmsg = getWebSpectrum(cube, cube.ra, cube.dec, byradec=True)
        daplist = [p.full(web=True) for p in dm.properties]
        dapdefaults = dm.get_default_mapset()

        # build the uber map dictionary
        try:
            mapdict = buildMapDict(cube, dapdefaults, self._dapver)
            mapmsg = None
        except Exception as e:
            mapdict = [{'data': None, 'msg': 'Error', 'plotparams': None} for m in dapdefaults]
            mapmsg = 'Error getting maps: {0}'.format(e)
        else:
            output['mapstatus'] = 1

        if not webspec:
            output['error'] = 'Error: {0}'.format(specmsg)
        else:
            output['specstatus'] = 1

        output['image'] = get_manga_image(cube)
        output['spectra'] = webspec
        output['specmsg'] = specmsg
        output['maps'] = mapdict
        output['mapmsg'] = mapmsg
        output['dapmaps'] = daplist
        output['dapmapselect'] = dapdefaults

        output['dapbintemps'] = dm.get_bintemps(db_only=True)
        current_session['bintemp'] = '{0}-{1}'.format(dm.get_bintype(), dm.get_template())

        # try to jsonify the result
        try:
            jsonout = jsonify(result=output)
        except Exception as e:
            jsonout = jsonify(result={'specstatus': -1, 'mapstatus': -1, 'error': '{0}'.format(e)})

        return jsonout
Ejemplo n.º 7
0
Archivo: cube.py Proyecto: zpace/marvin
    def index(self):
        '''Returns general cube info

        .. :quickref: Cube; Get general cube info

        :query string release: the release of MaNGA
        :resjson int status: status of response. 1 if good, -1 if bad.
        :resjson string error: error message, null if None
        :resjson json inconfig: json of incoming configuration
        :resjson json utahconfig: json of outcoming configuration
        :resjson string traceback: traceback of an error, null if None
        :resjson string data: data message
        :resheader Content-Type: application/json
        :statuscode 200: no error
        :statuscode 422: invalid input parameters

        **Example request**:

        .. sourcecode:: http

           GET /marvin/api/cubes/ HTTP/1.1
           Host: api.sdss.org
           Accept: application/json, */*

        **Example response**:

        .. sourcecode:: http

           HTTP/1.1 200 OK
           Content-Type: application/json
           {
              "status": 1,
              "error": null,
              "inconfig": {"release": "MPL-5"},
              "utahconfig": {"release": "MPL-5", "mode": "local"},
              "traceback": null,
              "data": "this is a cube!"
           }

        '''
        av.manual_parse(self, request)
        self.results['status'] = 1
        self.results['data'] = 'this is a cube!'
        return jsonify(self.results)
Ejemplo n.º 8
0
    def selectmpl(self):
        ''' Global selection of the MPL/DR versions '''
        args = av.manual_parse(self, request, use_params='index')
        version = args.get('release', None)
        current_session['release'] = version
        drpver, dapver = config.lookUpVersions(release=version)
        current_session['drpver'] = drpver
        current_session['dapver'] = dapver
        out = {'status': 1, 'msg': 'Success', 'current_release': version,
               'current_drpver': drpver, 'current_dapver': dapver}

        return jsonify(result=out)
Ejemplo n.º 9
0
    def get(self, plateid):
        ''' Retrieve info for a given plate id '''

        # validate the input
        args = av.manual_parse(self, request)

        self.plate['plateid'] = args.get('plateid')
        pinputs = {
            'plate': plateid,
            'mode': 'local',
            'nocubes': True,
            'release': self._release
        }
        try:
            plate = mPlate(**pinputs)
        except MarvinError as e:
            self.plate['plate'] = None
            self.plate['drpver'] = self._drpver
            self.plate[
                'error'] = 'Could not grab Plate for id {0}: {1}'.format(
                    plateid, e)
        else:
            self.plate['plate'] = plate
            self.plate['drpver'] = plate._drpver
            tmpfile = plate._getFullPath(url=True)
            self.plate['sasurl'] = plate.platedir

        # Get images for plate
        imfiles = None
        try:
            imfiles = getImagesByPlate(plateid=plateid,
                                       as_url=True,
                                       mode='local',
                                       release=self._release)
        except MarvinError as e:
            self.plate[
                'error'] = 'Error: could not get images for plate {0}: {1}'.format(
                    plateid, e)
        else:
            # thumbs = [imfiles.pop(imfiles.index(t)) if 'thumb' in t else t for t in imfiles]
            # plateifu = ['-'.join(re.findall('\d{3,5}', im)) for im in imfiles]
            images = buildImageDict(imfiles)

        # if image grab failed, make placeholders
        if not imfiles:
            images = buildImageDict(imfiles, test=True, num=29)

        # Add images to dict
        self.plate['images'] = images

        return render_template('plate.html', **self.plate)
Ejemplo n.º 10
0
    def webtable(self):
        ''' Do a query for Bootstrap Table interaction in Marvin web '''

        form = processRequest(request=request)
        args = av.manual_parse(self, request, use_params='query')

        #{'sort': u'cube.mangaid', 'task': None, 'end': None, 'searchfilter': None,
        #'paramdisplay': None, 'start': None, 'rettype': None, 'limit': 10, 'offset': 30,
        #'release': u'MPL-4', 'params': None, 'order': u'asc'}

        # remove args
        __tmp__ = args.pop('release', None)
        __tmp__ = args.pop('searchfilter', None)
        limit = args.get('limit')
        offset = args.get('offset')

        # set parameters
        searchvalue = current_session.get('searchvalue', None)
        returnparams = current_session.get('returnparams', None)
        # limit = form.get('limit', 10, type=int)
        # offset = form.get('offset', None, type=int)
        # order = form.get('order', None, type=str)
        # sort = form.get('sort', None, type=str)
        # search = form.get('search', None, type=str)

        # exit if no searchvalue is found
        if not searchvalue:
            output = jsonify({
                'webtable_error': 'No searchvalue found',
                'status': -1
            })
            return output

        # do query
        q, res = doQuery(searchfilter=searchvalue,
                         release=self._release,
                         returnparams=returnparams,
                         **args)
        # q, res = doQuery(searchfilter=searchvalue, release=self._release,
        #                  limit=limit, order=order, sort=sort, returnparams=returnparams)
        # get subset on a given page
        __results__ = res.getSubset(offset, limit=limit)
        # get keys
        cols = res.mapColumnsToParams()
        # create output
        rows = res.getDictOf(format_type='listdict')
        output = {'total': res.totalcount, 'rows': rows, 'columns': cols}
        output = jsonify(output)
        return output
Ejemplo n.º 11
0
 def galidselect(self):
     ''' Route that handle the Navbar plate/galaxy id search form '''
     args = av.manual_parse(self, request, use_params='index', required='galid')
     galid = args.get('galid', None)
     if not galid:
         # if not galid return main page
         return redirect(url_for('index_page.Marvin:index'))
     else:
         idtype = parseIdentifier(galid)
     # check the idtype
     if idtype == 'plateifu' or idtype == 'mangaid':
         return redirect(url_for('galaxy_page.Galaxy:get', galid=galid))
     elif idtype == 'plate':
         return redirect(url_for('plate_page.Plate:get', plateid=galid))
     else:
         return redirect(url_for('index_page.Marvin:index'))
Ejemplo n.º 12
0
    def init_nsaplot(self):
        args = av.manual_parse(self, request, use_params='galaxy', required='plateifu')
        #self._drpver, self._dapver, self._release = parseSession()
        cubeinputs = {'plateifu': args.get('plateifu'), 'release': self._release}

        # get the default nsa choices
        nsachoices = self.galaxy.get('nsachoices', None)
        if not nsachoices:
            nsachoices = {'1': {'y': 'z', 'x': 'elpetro_logmass', 'xtitle': 'Stellar Mass',
                                'ytitle': 'Redshift', 'title': 'Redshift vs Stellar Mass'},
                          '2': {'y': 'elpetro_absmag_g_r', 'x': 'elpetro_absmag_i', 'xtitle': 'AbsMag_i',
                                'ytitle': 'Abs. g-r', 'title': 'Abs. g-r vs Abs. Mag i'}}

        # get cube (self.galaxy['cube'] does not work)
        try:
            cube = Cube(**cubeinputs)
        except Exception as e:
            cube = None

        # get some nsa params
        if not cube:
            output = {'nsamsg': 'No cube found', 'nsa': None, 'status': -1}
        else:
            # get the galaxy nsa parameters
            cols = self.galaxy.get('nsaplotcols')
            try:
                nsadict, nsacols = make_nsa_dict(cube.nsa)
                nsa = {args.get('plateifu'): nsadict}
            except Exception as e:
                output = {'nsamsg': e.message, 'status': -1, 'nsa': None}
            else:
                # get the sample nsa parameters
                try:
                    nsacache = 'nsa_{0}'.format(self._release.lower().replace('-', ''))
                    nsadict = get_nsa_dict(nsacache, self._drpver)
                except Exception as e:
                    output = {'nsamsg': 'Failed to retrieve sample NSA: {0}'.format(e), 'status': -1, 'nsa': nsa, 'nsachoices': nsachoices}
                else:
                    #nsadict = [(_db_row_to_dict(n[0], remove_columns=['pk', 'catalogue_pk']), n[1]) for n in allnsa]
                    nsasamp = {c: [n[0][c.split('_i')[0]][5] if 'absmag_i' in c or 'mtol_i' in c else n[0][c] for n in nsadict] for c in cols}
                    nsasamp['plateifu'] = [n[1] for n in nsadict]
                    nsasamp = remove_nans(nsasamp)
                    nsa['sample'] = nsasamp
                    output = {'nsamsg': None, 'status': 1, 'nsa': nsa, 'nsachoices': nsachoices, 'nsaplotcols': cols}
        return jsonify(result=output)
Ejemplo n.º 13
0
def get_spaxel_cache(*args, **kwargs):
    ''' Function used to generate the route cache key

    Cache key when using cache.memoize or cache.cached decorator.
    memoize remembers input methods arguments; cached does not.

    Parameters:
        args (list):
            a list of the fx/method route call and object instance (self)
        kwargs (dict):
            a dictonary of arguments passed into the method
    Returns:
        A string used for the cache key lookup
    '''
    # get the method and self instance
    fxn, inst = args

    # parse the form request to extract any parameters
    reqargs = av.manual_parse(inst, request, use_params='galaxy', required=['plateifu', 'type'], makemulti=True)

    maptype = reqargs.get('type', None)
    if maptype == 'optical':
        mousecoords = reqargs.getlist('mousecoords[]', type=float)
        infile = current_session['imagefile']
        radec = convertImgCoords(mousecoords, infile, to_radec=True)
        ww = WCS(current_session['wcs'])
        cube_shape = [current_session['cube_shape']] * 2
        y, x = convertCoords(radec, wcs=ww, shape=cube_shape,
                             mode='sky', xyorig='lower').T
        y = y[0]
        x = x[0]
    elif maptype == 'heatmap':
        x = reqargs.get('x', None, type=int)
        y = reqargs.get('y', None, type=int)

    plateifu = reqargs.get('plateifu').replace('-', '_')
    release = inst._release.lower().replace('-', '')

    # create unique cache key name
    key = 'getspaxel_{0}_{1}_{2}_{3}'.format(
        release, plateifu, x, y)

    return key
Ejemplo n.º 14
0
    def get(self, galid):
        ''' Retrieve info for a given cube '''

        # determine type of galid
        args = av.manual_parse(self, request, use_params='galaxy')
        self.galaxy['id'] = args['galid']
        idtype = parseIdentifier(galid)
        if idtype in ['plateifu', 'mangaid']:
            # set plateifu or mangaid
            self.galaxy['idtype'] = idtype
            galaxyid = {self.galaxy['idtype']: galid, 'release': self._release}

            # Get cube
            try:
                cube = Cube(**galaxyid)
            except MarvinError as e:
                self.galaxy['cube'] = None
                self.galaxy['error'] = 'MarvinError: {0}'.format(e)
                return render_template("galaxy.html", **self.galaxy)
            else:
                self.galaxy['cube'] = cube
                self.galaxy['daplink'] = getDapRedux(release=self._release)
                # get SAS url links to cube, rss, maps, image
                if Path:
                    sdss_path = Path()
                    self.galaxy['image'] = sdss_path.url('mangaimage', drpver=cube._drpver, plate=cube.plate, ifu=cube.ifu, dir3d=cube.dir3d)
                    cubelink = sdss_path.url('mangacube', drpver=cube._drpver, plate=cube.plate, ifu=cube.ifu)
                    rsslink = sdss_path.url('mangarss', drpver=cube._drpver, plate=cube.plate, ifu=cube.ifu)
                    maplink = getDefaultMapPath(release=self._release, plate=cube.plate, ifu=cube.ifu, daptype='SPX-GAU-MILESHC', mode='MAPS')
                    self.galaxy['links'] = {'cube': cubelink, 'rss': rsslink, 'map': maplink}
                else:
                    self.galaxy['image'] = cube.data.image

            # Get the initial spectrum
            if cube:
                daplist = get_dap_maplist(self._dapver, web=True)
                dapdefaults = get_default_mapset(self._dapver)
                self.galaxy['cube'] = cube
                self.galaxy['toggleon'] = current_session.get('toggleon', 'false')
                self.galaxy['cubehdr'] = cube.header
                self.galaxy['quality'] = ('DRP3QUAL', cube.quality_flag.mask, cube.quality_flag.labels)
                self.galaxy['mngtarget'] = {'bits': [it.mask for it in cube.target_flags if it.mask != 0],
                                            'labels': [it.labels for it in cube.target_flags if len(it.labels) > 0],
                                            'names': [''.join(('MNGTARG', it.name[-1])) for it in cube.target_flags if it.mask != 0]}

                # make the nsa dictionary
                hasnsa = cube.nsa is not None
                self.galaxy['hasnsa'] = hasnsa
                if hasnsa:
                    cols = self.galaxy.get('nsaplotcols')
                    nsadict, nsacols = make_nsa_dict(cube.nsa)
                    nsatmp = [nsacols.pop(nsacols.index(i)) for i in cols]
                    nsatmp.extend(nsacols)
                    self.galaxy['nsacols'] = nsatmp
                    self.galaxy['nsadict'] = nsadict

                self.galaxy['dapmaps'] = daplist
                self.galaxy['dapmapselect'] = dapdefaults
                dm = datamodel[self._dapver]
                self.galaxy['dapbintemps'] = dm.get_bintemps(db_only=True)
                current_session['bintemp'] = '{0}-{1}'.format(dm.get_bintype(), dm.get_template())
                # TODO - make this general - see also search.py for querystr
                self.galaxy['cubestr'] = ("<html><samp>from marvin.tools.cube import Cube<br>cube = \
                    Cube(plateifu='{0}')<br># access the header<br>cube.header<br># get NSA data<br>\
                    cube.nsa<br></samp></html>".format(cube.plateifu))

                self.galaxy['spaxelstr'] = ("<html><samp>from marvin.tools.cube import Cube<br>cube = \
                    Cube(plateifu='{0}')<br># get a spaxel<br>spaxel=cube[16, 16]<br>flux = \
                    spaxel.flux<br>wave = flux.wavelength<br>ivar = flux.ivar<br>mask = \
                    flux.mask<br>flux.plot()<br></samp></html>".format(cube.plateifu))

                self.galaxy['mapstr'] = ("<html><samp>from marvin.tools.maps import Maps<br>maps = \
                    Maps(plateifu='{0}')<br>print(maps)<br># get an emission \
                    line map<br>haflux = maps.emline_gflux_ha_6564<br>values = \
                    haflux.value<br>ivar = haflux.ivar<br>mask = haflux.mask<br>haflux.plot()<br>\
                    </samp></html>".format(cube.plateifu))
        else:
            self.galaxy['error'] = 'Error: Galaxy ID {0} must either be a Plate-IFU, or MaNGA-Id designation.'.format(galid)
            return render_template("galaxy.html", **self.galaxy)

        return render_template("galaxy.html", **self.galaxy)
Ejemplo n.º 15
0
    def index(self):

        # Attempt to retrieve search parameters
        form = processRequest(request=request)
        self.search['formparams'] = form
        self.search['latest_dr'] = self._release.lower() if 'DR' in self._release else config._get_latest_release(dr_only=True).lower()

        # set the search form and form validation
        searchform = self.mf.SearchForm(form)
        searchform.returnparams.choices = [(k.lower(), k) for k in query_params.list_params()]

        # Add the forms and parameters
        self.search['paramdata'] = query_params
        self.search['guideparams'] = [{'id': p.full, 'optgroup': group.name, 'type': 'double' if p.dtype == 'float' else p.dtype, 'validation': {'step': 'any'}} for group in query_params for p in group]
        self.search['searchform'] = searchform
        self.search['placeholder'] = getRandomQuery()
        self.search['returnurl'] = 'https://sdss-marvin.readthedocs.io/en/stable/datamodel/{0}.html#{0}query'.format(
            current_session['release'].lower().replace('-', ''))

        # If form parameters then try to do a search
        if form:
            self.search.update({'results': None, 'errmsg': None})

            args = av.manual_parse(self, request, use_params='search')
            # get form parameters
            searchvalue = form['searchbox']  # search filter input
            returnparams = form.getlist('returnparams', type=str)  # dropdown select
            self.search.update({'returnparams': returnparams})
            current_session.update({'searchvalue': searchvalue, 'returnparams': returnparams})

            # if main form passes validation then do search
            if searchform.validate():
                # try the query
                try:
                    q, res = doQuery(search_filter=searchvalue, release=self._release, return_params=returnparams)
                except (MarvinError, KeyError) as e:
                    self.search['errmsg'] = 'Could not perform query: {0}'.format(refine_error(e))
                except NotImplementedError as e:  # DAP queries disabled
                    self.search['errmsg'] = 'Could not perform query: {0}'.format(e)
                else:
                    self.search['filter'] = q.search_filter
                    self.search['count'] = res.totalcount
                    self.search['runtime'] = res.query_time.total_seconds()
                    if res.count > 0:
                        cols = res.columns.remote
                        rows = res.getDictOf(format_type='listdict')
                        output = {'total': res.totalcount, 'rows': rows, 'columns': cols, 'limit': None, 'offset': None}
                    else:
                        output = None
                    self.search['results'] = output
                    self.search['reslen'] = len(res.results)
                    if returnparams:
                        returnparams = [str(r) for r in returnparams]
                    rpstr = 'return_params={0} <br>'.format(returnparams) if returnparams else ''
                    qstr = ', return_params=returnparams' if returnparams else ''
                    self.search['querystring'] = ("<html><samp>from marvin.tools.query import Query<br>\
                        myfilter='{0}'<br> {1}\
                        q = Query(search_filter=myfilter{2})<br>\
                        r = q.run()<br></samp></html>".format(searchvalue, rpstr, qstr))

        return render_template('search.html', **self.search)
Ejemplo n.º 16
0
    def initDynamic(self):
        ''' Route to run when the dynamic toggle is initialized
            This creates the web spectrum and dap heatmaps
        '''

        # get the form parameters
        args = av.manual_parse(self,
                               request,
                               use_params='galaxy',
                               required='plateifu')
        #self._drpver, self._dapver, self._release = parseSession()

        # turning toggle on
        current_session['toggleon'] = args.get('toggleon')

        # get the cube
        cubeinputs = {
            'plateifu': args.get('plateifu'),
            'release': self._release
        }
        cube = Cube(**cubeinputs)
        output = {'specstatus': -1, 'mapstatus': -1}

        # get web spectrum
        webspec, specmsg = getWebSpectrum(cube,
                                          cube.ra,
                                          cube.dec,
                                          byradec=True)

        daplist = get_dap_maplist(self._dapver, web=True)
        dapdefaults = get_default_mapset(self._dapver)

        # build the uber map dictionary
        try:
            mapdict = buildMapDict(cube, dapdefaults, self._dapver)
            mapmsg = None
        except Exception as e:
            mapdict = [{
                'data': None,
                'msg': 'Error',
                'plotparams': None
            } for m in dapdefaults]
            mapmsg = 'Error getting maps: {0}'.format(e)
        else:
            output['mapstatus'] = 1

        if not webspec:
            output['error'] = 'Error: {0}'.format(specmsg)
        else:
            output['specstatus'] = 1

        sdss_path = Path()
        output['image'] = sdss_path.url('mangaimage',
                                        drpver=cube._drpver,
                                        plate=cube.plate,
                                        ifu=cube.ifu,
                                        dir3d=cube.dir3d)
        output['spectra'] = webspec
        output['specmsg'] = specmsg
        output['maps'] = mapdict
        output['mapmsg'] = mapmsg
        output['dapmaps'] = daplist
        output['dapbintemps'] = _get_bintemps(self._dapver)
        current_session['bintemp'] = '{0}-{1}'.format(
            _get_bintype(self._dapver), _get_template_kin(self._dapver))

        return jsonify(result=output)
Ejemplo n.º 17
0
    def initDynamic(self):
        ''' Route to run when the dynamic toggle is initialized
            This creates the web spectrum and dap heatmaps
        '''

        # get the form parameters
        args = av.manual_parse(self,
                               request,
                               use_params='galaxy',
                               required='plateifu')

        # datamodel
        dm = datamodel[self._dapver]

        # turning toggle on
        nowebsession = marvin.config._custom_config.get('no_web_session', None)
        if not nowebsession:
            current_session['toggleon'] = args.get('toggleon')

        # get the cube
        cubeinputs = {
            'plateifu': args.get('plateifu'),
            'release': self._release
        }
        cube = Cube(**cubeinputs)
        output = {'specstatus': -1, 'mapstatus': -1}

        # get web spectrum
        webspec, specmsg, badspots = getWebSpectrum(cube,
                                                    cube.ra,
                                                    cube.dec,
                                                    byradec=True)
        daplist = [p.full(web=True) for p in dm.properties]
        dapdefaults = dm.get_default_mapset()

        # select any DAP maps and bin-template from the session
        selected_bintemp = current_session.get('bintemp', None)
        selected_maps = current_session.get('selected_dapmaps', dapdefaults)

        # check for correct bintemp
        if selected_bintemp and selected_bintemp not in dm.get_bintemps(
                db_only=True):
            selected_bintemp = '{0}-{1}'.format(dm.get_bintype(),
                                                dm.get_template())

        # build the uber map dictionary
        try:
            mapdict = buildMapDict(cube,
                                   selected_maps,
                                   self._dapver,
                                   bintemp=selected_bintemp)
            mapmsg = None
        except Exception as e:
            mapdict = [{
                'data': None,
                'msg': 'Error',
                'plotparams': None
            } for m in dapdefaults]
            mapmsg = 'Error getting maps: {0}'.format(e)
        else:
            output['mapstatus'] = 1

        if not webspec:
            output['error'] = 'Error: {0}'.format(specmsg)
        else:
            output['specstatus'] = 1

        output['image'] = cube.getImage().url
        output['spectra'] = webspec
        output['specmsg'] = specmsg
        output['maps'] = mapdict
        output['mapmsg'] = mapmsg
        output['dapmaps'] = daplist
        output['dapmapselect'] = selected_maps
        output['daplines'] = dm.get_channels('emline_gflux', formatted=True)
        output['badspots'] = badspots

        output['dapbintemps'] = dm.get_bintemps(db_only=True)
        if 'bintemp' not in current_session:
            current_session['bintemp'] = '{0}-{1}'.format(
                dm.get_bintype(), dm.get_template())

        # try to jsonify the result
        try:
            jsonout = jsonify(result=output)
        except Exception as e:
            jsonout = jsonify(result={
                'specstatus': -1,
                'mapstatus': -1,
                'error': '{0}'.format(e)
            })

        return jsonout
Ejemplo n.º 18
0
    def get(self, galid):
        ''' Retrieve info for a given cube '''

        # determine type of galid
        args = av.manual_parse(self, request, use_params='galaxy')
        self.galaxy['id'] = args['galid']
        self.galaxy['latest_dr'] = self._release.lower(
        ) if 'DR' in self._release else marvin.config._get_latest_release(
            dr_only=True).lower()
        idtype = parseIdentifier(galid)
        if idtype in ['plateifu', 'mangaid']:
            # set plateifu or mangaid
            self.galaxy['idtype'] = idtype
            galaxyid = {self.galaxy['idtype']: galid, 'release': self._release}

            # Get cube
            try:
                cube = Cube(**galaxyid)
            except MarvinError as e:
                self.galaxy['cube'] = None
                errmsg = 'MarvinError: {0}'.format(e)

                # check target status and fine-tune the error message
                if idtype == 'mangaid':
                    status = target_status(galid, drpver=self._drpver)
                    if status == 'not yet observed':
                        errmsg = '{0} is a valid target but has not yet been observed'.format(
                            galid)
                    elif status == 'not valid target':
                        errmsg = '{0} is not valid MaNGA target.  Check your syntax'.format(
                            galid)

                self.galaxy['error'] = errmsg
                return render_template("galaxy.html", **self.galaxy)
            else:
                dm = datamodel[self._dapver]
                self.galaxy['cube'] = cube
                self.galaxy['daplink'] = getDapRedux(release=self._release)
                # get SAS url links to cube, rss, maps, image
                if Path:
                    is_public = 'DR' in self._release
                    path_release = self._release.lower() if is_public else None
                    sdss_path = Path(public=is_public, release=path_release)
                    self.galaxy['image'] = cube.getImage().url
                    cubelink = sdss_path.url('mangacube',
                                             drpver=cube._drpver,
                                             plate=cube.plate,
                                             ifu=cube.ifu)
                    rsslink = sdss_path.url('mangarss',
                                            drpver=cube._drpver,
                                            plate=cube.plate,
                                            ifu=cube.ifu)
                    daptype = "{0}-{1}".format(dm.default_bintype,
                                               dm.default_template)
                    maplink = getDefaultMapPath(release=self._release,
                                                plate=cube.plate,
                                                ifu=cube.ifu,
                                                daptype=daptype,
                                                mode='MAPS')
                    mclink = getDefaultMapPath(release=self._release,
                                               plate=cube.plate,
                                               ifu=cube.ifu,
                                               daptype=daptype,
                                               mode='LOGCUBE')
                    self.galaxy['links'] = {
                        'cube': cubelink,
                        'rss': rsslink,
                        'map': maplink,
                        'mc': mclink
                    }
                else:
                    self.galaxy['image'] = cube.data.image

            # Get the initial spectrum
            if cube:
                dm = datamodel[self._dapver]
                daplist = [p.full(web=True) for p in dm.properties]
                dapdefaults = dm.get_default_mapset()
                self.galaxy['cube'] = cube
                self.galaxy['toggleon'] = current_session.get(
                    'toggleon', 'false')
                self.galaxy['cubehdr'] = cube.header
                self.galaxy['quality'] = ('DRP3QUAL', cube.quality_flag.mask,
                                          cube.quality_flag.labels)
                self.galaxy['mngtarget'] = {
                    'bits':
                    [it.mask for it in cube.target_flags if it.mask != 0],
                    'labels': [
                        it.labels for it in cube.target_flags
                        if len(it.labels) > 0
                    ],
                    'names': [
                        ''.join(('MNGTARG', it.name[-1]))
                        for it in cube.target_flags if it.mask != 0
                    ]
                }

                # make the nsa dictionary
                hasnsa = cube.nsa is not None
                self.galaxy['hasnsa'] = hasnsa
                if hasnsa:
                    cols = self.galaxy.get('nsaplotcols')
                    nsadict, nsacols = make_nsa_dict(cube.nsa)
                    nsatmp = [
                        nsacols.pop(nsacols.index(i)) for i in cols
                        if i in nsacols
                    ]
                    nsatmp.extend(nsacols)
                    self.galaxy['nsacols'] = nsatmp
                    self.galaxy['nsadict'] = nsadict

                self.galaxy['dapmaps'] = daplist
                self.galaxy['dapmapselect'] = current_session.get(
                    'selected_dapmaps', dapdefaults)
                dm = datamodel[self._dapver]
                self.galaxy['dapbintemps'] = dm.get_bintemps(db_only=True)
                if 'bintemp' not in current_session or current_session[
                        'bintemp'] not in self.galaxy['dapbintemps']:
                    current_session['bintemp'] = '{0}-{1}'.format(
                        dm.get_bintype(), dm.get_template())

                # get default map quality
                maps = cube.getMaps(
                    plateifu=cube.plateifu,
                    mode='local',
                    bintype=current_session['bintemp'].split('-')[0])
                mapqual = ('DAPQUAL', maps.quality_flag.mask,
                           maps.quality_flag.labels)
                self.galaxy['mapquality'] = mapqual

                # TODO - make this general - see also search.py for querystr
                self.galaxy['cubestr'] = (
                    "<html><samp>from marvin.tools.cube import Cube<br>cube = \
                    Cube(plateifu='{0}')<br># access the header<br>cube.header<br># get NSA data<br>\
                    cube.nsa<br></samp></html>".format(cube.plateifu))

                self.galaxy['spaxelstr'] = (
                    "<html><samp>from marvin.tools.cube import Cube<br>cube = \
                    Cube(plateifu='{0}')<br># get a spaxel by slicing cube[i,j]<br>spaxel=cube[16, 16]<br>flux = \
                    spaxel.flux<br>wave = flux.wavelength<br>ivar = flux.ivar<br>mask = \
                    flux.mask<br>flux.plot()<br></samp></html>".format(
                        cube.plateifu))

                self.galaxy['mapstr'] = (
                    "<html><samp>from marvin.tools.maps import Maps<br>maps = \
                    Maps(plateifu='{0}')<br>print(maps)<br># get an emission \
                    line map<br>haflux = maps.emline_gflux_ha_6564<br>values = \
                    haflux.value<br>ivar = haflux.ivar<br>mask = haflux.mask<br>haflux.plot()<br>\
                    </samp></html>".format(cube.plateifu))
        else:
            self.galaxy[
                'error'] = 'Error: Galaxy ID {0} must either be a Plate-IFU, or MaNGA-Id designation.'.format(
                    galid)
            return render_template("galaxy.html", **self.galaxy)

        return render_template("galaxy.html", **self.galaxy)
Ejemplo n.º 19
0
    def index(self):

        # Attempt to retrieve search parameters
        form = processRequest(request=request)
        self.search['formparams'] = form

        print('search form', form, type(form))
        # set the search form and form validation
        searchform = self.mf.SearchForm(form)
        q = Query(release=self._release)
        # allparams = q.get_available_params()
        bestparams = q.get_best_params()
        searchform.returnparams.choices = [(k.lower(), k) for k in bestparams]
        searchform.parambox.validators = [
            all_in(bestparams), validators.Optional()
        ]

        # Add the forms
        self.search['searchform'] = searchform
        self.search['placeholder'] = getRandomQuery()

        #from flask import abort
        #abort(500)

        # If form parameters then try to do a search
        if form:
            self.search.update({'results': None, 'errmsg': None})

            args = av.manual_parse(self, request, use_params='search')
            print('search args', args)

            # get form parameters
            searchvalue = form['searchbox']  # search filter input
            returnparams = form.getlist('returnparams',
                                        type=str)  # dropdown select
            parambox = form.get('parambox', None,
                                type=str)  # from autocomplete
            if parambox:
                parms = parambox.split(',')
                parms = parms if parms[-1].strip() else parms[:-1]
                parambox = parms if parambox else None
            # Select the one that is not none
            returnparams = returnparams if returnparams and not parambox else \
                parambox if parambox and not returnparams else \
                list(set(returnparams) | set(parambox)) if returnparams and parambox else None
            print('return params', returnparams)
            current_session.update({
                'searchvalue': searchvalue,
                'returnparams': returnparams
            })

            # if main form passes validation then do search
            if searchform.validate():
                # try the query
                try:
                    q, res = doQuery(searchfilter=searchvalue,
                                     release=self._release,
                                     returnparams=returnparams)
                except MarvinError as e:
                    self.search[
                        'errmsg'] = 'Could not perform query: {0}'.format(e)
                else:
                    self.search['filter'] = q.strfilter
                    self.search['count'] = res.totalcount
                    self.search['runtime'] = res.query_runtime.total_seconds()
                    if res.count > 0:
                        cols = res.mapColumnsToParams()
                        rows = res.getDictOf(format_type='listdict')
                        output = {
                            'total': res.totalcount,
                            'rows': rows,
                            'columns': cols
                        }
                    else:
                        output = None
                    self.search['results'] = output
                    self.search['reslen'] = len(res.results)
                    if returnparams:
                        returnparams = [str(r) for r in returnparams]
                    rpstr = 'returnparams={0} <br>'.format(
                        returnparams) if returnparams else ''
                    qstr = ', returnparams=returnparams' if returnparams else ''
                    self.search['querystring'] = (
                        "<html><samp>from marvin import \
                        config<br>from marvin.tools.query import Query<br>config.mode='remote'<br>\
                        filter='{0}'<br> {1}\
                        q = Query(searchfilter=filter{2})<br>\
                        r = q.run()<br></samp></html>".format(
                            searchvalue, rpstr, qstr))

        return render_template('search.html', **self.search)