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
def get_buildtree(vertices, indices, normals): doc = XMLNode("buildspace") gen = doc.generator gen.config.attrs = {"a": 10} gen.attrs = (("id", "1"), ("seed", "1"), ("name", "generator"), ("location", "None"), ("bbox", "None")) gen_geoms = gen.geoms geom = gen_geoms.geom geom.attrs = ( ("shape", "geom"), ("bbox", "None"), ("area", "None"), ("volume", "None"), ("location", "None"), ("visible", 1), ) geom.mesh = "%s;\n%s;\n%s;" % (repr(vertices), repr(normals), repr(indices)) sgen = gen.sub_generators s = stringio() doc.serialize(s) build_tree = s.getvalue() return build_tree
def _load_image_from_api(self): ''' Load an image from a remote location ''' filepath = self._getFullPath() response = requests.get(self.url) if not response.ok: raise MarvinError( 'Error: remote filepath {0} does not exist'.format(filepath)) else: fileobj = stringio(response.content) self.data = self._open_image(fileobj, filepath=self.url)
def test_load_file_object(self): """compdesc.load returns a component with packages""" handle = stringio('''<?xml version="1.0" encoding="utf-8"?> <component> <contents> <deb ref="sha-1:aaa"/> </contents> </component> ''') desc = ComponentDescriptor('a.xml', handle) cache = ShamCache() cache.add(MockPackage('a', '1', deb, 'sha-1:aaa')) component = desc.load(cache) assert isinstance(component, Component) self.assert_equal('a.xml', desc.filename) self.assert_equal(1, len(list(component.iter_packages()))) self.assert_equal(1, len(list(component.iter_direct_packages()))) self.assert_equal(['sha-1:aaa'], [ p.blob_id for p in component.iter_packages() ]) self.assert_equal(0, len(list(component.iter_direct_components()))) self.assert_equal(0, len(list(component.iter_components())))
def test_load_file_object(self): """compdesc.load returns a component with packages""" handle = stringio('''<?xml version="1.0" encoding="utf-8"?> <component> <contents> <deb ref="sha-1:aaa"/> </contents> </component> ''') desc = ComponentDescriptor('a.xml', handle) cache = ShamCache() cache.add(MockPackage('a', '1', deb, 'sha-1:aaa')) component = desc.load(cache) assert isinstance(component, Component) self.assert_equal('a.xml', desc.filename) self.assert_equal(1, len(list(component.iter_packages()))) self.assert_equal(1, len(list(component.iter_direct_packages()))) self.assert_equal(['sha-1:aaa'], [p.blob_id for p in component.iter_packages()]) self.assert_equal(0, len(list(component.iter_direct_components()))) self.assert_equal(0, len(list(component.iter_components())))
def showImage(path=None, plateifu=None, release=None, return_image=True, show_image=True, mode=None): ''' Crudely and coarsely show a galaxy image that has been downloaded .. deprecated:: 2.3.0 Use :class:`marvin.tools.image.Image` or :func:`show_image` instead. This utility function quickly allows you to display a PNG IFU image that is located in your local SAS or from the remote Utah SAS. A PIL Image object is also returned which allows you to manipulate the image after the fact. See :ref:`marvin-image-show` for example usage. Either the path or plateifu keyword is required. Parameters: path (str): A string filepath to a local IFU image plateifu (str): A plateifu designation used to look for the IFU image in your local SAS return_image (bool): If ``True``, returns the PIL Image object for image manipulation. Default is ``True``. show_image (bool): If ``True``, shows the requested image that is opened internally mode ({'local', 'remote', 'auto'}): The load mode to use. See :doc:`Mode secision tree</mode_decision>`. release (str): The release version of the images to return Returns: image (PIL Image or None): If return_image is set, returns a PIL Image object to allow for image manipulation, else returns None. ''' warnings.warn('showImage is deprecated as of Marvin 2.3.0. ' 'Please use marvin.tools.image.Image instead.', MarvinDeprecationWarning) # check inputs release = release if release else marvin.config.release drpver, __ = marvin.config.lookUpVersions(release=release) args = [path, plateifu] assert any(args), 'A filepath or plateifu must be specified!' imgname = 'mangaimagenew' if check_versions(drpver, 'v2_5_3') else 'mangaimage' # check path if path: if type(path) == list and len(path) > 1: raise MarvinError('showImage currently only works on a single input at a time') filepath = path[0] if type(path) == list else path # Deal with the mode if mode == 'local' and 'https://data.sdss.org' in filepath: raise MarvinError('Remote url path not allowed in local mode') elif mode == 'remote' and 'https://data.sdss.org' not in filepath: raise MarvinError('Local path not allowed in remote mode') elif mode == 'auto': if 'https://data.sdss.org' in filepath: mode = 'remote' else: mode = 'local' def _do_local_plateifu(name): full = http_access.full(name, plate=plateid, drpver=drpver, ifu=ifu, dir3d='*') filepath = http_access.expand('', full=full) if filepath: filepath = filepath[0] return filepath else: raise MarvinError('Error: No files found locally to match plateifu {0}. ' 'Use one of the image utility functions to download them first or ' 'switch to remote mode'.format(plateifu)) def _do_remote_plateifu(name): filepath = http_access.url(name, plate=plateid, drpver=drpver, ifu=ifu, dir3d='stack') return filepath # check plateifu if plateifu: plateid, ifu = plateifu.split('-') http_access = HttpAccess(verbose=False) if mode == 'local': filepath = _do_local_plateifu(imgname) elif mode == 'remote': filepath = _do_remote_plateifu(imgname) elif mode == 'auto': try: filepath = _do_local_plateifu(imgname) mode = 'local' except MarvinError: marvin.log.debug('Local mode failed. Trying remote.') filepath = _do_remote_plateifu(imgname) mode = 'remote' # check if filepath exists either locally or remotely if mode == 'local': if not filepath or not os.path.isfile(filepath): raise MarvinError('Error: local filepath {0} does not exist. '.format(filepath)) else: fileobj = filepath elif mode == 'remote': r = requests.get(filepath) if not r.ok: raise MarvinError('Error: remote filepath {0} does not exist'.format(filepath)) else: fileobj = stringio(r.content) # Open the image try: image = PIL.Image.open(fileobj) except IOError as e: print('Error: cannot open image') image = None else: image.filename = filepath if image and show_image: # show the image image.show() # return the PIL Image object if return_image: return image else: return None