예제 #1
0
def fnoise_list(mode = 'GFields'):
    filelist = np.loadtxt(sys.argv[1],dtype=str)
    obsid = np.array([int(f.split('-')[1]) for f in filelist])
    filelist = filelist[np.argsort(obsid)]
    obsid = np.sort(obsid)

    fnoise = np.zeros(filelist.size)
    enoise = np.zeros(filelist.size)
    feed = None
    ifeed = 0
    isfg4 = np.zeros(filelist.size,dtype=bool)
    dist = np.zeros(filelist.size)
    pyplot.figure(figsize=(20,5))
    for ifile, filename in enumerate(filelist):
        
        print(filename)
        try:
            data = h5py.File(filename,'r')
        except OSError:
            print('{} cannot be opened (Resource unavailable)'.format(filename))
            fnoise[ifile] = np.nan

        if mode.lower() in data['level1/comap'].attrs['source'].decode('utf-8').lower():
            isfg4[ifile] = True

        try:
            fits = data['level2/fnoise_fits'][ifeed,-1,1:-2,:]
            fnoise[ifile] = np.median(fits[:,1])
            enoise[ifile] = np.sqrt(np.median(np.abs(fits[:,1]-fnoise[ifile])**2))*1.4826
        except:
            print('{} not processed'.format(filename.split('/')[-1]))
            fnoise[ifile] = np.nan

        if isinstance(feed, type(None)):
            feed = data['level1/spectrometer/feeds'][ifeed]

        # Calculate sun distance
        mjd = data['level1/spectrometer/MJD'][0:1]
        lon=-118.2941
        lat=37.2314
        ra_sun, dec_sun, raddist = Coordinates.getPlanetPosition('SUN', lon, lat, mjd)
        az_sun, el_sun = Coordinates.e2h(ra_sun, dec_sun, mjd, lon, lat)
        ra  = data['level1/spectrometer/pixel_pointing/pixel_ra'][0,0:1]
        dec = data['level1/spectrometer/pixel_pointing/pixel_dec'][0,0:1]
        dist[ifile] = el_sun[0]#angular_seperation(ra_sun, ra, dec_sun, dec)
        data.close()
    good = (fnoise > -1.2) & (fnoise < -0.5) & np.isfinite(fnoise) & (fnoise != -1)
    with open('Plots/{}_good.list'.format(mode),'w') as f:
        for line in filelist[good]:
            f.write('{}\n'.format(line))

    pyplot.errorbar(np.arange(fnoise.size),fnoise,fmt='.',yerr=enoise,capsize=3)
    pyplot.errorbar(np.arange(fnoise.size)[good],fnoise[good],fmt='.',yerr=enoise[good],capsize=3)

    pyplot.xticks(np.arange(fnoise.size),obsid, rotation=90,size=8)
    pyplot.ylim(-2,-0.8)
    pyplot.grid()
    pyplot.savefig('Plots/Fnoise_feed{}_{}.png'.format(feed,mode),bbox_inches='tight')
    pyplot.savefig('Plots/Fnoise_feed{}_{}.pdf'.format(feed,mode),bbox_inches='tight')
    pyplot.clf()
예제 #2
0
    def setLevel1(self, datafile, source=''):
        """
        """
        self.setSource(source)

        self.teleLon = self.datafile['hk/antenna0/tracker/siteActual'][
            0, 0] / (60.**2 * 1000.)
        self.teleLat = self.datafile['hk/antenna0/tracker/siteActual'][
            0, 1] / (60.**2 * 1000.)

        self.datafile = datafile

        self.attributes = self.datafile['comap'].attrs

        self.tsamp = float(self.attributes['tsamp'].decode())
        self.obsid = self.attributes['obsid'].decode()
        self.source = self.attributes['source'].decode()

        # load but do not read yet.
        self.x = self.datafile['spectrometer/pixel_pointing/pixel_ra']
        self.y = self.datafile['spectrometer/pixel_pointing/pixel_dec']
        self.utc = self.datafile['spectrometer/MJD']
        sunra, sundec, sundist = Coordinates.getPlanetPosition('Sun',
                                                               self.teleLon,
                                                               self.teleLat,
                                                               self.utc[:],
                                                               returnall=True)
        sunra, sundec = Coordinates.precess(sunra, sundec, self.utc[:])
        pa = Coordinates.pa(sunra, sundec, self.utc, self.teleLon,
                            self.teleLat)
        for i in range(self.x.shape[0]):
            self.x[i, :], self.y[i, :] = Coordinates.Rotate(
                self.x[i, :], self.y[i, :], sunra, sundec, -pa)

        self.xCoordinateName = r'$\Delta$A'
        self.yCoordinateName = r'$\Delta$E'

        self.el = self.datafile['spectrometer/pixel_pointing/pixel_el']

        self.tod_bavg = self.datafile['spectrometer/band_average']
        self.features = self.datafile['spectrometer/features'][:]
        self.mask = np.ones(self.features.size).astype(int)
        self.mask[self.featureBits(self.features.astype(float), 13)] = 0
        self.mask[self.features == 0] = 0
        self.mask = self.mask.astype(int)

        # If we don't spe
        self.setCrval()

        self.setWCS(self.crval, self.cdelt, self.crpix, self.ctype)
