def test_region_wcs(ref_name, reg_name, header_name): header = Header.fromtextfile(join(rootdir, header_name)) wcs = astropy.wcs.WCS(header=header) # Header-only ref_region = pyregion_open(join(rootdir, ref_name)).as_imagecoord(header) # With WCS r = pyregion_open(join(rootdir, reg_name)).as_imagecoord(header, wcs=wcs) assert len(r) == len(ref_region) for ref_reg, reg in zip(ref_region, r): if reg.name == "rotbox": reg.name = "box" assert ref_reg.name == reg.name # Normalize everything like angles ref_list = np.asarray(ref_reg.coord_list) reg_list = np.asarray(reg.coord_list) assert_allclose((ref_list + 180) % 360 - 180, (reg_list + 180) % 360 - 180, atol=0.03) assert ref_reg.exclude == reg.exclude
def make_header(self, ra, dec, width, pix_size): """ This function ... :return: """ # ra and dec are taken from the ra2000 and de2000 of the attached DustPedia_LEDAWISE_Herschel.csv table # width is 0.5 degrees for galaxies with D25<6 arcmin, and 1 degree for galaxies with D25>=6 arcmin (as listed in DustPedia_LEDAWISE_Herschel.csv) # pix_size is 3.2 for GALEX, and 0.45 for SDSS. # ra = ra.to("deg").value dec = dec.to("deg").value width = width.to("deg").value pix_size = pix_size.to("arcsec").value # Determine the path to the temporary header file header_path = fs.join(self.temp_path, "header.hdr") # Create the header montage.commands.mHdr(str(ra) + ' ' + str(dec), width, header_path, pix_size=pix_size) # Load the header return Header.fromtextfile(header_path)
def images_header(self): """ This function ... :return: """ return Header.fromtextfile(self.environment.images_header_path)
def header(self): """ This function ... :return: """ # Determine the path to the header file return Header.fromtextfile(header_path)
def get_images_header(modeling_path): """ This function ... :param modeling_path: :return: """ return Header.fromtextfile(modeling_path)
def make_header(ra, dec, width, pix_size, returns="header"): """ This function ... :param ra: :param dec: :param width: :param pix_size: :param returns: :return: """ import montage_wrapper as montage # ra and dec are taken from the ra2000 and de2000 of the attached DustPedia_LEDAWISE_Herschel.csv table # width is 0.5 degrees for galaxies with D25<6 arcmin, and 1 degree for galaxies with D25>=6 arcmin (as listed in DustPedia_LEDAWISE_Herschel.csv) # pix_size is 3.2 for GALEX, and 0.45 for SDSS. # Convert to degrees and the pixelsize in arcseconds ra_deg = ra.to("deg").value dec_deg = dec.to("deg").value width_deg = width.to("deg").value pix_size_arcsec = pix_size.to("arcsec").value # Determine the path to the temporary header file header_path = fs.join(temp_montage_path, "header.hdr") # Create the header montage.commands.mHdr(str(ra_deg) + ' ' + str(dec_deg), width_deg, header_path, pix_size=pix_size_arcsec) # Load the header if returns == "header": return Header.fromtextfile(header_path) elif returns == "path": return header_path elif returns == ["header", "path"]: return Header.fromtextfile(header_path), header_path elif returns == ["path", "header"]: return header_path, Header.fromtextfile(header_path) else: raise ValueError("Invalid option for 'returns'")
def from_header_file(cls, path): """ This function ... :param path: :return: """ # Load the header header = Header.fromtextfile(path) # Create and return the coordinate system system = cls(header=header) system.path = path return system
def test_region(ref_name, reg_name, header_name): header = Header.fromtextfile(join(rootdir, header_name)) ref_region = pyregion_open(join(rootdir, ref_name)).as_imagecoord(header) r = pyregion_open(join(rootdir, reg_name)).as_imagecoord(header) assert len(r) == len(ref_region) for ref_reg, reg in zip(ref_region, r): if reg.name == "rotbox": reg.name = "box" assert ref_reg.name == reg.name # Normalize everything like angles ref_list = np.asarray(ref_reg.coord_list) reg_list = np.asarray(reg.coord_list) assert_allclose((ref_list + 180) % 360 - 180, (reg_list + 180) % 360 - 180, atol=0.03) assert ref_reg.exclude == reg.exclude
def load_maps(self): """ This function ... :return: """ # Inform the user log.info("Loading the input maps ...") # Determine path to maps directory maps_path = fs.join(m81_data_path, "maps") # Determine the path to the header file header_path = fs.join(maps_path, "header.txt") header = Header.fromtextfile(header_path) wcs = CoordinateSystem(header=header) # Old stars old_map_path = fs.join(maps_path, old_filename) old_map = Frame.from_file(old_map_path) old_map.wcs = wcs self.maps["old"] = old_map # young stars young_map_path = fs.join(maps_path, young_filename) young_map = Frame.from_file(young_map_path) young_map.wcs = wcs self.maps["young"] = young_map # Ionizing stars ionizing_map_path = fs.join(maps_path, ionizing_filename) ionizing_map = Frame.from_file(ionizing_map_path) ionizing_map.wcs = wcs self.maps["ionizing"] = ionizing_map # Dust dust_map_path = fs.join(maps_path, dust_filename) dust_map = Frame.from_file(dust_map_path) dust_map.wcs = wcs self.maps["dust"] = dust_map
crfile = Path(input('Coord File? \'https:... x y author\' -- format ')) regFile = Path(input('Region file directory? ')) arcsec2deg = 1 / 3600 regRadius = 10. * arcsec2deg fp = open(crfile, 'r') ll = fp.readlines() fp.close() for imgName in range(len(ll)): try: p = Path(ll[imgName].split()[0]) print(p) header = p.stem.replace(p.stem[p.stem.find('_', 8):], '.head') print(header) imgHeader = Header.fromtextfile(str(imgHeaderPath.joinpath(header))) pngX, pngY = float(ll[imgName].split()[1]), float( ll[imgName].split()[2]) if ll[imgName].split()[0][-3:] == 'jpg': # pixX, pixY = pngX*1.689, 2200-(pngY*1.686) pixX, pixY = pngX / .5, (1100 - pngY) / .5 elif ll[imgName].split()[0][-3:] == 'png': pixX, pixY = (pngX / (2487 / 2100)) / .5, ((1306 - pngY) / (1306 / 1100)) / .5 else: print('what the hell happened!?!') break xy = np.column_stack((pixX, pixY)) print(header) w = WCS(imgHeader) coords = w.wcs_pix2world(xy, 1)
def test_calculate_rotation_angle(region_frame, header_name, rot_angle): header = Header.fromtextfile(os.path.join(rootdir, header_name)) assert_allclose(_calculate_rotation_angle(region_frame, header), rot_angle, atol=0.001)
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText from astropy.io.fits import Header from astropy.wcs import WCS from wcsaxes import WCSAxes import pyregion region_list = ["test_text.reg", "test_context.reg"] # Create figure fig = plt.figure(figsize=(8, 4)) # Parse WCS information header = Header.fromtextfile("sample_fits01.header") wcs = WCS(header) # Create axes ax1 = WCSAxes(fig, [0.1, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax1) ax2 = WCSAxes(fig, [0.5, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax2) # Hide labels on y axis ax2.coords[1].set_ticklabel_position("") for ax, reg_name in zip([ax1, ax2], region_list): ax.set_xlim(300, 1300) ax.set_ylim(300, 1300)
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText from astropy.io.fits import Header from astropy.wcs import WCS from wcsaxes import WCSAxes import pyregion region_list = ["test_text.reg", "test_context.reg"] # Create figure fig = plt.figure(figsize=(8, 4)) # Parse WCS information header = Header.fromtextfile('sample_fits01.header') wcs = WCS(header) # Create axes ax1 = WCSAxes(fig, [0.1, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax1) ax2 = WCSAxes(fig, [0.5, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax2) # Hide labels on y axis ax2.coords[1].set_ticklabel_position('') for ax, reg_name in zip([ax1, ax2], region_list): ax.set_xlim(300, 1300) ax.set_ylim(300, 1300)
def demo_header(): return Header.fromtextfile("sample_fits02.header")
def print_shape_list(shape_list): for idx, shape in enumerate(shape_list, start=1): print("[region %d]" % idx) print() print("%s; %s(%s)" % (shape.coord_format, shape.name, ", ".join([str(s) for s in shape.coord_list]))) print(shape.attr[0]) print(", ".join(["%s=%s" % (k, v.strip()) for k, v in list(shape.attr[1].items())])) print() if __name__ == "__main__": print("** coordinate in FK5 **") print() filename = "test01_print.reg" # filename = "test_text.reg" # filename = "test01.reg" shape_list = pyregion.open(filename) print_shape_list(shape_list) print() print() print("** coordinate in image **") print() header = Header.fromtextfile("test.header") shape_list2 = shape_list.as_imagecoord(header=header) print_shape_list(shape_list2)
def test_header(): return Header.fromtextfile("test.header")
def demo_header(): return Header.fromtextfile(join(rootdir, "sample_fits01.header"))
def demo_header(): from astropy.io.fits import Header return Header.fromtextfile("sample_fits01.header")
def print_shape_list(shape_list): for idx, shape in enumerate(shape_list, start=1): print("[region %d]" % idx) print() print("%s; %s(%s)" % (shape.coord_format, shape.name, ", ".join( [str(s) for s in shape.coord_list]))) print(shape.attr[0]) print(", ".join([ "%s=%s" % (k, v.strip()) for k, v in list(shape.attr[1].items()) ])) print() if __name__ == "__main__": print("** coordinate in FK5 **") print() filename = "test01_print.reg" # filename = "test_text.reg" # filename = "test01.reg" shape_list = pyregion.open(filename) print_shape_list(shape_list) print() print() print("** coordinate in image **") print() header = Header.fromtextfile("test.header") shape_list2 = shape_list.as_imagecoord(header=header) print_shape_list(shape_list2)
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText from astropy.io.fits import Header from astropy.wcs import WCS from astropy.visualization.wcsaxes import WCSAxes import pyregion region_list = [ "test_text.reg", "test_context.reg", ] # Create figure fig = plt.figure(figsize=(8, 4)) # Parse WCS information header = Header.fromtextfile('sample_fits01.header') wcs = WCS(header) # Create axes ax1 = WCSAxes(fig, [0.1, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax1) ax2 = WCSAxes(fig, [0.5, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax2) # Hide labels on y axis ax2.coords[1].set_ticklabel_position('') for ax, reg_name in zip([ax1, ax2], region_list): ax.set_xlim(300, 1300) ax.set_ylim(300, 1300)
def header(): return Header.fromtextfile(join(rootdir, "sample_fits01.header"))
import astropy.units as u imgHeaderPath = Path('headers') regFile = Path('pixCoords/xyreg.lis') regDirec = Path('regionFiles/xyregs') finalReg = Path('regionFiles/skyregs') arcsec2deg = 1 / 3600 regRadius = 20. * arcsec2deg fp = open(regFile, 'r') ll = fp.readlines() fp.close() for name in range(len(ll)): # print(regName) regName = Path(ll[name]) print(regName) imgName = regName.with_suffix('') print(imgName) imgName = imgName.with_suffix('') print(imgName) reg = read_ds9(regDirec.joinpath(regName).with_suffix('.reg')) hdr = Header.fromtextfile( str(imgHeaderPath.joinpath(imgName).with_suffix('.head'))) w = WCS(hdr) for rr in range(len(reg)): reg[rr] = reg[rr].to_sky(wcs=w) write_ds9(reg, finalReg.joinpath(imgName).with_suffix('.sky.reg'))
def test_calculate_rotation_angle(region_frame, header_name, rot_angle): header = Header.fromtextfile(os.path.join(rootdir, header_name)) assert_allclose( _calculate_rotation_angle(region_frame, header), rot_angle, atol=0.001 )