def get_catalog_t(skypos, angle): center = pycoo.spherical_to_cartesian(1, skypos[1]*np.pi/180., skypos[0]*np.pi/180.) rad = np.cos(angle*np.pi/180.) #print(center, rad) #load catalog hdulist = pyfits.open('../data/bstar.fits') length = int(hdulist[1].header['NAXIS2']) data = hdulist[1].data stars = np.zeros((length,2)) stars[:,0] = data['ra'] stars[:,1] = data['dec'] stars_rad = stars*np.pi/180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T sep = np.dot(stars_car, center) coo = stars[sep>=rad, :] coo_list = [] for i in range(0,coo.shape[0]): tmp_coo = (coo[i,0], coo[i,1]) coo_list.append(tmp_coo) return coo_list
def angle_filter_out(data, skypos, angle): center = pycoo.spherical_to_cartesian(1, skypos[1]*np.pi/180., skypos[0]*np.pi/180.) rad = np.cos(angle*np.pi/180.) #print(center, rad) data_rad = data*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, data_rad[:,1], data_rad[:,0]) data_car = np.array([X,Y,Z], dtype='float64').T sep = np.dot(data_car, center) coo = data[sep<=rad, :] return coo
def angle_filter(data, skypos, angle): center = pycoo.spherical_to_cartesian(1, skypos[1] * np.pi / 180., skypos[0] * np.pi / 180.) rad = np.cos(angle * np.pi / 180.) #print(center, rad) data_rad = data * np.pi / 180. X, Y, Z = pycoo.spherical_to_cartesian(1, data_rad[:, 1], data_rad[:, 0]) data_car = np.array([X, Y, Z], dtype='float64').T sep = np.dot(data_car, center) mask = sep >= rad coo = data[sep >= rad, :] return coo, mask
def rotar(): import numpy as np from astropy import coordinates as coo from astropy import units as u from scipy.spatial.transform import Rotation as R import math from random import random # alpha in [0, 360] deg alpha = np.random.rand()*360.*u.deg # delta in [-90, 90] deg delta = math.acos(random()*2.-1.)*u.rad delta = 90.*u.deg - delta.to(u.deg) phi = 90.*u.deg v = coo.spherical_to_cartesian(1., delta, alpha) r1 = R.from_euler('z', -alpha.value, degrees=True) r2 = R.from_euler('y', -90.+delta.value, degrees=True) r3 = R.from_euler('z', -phi.value, degrees=True) v1 = r1.apply(v) v2 = r2.apply(v1) v_new = r3.apply(v2)
def polygon_perimeter(vertices): ''' Calculate the perimeter of a spherical polygon over a unit sphere. Usage: perimeter = polygon_perimeter(vertices) Inputs: vertices -> [float 2d array] Vertices of a spherical polygon in format of [[lat_0,lon_0],..,[lat_n,lon_n]] with unit of degrees. Vertices can be arranged either counterclockwise or clockwise. Outputs: perimeter -> [float] Perimeter of the spherical polygon in radians. It is independent of how the vertices are arranged. Note: The spherical polygon has a latitude range of [-90,90] and a longitude range of [-180,180] or [0,360]. ''' lats, lons = vertices[:, 0], vertices[:, 1] N = len(vertices) polygon_cartesian = spherical_to_cartesian(np.ones(N), lats * u.deg, lons * u.deg) polygon_xyz = np.array(polygon_cartesian).T perimeter = 0 for i in range(N - 1): inner_prod = np.dot(polygon_xyz[i], polygon_xyz[i + 1]) # inner product for any two unit vectors should be in [-1,1] if inner_prod > 1: inner_prod = 1 if inner_prod < -1: inner_prod = -1 perimeter += np.arccos(inner_prod) return perimeter
def extract_cartesian_coords(stars): """Return cartesian coordinates for a DataFrame containing ra, dec, and distance. Parameters: ------ stars: DataFrame DataFrame containing ra, dec and distance values Returns: ------- x: array The first cartesian coordinate y: array The second cartesian coordinate z: array The third cartesian coordinate """ ra = np.asarray(stars['ra']) dec = np.asarray(stars['dec']) # print(ra[:10]) # NB: the conversion function requires all units to be the same, hence # the use of parallax rather than 1/parallax here. dist = np.asarray(stars['parallax']) x, y, z = spherical_to_cartesian(dist, dec, ra) return x, y, z
def spd_pgs_do_fac(data_in, mat): """ Applies field aligned coordinate transformation to input data Input: data_in: dict Particle data structure to be rotated mat: numpy.ndarray The 3x3 field-aligned rotation matrix to apply to the data Returns: Rotated particle data structure """ data_out = data_in.copy() rvals = np.ones(data_in['data'].shape) cart_data = spherical_to_cartesian(rvals, data_in['theta'] * np.pi / 180.0, data_in['phi'] * np.pi / 180.0) x = mat[0, 0] * cart_data[0] + mat[0, 1] * cart_data[1] + mat[ 0, 2] * cart_data[2] y = mat[1, 0] * cart_data[0] + mat[1, 1] * cart_data[1] + mat[ 1, 2] * cart_data[2] z = mat[2, 0] * cart_data[0] + mat[2, 1] * cart_data[1] + mat[ 2, 2] * cart_data[2] sphere_data = cartesian_to_spherical(x, y, z) data_out['theta'] = sphere_data[1].value * 180.0 / np.pi data_out['phi'] = sphere_data[2].value * 180.0 / np.pi return data_out
def get_fields_of_view(catch): query = (catch.db.session.query(catch.source.fov).yield_per(10000)) for fov, in query: ra, dec = np.radians( np.array([c.split(':') for c in fov.split(',')], float)).T xyz = spherical_to_cartesian(1, dec, ra) yield np.array(xyz).T
def readhealcat_polygon(ra,dec,nside=32,outfile=None,path='/line12/Pan-STARRS/chunks-qz-star-v2/',silent=True,prefix='ps1-',postfix='.fits'): """ read the healpix catalog within a spheric rectangle ra,dec: in degrees array """ ra=np.deg2rad(ra) dec=np.deg2rad(dec) if len(ra) != len(dec) or len(ra) < 3: raise ValueError('ra and dec should have same size and size should > 3 to present a polygon') x,y,z=c.spherical_to_cartesian(1.0,dec,ra) vertices=np.array([x,y,z]).transpose() pix=query_polygon(nside, vertices, inclusive=True) npix=pix.size nd=len(str(healpy.nside2npix(nside))) if not silent: print('Total '+str(npix)+' healpix pixels:') cat=[] for ipix in pix: ipixstr='{ipix:0{width}}'.format(ipix=ipix,width=nd) catname=os.path.join(path,prefix+ipixstr+postfix) if not os.path.isfile(catname): if not silent: print(catname+' not exist!') continue if not silent: print('reading '+catname) cat.append(table.Table.read(catname,format='fits')) totalcat = [] if cat != []: totalcat=table.vstack(cat) if outfile is not None: totalcat.write(outfile,format='fits',overwrite=True) return totalcat
def velocity_at_point(omega_cartesian, location, mode): if mode == 'geocentric': lat, lon, r = location pos_cartesian = spherical_to_cartesian(r, lat, lon) else: if mode == 'geodetic': lat_geode, lon, h = location pos_cartesian = EarthLocation.from_geodetic(lon, lat_geode, h) x, y, z = pos_cartesian.x, pos_cartesian.y, pos_cartesian.z pos_cartesian = (x, y, z) elif mode == 'cartesian': x, y, z = location pos_cartesian = location r, lat_rad, lon_rad = cartesian_to_spherical(x, y, z) lat, lon = lat_rad.to(u.deg), lon_rad.to(u.deg) velocity_en = euler_matrix(lat, lon, r) @ omega_cartesian velocity_cartesian = np.cross(omega_cartesian, pos_cartesian) velocity_xyz = vel_unit_conver(velocity_cartesian) velocity_en = vel_unit_conver(velocity_en) speed = norm(velocity_xyz) azimuth = np.arctan2(velocity_en[0], velocity_en[1]).to(u.deg) return velocity_xyz, velocity_en, speed, azimuth
def __init__(self, p1, p2, telescope_position, weight=1): """The constructor takes two coordinates in the horizontal frame (alt, az) which define a plane perpedicular to the camera. Parameters ----------- p1: astropy.coordinates.SkyCoord One of two direction vectors which define the plane. This coordinate has to be defined in the ctapipe.coordinates.HorizonFrame p2: astropy.coordinates.SkyCoord One of two direction vectors which define the plane. This coordinate has to be defined in the ctapipe.coordinates.HorizonFrame telescope_position: np.array(3) Position of the telescope on the ground weight : float, optional weight of this plane for later use during the reconstruction Notes ----- c: numpy.ndarray(3) :math:`\vec c = (\vec a \times \vec b) \times \vec a` :math:`\rightarrow` a and c form an orthogonal base for the great circle (only orthonormal if a and b are of unit-length) norm: numpy.ndarray(3) normal vector of the circle's plane, perpendicular to a, b and c """ self.pos = telescope_position # astropy's coordinates system rotates counter clockwise. Apparently we assume it to # be clockwise self.a = np.array(spherical_to_cartesian(1, p1.alt, -p1.az)).ravel() self.b = np.array(spherical_to_cartesian(1, p2.alt, -p2.az)).ravel() # a and c form an orthogonal basis for the great circle # not really necessary since the norm can be calculated # with a and b just as well self.c = np.cross(np.cross(self.a, self.b), self.a) # normal vector for the plane defined by the great circle self.norm = normalise(np.cross(self.a, self.c)) # some weight for this circle # (put e.g. uncertainty on the Hillas parameters # or number of PE in here) self.weight = weight
def __init__(self, p1, p2, telescope_position, weight=1): r"""The constructor takes two coordinates in the horizontal frame (alt, az) which define a plane perpendicular to the camera. Parameters ----------- p1: astropy.coordinates.SkyCoord One of two direction vectors which define the plane. This coordinate has to be defined in the ctapipe.coordinates.AltAz p2: astropy.coordinates.SkyCoord One of two direction vectors which define the plane. This coordinate has to be defined in the ctapipe.coordinates.AltAz telescope_position: np.array(3) Position of the telescope on the ground weight : float, optional weight of this plane for later use during the reconstruction Notes ----- c: numpy.ndarray(3) :math:`\vec c = (\vec a \times \vec b) \times \vec a` :math:`\rightarrow` a and c form an orthogonal base for the great circle (only orthonormal if a and b are of unit-length) norm: numpy.ndarray(3) normal vector of the circle's plane, perpendicular to a, b and c """ self.pos = telescope_position # astropy's coordinates system rotates counter clockwise. Apparently we assume it to # be clockwise self.a = np.array(spherical_to_cartesian(1, p1.alt, -p1.az)).ravel() self.b = np.array(spherical_to_cartesian(1, p2.alt, -p2.az)).ravel() # a and c form an orthogonal basis for the great circle # not really necessary since the norm can be calculated # with a and b just as well self.c = np.cross(np.cross(self.a, self.b), self.a) # normal vector for the plane defined by the great circle self.norm = normalise(np.cross(self.a, self.c)) # some weight for this circle # (put e.g. uncertainty on the Hillas parameters # or number of PE in here) self.weight = weight
def griddata(phi, theta, data): r = np.ones(len(phi)) phi_rad = phi * np.pi / 180.0 theta_rad = theta * np.pi / 180.0 cart_temp = spherical_to_cartesian(r, theta_rad, phi_rad) points = np.stack(cart_temp).T return NearestNDInterpolator(points, data)
def __init__(self, local_az=None, local_zen=None, local_vector=None, name_list=None, color_list=None): if (local_az is not None) and (local_zen is not None) and (local_vector is None): self.vector = np.array( spherical_to_cartesian(1, local_az, local_zen)).T elif ((local_az is None) or (local_zen is None)) and (local_vector is not None): self.vector = np.array(local_vector) else: self.vector = np.array( [[0.2446677589, 0.2523893824, 0.9361823057], [0.5017318971, 0.5036621127, 0.7032706462], [0.5233876659, 0.8520868147, -0.0036651682], [0.5009495177, -0.5032279093, 0.7041386753], [0.5468267487, -0.8372325378, -0.0047123847], [0.9982910766, 0.0584352143, 0.0005236008], [-0.2471260191, -0.2465229020, 0.9370993606], [-0.5135631636, -0.5067957667, 0.6923950822], [-0.5503349679, -0.8349438131, 0.0005235846], [-0.5064476761, 0.5030998708, 0.7002865795], [-0.5552650628, 0.8316411478, -0.0073303046], [-0.9978547710, -0.0652279514, -0.0055850266], [1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]]) if ((local_az is None) or (local_zen is None)) and ( local_vector is None) and (name_list is None): self.name_list = np.array([ 'n0', 'n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'n9', 'na', 'nb', 'b0', 'b1' ]) else: if name_list is None: self.name_list = np.arange(len(self.vector)) else: if len(name_list) == len(self.vector): self.name_list = name_list else: self.name_list = np.arange(len(self.vector)) if ((local_az is None) or (local_zen is None)) and ( local_vector is None) and (color_list is None): self.color_list = ['#74787c'] * len(self.vector) else: if color_list is None: self.color_list = np.arange(len(self.vector)) else: if len(color_list) == len(self.vector): self.color_list = color_list else: self.color_list = ['#74787c'] * len(self.vector)
def get_catalog_matched(skypos, angle, stars, stars_car, data): center = pycoo.spherical_to_cartesian(1, skypos[1]*np.pi/180., skypos[0]*np.pi/180.) rad = np.cos(angle*np.pi/180.) #print(center, rad) #mask = (data['BTmag']>8) #& (data['BTmag']<18) sep = np.dot(stars_car, center) mask = sep>=rad coo = stars[mask, :] return coo, data[mask]
def __init__(self, quaternion, local_az, local_zen): self.quaternion = quaternion self.local_az = local_az self.local_zen = local_zen print(self.local_az, self.local_zen) self.vector = np.array( spherical_to_cartesian(1, self.local_az, self.local_zen)).T self.mat = self.get_mat(self.quaternion[0], self.quaternion[1], self.quaternion[2], self.quaternion[3]) self.detector_index, self.center_all = self.get_center()
def make_catalog_random_positions_sphere(size, center='Earth', distance=Quantity([0, 1], 'Mpc')): """Sample random source locations in a sphere. This can be used to generate an isotropic source population to represent extra-galactic sources. Parameters ---------- size : int Number of sources center : {'Earth', 'Milky Way'} Sphere center distance : `~astropy.units.Quantity` tuple Distance min / max range. Returns ------- catalog : `~astropy.table.Table` Source catalog with columns: - RAJ2000, DEJ2000 (deg) - GLON, GLAT (deg) - Distance (Mpc) """ lon, lat = sample_sphere(size) radius = sample_sphere_distance(distance[0], distance[1], size) # TODO: it shouldn't be necessary here to convert to cartesian ourselves ... x, y, z = spherical_to_cartesian(radius, lat, lon) pos = SkyCoord(x, y, z, frame='galactocentric', representation='cartesian') if center == 'Milky Way': pass elif center == 'Earth': # TODO: add shift Galactic center -> Earth raise NotImplementedError else: msg = 'Invalid center: {}\n'.format(center) msg += 'Choose one of: Earth, Milky Way' raise ValueError(msg) table = Table() table.meta['center'] = center icrs = pos.transform_to('icrs') table['RAJ2000'] = icrs.ra.to('deg') table['DEJ2000'] = icrs.dec.to('deg') galactic = icrs.transform_to('galactic') table['GLON'] = galactic.l.to('deg') table['GLAT'] = galactic.b.to('deg') table['Distance'] = icrs.distance.to('Mpc') return table
def load_catalog_matched(scan, cut=0): cfile='/scratch/dw1519/galex/data/extraction/Starcat-07-22-2017/starcat_{0}mapweight_fec_fwhm.npy'.format(scan) stars_data = np.load(cfile) stars = np.array([stars_data['RAJ2000'], stars_data['DEJ2000']]).T stars_rad = stars*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T flux = np.array([stars_data['flux'], stars_data['nuv']]).T cut = flux[:,1]>=cut return stars[cut], stars_car[cut], flux[cut]
def get_catalog_matched(skypos, angle, stars, stars_car, data): center = pycoo.spherical_to_cartesian(1, skypos[1] * np.pi / 180., skypos[0] * np.pi / 180.) rad = np.cos(angle * np.pi / 180.) #print(center, rad) #mask = (data['BTmag']>8) #& (data['BTmag']<18) sep = np.dot(stars_car, center) mask = sep >= rad coo = stars[mask, :] return coo, data[mask]
def get_catalog(skypos, angle): center = pycoo.spherical_to_cartesian(1, skypos[1] * np.pi / 180., skypos[0] * np.pi / 180.) rad = np.cos(angle * np.pi / 180.) print(center, rad) #load catalog hdulist = pyfits.open('../data/bstar.fits') length = int(hdulist[1].header['NAXIS2']) data = hdulist[1].data stars = np.zeros((length, 2)) stars[:, 0] = data['ra'] stars[:, 1] = data['dec'] stars_rad = stars * np.pi / 180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:, 1], stars_rad[:, 0]) stars_car = np.array([X, Y, Z], dtype='float64').T sep = np.dot(stars_car, center) coo = stars[sep >= rad, :] return coo
def space_parallax(self, time_to_treat, satellite_name, telescope): """ Compute the position shift due to the distance of the obervatories from the Earth center. Please have a look on : "Parallax effects in binary microlensing events" Hardy, S.J and Walker, M.A. 1995. http://adsabs.harvard.edu/abs/1995MNRAS.276L..79H :param time_to_treat: a numpy array containing the time where you want to compute this effect. :param satellite_name: the name of the observatory. Have to be recognize by JPL HORIZON. :return: the shift induce by the distance of the telescope to the Earth center. :rtype: array_like **WARNING** : slalib use MJD time definition, which is MJD = JD-2400000.5 """ tstart = min(time_to_treat) - 1 tend = max(time_to_treat) + 1 if len(telescope.spacecraft_positions) != 0: # allow to pass the user to give his own ephemeris satellite_positions = np.array(telescope.spacecraft_positions) else: # call JPL! satellite_positions = produce_horizons_ephem(satellite_name, tstart, tend, observatory='Geocentric', step_size='1440m', verbose=False)[1] telescope.spacecraft_positions = satellite_positions satellite_positions = np.array(satellite_positions) dates = satellite_positions[:, 0].astype(float) ra = satellite_positions[:, 1].astype(float) dec = satellite_positions[:, 2].astype(float) distances = satellite_positions[:, 3].astype(float) interpolated_ra = interpolate.interp1d(dates, ra) interpolated_dec = interpolate.interp1d(dates, dec) interpolated_distance = interpolate.interp1d(dates, distances) ra_interpolated = interpolated_ra(time_to_treat) dec_interpolated = interpolated_dec(time_to_treat) distance_interpolated = interpolated_distance(time_to_treat) #delta_satellite = [] #for index_time in range(len(time_to_treat)): # delta_satellite.append(distance_interpolated[index_time] * slalib.sla_dcs2c( # ra_interpolated[index_time] * np.pi / 180, # dec_interpolated[index_time] * np.pi / 180)) x, y, z = spherical_to_cartesian(distance_interpolated, dec_interpolated* np.pi / 180, ra_interpolated * np.pi / 180) delta_satellite = np.c_[x.value, y.value, z.value] #delta_satellite = np.array(delta_satellite) delta_satellite_projected = np.array( [np.dot(delta_satellite, self.North), np.dot(delta_satellite, self.East)]) return delta_satellite_projected
def load_catalog_matched(scan, cut=0): cfile = '/scratch/dw1519/galex/data/extraction/Starcat-07-22-2017/starcat_{0}mapweight_fec_fwhm.npy'.format( scan) stars_data = np.load(cfile) stars = np.array([stars_data['RAJ2000'], stars_data['DEJ2000']]).T stars_rad = stars * np.pi / 180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:, 1], stars_rad[:, 0]) stars_car = np.array([X, Y, Z], dtype='float64').T flux = np.array([stars_data['flux'], stars_data['nuv']]).T cut = flux[:, 1] >= cut return stars[cut], stars_car[cut], flux[cut]
def load_catalog(): hdulist = pyfits.open('../data/tycho2.fits') star_data = hdulist[1].data length = star_data.shape[0] stars = np.zeros((length,2)) stars[:,0] = star_data['RAJ2000'] stars[:,1] = star_data['DEJ2000'] stars_rad = stars*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T hdulist.close() return stars, stars_car, star_data
def get_catalog_new(skypos, angle, stars_car, stars): center = pycoo.spherical_to_cartesian(1, skypos[1]*np.pi/180., skypos[0]*np.pi/180.) rad = np.cos(angle*np.pi/180.) #print(center, rad) sep = np.dot(stars_car, center) coo = stars[sep>=rad, :] coo_list = [] for i in range(0,coo.shape[0]): tmp_coo = (coo[i,0], coo[i,1]) coo_list.append(tmp_coo) return coo_list
def get_star_pos_new(filename, limit): data = pyfits.open(filename)[1].data nuv = data['nuv_cntpsec'] mask = (nuv<limit) & (nuv>20) data = data[mask] length = data.shape[0] stars = np.zeros((length,2)) stars[:,0] = data['ra'] stars[:,1] = data['dec'] stars_rad = stars*np.pi/180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T return stars_car, stars
def altaz_to_righthanded_cartesian(alt, az): """Turns an alt/az coordinate into a 3d direction in a right-handed coordinate system. This is because azimuths are in a left-handed system. See e.g: https://github.com/astropy/astropy/issues/5300 Parameters ---------- alt: u.Quantity altitude az: u.Quantity azimuth """ return np.array(spherical_to_cartesian(r=1, lat=alt, lon=-az))
def get_star_pos_new(filename, limit): data = pyfits.open(filename)[1].data nuv = data['nuv_cntpsec'] mask = (nuv < limit) & (nuv > 20) data = data[mask] length = data.shape[0] stars = np.zeros((length, 2)) stars[:, 0] = data['ra'] stars[:, 1] = data['dec'] stars_rad = stars * np.pi / 180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:, 1], stars_rad[:, 0]) stars_car = np.array([X, Y, Z], dtype='float64').T return stars_car, stars
def get_catalog_tycho(skypos, angle): center = pycoo.spherical_to_cartesian(1, skypos[1]*np.pi/180., skypos[0]*np.pi/180.) rad = np.cos(angle*np.pi/180.) print(center, rad) #load catalog hdulist = pyfits.open('../data/tycho2.fits') length = int(hdulist[1].header['NAXIS2']) data = hdulist[1].data mask = data['BTmag']>8 data = data[mask] length = data.shape[0] stars = np.zeros((length,2)) stars[:,0] = data['RAJ2000'] stars[:,1] = data['DEJ2000'] stars_rad = stars*np.pi/180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T sep = np.dot(stars_car, center) coo = stars[sep>=rad, :] return coo
def from_coordinates(cls, lat, long, height, attitude=None, Rbody=const.RMOON, convert_to_radians=False): if convert_to_radians: lat, long = map(np.radians, (lat, long)) position = np.array(spherical_to_cartesian(Rbody + height, lat, long)) return cls(position=position, attitude=attitude, primary_body_radius=Rbody)
def load_catalog_matched(scan, cut=0): cfile='/scratch/dw1519/galex/fits/scan_map/catalog/08-10-2017_1/starcat_{0}mapweight_fec_fwhm.npy'.format(scan) try: stars_data = np.load(cfile) except IOError: stars_data = convert_catalog(cfile) stars = np.array([stars_data['RAJ2000'], stars_data['DEJ2000']]).T stars_rad = stars*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T flux = np.array([stars_data['flux'], stars_data['nuv']]).T cut = flux[:,1]>=cut return stars[cut], stars_car[cut], flux[cut]
def test_sphere_cart(): """ Tests the spherical <-> cartesian transform functions """ from astropy.utils import NumpyRNGContext from astropy.coordinates import spherical_to_cartesian, cartesian_to_spherical x, y, z = spherical_to_cartesian(1, 0, 0) assert_allclose(x, 1) assert_allclose(y, 0) assert_allclose(z, 0) x, y, z = spherical_to_cartesian(0, 1, 1) assert_allclose(x, 0) assert_allclose(y, 0) assert_allclose(z, 0) x, y, z = spherical_to_cartesian(5, 0, np.arcsin(4. / 5.)) assert_allclose(x, 3) assert_allclose(y, 4) assert_allclose(z, 0) r, lat, lon = cartesian_to_spherical(0, 1, 0) assert_allclose(r, 1) assert_allclose(lat, 0 * u.deg) assert_allclose(lon, np.pi / 2 * u.rad) # test round-tripping with NumpyRNGContext(13579): x, y, z = np.random.randn(3, 5) r, lat, lon = cartesian_to_spherical(x, y, z) x2, y2, z2 = spherical_to_cartesian(r, lat, lon) assert_allclose(x, x2) assert_allclose(y, y2) assert_allclose(z, z2)
def inside_polygon(point,vertices,arrangement): ''' Determine if a single point is inside a spherical polygon. Usage: flag = inside_polygon(point,vertices) Inputs: point -> [float array with 2 elements] Point to be determined in form of [lat,lon] with unit of degrees. vertices -> [float 2d array] Vertices of a spherical polygon in form of [[lat_0,lon_0],..,[lat_n,lon_n]] with unit of degrees. arrangement -> [str] Arrangement of the vertices. Avaliable options are Counterclockwise and Clockwise. Outputs: flag -> [bool] If True, the point is inside the polygon, otherwise, it is outside. Note: The spherical polygon has a latitude range of [-90°,90°] and a longitude range of [-180°,180°] or [0°,360°]. ''' N = len(vertices) lat0,lon0 = point[0],point[1] lats,lons = vertices[:,0],vertices[:,1] # Rotate the single point and polygon so that the North Pole axis passes through the single point. transform = Rotation.from_euler('zy', [-lon0,lat0 - 90], degrees=True) polygon_cartesian = spherical_to_cartesian(np.ones(N),lats*u.deg,lons*u.deg) polygon_cartesian_transformed = transform.apply(np.stack(polygon_cartesian).T) xs,ys,zs = [polygon_cartesian_transformed[:,i] for i in range(3)] polygon_spherical_transformed = cartesian_to_spherical(xs,ys,zs) lons_transformed = polygon_spherical_transformed[2].value # unit in rad sum_angle = 0 flag = False for i in range(N-1): dlon = lons_transformed[i+1] - lons_transformed[i] if dlon > np.pi: dlon = -2*np.pi + dlon if dlon < -np.pi: dlon = 2*np.pi + dlon sum_angle += dlon # The single point and the sides of the polygon form N spherical triangles. # If the single point is inside the counterclockwise polygon, the sum of the opposite angles should be 2π. if arrangement == 'Counterclockwise': if np.abs(sum_angle - 2*np.pi) < 0.1: flag = True # If the single point is inside the clockwise polygon, the sum of the opposite angles should be -2π. elif arrangement == 'Clockwise': if np.abs(sum_angle + 2*np.pi) < 0.1: flag = True else: raise Exception('Arrangement of the vertices can either be Counterclockwise or Clockwise.') return flag
def get_catalog_new(skypos, angle, stars_car, stars): center = pycoo.spherical_to_cartesian(1, skypos[1] * np.pi / 180., skypos[0] * np.pi / 180.) rad = np.cos(angle * np.pi / 180.) #print(center, rad) sep = np.dot(stars_car, center) coo = stars[sep >= rad, :] coo_list = [] for i in range(0, coo.shape[0]): tmp_coo = (coo[i, 0], coo[i, 1]) coo_list.append(tmp_coo) return coo_list
def get_star_pos(filename): data = np.loadtxt(filename, skiprows=4) print(data) length = data.shape[0] stars = np.zeros((length, 5)) stars[:, 0] = data[:, 10] stars[:, 1] = data[:, 11] stars[:, 2] = data[:, 0] stars[:, 3] = data[:, 3] stars[:, 4] = data[:, 7] stars_rad = stars * np.pi / 180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:, 1], stars_rad[:, 0]) stars_car = np.array([X, Y, Z], dtype='float64').T return stars_car, stars
def get_star_pos(filename): data = np.loadtxt(filename, skiprows=4) print(data) length = data.shape[0] stars = np.zeros((length,5)) stars[:,0] = data[:, 10] stars[:,1] = data[:, 11] stars[:,2] = data[:, 0] stars[:,3] = data[:, 3] stars[:,4] = data[:, 7] stars_rad = stars*np.pi/180. #convert (ra, dec) to (x, y, z) X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T return stars_car, stars
def __init__(self, w, ll, pole=None, body_basis=None): from astropy.coordinates import spherical_to_cartesian from .generators import Vej self.w = w self.ll = ll if body_basis is None: if pole is None: body_basis = np.array(((1, 0, 0), (0, 1, 0), (0, 0, 1)), float) else: body_basis = Vej.pole2basis(pole) self.body_basis = body_basis # active area normal vector self.normal = spherical_to_cartesian(1.0, ll[1], ll[0])
def __init__(self, name='a'): az = np.array([ 0, 40, 40, 40, 40, 40, 40, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 90, 90, 90, 90, 90, 90, 90, 90 ]) * u.deg zen = np.array([ 0, 332.17, 278.67, 225.17, 152.17, 98.67, 45.17, 350.5, 314.5, 278.5, 242.5, 206.5, 170.5, 134.5, 98.5, 62.5, 26.5, 0, 305, 270, 215, 180, 125, 90, 35 ]) * u.deg self.detetor_vector = np.array( spherical_to_cartesian(1, 90 * u.deg - az, zen)).T name_list = [] for i in range(len(az)): name_list.append(name + str(i)) self.name = np.array(name_list) self.eff_angle = np.array([60] * 25)
def readhealcat_rectangle(ra,dec,width=(0.6,0.6),nside=32,outfile=None,path='/line12/Pan-STARRS/chunks-qz-star-v2/',silent=True,prefix='ps1-',postfix='.fits',slim=False): """ read the healpix catalog within a spheric rectangle ra,dec: in degrees array """ if np.isscalar(width) or len(width) == 1: width=(width,width) ra=np.deg2rad(ra) dec=np.deg2rad(dec) wra=np.deg2rad(width[0]/np.cos(dec)) wdec=np.deg2rad(width[1]) ra1=ra-wra/2.0 dec1=dec-wdec/2.0 ra2=ra-wra/2.0 dec2=dec+wdec/2.0 ra3=ra+wra/2.0 dec3=dec+wdec/2.0 ra4=ra+wra/2.0 dec4=dec-wdec/2.0 x,y,z=c.spherical_to_cartesian(1.0,[dec1,dec2,dec3,dec4],[ra1,ra2,ra3,ra4]) vertices=np.array([x,y,z]).transpose() pix=query_polygon(nside, vertices, inclusive=True) npix=pix.size nd=len(str(healpy.nside2npix(nside))) if not silent: print('Total '+str(npix)+' healpix pixels:') cat=[] for ipix in pix: ipixstr='{ipix:0{width}}'.format(ipix=ipix,width=nd) catname=os.path.join(path,prefix+ipixstr+postfix) if not os.path.isfile(catname): if not silent: print(catname+' not exist!') continue if not silent: print('reading '+catname) cat.append(table.Table.read(catname,format='fits')) totalcat = [] if cat != []: totalcat=table.vstack(cat) if slim: mask=(totalcat['RA'] > np.rad2deg(ra1)) & (totalcat['RA'] < np.rad2deg(ra3)) & (totalcat['DEC'] > np.rad2deg(dec1)) & (totalcat['DEC'] < np.rad2deg(dec3)) totalcat=totalcat[mask] if totalcat != [] and outfile is not None: totalcat.write(outfile,format='fits',overwrite=True) return totalcat
def set_coordinates(self, lat, long, height=None, point_nadir=False, convert_to_radians=True): if height is None: height = self.height if convert_to_radians: lat, long = map(np.radians, (lat, long)) self.position = np.array( spherical_to_cartesian(self._primary_body_radius + height, lat, long)) if point_nadir: self.point_nadir()
def estimate_h_max(self, hillas_dict, subarray, pointing_alt, pointing_az): weights = [] tels = [] dirs = [] for tel_id, moments in hillas_dict.items(): focal_length = subarray.tel[tel_id].optics.equivalent_focal_length pointing = SkyCoord( alt=pointing_alt[tel_id], az=pointing_az[tel_id], frame='altaz' ) hf = HorizonFrame( array_direction=pointing, pointing_direction=pointing ) cf = CameraFrame( focal_length=focal_length, array_direction=pointing, pointing_direction=pointing ) cog_coord = SkyCoord(x=moments.cen_x, y=moments.cen_y, frame=cf) cog_coord = cog_coord.transform_to(hf) cog_direction = spherical_to_cartesian(1, cog_coord.alt, cog_coord.az) cog_direction = np.array(cog_direction).ravel() weights.append(self.hillas_planes[tel_id].weight) tels.append(self.hillas_planes[tel_id].pos) dirs.append(cog_direction) # minimising the test function pos_max = minimize(dist_to_line3d, np.array([0, 0, 10000]), args=(np.array(tels), np.array(dirs), np.array(weights)), method='BFGS', options={'disp': False} ).x return pos_max[2] * u.m
def scale_point(point, center, scaler): ''' scales_point scaled the radial value of a point about a given center scale_dict takes a contour and performs an isotropic expansion/contraction. A contour is translated so that center is at [0,0,0], it's then converted to spherical polar coordinates, the radius is scaled, and then it is convert back to a dict in cartesian. Parameters ---------- point : list point to be scaled [z,y,x] center : 1d ndarray the point about which to scale isotropically (np.array([z,y,x])) scaler : float amount by which to scale Returns ------- scaled_point : list the scaled point, with and integer z value [z,y,x] zunique_scaled : set contains all z values corresponding to slices containing scaled contour values indexmin_scaled : 1d ndarray point giving the the overall minimum index of the box containing the scaled contour, for plotting indexmax_scaled : 1d ndarray point giving the the overall maximum index of the box containing the scaled contour, for plotting ''' centered_point = point - center # shift point so that centroid is located at [0,0,0], point and center must be ndarrays polar_point = cartesian_to_spherical(centered_point[2], centered_point[1], centered_point[0]) scaled_polar_point = [ scaler * polar_point[0], polar_point[1], polar_point[2] ] # scale radius relative to centroid scaled_centered_point = np.array( spherical_to_cartesian(scaled_polar_point[0], scaled_polar_point[1], scaled_polar_point[2])) scaled_point = np.add( scaled_centered_point[::-1], center ) # reverse point to be [z,y,x] and revert back to origional location return [round(scaled_point[0]), scaled_point[1], scaled_point[2]]
def readps1_rectangle(ra,dec,width=(0.6,0.6),outfile=None,path='/line12/Pan-STARRS/chunks-qz-star-v2/',silent=True): """ read the PS1 catalog within a spheric rectangle ra,dec: in degrees array """ if np.isscalar(width) or len(width) == 1: width=(width,width) ra=np.deg2rad(ra) dec=np.deg2rad(dec) wra=np.deg2rad(width[0]/np.cos(dec)) wdec=np.deg2rad(width[1]) ra1=ra-wra/2.0 dec1=dec-wdec/2.0 ra2=ra-wra/2.0 dec2=dec+wdec/2.0 ra3=ra+wra/2.0 dec3=dec+wdec/2.0 ra4=ra+wra/2.0 dec4=dec-wdec/2.0 x,y,z=c.spherical_to_cartesian(1.0,[dec1,dec2,dec3,dec4],[ra1,ra2,ra3,ra4]) vertices=np.array([x,y,z]).transpose() pix=query_polygon(32, vertices, inclusive=True) npix=pix.size if not silent: print('Total '+str(npix)+' healpix pixels:') cat=[] for ipix in pix: catname=os.path.join(path,'ps1-'+'%5.5d'%ipix+'.fits') if not os.path.isfile(catname): continue if not silent: print('reading '+catname) cat.append(table.Table.read(catname,format='fits')) totalcat = [] if cat != []: totalcat=table.vstack(cat) if outfile is not None: totalcat.write(outfile,format='fits',overwrite=True) return totalcat
def get_median_lonlat(lon, lat): "lon, lat in degrees" x, y, z = spherical_to_cartesian(1, (lat / 180. * np.pi), (lon / 180. * np.pi)) r, lat, lon = cartesian_to_spherical(np.median(x), np.median(y), np.median(z)) return lon, lat
def run_one_r_sec(pid, scan_name, step, resolution, asp_cal, t0, t1, duration, dis_map, outdir, return_dict): print('run one r sec') num_co = int(resolution/step) #load asp file hdulist = pyfits.open('../AIS_GAL_SCAN/asprta/%s-asprta.fits'%scan_name) co_data = hdulist[1].data length = co_data.shape[0] name = re.split('-', scan_name)[0] scst_tmp = pyfits.open('../AIS_GAL_SCAN/scst/%s-scst.fits'%name)[1].data scst_time = scst_tmp['pktime'] T = co_data['T'] ix_tmp = np.digitize(T, scst_time) ix_mask = ix_tmp<scst_time.shape[0] ix_tmp = ix_tmp[ix_mask] co_data = co_data[ix_mask] hv = scst_tmp['hvnom_nuv'] mask = hv[ix_tmp]>0 ix_tmp = ix_tmp[mask] co_data = co_data[mask] hdulist = pyfits.open('../data/tycho2.fits') star_data = hdulist[1].data length = star_data.shape[0] stars = np.zeros((length,2)) stars[:,0] = star_data['RAJ2000'] stars[:,1] = star_data['DEJ2000'] stars_rad = stars*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T ''' try: asp_cal = np.load('../data/photon_list/%s_asp_new.npy'%scan_name) except IOError: T = co_data['T'] ra = co_data['ra'] dec = co_data['dec'] roll = co_data['roll'] t_new = np.arange((T.shape[0]-1)*200)*0.005+T[0] ra_new = np.interp(t_new, T, ra) dec_new = np.interp(t_new, T, dec) roll_new = np.interp(t_new, T, roll) asp_cal = np.array([t_new, ra_new, dec_new, roll_new]).T np.save('../data/photon_list/%s_asp_new.npy'%scan_name, asp_cal) ''' #cal_initial = int((co_data[1][0]-0.5)*1000) #offsets = np.load('../data/%s/cata/offsets_inter_half_sec.npy'%scan_name) angle_list = [0.] print length data1 = [] data2 = [] data3 = [] start = 0 c_list = [] for start in range(t0, t1, duration): print start data1 = [] data2 = [] data3 = [] info_list = [] for initial_sec in range(start, start+duration): print 'time:%d'%initial_sec file_path = '../data/%s/cata/centroids_rot%d.npy'%(scan_name, initial_sec) intitial_asp = co_data[initial_sec] #rot = intitial_asp[3] #center = np.array([intitial_asp[1], intitial_asp[2]]) #use the first order calibrated asp instead of the original one #center = cal_asp_r(offsets, cal_initial, intitial_asp[0]*1000, intitial_asp[1:3], 200) #print(intitial_asp) skypos = [0.0, 0.0] skyrange = [0.02, 0.02] if step == 1: skyrange = [0.04, 0.04]#[0.3, 0.3] elif step == 0.5: skyrange = [0.04, 0.04] initial_time = intitial_asp[0]-step/2. tranges = [] for sec in range(num_co): tranges.append([initial_time+step*sec, initial_time+step*(sec+1)]) print tranges time_c = np.mean(tranges, axis=1) print 'center time:' print time_c #data = get_data_cal(tranges, scan_name, cal_initial, offsets) data = get_data_fast(tranges, scan_name) print 'number of the photons:' print len(data) centroids = [] center_time = [] for sec in range(num_co): center_time.append(time_c[sec]) if len(data[sec]) == 0: centroid = np.array([0.0, 0.0, 0.0]) centroids.append(centroid) print centroid continue arg = np.argmin(np.absolute(asp_cal[:,0]-time_c[sec])) center = asp_cal[arg, 1:3] data_sec, info, rot = dis_correct(np.array(data[sec], dtype='float64'), dis_map, asp_cal) #data_sec, info = no_dis(np.array(data[sec], dtype='float64'), dis_map, asp_cal) coo1 = np.array(data_sec, dtype='float64') aperture = 0.69 #coo1 = np.array(data[sec], dtype='float64')[:,-3:-1] coo2 = catalog_fits.get_catalog_tycho_fast(center, aperture, stars, stars_car, star_data) #coo2 = catalog_fits.get_catalog(center, aperture) #coo1 = angle_filter(coo1, center, 0.6) #coo2 = angle_filter(coo2, center, 0.6) ''' fig = plt.gcf() fig.set_size_inches(8,8) plt.plot(coo1[:,0], coo1[:,1], '.k', markersize=0.1) plt.plot(coo3[:,0], coo3[:,1], '+r', markersize=12) plt.plot(coo2[:,0], coo2[:,1], '+b', markersize=8) #plt.show() plt.ylabel('Dec') plt.xlabel('RA') plt.xlim(center[0]-0.65, center[0]+0.65) plt.ylim(center[1]-0.65, center[1]+0.65) plt.tight_layout() plt.savefig('/home/dw1519/galex/plots/photons.png', dpi=190) plt.clf() plt.close() ''' ''' #plot field #plt.plot(asp_cal[:,1],'.b') #plt.show() print center print coo1.shape print np.min(coo1[:,0]), np.max(coo1[:,0]) print np.min(coo1[:,1]), np.max(coo1[:,1]) wcs = imagetools.define_wcs(center,[1.5,1.5],width=False,height=False,verbose=0,pixsz=0.002) imsz = imagetools.deg2pix(center, [1.5,1.5], 0.002) foc = wcs.sip_pix2foc(wcs.wcs_world2pix(coo1,1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) H_new = H.copy() data = H_new.byteswap(True).newbyteorder() data = data.copy(order='C') data = data.byteswap(True).newbyteorder() c_data = c3.find_source(data, 1) if c_data is not None: cx, cy, max_value = c_data print cy, cx centroid = wcs.wcs_pix2world(wcs.sip_foc2pix([[cx, cy]],1),1)[0] coo1 = angle_filter_out(coo1, centroid, 0.01) foc = wcs.sip_pix2foc(wcs.wcs_world2pix(coo1,1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), vmax=10) plt.colorbar() plt.show() foc = wcs.sip_pix2foc(wcs.wcs_world2pix(coo2,1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys')) plt.show() ''' #xi, eta = gn.gnomfwd_simple(coo1[:,0], coo1[:,1], center[0], center[1], -rot, 1/36000., 0.0) ''' max_now = -1000 cent_now = [] for angle in angle_list: centroid, max_value, flux = get_corr_map(coo2, coo1, skypos, skyrange, sec, 0.0002, initial_sec, '1') if max_value>max_now: max_now = max_value cent_now = np.append(centroid, angle) #centroid.append(angle) centroids.append(cent_now) print centroids ''' #np.save('../data/%s/cata/time_rot%d.npy'%(scan_name, initial_sec), center_time) #np.save('../data/%s/cata/centroids_rot%d.npy'%(scan_name, initial_sec), centroids) data1.append(coo1) data2.append(coo2) data3.append(rot) info_list.append(info) #plt.plot(data3, '.k') #plt.savefig('/home/dw1519/dw1519/galex/plots/co239_c_l_new/{0}_rot.pdf'.format(start), dpi=190) #plt.clf() data1 = np.concatenate(data1, axis=0) info_list = np.concatenate(info_list, axis=0) data2 = np.concatenate(data2, axis=0) print data2.shape ar, index = np.unique(data2[:,0], return_index=True) data2 = data2[index] print data2.shape ''' sky_data = SkyCoord(data1, unit='deg', frame=FK5, equinox='J2000.0') gal = sky_data.transform_to(Galactic) data1 = np.concatenate((np.array([gal.l.deg]).T, np.array([gal.b.deg]).T), axis=1) sky_data = SkyCoord(data2, unit='deg', frame=FK5, equinox='J2000.0') gal = sky_data.transform_to(Galactic) data2 = np.concatenate((np.array([gal.l.deg]).T, np.array([gal.b.deg]).T), axis=1) data3 = np.concatenate(data3, axis=0) #np.array(data3) ''' ''' H,xedges,yedges=np.histogram2d(info_list[:,0], info_list[:,1]) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal') plt.colorbar() plt.xlabel('xa') plt.ylabel('ya') plt.savefig('/home/dw1519/dw1519/galex/plots/co239_s/xa_ya{0}.pdf'.format(start), dpi=190) plt.clf() H,xedges,yedges=np.histogram2d(info_list[:,0], info_list[:,2]) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal') plt.colorbar() plt.xlabel('xa') plt.ylabel('q') plt.savefig('/home/dw1519/dw1519/galex/plots/co239_s/xa_q{0}.pdf'.format(start), dpi=190) plt.clf() H,xedges,yedges=np.histogram2d(info_list[:,1], info_list[:,2]) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), aspect='equal') plt.colorbar() plt.xlabel('ya') plt.ylabel('q') plt.savefig('/home/dw1519/dw1519/galex/plots/co239_s/ya_q{0}.pdf'.format(start), dpi=190) plt.clf() ''' print data1.shape print data2.shape print info_list.shape centroid, max_value, flux = get_corr_map(data2, data1, skypos, skyrange, sec, 0.0001, initial_sec, '{0}'.format(start), info_list, data3, outdir) print centroid c_list.append(centroid) np.save('{0}/noc_list.npy'.format(outdir), c_list)
def run_one_r_sec(pid, scan_name, step, resolution, asp_cal, t0, t1, duration, dis_map, outdir, return_dict): print('run one r sec') num_co = int(resolution/step) #load asp file hdulist = pyfits.open('../AIS_GAL_SCAN/asprta/%s-asprta.fits'%scan_name) co_data = hdulist[1].data length = co_data.shape[0] name = re.split('-', scan_name)[0] scst_tmp = pyfits.open('../AIS_GAL_SCAN/scst/%s-scst.fits'%name)[1].data scst_time = scst_tmp['pktime'] T = co_data['T'] ix_tmp = np.digitize(T, scst_time) ix_mask = ix_tmp<scst_time.shape[0] ix_tmp = ix_tmp[ix_mask] co_data = co_data[ix_mask] hv = scst_tmp['hvnom_nuv'] mask = hv[ix_tmp]>0 ix_tmp = ix_tmp[mask] co_data = co_data[mask] hdulist = pyfits.open('../data/tycho2.fits') star_data = hdulist[1].data length = star_data.shape[0] stars = np.zeros((length,2)) stars[:,0] = star_data['RAJ2000'] stars[:,1] = star_data['DEJ2000'] stars_rad = stars*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T ''' try: asp_cal = np.load('../data/photon_list/%s_asp_new.npy'%scan_name) except IOError: T = co_data['T'] ra = co_data['ra'] dec = co_data['dec'] roll = co_data['roll'] t_new = np.arange((T.shape[0]-1)*200)*0.005+T[0] ra_new = np.interp(t_new, T, ra) dec_new = np.interp(t_new, T, dec) roll_new = np.interp(t_new, T, roll) asp_cal = np.array([t_new, ra_new, dec_new, roll_new]).T np.save('../data/photon_list/%s_asp_new.npy'%scan_name, asp_cal) ''' #cal_initial = int((co_data[1][0]-0.5)*1000) #offsets = np.load('../data/%s/cata/offsets_inter_half_sec.npy'%scan_name) angle_list = [0.] print length data1 = [] data2 = [] data3 = [] start = 0 c_list = [] for start in range(t0, t1, duration): print start data1 = [] data2 = [] data3 = [] info_list = [] for initial_sec in range(start, start+duration): print 'time:%d'%initial_sec file_path = '../data/%s/cata/centroids_rot%d.npy'%(scan_name, initial_sec) intitial_asp = co_data[initial_sec] #rot = intitial_asp[3] #center = np.array([intitial_asp[1], intitial_asp[2]]) #use the first order calibrated asp instead of the original one #center = cal_asp_r(offsets, cal_initial, intitial_asp[0]*1000, intitial_asp[1:3], 200) #print(intitial_asp) skypos = [0.0, 0.0] skyrange = [0.02, 0.02] if step == 1: skyrange = [0.04, 0.04]#[0.3, 0.3] elif step == 0.5: skyrange = [0.04, 0.04] initial_time = intitial_asp[0]-step/2. tranges = [] for sec in range(num_co): tranges.append([initial_time+step*sec, initial_time+step*(sec+1)]) print tranges time_c = np.mean(tranges, axis=1) print 'center time:' print time_c #data = get_data_cal(tranges, scan_name, cal_initial, offsets) data = get_data_fast(tranges, scan_name) print 'number of the photons:' print len(data) centroids = [] center_time = [] for sec in range(num_co): center_time.append(time_c[sec]) if len(data[sec]) == 0: centroid = np.array([0.0, 0.0, 0.0]) centroids.append(centroid) print centroid continue arg = np.argmin(np.absolute(asp_cal[:,0]-time_c[sec])) center = asp_cal[arg, 1:3] data_sec, info, rot = dis_correct(np.array(data[sec], dtype='float64'), dis_map, asp_cal) #data_sec, info = no_dis(np.array(data[sec], dtype='float64'), dis_map, asp_cal) coo1 = np.array(data_sec, dtype='float64') aperture = 0.69 #coo1 = np.array(data[sec], dtype='float64')[:,-3:-1] coo2 = catalog_fits.get_catalog_tycho_fast(center, aperture, stars, stars_car, star_data) #coo2 = catalog_fits.get_catalog(center, aperture) #coo1 = angle_filter(coo1, center, 0.6) #coo2 = angle_filter(coo2, center, 0.6) data1.append(coo1) data2.append(coo2) data3.append(rot) info_list.append(info) data1 = np.concatenate(data1, axis=0) info_list = np.concatenate(info_list, axis=0) data2 = np.concatenate(data2, axis=0) print data2.shape ar, index = np.unique(data2[:,0], return_index=True) data2 = data2[index] print data2.shape data3 = np.concatenate(data3, axis=0) #np.array(data3) print data1.shape print data2.shape print info_list.shape centroid, max_value, flux = get_corr_map(data2, data1, skypos, skyrange, sec, 0.0001, initial_sec, '{0}'.format(start), info_list, data3, outdir) print centroid c_list.append(centroid) np.save('{0}/noc_list.npy'.format(outdir), c_list)
def run_flat_multichunks_black_sec(pid, file_name, imsz, cata_lists, aperture_list, csvs, chunk_num, return_dict): #star_num = len(count_list) tranges = [] counts = [] cata_car_list = [] for cata in cata_lists: cata[:,1:3] = cata[:,1:3]*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, cata[:,2], cata[:,1]) cata_car = np.array([X,Y,Z], dtype='float64').T cata_car_list.append(cata_car) aperture_list_rad = np.cos(np.array(aperture_list)*np.pi/180.) hdulist = pyfits.open('../AIS_GAL_SCAN/asprta/%s-asprta.fits'%file_name) offsets = np.load('../data/%s/cata/offsets_inter_half_sec.npy'%file_name) co_data = hdulist[1].data initial = int((co_data[1][0]-0.5)*1000) step = 200 chunks = split_seq(csvs, chunk_num) for csv_list in chunks: count = np.zeros(imsz) trange = [0,0] with open(csv_list[0], 'rb') as file: reader = csv.reader(file) trange[0] = int(reader.next()[0]) time = 0 for csv_file in csv_list: with open(csv_file, 'rb') as file: reader = csv.reader(file) data = [] i = 1 id = np.array([1,0,0,0,0,0,1,1,0,0,0])>0 for row in reader: time = int(row[0]) row = cal_photon_r(offsets, initial, time, row, step) center = pycoo.spherical_to_cartesian(1, row[9]*np.pi/180., row[8]*np.pi/180.) in_num = 0 for list_num in range(3): aperture_rad = aperture_list_rad[list_num] cata_car = cata_car_list[list_num] sep = np.dot(cata_car, center) coo = cata_car[sep>=aperture_rad,:] in_num += len(coo) if in_num==0: data.append(row[id]) #hist the data if i%200000 == 0: H = hist_flat(data, imsz) count += H data=H = None data = [] gc.collect() print i i+=1 #hist the remaining data if len(data)>0: H = hist_flat(data, imsz) count += H trange[1] = time tranges.append(trange) counts.append(count) return_dict[pid] = (counts, tranges)
step = 10 offsets = np.load('../data/05_r/cata/offsets300-1343_10_new.npy') print offsets.shape initial = int((co_data[300][0]-0.5)*1000) skypos_f, skyrange_f = ([0, 0], [1.33333336,1.33333336]) imsz_f = imagetools.deg2pix(skypos_f, skyrange_f, 0.0016666667) count_f = np.zeros(imsz_f) wcs_f = imagetools.define_wcs(skypos_f,skyrange_f,width=False,height=False,verbose=0,pixsz=0.0016666667) print imsz_f cata = spi.load_obj('stars05') cata_a = np.array(list(cata)) cata_len = len(cata) cata_a = cata_a*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, cata_a[:,1], cata_a[:,0]) cata_car = np.array([X,Y,Z], dtype='float64').T rad = np.cos(0.00250*np.pi/180.) with open('../data/csv_list05_full') as f: csv_list = f.read().splitlines() for csv_file in csv_list: print csv_file with open(csv_file, 'rb') as file: reader = csv.reader(file) data = [] data_f = [] i = 1 trange[0] = float(reader.next()[0])/1000 id = np.array([1,0,0,0,0,0,0,0,1,1,0])>0 id_f = np.array([1,0,0,0,0,0,1,1,0,0,0])>0
def run_one(pid, file_name, imsz_list, wcs_list, cata_list, csv_list, return_dict): count_list = [] for imsz in imsz_list: count = np.zeros(imsz) count_list.append(count) star_num = len(count_list) cata_list[:,1:3] = cata_list[:,1:3]*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, cata_list[:,2], cata_list[:,1]) cata_car = np.array([X,Y,Z], dtype='float64').T aperture_size = 8./60./60. rad = np.cos(aperture_size*np.pi/180.) annulus = np.cos([9./60./60.*np.pi/180., 11./60./60.*np.pi/180.]) hdulist = pyfits.open('../AIS_GAL_SCAN/asprta/%s-asprta.fits'%file_name) offsets = np.load('../data/%s/cata/offsets1_10_new.npy'%file_name) co_data = hdulist[1].data initial = int((co_data[1][0]-0.5)*1000) step = 10 for csv_file in csv_list: with open(csv_file, 'rb') as file: reader = csv.reader(file) data = [] i = 1 trange[0] = float(reader.next()[0])/1000 id = np.array([1,0,0,0,0,0,0,0,1,1,0])>0 for row in reader: time = int(row[0]) row = cal_photon_r(offsets, initial, time, row, step) #add row into data #data.append(row[id]) center = pycoo.spherical_to_cartesian(1, row[9]*np.pi/180., row[8]*np.pi/180.) sep = np.dot(cata_car, center) coo = cata_list[sep>=rad,:] ann_coo = cata_list[np.logical_and(sep>=annulus[1], sep<=annulus[0]),:] for photon in coo: with open('../data/%s/star/extra/star_%d-%d.csv'%(file_name, photon[0], pid), 'ab') as star_csv: writer = csv.writer(star_csv) writer.writerow(row) star_csv.close() for photon in ann_coo: with open('../data/%s/star/extra/star_bkg_%d-%d.csv'%(file_name, photon[0], pid), 'ab') as star_bkg_csv: writer = csv.writer(star_bkg_csv) writer.writerow(row) star_bkg_csv.close() ''' #hist the data if i%200000 == 0: for star in range(star_num): H = hist(data, wcs_list[star], imsz_list[star]) count_list[star] += H trange[1] = float(time)/1000 data=H = None data = [] gc.collect() print i i+=1 #hist the remaining data if len(data)>0: for star in range(star_num): H = hist(data, wcs_list[star], imsz_list[star]) count_list[star] += H trange[1] = float(time)/1000 ''' tranges.append(trange) return_dict[pid] = (count_list, tranges)
def run_one_r_sec(pid, scan_name, step, resolution, duration, asp_cal, start, end, dis_map, return_dict): print('run one r sec') num_co = int(resolution/step) #load asp file hdulist = pyfits.open('../AIS_GAL_SCAN/asprta/%s-asprta.fits'%scan_name) co_data = hdulist[1].data length = co_data.shape[0] hdulist = pyfits.open('../data/tycho2.fits') star_data = hdulist[1].data length = star_data.shape[0] stars = np.zeros((length,2)) stars[:,0] = star_data['RAJ2000'] stars[:,1] = star_data['DEJ2000'] stars_rad = stars*np.pi/180. X, Y, Z = pycoo.spherical_to_cartesian(1, stars_rad[:,1], stars_rad[:,0]) stars_car = np.array([X,Y,Z], dtype='float64').T ''' try: asp_cal = np.load('../data/photon_list/%s_asp_new.npy'%scan_name) except IOError: T = co_data['T'] ra = co_data['ra'] dec = co_data['dec'] roll = co_data['roll'] t_new = np.arange((T.shape[0]-1)*200)*0.005+T[0] ra_new = np.interp(t_new, T, ra) dec_new = np.interp(t_new, T, dec) roll_new = np.interp(t_new, T, roll) asp_cal = np.array([t_new, ra_new, dec_new, roll_new]).T np.save('../data/photon_list/%s_asp_new.npy'%scan_name, asp_cal) ''' #cal_initial = int((co_data[1][0]-0.5)*1000) #offsets = np.load('../data/%s/cata/offsets_inter_half_sec.npy'%scan_name) angle_list = [0.] print length #start = 33#1300#493#1200 #end = 1340 for initial_sec in range(start, end): print 'time:%d'%initial_sec file_path = '../data/%s/cata/centroids_rot%d.npy'%(scan_name, initial_sec) intitial_asp = co_data[initial_sec] rot = intitial_asp[3] #center = np.array([intitial_asp[1], intitial_asp[2]]) #use the first order calibrated asp instead of the original one #center = cal_asp_r(offsets, cal_initial, intitial_asp[0]*1000, intitial_asp[1:3], 200) #print(intitial_asp) skypos = [0.0, 0.0] skyrange = [0.6, 0.6] ''' if step == 1: skyrange = [0.04, 0.04]#[0.3, 0.3] elif step == 0.5: skyrange = [0.02, 0.02] ''' initial_time = intitial_asp[0] tranges = [] for sec in range(num_co): tranges.append([initial_time+step*sec-duration/2., initial_time+step*sec+duration/2.]) print tranges time_c = np.mean(tranges, axis=1) print 'center time:' print time_c #data = get_data_cal(tranges, scan_name, cal_initial, offsets) data = get_data_fast(tranges, scan_name) print 'number of the photons:' print len(data) centroids = [] center_time = [] for sec in range(num_co): center_time.append(time_c[sec]) if len(data[sec]) == 0: centroid = np.array([0.0, 0.0, 0.0]) centroids.append(centroid) print centroid continue arg = np.argmin(np.absolute(asp_cal[:,0]-time_c[sec])) center = asp_cal[arg, 1:3] data_sec = dis_correct(np.array(data[sec], dtype='float64'), dis_map, asp_cal) coo1 = np.array(data_sec, dtype='float64') aperture = 0.69 #coo1 = np.array(data[sec], dtype='float64')[:,-3:-1] #coo2 = catalog_fits.get_catalog_tycho(center, aperture) coo2 = catalog_fits.get_catalog_tycho_fast(center, aperture, stars, stars_car, star_data) coo1, mask = angle_filter(coo1, center, 0.6) time_sec = np.array(data[sec], dtype='float64')[mask,0]/1000. weights = gaussian(time_sec-time_c[sec], 0, np.std(time_sec-time_c[sec])) coo2, mask = angle_filter(coo2, center, 0.6) ''' fig = plt.gcf() fig.set_size_inches(8,8) plt.plot(coo1[:,0], coo1[:,1], '.k', markersize=0.1) plt.plot(coo3[:,0], coo3[:,1], '+r', markersize=12) plt.plot(coo2[:,0], coo2[:,1], '+b', markersize=8) #plt.show() plt.ylabel('Dec') plt.xlabel('RA') plt.xlim(center[0]-0.65, center[0]+0.65) plt.ylim(center[1]-0.65, center[1]+0.65) plt.tight_layout() plt.savefig('/home/dw1519/galex/plots/photons.png', dpi=190) plt.clf() plt.close() ''' ''' #plot field #plt.plot(asp_cal[:,1],'.b') #plt.show() print center print coo1.shape print np.min(coo1[:,0]), np.max(coo1[:,0]) print np.min(coo1[:,1]), np.max(coo1[:,1]) wcs = imagetools.define_wcs(center,[1.5,1.5],width=False,height=False,verbose=0,pixsz=0.002) imsz = imagetools.deg2pix(center, [1.5,1.5], 0.002) foc = wcs.sip_pix2foc(wcs.wcs_world2pix(coo1,1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) H_new = H.copy() data = H_new.byteswap(True).newbyteorder() data = data.copy(order='C') data = data.byteswap(True).newbyteorder() c_data = c3.find_source(data, 1) if c_data is not None: cx, cy, max_value = c_data print cy, cx centroid = wcs.wcs_pix2world(wcs.sip_foc2pix([[cx, cy]],1),1)[0] coo1 = angle_filter_out(coo1, centroid, 0.01) foc = wcs.sip_pix2foc(wcs.wcs_world2pix(coo1,1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys'), vmax=10) plt.colorbar() plt.show() foc = wcs.sip_pix2foc(wcs.wcs_world2pix(coo2,1),1) H,xedges,yedges=np.histogram2d(foc[:,1]-0.5, foc[:,0]-0.5,\ bins=imsz, range=([ [0,imsz[0]],[0,imsz[1]] ])) plt.imshow(H,interpolation='None', cmap=plt.get_cmap('Greys')) plt.show() ''' #xi, eta = gn.gnomfwd_simple(coo1[:,0], coo1[:,1], center[0], center[1], -rot, 1/36000., 0.0) max_now = -1000 cent_now = [] for angle in angle_list: centroid, max_value, flux = get_corr_map(coo2, coo1, skypos, skyrange, sec, 0.0002, initial_sec, '1', weights) if max_value>max_now: max_now = max_value cent_now = np.append(centroid, angle) #centroid.append(angle) centroids.append(cent_now) print centroids np.save('../data/%s/cata/time_rot%d.npy'%(scan_name, initial_sec), center_time) np.save('../data/%s/cata/centroids_rot%d.npy'%(scan_name, initial_sec), centroids)