예제 #3
0
    def simulate(self,data):
        todshape = data['spectrometer/band_average'].shape
        self.feeds = data['spectrometer/feeds'][:]
        self.mjd = data['spectrometer/MJD'][:]
        self.az  = data['spectrometer/pixel_pointing/pixel_az'][...]
        self.el  = data['spectrometer/pixel_pointing/pixel_el'][...]
        self.ra  = data['spectrometer/pixel_pointing/pixel_ra'][...]
        self.dec = data['spectrometer/pixel_pointing/pixel_dec'][...]

        self.tod = np.zeros(todshape)

        self.source.ra,self.source.dec = Coordinates.precess2year(np.array([self.source.x]),
                                                                  np.array([self.source.y]),
                                                                  np.array([np.mean(self.mjd)]))

        nHorns, nSBs, nSamples = todshape
        rot = hp.rotator.Rotator(rot=[self.source.ra, self.source.dec])
        for j in range(nSBs):
            for i in tqdm(range(nHorns)):
                decRot, raRot = rot((90-self.dec[i,:])*np.pi/180., self.ra[i,:]*np.pi/180.)
                #self.tod[i,j,:] += self.Gauss2D(self.source.amplitude(30, self.fwhm),
                #                           0,0,self.fwhm/2.355,
                #                           raRot*180./np.pi, (np.pi/2.-decRot)*180./np.pi)

                self.tod[i,j,:] += self.Diffuse(self.ra[i,:],self.dec[i,:])
                self.tod[i,j,:] += self.Trec
                #self.tod[i,j,:] += self.Atmos(self.el[i,:])
                self.tod[i,j,:] += self.ReceiverNoise(self.tod[i,j,:])
예제 #4
0
def query_slice(x0, y0, x1, y1, wcs, shape, width=None):

    nypix, nxpix = shape
    xpix, ypix = np.meshgrid(np.arange(nxpix), np.arange(nypix))
    xpix_world, ypix_world = wcs.wcs_pix2world(xpix.flatten(), ypix.flatten(),
                                               0)

    if isinstance(width, type(None)):
        width = np.abs(wcs.wcs.cdelt[1])

    m = (y1 - y0) / (x1 - x0)
    yvec = m * (xpix_world - x0) + y0

    xmid = (x1 + x0) / 2.
    if x1 != x0:
        xwidth = np.abs(x1 - x0) / 2.
    else:
        xwidth = width
    ymid = (y1 + y0) / 2.
    if y1 != y0:
        ywidth = np.abs(y1 - y0) / 2.
    else:
        ywidth = width

    select = (np.abs(yvec - ypix_world) < width) & (np.abs(
        ypix_world - ymid) < ywidth) & (np.abs(xpix_world - xmid) < xwidth)
    angular_dist = Coordinates.AngularSeperation(x0, y0, xpix_world[select],
                                                 ypix_world[select])
    return select, xpix_world[select], ypix_world[select], angular_dist
예제 #5
0
    def karim2014(self,
                  nu,
                  mjd=51544,
                  lon=0,
                  lat=0,
                  source='jupiter',
                  allpos=False,
                  return_jansky=False,
                  **kwargs):
        """
        args:
        nu - in GHz
        mjd - 
        """

        self.P = {'a': jupfit[0], 'b': jupfit[1], 'c': 0}

        r0, d0, dist = Coordinates.getPlanetPosition(source,
                                                     lon,
                                                     lat,
                                                     mjd,
                                                     allpos=allpos)
        jupAng = self.jupAng0 * (5.2 / dist)**2

        if return_jansky:
            return 2. * kb * (nu / cspeed)**2 * 10**jupfit(np.log10(
                nu / 30.)) * jupAng * 1e26
        else:
            return 10**jupfit(np.log10(nu / 30.)) * jupAng, dist
