def tofits(filename, data, hdr=None, clobber=False): """simple pyfits wrapper to make saving fits files easier.""" hdu = PrimaryHDU(data) if not (hdr is None): hdu.header += hdr hdulist = HDUList([hdu]) hdulist.writeto(filename, overwrite=clobber, output_verify='ignore')
def trim_throughput(indir, outdir): '''downsample throughput files''' assert os.path.basename(indir) == 'throughput' if not os.path.exists(outdir): os.makedirs(outdir) for targettype in ('elg', 'lrg', 'perfect', 'qso', 'sky', 'star'): filename = 'fiberloss-{}.dat'.format(targettype) shutil.copy(os.path.join(indir, filename), os.path.join(outdir, filename)) for filename in ['thru-b.fits', 'thru-r.fits', 'thru-z.fits']: fx = fits.open(indir + '/' + filename) hdus = HDUList() hdus.append(fx[0]) hdus.append(BinTableHDU(fx[1].data[::20], header=fx[1].header)) hdus.append(BinTableHDU(fx[2].data[::20], header=fx[2].header)) hdus.writeto(outdir + '/' + filename) fx.close() for filename in [ 'DESI-0347_blur.ecsv', 'DESI-0347_offset.ecsv', 'DESI-0347_random_offset_1.fits' ]: shutil.copy(os.path.join(indir, filename), os.path.join(outdir, filename))
def export_visibility_to_fits(vis: Visibility, fits_file: str): hdu = HDUList([ PrimaryHDU(), configuration_to_hdu(vis.configuration), visibility_to_hdu(vis) ]) with open(fits_file, "w") as f: hdu.writeto(f, checksum=True)
def trim_quickpsf(indir, outdir, filename): assert os.path.abspath(indir) != os.path.abspath(outdir) infile = os.path.join(indir, filename) outfile = os.path.join(outdir, filename) fx = fits.open(infile) hdus = HDUList() hdus.append(fx[0]) for i in [1,2,3]: d = fx[i].data hdus.append(BinTableHDU(d[::10], header=fx[i].header)) hdus.writeto(outfile, clobber=True) fx.close()
def trim_psf(indir, outdir, filename): assert os.path.abspath(indir) != os.path.abspath(outdir) infile = os.path.join(indir, filename) outfile = os.path.join(outdir, filename) fx = fits.open(infile) hdus = HDUList() #- HDU 0 XCOEFF - data unchanged but update keywords for less samples xcoeff = fx[0].data hdr = fx[0].header hdr['NWAVE'] = 3 #- down from 11 hdr['CRPIX1'] = 23 #- 23=45//2+1, down from 113=225//2+1 hdr['CRPIX1'] = 23 hdr['CDELT1'] = 0.005 #- 5mm instead of 1mm hdr['CDELT2'] = 0.005 #- 5mm instead of 1mm hdr['PIXSIZE'] = 0.005 #- 5mm instead of 1mm hdus.append(PrimaryHDU(xcoeff, header=hdr)) hdus.append(fx['YCOEFF']) #- subsample spots inspots = fx['SPOTS'].data spots = np.zeros((3,3,45,45)) spots[0,0] = rebin_image(inspots[0,0], 5) spots[1,0] = rebin_image(inspots[5,0], 5) spots[2,0] = rebin_image(inspots[10,0], 5) spots[0,1] = rebin_image(inspots[0,5], 5) spots[1,1] = rebin_image(inspots[5,5], 5) spots[2,1] = rebin_image(inspots[10,5], 5) spots[0,2] = rebin_image(inspots[0,10], 5) spots[1,2] = rebin_image(inspots[5,10], 5) spots[2,2] = rebin_image(inspots[10,10], 5) hdus.append(ImageHDU(spots, header=fx['SPOTS'].header)) #- subsample spots x,y locations dx = fx['SPOTX'].data hdus.append(ImageHDU(dx[::5, ::5], header=fx['SPOTX'].header)) dy = fx['SPOTY'].data hdus.append(ImageHDU(dy[::5, ::5], header=fx['SPOTY'].header)) #- Fiberpos unchanged hdus.append(fx['FIBERPOS']) #- Subsample SPOTPOS and SPOTWAVE d = fx['SPOTPOS'].data hdus.append(ImageHDU(d[::5], header=fx['SPOTPOS'].header)) d = fx['SPOTWAVE'].data hdus.append(ImageHDU(d[::5], header=fx['SPOTWAVE'].header)) hdus.writeto(outfile, clobber=True) fx.close()
def save_fits(self, folder: str) -> str: data = self.data.copy() assert data.dtype == np.float32 and data.max() <= 1.0 and data.min() >= 0.0, f"{data.dtype} {data.max()} {data.min()}" hdu = PrimaryHDU( data=self.data, header=self.fits_header, ) l = HDUList([hdu]) path = join(folder, f"{self.key}.fits") l.writeto(path, overwrite=True) return path
async def write_fits(self, filename: str, hdulist: fits.HDUList, *args: Any, **kwargs: Any) -> None: """Convenience function for writing an Image to a FITS file. Args: filename: Name of file to write. hdulist: hdu list to write. """ # open file async with self.open_file(filename, "wb") as f: with io.BytesIO() as bio: hdulist.writeto(bio, *args, **kwargs) await f.write(bio.getvalue())
def test_ignore_hdus_report(self, capsys): a = np.arange(100).reshape(10, 10) b = a.copy() + 1 ha = Header([('A', 1), ('B', 2), ('C', 3)]) phdu_a = PrimaryHDU(header=ha) phdu_b = PrimaryHDU(header=ha) ihdu_a = ImageHDU(data=a, name='SCI') ihdu_b = ImageHDU(data=b, name='SCI') hdulist_a = HDUList([phdu_a, ihdu_a]) hdulist_b = HDUList([phdu_b, ihdu_b]) tmp_a = self.temp('testa.fits') tmp_b = self.temp('testb.fits') hdulist_a.writeto(tmp_a) hdulist_b.writeto(tmp_b) numdiff = fitsdiff.main([tmp_a, tmp_b, "-u", "SCI"]) assert numdiff == 0 out, err = capsys.readouterr() assert "testa.fits" in out assert "testb.fits" in out
class TestMultipleHDU: def setup_class(self): self.data1 = np.array(list( zip([1, 2, 3, 4], ['a', 'b', 'c', 'd'], [2.3, 4.5, 6.7, 8.9])), dtype=[('a', int), ('b', 'U1'), ('c', float)]) self.data2 = np.array(list( zip([1.4, 2.3, 3.2, 4.7], [2.3, 4.5, 6.7, 8.9])), dtype=[('p', float), ('q', float)]) self.data3 = np.array(list(zip([1, 2, 3, 4], [2.3, 4.5, 6.7, 8.9])), dtype=[('A', int), ('B', float)]) hdu0 = PrimaryHDU() hdu1 = BinTableHDU(self.data1, name='first') hdu2 = BinTableHDU(self.data2, name='second') hdu3 = ImageHDU(np.ones((3, 3)), name='third') hdu4 = BinTableHDU(self.data3) self.hdus = HDUList([hdu0, hdu1, hdu2, hdu3, hdu4]) self.hdusb = HDUList([hdu0, hdu3, hdu2, hdu1]) self.hdus3 = HDUList([hdu0, hdu3, hdu2]) self.hdus2 = HDUList([hdu0, hdu1, hdu3]) self.hdus1 = HDUList([hdu0, hdu1]) def teardown_class(self): del self.hdus def setup_method(self, method): warnings.filterwarnings('always') def test_read(self, tmpdir): filename = str(tmpdir.join('test_read.fits')) self.hdus.writeto(filename) with pytest.warns(AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=1\)"): t = Table.read(filename) assert equal_data(t, self.data1) filename = str(tmpdir.join('test_read_2.fits')) self.hdusb.writeto(filename) with pytest.warns(AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=2\)"): t3 = Table.read(filename) assert equal_data(t3, self.data2) def test_read_with_hdu_0(self, tmpdir): filename = str(tmpdir.join('test_read_with_hdu_0.fits')) self.hdus.writeto(filename) with pytest.raises(ValueError) as exc: Table.read(filename, hdu=0) assert exc.value.args[0] == 'No table found in hdu=0' @pytest.mark.parametrize('hdu', [1, 'first']) def test_read_with_hdu_1(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_1.fits')) self.hdus.writeto(filename) t = Table.read(filename, hdu=hdu) assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [2, 'second']) def test_read_with_hdu_2(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_2.fits')) self.hdus.writeto(filename) t = Table.read(filename, hdu=hdu) assert equal_data(t, self.data2) @pytest.mark.parametrize('hdu', [3, 'third']) def test_read_with_hdu_3(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_3.fits')) self.hdus.writeto(filename) with pytest.raises(ValueError, match='No table found in hdu=3'): Table.read(filename, hdu=hdu) def test_read_with_hdu_4(self, tmpdir): filename = str(tmpdir.join('test_read_with_hdu_4.fits')) self.hdus.writeto(filename) t = Table.read(filename, hdu=4) assert equal_data(t, self.data3) @pytest.mark.parametrize('hdu', [2, 3, '1', 'second', '']) def test_read_with_hdu_missing(self, tmpdir, hdu): filename = str(tmpdir.join('test_warn_with_hdu_1.fits')) self.hdus1.writeto(filename) with pytest.warns(AstropyDeprecationWarning, match=rf"Specified hdu={hdu} not found, " r"reading in first available table \(hdu=1\)"): t1 = Table.read(filename, hdu=hdu) assert equal_data(t1, self.data1) @pytest.mark.parametrize('hdu', [0, 2, 'third']) def test_read_with_hdu_warning(self, tmpdir, hdu): filename = str(tmpdir.join('test_warn_with_hdu_2.fits')) self.hdus2.writeto(filename) with pytest.warns(AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=1\)"): t2 = Table.read(filename, hdu=hdu) assert equal_data(t2, self.data1) @pytest.mark.parametrize('hdu', [0, 1, 'third']) def test_read_in_last_hdu(self, tmpdir, hdu): filename = str(tmpdir.join('test_warn_with_hdu_3.fits')) self.hdus3.writeto(filename) with pytest.warns(AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=2\)"): t3 = Table.read(filename, hdu=hdu) assert equal_data(t3, self.data2) def test_read_from_hdulist(self): with pytest.warns(AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=1\)"): t = Table.read(self.hdus) assert equal_data(t, self.data1) with pytest.warns(AstropyUserWarning, match=r"hdu= was not specified but multiple tables " r"are present, reading in first available " r"table \(hdu=2\)"): t3 = Table.read(self.hdusb) assert equal_data(t3, self.data2) def test_read_from_hdulist_with_hdu_0(self): with pytest.raises(ValueError) as exc: Table.read(self.hdus, hdu=0) assert exc.value.args[0] == 'No table found in hdu=0' @pytest.mark.parametrize('hdu', [1, 'first', None]) def test_read_from_hdulist_with_single_table(self, hdu): t = Table.read(self.hdus1, hdu=hdu) assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [1, 'first']) def test_read_from_hdulist_with_hdu_1(self, hdu): t = Table.read(self.hdus, hdu=hdu) assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [2, 'second']) def test_read_from_hdulist_with_hdu_2(self, hdu): t = Table.read(self.hdus, hdu=hdu) assert equal_data(t, self.data2) @pytest.mark.parametrize('hdu', [3, 'third']) def test_read_from_hdulist_with_hdu_3(self, hdu): with pytest.raises(ValueError, match='No table found in hdu=3'): Table.read(self.hdus, hdu=hdu) @pytest.mark.parametrize('hdu', [0, 2, 'third']) def test_read_from_hdulist_with_hdu_warning(self, hdu): with pytest.warns(AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=1\)"): t2 = Table.read(self.hdus2, hdu=hdu) assert equal_data(t2, self.data1) @pytest.mark.parametrize('hdu', [2, 3, '1', 'second', '']) def test_read_from_hdulist_with_hdu_missing(self, hdu): with pytest.warns(AstropyDeprecationWarning, match=rf"Specified hdu={hdu} not found, " r"reading in first available table \(hdu=1\)"): t1 = Table.read(self.hdus1, hdu=hdu) assert equal_data(t1, self.data1) @pytest.mark.parametrize('hdu', [0, 1, 'third']) def test_read_from_hdulist_in_last_hdu(self, hdu): with pytest.warns(AstropyDeprecationWarning, match=rf"No table found in specified hdu={hdu}, " r"reading in first available table \(hdu=2\)"): t3 = Table.read(self.hdus3, hdu=hdu) assert equal_data(t3, self.data2) @pytest.mark.parametrize('hdu', [None, 1, 'first']) def test_read_from_single_hdu(self, hdu): t = Table.read(self.hdus[1]) assert equal_data(t, self.data1)
class TestMultipleHDU: def setup_class(self): self.data1 = np.array(list(zip([1, 2, 3, 4], ['a', 'b', 'c', 'd'], [2.3, 4.5, 6.7, 8.9])), dtype=[(str('a'), int), (str('b'), str('U1')), (str('c'), float)]) self.data2 = np.array(list(zip([1.4, 2.3, 3.2, 4.7], [2.3, 4.5, 6.7, 8.9])), dtype=[(str('p'), float), (str('q'), float)]) hdu1 = PrimaryHDU() hdu2 = BinTableHDU(self.data1, name='first') hdu3 = BinTableHDU(self.data2, name='second') self.hdus = HDUList([hdu1, hdu2, hdu3]) def teardown_class(self): del self.hdus def setup_method(self, method): warnings.filterwarnings('always') def test_read(self, tmpdir): filename = str(tmpdir.join('test_read.fits')) self.hdus.writeto(filename) with catch_warnings() as l: t = Table.read(filename) assert len(l) == 1 assert str(l[0].message).startswith( 'hdu= was not specified but multiple tables are present, reading in first available table (hdu=1)') assert equal_data(t, self.data1) def test_read_with_hdu_0(self, tmpdir): filename = str(tmpdir.join('test_read_with_hdu_0.fits')) self.hdus.writeto(filename) with pytest.raises(ValueError) as exc: Table.read(filename, hdu=0) assert exc.value.args[0] == 'No table found in hdu=0' @pytest.mark.parametrize('hdu', [1, 'first']) def test_read_with_hdu_1(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_1.fits')) self.hdus.writeto(filename) with catch_warnings() as l: t = Table.read(filename, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [2, 'second']) def test_read_with_hdu_2(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_2.fits')) self.hdus.writeto(filename) with catch_warnings() as l: t = Table.read(filename, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data2) def test_read_from_hdulist(self): with catch_warnings() as l: t = Table.read(self.hdus) assert len(l) == 1 assert str(l[0].message).startswith( 'hdu= was not specified but multiple tables are present, reading in first available table (hdu=1)') assert equal_data(t, self.data1) def test_read_from_hdulist_with_hdu_0(self, tmpdir): with pytest.raises(ValueError) as exc: Table.read(self.hdus, hdu=0) assert exc.value.args[0] == 'No table found in hdu=0' @pytest.mark.parametrize('hdu', [1, 'first']) def test_read_from_hdulist_with_hdu_1(self, tmpdir, hdu): with catch_warnings() as l: t = Table.read(self.hdus, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [2, 'second']) def test_read_from_hdulist_with_hdu_2(self, tmpdir, hdu): with catch_warnings() as l: t = Table.read(self.hdus, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data2) def test_read_from_single_hdu(self): with catch_warnings() as l: t = Table.read(self.hdus[1]) assert len(l) == 0 assert equal_data(t, self.data1)
class TestMultipleHDU: def setup_class(self): self.data1 = np.array(list( zip([1, 2, 3, 4], ['a', 'b', 'c', 'd'], [2.3, 4.5, 6.7, 8.9])), dtype=[('a', int), ('b', 'U1'), ('c', float)]) self.data2 = np.array(list( zip([1.4, 2.3, 3.2, 4.7], [2.3, 4.5, 6.7, 8.9])), dtype=[('p', float), ('q', float)]) hdu1 = PrimaryHDU() hdu2 = BinTableHDU(self.data1, name='first') hdu3 = BinTableHDU(self.data2, name='second') self.hdus = HDUList([hdu1, hdu2, hdu3]) def teardown_class(self): del self.hdus def setup_method(self, method): warnings.filterwarnings('always') def test_read(self, tmpdir): filename = str(tmpdir.join('test_read.fits')) self.hdus.writeto(filename) with catch_warnings() as l: t = Table.read(filename) assert len(l) == 1 assert str(l[0].message).startswith( 'hdu= was not specified but multiple tables are present, reading in first available table (hdu=1)' ) assert equal_data(t, self.data1) def test_read_with_hdu_0(self, tmpdir): filename = str(tmpdir.join('test_read_with_hdu_0.fits')) self.hdus.writeto(filename) with pytest.raises(ValueError) as exc: Table.read(filename, hdu=0) assert exc.value.args[0] == 'No table found in hdu=0' @pytest.mark.parametrize('hdu', [1, 'first']) def test_read_with_hdu_1(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_1.fits')) self.hdus.writeto(filename) with catch_warnings() as l: t = Table.read(filename, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [2, 'second']) def test_read_with_hdu_2(self, tmpdir, hdu): filename = str(tmpdir.join('test_read_with_hdu_2.fits')) self.hdus.writeto(filename) with catch_warnings() as l: t = Table.read(filename, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data2) def test_read_from_hdulist(self): with catch_warnings() as l: t = Table.read(self.hdus) assert len(l) == 1 assert str(l[0].message).startswith( 'hdu= was not specified but multiple tables are present, reading in first available table (hdu=1)' ) assert equal_data(t, self.data1) def test_read_from_hdulist_with_hdu_0(self, tmpdir): with pytest.raises(ValueError) as exc: Table.read(self.hdus, hdu=0) assert exc.value.args[0] == 'No table found in hdu=0' @pytest.mark.parametrize('hdu', [1, 'first']) def test_read_from_hdulist_with_hdu_1(self, tmpdir, hdu): with catch_warnings() as l: t = Table.read(self.hdus, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data1) @pytest.mark.parametrize('hdu', [2, 'second']) def test_read_from_hdulist_with_hdu_2(self, tmpdir, hdu): with catch_warnings() as l: t = Table.read(self.hdus, hdu=hdu) assert len(l) == 0 assert equal_data(t, self.data2) def test_read_from_single_hdu(self): with catch_warnings() as l: t = Table.read(self.hdus[1]) assert len(l) == 0 assert equal_data(t, self.data1)
def reduce(self, has_sets): self.create_master_bias() self.create_master_flat() #new directory within directory containing raw images to store #program output in newdir = self.directory + Constants.working_directory #creates new directory if not already exists if not os.path.exists(newdir): os.mkdir(newdir) #new directory within directory defined above to contain processed #images newdir = newdir + Constants.image_directory #creates image directory if not already exists if not os.path.exists(newdir): os.mkdir(newdir) #length of image name regex strlen = len(self.image_names) i = 0 #loop througheach file in directory for file in os.listdir(self.directory): #if the first strlen characters are the image name regex then #this file is an image to be processed if file[: strlen] == self.image_names and Constants.fits_extension in file: #get image filter value print(file) filter = getval(self.directory + file, 'FILTER', ignore_missing_end=True) #if the filter of the image matches the required filter then if filter[:1] == self.fil or filter == self.fil: #get image data and header data = getdata(self.directory + file, ignore_missing_end=True) Constants.image_width = len(data) Constants.image_height = len(data[0]) #subtract bias from image data = data - self.master_bias #divide image by flatfield divided by median of flatfield data = data / (self.master_flat / self.flat_median) head = getheader(self.directory + file, ignore_missing_end=True) #build filepath of processed image filepath = newdir + Constants.reduced_prefix + self.image_names #if raw images are stored in sets if (has_sets): #if has sets, then files are stored with the following #suffix format 'name_1_001', 'name_3_020' etc. The #following code splits the name string to find the #set number and image number array = file.split("_") set = int(array[len(array) - 2]) i = int(array[len(array) - 1].split(".")[0]) #finds the maximum set size and image number #encountered, so the program in subsequent steps #knows to iterate from 1 to n_sets and 1 to set_size #when processing the image data if i > self.set_size: self.set_size = i if set > self.n_sets: self.n_sets = set filepath += "_" + str( set) + "_" + Utilities.format_index(i) else: i += 1 #length of image number n_length = len(str(i)) #format image number string to format '001', '025', '312' #etc for j in range(3 - n_length): filepath += "0" filepath += i filepath += Constants.fits_extension # ============================================================================= # if set == 1 and i == 1: # # pf = PositionFinder.PositionFinder(newdir, hdul) # pf.getWCS() # return # ============================================================================= if True: width = len(data) height = len(data[0]) x = int((width * 0.1) + ((set * 50 + i) / 400) * (0.7 * width)) y = int(height / 2) print(x, y, set, i) for k in range(-3, 3, 1): for l in range(-3, 3, 1): n = (k**2 + l**2)**0.5 if n == 0: n = 1 data[x + k][y + l] = 4.2 / n * 8000 #export processed image to file hdu = PrimaryHDU(data, head) hdul = HDUList([hdu], None) hdul.writeto(filepath, overwrite=True)
def reduce(self, has_sets): newdir = self.objdir + Constants.working_directory if not os.path.exists(newdir): os.mkdir(newdir) newdir = newdir + Constants.image_directory if not os.path.exists(newdir): os.mkdir(newdir) strlen=len(self.image_names) i = 0 for file in os.listdir(self.objdir): print(file) if file[:strlen]==self.image_names: filter=getval(self.objdir+file,'FILTER',ignore_missing_end=True) print(filter, self.fil) if filter[:1]==self.fil or filter == self.fil: print(file,filter[:1]) data=(getdata(self.objdir+file,ignore_missing_end=True) - self.bias) / self.flat head=getheader(self.objdir+file,ignore_missing_end=True) hdu = PrimaryHDU(data, head) hdul = HDUList([hdu], None) filepath = newdir + Constants.reduced_prefix + self.image_names if(has_sets): array = file.split("_") set = array[1] i = array[2].split(".")[0] if i > self.set_size: self.set_size = i if set > n_sets: n_sets = set print(set) print(i) filepath += "_" + set + "_" + i else: i+=1 n_length = len(str(i)) for j in range(3-n_length): filepath += "0" filepath += i filepath += Constants.fits_extension print(newdir) print(filepath) hdul.writeto(filepath, overwrite=True) print(i)