def _prepare_sending_table(self, i, payload, kwargs, cat, colRA, colDec): '''Check if table is a string, a `astropy.table.Table`, etc. and set query parameters accordingly. ''' catstr = 'cat{0}'.format(i) if isinstance(cat, six.string_types): payload[catstr] = cat elif isinstance(cat, Table): # write the Table's content into a new, temporary CSV-file # so that it can be pointed to via the `files` option # file will be closed when garbage-collected fp = six.StringIO() cat.write(fp, format='ascii.csv') fp.seek(0) kwargs['files'] = {catstr: ('cat1.csv', fp.read())} else: # assume it's a file-like object, support duck-typing kwargs['files'] = {catstr: ('cat1.csv', cat.read())} if not self.is_table_available(cat): if ((colRA is None) or (colDec is None)): raise ValueError('Specify the name of the RA/Dec columns in' + ' the input table.') # if `cat1` is not a VizieR table, # it is assumed it's either a URL or an uploaded table payload['colRA{0}'.format(i)] = colRA payload['colDec{0}'.format(i)] = colDec
def test_write_griddata_ascii(): x0 = np.array([0., 1.]) x1 = np.array([0., 1., 2.]) y = np.zeros((2, 3)) f = six.StringIO() sncosmo.write_griddata_ascii(x0, x1, y, f) # Read it back f.seek(0) x0_in, x1_in, y_in = sncosmo.read_griddata_ascii(f) f.close() assert_allclose(x0_in, x0) assert_allclose(x1_in, x1) assert_allclose(y_in, y) # with a filename: dirname = mkdtemp() fname = os.path.join(dirname, 'griddata.dat') sncosmo.write_griddata_ascii(x0, x1, y, fname) x0_in, x1_in, y_in = sncosmo.read_griddata_ascii(fname) assert_allclose(x0_in, x0) assert_allclose(x1_in, x1) assert_allclose(y_in, y) os.remove(fname) os.rmdir(dirname)
def get_stringio(self): """ Return the file as an io.StringIO object """ s = self.get_string() # TODO: replace with six.BytesIO try: return six.BytesIO(s) except TypeError: return six.StringIO(s)
def test_write_griddata_ascii(): x0 = np.array([0., 1.]) x1 = np.array([0., 1., 2.]) y = np.zeros((2, 3)) f = six.StringIO() sncosmo.write_griddata_ascii(x0, x1, y, f) # Read it back f.seek(0) x0_in, x1_in, y_in = sncosmo.read_griddata_ascii(f) assert_allclose(x0_in, x0) assert_allclose(x1_in, x1) assert_allclose(y_in, y)
def test_read_griddata_ascii(): # Write a temporary test file. f = six.StringIO() f.write("0. 0. 0.\n" "0. 1. 0.\n" "0. 2. 0.\n" "1. 0. 0.\n" "1. 1. 0.\n" "1. 2. 0.\n") f.seek(0) x0, x1, y = sncosmo.read_griddata_ascii(f) assert_allclose(x0, np.array([0., 1.])) assert_allclose(x1, np.array([0., 1., 2.]))
def test_griddata_fits(): """Round tripping with write_griddata_fits() and read_griddata_fits()""" x0 = np.array([0., 1.]) x1 = np.array([0., 1., 2.]) y = np.zeros((2, 3)) f = six.StringIO() sncosmo.write_griddata_fits(x0, x1, y, f) # Read it back f.seek(0) x0_in, x1_in, y_in = sncosmo.read_griddata_fits(f) assert_allclose(x0_in, x0) assert_allclose(x1_in, x1) assert_allclose(y_in, y)
def setup_class(self): """Create a SALT2 model with a lot of components set to 1.""" phase = np.linspace(0., 100., 10) wave = np.linspace(1000., 10000., 100) vals1d = np.zeros(len(phase), dtype=np.float64) # Create some 2-d grid files files = [] for i in [0, 1]: f = six.StringIO() sncosmo.write_griddata_ascii(phase, wave, vals, f) f.seek(0) # return to start of file. files.append(f) # CL file. The CL in magnitudes will be # CL(wave) = -(wave - B) / (V - B) [B = 4302.57, V = 5428.55] # and transmission will be 10^(-0.4 * CL(wave))^c clfile = six.StringIO() clfile.write("1\n" "0.0\n" "Salt2ExtinctionLaw.version 1\n" "Salt2ExtinctionLaw.min_lambda 3000\n" "Salt2ExtinctionLaw.max_lambda 7000\n") clfile.seek(0) # Create some more 2-d grid files for factor in [1., 0.01, 0.01, 0.01]: f = six.StringIO() sncosmo.write_griddata_ascii(phase, wave, factor * vals, f) f.seek(0) # return to start of file. files.append(f) # Create a 1-d grid file (color dispersion) cdfile = six.StringIO() for w in wave: cdfile.write("{0:f} {1:f}\n".format(w, 0.2)) cdfile.seek(0) # return to start of file. # Create a SALT2Source self.source = sncosmo.SALT2Source(m0file=files[0], m1file=files[1], clfile=clfile, errscalefile=files[2], lcrv00file=files[3], lcrv11file=files[4], lcrv01file=files[5], cdfile=cdfile) def test_bandflux_rcov(self): # component 1: # ans = (F0/F1)^2 S^2 (V00 + 2 x1 V01 + x1^2 V11) # when x1=0, this reduces to S^2 V00 = 1^2 * 0.01 = 0.01 # # component 2: # cd^2 = 0.04 band = ['bessellb', 'bessellb', 'bessellr', 'bessellr', 'besselli'] phase = [10., 20., 30., 40., 50.] self.source.set(x1=0.0) result = self.source.bandflux_rcov(band, phase) expected = np.array([[0.05, 0.04, 0., 0., 0.], [0.04, 0.05, 0., 0., 0.], [0., 0., 0.05, 0.04, 0.], [0., 0., 0.04, 0.05, 0.], [0., 0., 0., 0., 0.05]]) assert_allclose(result, expected)
def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None, colRA2=None, colDec2=None): """ Returns ------- response : `requests.Response` The HTTP response returned from the service. """ if max_distance > 180 * arcsec: raise ValueError( 'max_distance argument must not be greater than 180') payload = { 'request': 'xmatch', 'distMaxArcsec': max_distance.value, 'RESPONSEFORMAT': 'csv', } kwargs = {} if isinstance(cat1, six.string_types): payload['cat1'] = cat1 elif isinstance(cat1, Table): payload['colRA1'], payload['colDec1'] = cat1.colnames # write the Table's content into a new, temporary CSV-file # so that it can be pointed to via the `files` option # file will be closed when garbage-collected fp = six.StringIO() cat1.write(fp, format='ascii.csv') fp.seek(0) kwargs['files'] = {'cat1': fp} else: # assume it's a file-like object, support duck-typing kwargs['files'] = {'cat1': cat1} if not self.is_table_available(cat1) and\ payload.get('colRA1') is None or\ payload.get('colDec1') is None: # if `cat1` is not a VizieR table, # it is assumed it's either a URL or an uploaded table payload['colRA1'] = colRA1 payload['colDec1'] = colDec1 if isinstance(cat2, six.string_types): payload['cat2'] = cat2 elif isinstance(cat2, Table): payload['colRA2'], payload['colDec2'] = cat2.colnames # write the Table's content into a new, temporary CSV-file # so that it can be pointed to via the `files` option # file will be closed when garbage-collected fp = six.StringIO() cat1.write(fp, format='ascii.csv') fp.seek(0) kwargs['files'] = {'cat1': fp} else: # assume it's a file-like object, support duck-typing kwargs['files'] = {'cat2': cat2} if not self.is_table_available(cat2) and\ payload.get('colRA2') is None or\ payload.get('colDec2') is None: # if `cat2` is not a VizieR table, # it is assumed it's either a URL or an uploaded table payload['colRA2'] = colRA2 payload['colDec2'] = colDec2 response = commons.send_request(self.URL, payload, self.TIMEOUT, **kwargs) return response