예제 #6
0
    def average_obs(self,filename,data):
        """
        Average TOD together
        """

        # --- Average down the data
        nHorns, nSBs, nChans, nSamples = data['spectrometer/tod'].shape
        nHorns = len(self.feeds)


        frequencies = data['spectrometer/frequency'][...]
        # Averaging the data either using a Tsys/Calvane measurement or assume equal weights
        try:
            # Altered to allow Skydip files to be calibrated using the neighbouring
            # cal-vane files (following SKYDIP-LISSAJOUS obs. plan)
            if 'Sky nod' in self.comment:
                cname, gain, tsys,spikes = self.getcalibration_skydip(data)
            else:
                cname, gain, tsys,spikes = self.getcalibration_obs(data)
        except ValueError:
            cname = '{}/{}_{}'.format(self.calvanedir,self.calvane_prefix,fname)
            gain = np.ones((2, nHorns, nSBs, nChans))
            tsys = np.ones((2, nHorns, nSBs, nChans))

        crval = [(18 + 47./60.+34.81/60.**2)*15,-(1 + 56./60.+31./60.**2)]

        # Future: Astro Calibration
        # if self.cal_mode.upper() == 'ASTRO':
        # gain = self.getcalibration_astro(data)
        self.output = np.zeros((nHorns,len(self.rrl_frequencies),11,nSamples))
        self.spectra  = np.zeros((len(self.feeds),len(self.rrl_qnumbers),21,nSamples))*np.nan
        self.velocity = np.zeros((len(self.feeds),len(self.rrl_qnumbers),21))*np.nan
        self.frequency = np.zeros((len(self.feeds),len(self.rrl_qnumbers),21))*np.nan
        for ifeed, feed in enumerate(tqdm(self.feeds,desc=self.name)):
            feed_array_index = self.feed_dict[feed]
            ra  = data['spectrometer/pixel_pointing/pixel_ra'][feed_array_index,...]
            dec = data['spectrometer/pixel_pointing/pixel_dec'][feed_array_index,...] 

            select = np.where((Coordinates.AngularSeperation(ra,dec,crval[0],crval[1]) < 1.5/60.))[0]
            d = data['spectrometer/tod'][feed_array_index,...] 
            #for rllfreq,frequency in enumerate(self.rrl_frequencies[1:2]):
            for iqno,(qno,rrl_freq) in enumerate(zip(self.rrl_qnumbers,self.rrl_frequencies)):
                for sb in range(nSBs):
                    # Weights/gains already snipped to just the feeds we want
                    if (rrl_freq > np.min(frequencies[sb])) & (rrl_freq < np.max(frequencies[sb])):
                        w, g,chan_flag = 1./tsys[0,ifeed, sb, :]**2, gain[0,ifeed, sb, :], spikes[0,ifeed,sb,:]
                        w[chan_flag] = 0
                        z = d[sb,:,:]
                        s1 = z[:,:]/g[:,None]
                        velocity = self.frequency2velocity(rrl_freq,frequencies[sb,:])
                        ichan = np.argmin((rrl_freq - frequencies[sb,:])**2)
                        lo = int(np.max([ichan - 10,0]))
                        hi = int(np.min([ichan + 11,nChans]))
                    
                        self.spectra[ifeed,iqno,:(hi-lo),:] = s1[lo:hi]
                        self.velocity[ifeed,iqno,:(hi-lo)]  = velocity[lo:hi]
                        self.frequency[ifeed,iqno,:(hi-lo)]  = frequencies[sb,lo:hi]
예제 #7
0
    def setWCS(self, *args):
        """
        Declare world coordinate system for plots
        """

        crval, cdelt, crpix, ctype, nxpix, nypix = args
        if isinstance(crval[0], str):
            crval[0] = Coordinates.sex2deg(crval[0], hours=True)
            crval[1] = Coordinates.sex2deg(crval[1], hours=False)

        self.wcs = wcs.WCS(naxis=2)
        self.wcs.wcs.crval = crval
        self.wcs.wcs.cdelt = cdelt
        self.wcs.wcs.crpix = crpix
        self.wcs.wcs.ctype = ctype

        self.crval = self.wcs.wcs.crval
        self.cdelt = self.wcs.wcs.cdelt
        self.crpix = self.wcs.wcs.crpix
        self.ctype = self.wcs.wcs.ctype
        self.nxpix = nxpix
        self.nypix = nypix
예제 #8
0
    def run(self, data):
        """
        Expects a level2 file structure to be passed.
        """
        fname = data.filename.split('/')[-1]

        az = data['level1/spectrometer/pixel_pointing/pixel_az'][0, :]
        el = data['level1/spectrometer/pixel_pointing/pixel_el'][0, :]
        mjd = data['level1/spectrometer/MJD'][:]

        self.distances = {k: np.zeros(az.size) for k in ['sun', 'moon']}

        for src, v in self.distances.items():
            s_az, s_el, s_ra, s_dec = Coordinates.sourcePosition(
                src, mjd, Coordinates.comap_longitude,
                Coordinates.comap_latitude)
            self.distances[src] = Coordinates.AngularSeperation(
                az, el, s_az, s_el)

        sources = list(self.distances.keys())
        for src in sources:
            self.distances[f'{src}_mean'] = np.array(
                [np.mean(self.distances[src])])
예제 #9
0
    def readPixels(self, i, filename):
        """
        Reads data
        """

        d = h5py.File(filename, 'r')

        # --- Feed position indices can change
        self.FeedIndex = GetFeeds(d['level1/spectrometer/feeds'][...],
                                  self.Feeds)

        # We store all the pointing information
        #x0  = d['level1/spectrometer/pixel_pointing/pixel_ra'][self.FeedIndex,:]
        #y0  = d['level1/spectrometer/pixel_pointing/pixel_dec'][self.FeedIndex,:]
        x = d['level1/spectrometer/pixel_pointing/pixel_az'][self.FeedIndex, :]
        y = d['level1/spectrometer/pixel_pointing/pixel_el'][self.FeedIndex, :]
        mjd = d['level1/spectrometer/MJD'][:]
        for j in range(x.shape[0]):
            x[j], y[j] = Coordinates.h2e_full(x[j], y[j], mjd,
                                              Coordinates.comap_longitude,
                                              Coordinates.comap_latitude)

        #pyplot.plot((x0[0]-x[0])*60)
        #pyplot.plot((y0[0]-y[0])*60)
        #pyplot.show()

        scan_edges = d['level2/Statistics/scan_edges'][...]
        pixels = np.zeros((x.shape[0], self.datasizes[i]))
        last = 0
        for iscan, (start, end) in enumerate(scan_edges):
            N = int((end - start) // self.offset_length * self.offset_length)
            end = start + N
            xc = x[:, start:end]
            yc = y[:, start:end]
            yshape = yc.shape
            # convert to Galactic
            if 'GLON' in self.naive.wcs.wcs.ctype[0]:
                rot = hp.rotator.Rotator(coord=['C', 'G'])
                gb, gl = rot((90 - yc.flatten()) * np.pi / 180.,
                             xc.flatten() * np.pi / 180.)
                xc, yc = gl * 180. / np.pi, (np.pi / 2 - gb) * 180. / np.pi

            pixels[:, last:last + N] = np.reshape(
                self.naive.getFlatPixels(xc.flatten(), yc.flatten()), yshape)
            last += N

        self.pixels[self.chunks[i][0]:self.chunks[i][1]] = pixels.flatten()
        d.close()
예제 #10
0
    def sky_signal(self,data,avg_tod):
        """
        1) Read in a sky map
        2) Sample at observed pixels
        3) Return time ordered data
        """

        feeds = np.arange(self.i_nFeeds,dtype=int)

        ra     = data['spectrometer/pixel_pointing/pixel_ra'][...]
        dec    = data['spectrometer/pixel_pointing/pixel_dec'][...]

        for ifeed in tqdm(feeds.flatten()):
            gl, gb = Coordinates.e2g(ra[ifeed], dec[ifeed]) 
            pixels = ang2pixWCS(self.signal_map_wcs, gl, gb,self.signal_map.shape)
            avg_tod[ifeed] += self.signal_map_flat[None,None,pixels]
예제 #11
0
    def readPixels(self, i, filename):
        """
        Reads data
        """

        d = h5py.File(filename, 'r')

        # --- Feed position indices can change
        self.FeedIndex = GetFeeds(d['level1/spectrometer/feeds'][...],
                                  self.Feeds)

        # We store all the pointing information
        x0 = d['level1/spectrometer/pixel_pointing/pixel_ra'][
            self.FeedIndex, :]
        y0 = d['level1/spectrometer/pixel_pointing/pixel_dec'][
            self.FeedIndex, :]
        x = d['level1/spectrometer/pixel_pointing/pixel_az'][self.FeedIndex, :]
        y = d['level1/spectrometer/pixel_pointing/pixel_el'][self.FeedIndex, :]
        mjd = d['level1/spectrometer/MJD'][:]
        for i in range(x.shape[0]):
            x[i], y[i] = Coordinates.h2e_full(x[i], y[i], mjd,
                                              Coordinates.comap_longitude,
                                              Coordinates.comap_latitude)

        scan_edges = d['level2/Statistics/scan_edges'][...]
        pixels = np.zeros((x.shape[0], self.datasizes[i]))
        last = 0
        for iscan, (start, end) in enumerate(scan_edges):
            N = int((end - start) // self.offset_length * self.offset_length)
            end = start + N
            xc = x[:, start:end]
            yc = y[:, start:end]
            pixels[:, last:last + N] = hp.ang2pix(
                self.nside, (np.pi / 2. - yc * np.pi / 180.),
                xc * np.pi / 180.,
                nest=True)
            last += N

        self.pixels[self.chunks[i][0]:self.chunks[i][1]] = pixels.flatten()
예제 #12
0
def JupiterFlux(nu,
                mjd,
                lon=0,
                lat=0,
                source='jupiter',
                allpos=False,
                return_jansky=False):
    """
    """

    r0, d0, dist = Coordinates.getPlanetPosition(source,
                                                 lon,
                                                 lat,
                                                 mjd,
                                                 allpos=allpos)

    jupAng = jupAng0 * (5.2 / dist)**2

    if return_jansky:
        return 2. * kb * (nu / cspeed)**2 * 10**jupfit(np.log10(
            nu / 30.)) * jupAng * 1e26
    else:
        return 10**jupfit(np.log10(nu / 30.)) * jupAng, dist
예제 #13
0
def udgrade_map_wcs(map_in,
                    wcs_in,
                    wcs_target,
                    shape_in,
                    shape_target,
                    ordering='C',
                    weights=None,
                    mask=None,
                    mask_wcs=None):
    """
    """

    if isinstance(weights, type(None)):
        weights = np.ones(map_in.size)
    if isinstance(mask, type(None)):
        mask = np.zeros(map_in.size, dtype=bool)

    # Get pixel coordinates of the input wcs
    nypix, nxpix = shape_in
    ypix, xpix = np.meshgrid(np.arange(nypix), np.arange(nxpix))
    if ordering == 'C':
        ra, dec = wcs_in.wcs_pix2world(xpix.T.flatten(), ypix.T.flatten(), 0)
    else:
        ra, dec = wcs_in.wcs_pix2world(xpix.flatten(), ypix.flatten(), 0)

    c0 = wcs_in.wcs.ctype[0].split('-')[0]
    c1 = wcs_target.wcs.ctype[0].split('-')[0]
    if c0 != c1:
        if c0 == 'GLON':
            ra, dec = Coordinates.g2e(ra, dec)
        else:
            ra, dec = Coordinates.e2g(ra, dec)

    if not isinstance(mask_wcs, type(None)):
        #nypix,nxpix = mask.shape
        #ypix,xpix = np.meshgrid(np.arange(nypix),np.arange(nxpix))
        #ra_mask, dec_mask = wcs_in.wcs_pix2world(xpix.flatten(), ypix.flatten(),0)

        ra_mask, dec_mask = wcs_in.wcs_pix2world(xpix.T.flatten(),
                                                 ypix.T.flatten(), 0)
        c0 = wcs_in.wcs.ctype[0].split('-')[0]
        c1 = mask_wcs.wcs.ctype[0].split('-')[0]
        if c0 != c1:
            if c0 == 'GLON':
                ra_mask, dec_mask = Coordinates.g2e(ra_mask, dec_mask)
            else:
                ra_mask, dec_mask = Coordinates.e2g(ra_mask, dec_mask)
        #xp_mask, yp_mask = mask_wcs.wcs_world2pix(ra_mask, dec_mask, 0)

        pix_mask = ang2pixWCS(mask_wcs, ra_mask, dec_mask, mask.shape)
        mask_flat = mask.flatten()
        weights[~mask_flat[pix_mask]] = 0

    # Convert to pixel coordinate of the output wcs
    #pix_target = ang2pixWCS(wcs_target, ra.flatten(), dec.flatten(), ctype=wcs_target.wcs.ctype)

    nypix, nxpix = shape_target
    xpix, ypix = np.floor(
        np.array(wcs_target.wcs_world2pix(ra.flatten(), dec.flatten(),
                                          0))).astype('int64')
    pix_target = (xpix + ypix * nxpix).astype(np.int64)

    bad = (xpix >= nxpix) | (xpix < 0) | (ypix >= nypix) | (nypix < 0)
    pix_target[bad] = -1

    # Create empty target map
    map_out = np.zeros(shape_target).flatten().astype(np.float64)
    hit_out = np.zeros(shape_target).flatten().astype(np.float64)

    # Bin data to target map
    good = np.isfinite(map_in) & np.isfinite(weights) & (weights > 0) & (
        pix_target != -1)
    binFuncs.binValues(map_out,
                       pix_target[good].astype(np.int64),
                       weights=(map_in[good] / weights[good]).astype(
                           np.float64))  #, mask=good.astype(np.int64))
    binFuncs.binValues(hit_out,
                       pix_target[good].astype(np.int64),
                       weights=(1. / weights[good]).astype(
                           np.float64))  #,mask=good.astype(np.int64))

    return np.reshape(map_out / hit_out,
                      shape_target), np.reshape(1. / hit_out, shape_target)
예제 #14
0
    def create_maps(self, data, tod, filters, sel):
        """
        Bin maps into instrument frame centred on source
        """

        mjd = data['level1/spectrometer/MJD'][:]

        # We do Jupiter in the Az/El frame but celestial in sky frame
        #if self.source.upper() == 'JUPITER':
        az = data['level1/spectrometer/pixel_pointing/pixel_az'][:]
        el = data['level1/spectrometer/pixel_pointing/pixel_el'][:]
        N = az.shape[1] // 2 * 2
        daz = np.gradient(az[0, :]) * 50.
        daz = daz[sel]
        az = az[:, sel]
        el = el[:, sel]
        cw = daz > 1e-2
        ccw = daz < 1e-2

        mjd = mjd[sel]

        npix = self.Nx * self.Ny

        temp_maps = {
            'map': np.zeros(npix, dtype=np.float64),
            'cov': np.zeros(npix, dtype=np.float64)
        }

        maps = {
            'map':
            np.zeros(
                (tod.shape[0], tod.shape[1], tod.shape[2], self.Nx, self.Ny)),
            'cov':
            np.zeros(
                (tod.shape[0], tod.shape[1], tod.shape[2], self.Nx, self.Ny))
        }

        feed_avg = {
            'map': np.zeros((tod.shape[0], self.Nx, self.Ny)),
            'cov': np.zeros((tod.shape[0], self.Nx, self.Ny))
        }

        scan_maps = {
            'CW': {
                'map': np.zeros((self.Nx, self.Ny)),
                'cov': np.zeros((self.Nx, self.Ny))
            },
            'CCW': {
                'map': np.zeros((self.Nx, self.Ny)),
                'cov': np.zeros((self.Nx, self.Ny))
            }
        }

        azSource, elSource, raSource, decSource = Coordinates.sourcePosition(
            self.source, mjd, self.lon, self.lat)
        self.src_el = np.mean(elSource)
        self.src_az = np.mean(azSource)
        for ifeed in tqdm(self.feedlist,
                          desc=f'{self.name}:create_maps:{self.source}'):
            feed_tod = tod[ifeed, ...]

            #if self.source.upper() == 'JUPITER':
            x, y = Coordinates.Rotate(azSource, elSource, az[ifeed, :],
                                      el[ifeed, :], 0)

            pixels, pX, pY = self.getpixels(x, y, self.dx, self.dy, self.Nx,
                                            self.Ny)

            mask = np.ones(pixels.size, dtype=int)
            for isb in range(tod.shape[1]):
                for ichan in range(1, tod.shape[2] - 1):  # Always skip edges
                    for k in temp_maps.keys():
                        temp_maps[k][:] = 0.

                    z = (feed_tod[isb, ichan, sel] -
                         filters[ifeed, isb, ichan])
                    mask[:] = 1
                    mask[(pixels == -1) | np.isnan(z) | np.isinf(z)] = 0

                    if np.sum(np.isfinite(z)) == 0:
                        continue

                    rms = stats.AutoRMS(z)

                    weights = {
                        'map': z.astype(np.float64) / rms**2,
                        'cov': np.ones(z.size) / rms**2
                    }
                    for k in temp_maps.keys():
                        binFuncs.binValues(temp_maps[k],
                                           pixels,
                                           weights=weights[k],
                                           mask=mask)
                        maps[k][ifeed, isb, ichan,
                                ...] = np.reshape(temp_maps[k],
                                                  (self.Ny, self.Nx))
                        feed_avg[k][ifeed,
                                    ...] += np.reshape(temp_maps[k],
                                                       (self.Ny, self.Nx))

                    if (ifeed == 0):
                        for (key, direction) in zip(['CW', 'CCW'], [cw, ccw]):
                            for k in temp_maps.keys():
                                temp_maps[k][:] = 0.
                                binFuncs.binValues(
                                    temp_maps[k],
                                    pixels[direction],
                                    weights=weights[k][direction],
                                    mask=mask[direction])
                                scan_maps[key][k] += np.reshape(
                                    temp_maps[k], (self.Ny, self.Nx))

        xygrid = np.meshgrid(
            (np.arange(self.Nx) + 0.5) * self.dx - self.Nx * self.dx / 2.,
            (np.arange(self.Ny) + 0.5) * self.dy - self.Ny * self.dy / 2.)

        feed_avg['xygrid'] = xygrid
        maps['xygrid'] = xygrid

        feed_avg = self.average_maps(feed_avg)

        for key in scan_maps.keys():
            scan_maps[key] = self.average_maps(scan_maps[key])
            scan_maps[key]['xygrid'] = xygrid

        map_axes = np.array([a for a in maps['map'].shape])
        map_axes[2] = int(map_axes[2] / self.binwidth)
        map_axes = np.insert(map_axes, 3, self.binwidth)
        maps['map'] = np.nansum(np.reshape(maps['map'], map_axes), axis=3)
        maps['cov'] = np.nansum(np.reshape(maps['cov'], map_axes), axis=3)
        maps = self.average_maps(maps)

        self.map_freqs = np.mean(np.reshape(
            data[f'{self.level2}/frequency'][...], map_axes[1:4]),
                                 axis=-1)

        return maps, feed_avg, scan_maps
예제 #15
0
    def MakeMap(self, tod, ra, dec, mjd, el):
        #takes a 1D tod array and makes a simple map

        #produce arrays for mapping
        npix = self.naxis[0] * self.naxis[1]

        pixbins = np.arange(0, npix + 1).astype(int)

        nHorns, nSBs, nChans, nSamples = tod.shape
        rms = Filtering.calcRMS(tod)

        maps = np.zeros((nHorns, nSBs, nChans, self.naxis[0], self.naxis[1]))
        for i in range(nHorns):

            good = (np.isnan(ra[i, :]) == False) & (np.isnan(tod[i, 0, 0])
                                                    == False)
            pa = Coordinates.pa(ra[i, good], dec[i, good], mjd[good], self.lon,
                                self.lat)
            x, y = Coordinates.Rotate(ra[i, good], dec[i, good], self.x0,
                                      self.y0, -pa)

            nbins = 10
            xbins = np.linspace(np.min(x), np.max(x), nbins + 1)
            xmids = (xbins[1:] + xbins[:-1]) / 2.
            xbw, _ = np.histogram(x, xbins)
            ybw, _ = np.histogram(y, xbins)

            todAvg = np.nanmean(np.nanmean(tod[i, ...], axis=0), axis=0)
            fitx0, fity0 = self.initialPeak(todAvg[good], x, y)
            r = np.sqrt((x - fitx0)**2 + (y - fity0)**2)
            close = (r < 6. / 60.)

            pix = ang2pixWCS(self.wcs, x, y).astype('int')
            mask = np.where((pix != -1))[0]

            h, b = np.histogram(pix,
                                pixbins,
                                weights=(pix != -1).astype(float))
            self.hits = np.reshape(h, (self.naxis[0], self.naxis[1]))

            for j in range(nSBs):
                for k in range(1):  #nChans):
                    todmap = tod[i, j, k, good]

                    if self.filtertod:
                        txbw, _ = np.histogram(x, xbins, weights=todmap)
                        tybw, _ = np.histogram(y, xbins, weights=todmap)
                        fb = txbw / xbw
                        gd = np.isfinite(fb)
                        pmdl = np.poly1d(np.polyfit(xmids[gd], fb[gd], 1))
                        todmap -= pmdl(x)
                        fb = tybw / ybw
                        gd = np.isfinite(fb)
                        pmdl = np.poly1d(np.polyfit(xmids[gd], fb[gd], 1))
                        todmap -= pmdl(y)

                    w, b = np.histogram(pix[mask],
                                        pixbins,
                                        weights=todmap[mask])
                    #                    w, b = np.histogram(pix[:], pixbins, weights=tod[i,j,k,:])
                    m = np.reshape(w, (self.naxis[0], self.naxis[1]))
                    maps[i, j, k, ...] = m / self.hits

        return maps
예제 #16
0
 def getJupiter(self, data):
     mjd = data.getdset('spectrometer/MJD')
     self.x0, self.y0, self.dist = Coordinates.getPlanetPosition(
         'Jupiter', self.lon, self.lat, mjd)
     return self.x0, self.y0, self.dist
예제 #17
0
def fnoise_plots(mode,ifeed):
    filelist = np.loadtxt(sys.argv[1],dtype=str)
    obsid = np.array([int(f.split('-')[1]) for f in filelist])
    filelist = filelist[np.argsort(obsid)]
    obsid = np.sort(obsid)

    fnoise = np.zeros(filelist.size)
    enoise = np.zeros(filelist.size)
    feed = None
    isfg4 = np.zeros(filelist.size,dtype=bool)
    dist = np.zeros(filelist.size)

    fnoise_power = np.zeros((filelist.size,64*4))
    alphas = np.zeros((filelist.size,64*4))

    for ifile, filename in enumerate(filelist):
        
        try:
            data = h5py.File(filename,'r')
        except OSError:
            print('{} cannot be opened (Resource unavailable)'.format(filename))
            fnoise[ifile] = np.nan

        if mode.lower() in data['level1/comap'].attrs['source'].decode('utf-8').lower():
            isfg4[ifile] = True

        try:
            fits = data['level2/fnoise_fits'][ifeed,:,:,:]
            fnoise[ifile] = np.median(fits[:,1])
            enoise[ifile] = np.sqrt(np.median(np.abs(fits[:,1]-fnoise[ifile])**2))*1.4826
            ps = data['level2/powerspectra'][ifeed,:,:,:]
            rms = data['level2/wnoise_auto'][ifeed,:,:,:]
            nu = data['level2/freqspectra'][ifeed,:,:,:]
            freq = data['level1/spectrometer/frequency'][...]
            bw = 16
            freq = np.mean(np.reshape(freq, (freq.shape[0],freq.shape[1]//bw, bw)),axis=-1).flatten()
            sfreq = np.argsort(freq)
        
            fnoise_power[ifile,:] = (rms[:,:,0]**2 * (1/fits[:,:,0])**fits[:,:,1]).flatten()[sfreq]
            alphas[ifile,:] = (fits[:,:,1]).flatten()[sfreq]

            #print(nu.shape,ps.shape, rms.shape, fits.shape)
            #pyplot.plot(freq[sfreq],fnoise_power[ifile,:])
        except IOError:
            print('{} not processed'.format(filename.split('/')[-1]))
            fnoise[ifile] = np.nan

        if isinstance(feed, type(None)):
            feed = data['level1/spectrometer/feeds'][ifeed]

        # Calculate sun distance
        mjd = data['level1/spectrometer/MJD'][0:1]
        lon=-118.2941
        lat=37.2314
        ra_sun, dec_sun, raddist = Coordinates.getPlanetPosition('SUN', lon, lat, mjd)
        az_sun, el_sun = Coordinates.e2h(ra_sun, dec_sun, mjd, lon, lat)
        ra = data['level1/spectrometer/pixel_pointing/pixel_ra'][0,0:1]
        dec = data['level1/spectrometer/pixel_pointing/pixel_dec'][0,0:1]
        dist[ifile] = el_sun[0]#angular_seperation(ra_sun, ra, dec_sun, dec)
        data.close()


    # Plot obs ID vs fnoise power
    pyplot.imshow(np.log10(fnoise_power*1e3),aspect='auto',origin='lower',
                  extent=[np.min(freq),np.max(freq),-.5,fnoise_power.shape[0]-0.5])
    pyplot.yticks(np.arange(fnoise_power.shape[0])-0.5, obsid, rotation=0,
                  ha='right',va='center',size=10)
    ax = pyplot.gca()
    fig = pyplot.gcf()
    offset = ScaledTranslation(-0.08,0.02,fig.transFigure)
    for label in ax.yaxis.get_majorticklabels():
        label.set_transform(label.get_transform() + offset)
    pyplot.grid()
    pyplot.xlabel('Frequency (GHz)')
    pyplot.ylabel('obs ID')
    pyplot.colorbar(label=r'$\mathrm{log}_{10}$(mK)')
    pyplot.title('Feed {}'.format(feed))
    pyplot.savefig('Plots/fnoise_gfields_Feed{}.png'.format(feed),bbox_inches='tight')
    pyplot.clf()
    # Plot obs ID vs fnoise power
    pyplot.imshow(alphas,aspect='auto',origin='lower',vmin=-1.5,vmax=-0.9,
                  extent=[np.min(freq),np.max(freq),-.5,fnoise_power.shape[0]-0.5])
    pyplot.yticks(np.arange(fnoise_power.shape[0])-0.5, obsid, rotation=0,
                  ha='right',va='center',size=10)
    ax = pyplot.gca()
    fig = pyplot.gcf()
    for label in ax.yaxis.get_majorticklabels():
        label.set_transform(label.get_transform() + offset)
    pyplot.grid()
    pyplot.xlabel('Frequency (GHz)')
    pyplot.ylabel('obs ID')
    pyplot.colorbar(label=r'$\alpha$')
    pyplot.title('Feed {}'.format(feed))
    pyplot.savefig('Plots/alphas_gfields_Feed{}.png'.format(feed),bbox_inches='tight')
    pyplot.clf()
예제 #18
0
    def get_pixel_positions(self, azSource, elSource, az, el):
        x, y = Coordinates.Rotate(azSource, elSource, az, el, 0)

        pixels, pX, pY = self.getpixels(x, y, self.dx, self.dy, self.Nx,
                                        self.Ny)
        return pixels
예제 #19
0
    def create_maps(self, data, tod, filters, sel):
        """
        Bin maps into instrument frame centred on source
        """

        mjd = data['spectrometer/MJD'][:]

        # We do Jupiter in the Az/El frame but celestial in sky frame
        #if self.source.upper() == 'JUPITER':
        az = data['spectrometer/pixel_pointing/pixel_az'][:]
        el = data['spectrometer/pixel_pointing/pixel_el'][:]
        N = az.shape[1] // 2 * 2
        daz = np.gradient(az[0, :]) * 50.
        daz = daz[sel]
        az = az[:, sel]
        el = el[:, sel]
        cw = daz > 1e-2
        ccw = daz < 1e-2

        mjd = mjd[sel]

        npix = self.Nx * self.Ny

        temp_maps = {
            'map': np.zeros(npix, dtype=np.float64),
            'cov': np.zeros(npix, dtype=np.float64)
        }

        maps = {
            'maps': {
                'map': np.zeros(
                    (tod.shape[0], tod.shape[1], self.Nx, self.Ny)),
                'cov': np.zeros((tod.shape[0], tod.shape[1], self.Nx, self.Ny))
            }
        }
        maps['feed_avg'] = {
            'map': np.zeros((tod.shape[0], 1, self.Nx, self.Ny)),
            'cov': np.zeros((tod.shape[0], 1, self.Nx, self.Ny))
        }
        maps['CW'] = {
            'map': np.zeros((1, 1, self.Nx, self.Ny)),
            'cov': np.zeros((1, 1, self.Nx, self.Ny))
        }
        maps['CCW'] = {
            'map': np.zeros((1, 1, self.Nx, self.Ny)),
            'cov': np.zeros((1, 1, self.Nx, self.Ny))
        }

        selections = {
            k: selection
            for k, selection in zip(maps.keys(), [
                np.ones(az.shape[-1], dtype=bool),
                np.ones(az.shape[-1], dtype=bool), cw, ccw
            ])
        }
        slices = {
            k: sl
            for k, sl in zip(maps.keys(), [
                lambda ifeed, isb: [
                    slice(ifeed, ifeed + 1),
                    slice(isb, isb + 1),
                    slice(None),
                    slice(None)
                ], lambda ifeed, isb: [
                    slice(ifeed, ifeed + 1),
                    slice(None),
                    slice(None),
                    slice(None)
                ], lambda ifeed, isb:
                [slice(None),
                 slice(None),
                 slice(None),
                 slice(None)], lambda ifeed, isb:
                [slice(None),
                 slice(None),
                 slice(None),
                 slice(None)]
            ])
        }

        self.source_positions = {
            k: a
            for k, a in zip(['az', 'el', 'ra', 'dec'],
                            Coordinates.sourcePosition(self.source, mjd,
                                                       self.lon, self.lat))
        }
        self.source_positions['mean_el'] = np.mean(self.source_positions['el'])
        self.source_positions['mean_az'] = np.mean(self.source_positions['az'])

        for ifeed in tqdm(self.feedlist,
                          desc=f'{self.name}:create_maps:{self.source}'):
            feed_tod = tod[ifeed, ...]

            pixels = self.get_pixel_positions(self.source_positions['az'],
                                              self.source_positions['el'],
                                              az[ifeed, :], el[ifeed, :])

            mask = np.ones(pixels.size, dtype=int)
            for isb in range(tod.shape[1]):
                for k in temp_maps.keys():
                    temp_maps[k][:] = 0.
                z = (feed_tod[isb, sel] - filters[ifeed, isb])
                mask[:] = 1
                mask[(pixels == -1) | np.isnan(z) | np.isinf(z)] = 0

                if np.sum(np.isfinite(z)) == 0:
                    continue

                rms = stats.AutoRMS(z)

                weights = {
                    'map': z.astype(np.float64) / rms**2,
                    'cov': np.ones(z.size) / rms**2
                }
                for k in temp_maps.keys():
                    for mode, map_data in maps.items():
                        if ('CW' in mode) & (ifeed > 1):
                            continue
                        binFuncs.binValues(
                            temp_maps[k],
                            pixels[selections[mode]],
                            weights=weights[k][selections[mode]],
                            mask=mask[selections[mode]])
                        maps[mode][k][slices[mode](ifeed, isb)] = np.reshape(
                            temp_maps[k], (self.Ny, self.Nx))

        xygrid = np.meshgrid(
            (np.arange(self.Nx) + 0.5) * self.dx - self.Nx * self.dx / 2.,
            (np.arange(self.Ny) + 0.5) * self.dy - self.Ny * self.dy / 2.)

        for k, v in maps.items():
            maps[k] = self.average_maps(maps[k])
            maps[k]['xygrid'] = xygrid
        return maps