示例#1
0
def get_filter_file(filt, comment='#'):
    header = np.loadtxt(filt + '.csv', max_rows=2, delimiter=',', dtype='str')
    data = np.loadtxt(filt + '.csv', skiprows=2, delimiter=',')
    fname = filt.replace('/', '_')
    #Note: dtype in the following line is the detector type of the filter
    g = pyp.Filter(data[:, 0], data[:, 1], name = filt.replace('/', '_'), unit = header[1, 0], \
                   dtype = header[1, 1])
    #filters.append(g)
    return g, fname
    pass
示例#2
0
def synpassflux(wl, flux, band):
    # Calculate synthetic magnitudes
    n_bands = len(band)

    filt = np.genfromtxt("/Users/jonatanselsing/github/iPTF16geu/data/passbands/%s"%band)
    lamb_T, T = filt[:,0], filt[:,1]
    f = pyphot.Filter(lamb_T, T, name=band, dtype='photon', unit='Angstrom')
    fluxes = f.get_flux(wl, flux, axis=0)
    synmags = -2.5 * np.log10(fluxes) - f.AB_zero_mag
    cwav = np.mean(lamb_T)
    cwave = (float(max(lamb_T[T > np.percentile(T, 10)] - cwav)), float(cwav - min(lamb_T[T > np.percentile(T, 10)])))
    synmag_flux = ((synmags*u.ABmag).to((u.erg/(u.s * u.cm**2 * u.AA)), u.spectral_density(cwav * u.AA))).value
    return synmag_flux, cwav, cwave, synmags
示例#3
0
def makeFilterSet(filterNames = [], infile = 'filters.csv', libraryFile = 'filters.hd5'):
    """Download filter transmission curves from the Spanish Virtual Observatory.
    The resulting filter library is saved as an hd5 file, to be ingested into pyphot.
    INPUTS:
           1) filterNames, a list of the filter names as on the SVO/VOSA site. If specified, infile
           is ignored.
           2) infile, a two-column CSV file of which the second column must contain
           must contain the names of the SVO/VOSA filter files to download.
           The first column is not currently used, but can contain identifying information
           for each filter that connects it back to the data.
           The filter names can be in the order of occurrence in the data, and may include
           repetitions (as it is quite possible that the data is compiled from a number of
           differing sets of observations).
    OUTPUT: libraryFile, an HDF file containing information about all the filters for which this is requested.

    At the moment, the header in the output files only contains the DetectorType specification if the filter
        is a photon counter (DetectorType value = "1"). We read in the entire file and check for the occurrence
        of this line and set the detector type accordingly.
    """
    if filterNames == []:
        tin = Table.read(infile, format = 'csv', names = ('column', 'filtername'))
        filterNames = list(tin['filtername'])
    url = 'http://svo2.cab.inta-csic.es//theory/fps3/fps.php?ID='
    filters = []
    #Each filter is downloaded into a temporary file via curl.
    #   The temporary file is deleted after all the filters are downloaded.
    for f in filterNames:
    #for t in tin:
        print("Downloading filter " + f)
        _ = subprocess.call(['curl', '-o', 'temp.vot', url + f])
        with open('temp.vot') as g:
            content = g.readlines()
        if any("DetectorType" in c for c in content):
            det_type = 'photon'
        else:
            det_type = 'energy'
        temp = Table.read('temp.vot', format = 'votable')
        g = pyp.Filter(np.array(temp['Wavelength']), np.array(temp['Transmission']), \
                       name = f.replace('/','_'), unit = temp['Wavelength'].unit.name, \
                       dtype = det_type)
        filters.append(g)
    _ = subprocess.call(['rm', 'temp.vot'])
    #Instantiate an hdf5 object to store filter information
    h = h5py.File(libraryFile, 'w')
    h.create_group('filters')
    h.close()
    h = pyp.HDF_Library(source = libraryFile)
    #Add filters to this object, without repetition.
    _, u = np.unique([f.name for f in filters], return_index = True)
    for f in list(np.array(filters)[u]):
        h.add_filter(f)
示例#4
0
def get_filter_svo(filt,
                   url='http://svo2.cab.inta-csic.es//theory/fps3/fps.php?ID='
                   ):
    _ = subprocess.call(['curl', '-o', 'temp.vot', url + filt])
    with open('temp.vot') as f:
        content = f.readlines()
    if any("DetectorType" in c for c in content):
        det_type = 'photon'
    else:
        det_type = 'energy'
    #try:
    temp = Table.read('temp.vot', format='votable')
    #except ValueError:
    #    print("Filter ",filt," could not be found on the SVO. Please check the filter name. \n Continuing with the next filter")
    #    continue
    fname = filt.replace('/', '_')
    g = pyp.Filter(np.array(temp['Wavelength']), np.array(temp['Transmission']), \
                   name = filt.replace('/','_'), unit = temp['Wavelength'].unit.name, \
                   dtype = det_type)
    return g, fname
示例#5
0
def makeFilterSet(infile='filters.csv', outfile='filters.hd5'):
    """Download filter transmission curves from the Spanish Virtual Observatory and write into an HDF file.

    Keyword arguments:
    infile -- (default 'filters.csv')
        name of two-column CSV file, with the first column containing a unique name for the filter,
        and the second column containing the name of the filter as specified on the SVO/VOSA webpage. For
        example, a row "IRAC45, Spitzer/IRAC.I2" in infile will download the IRAC 4.5 µm filter curve.

        If the name in the second column does not match any filters on the SVO database, the code will
        search the local machine. This allows the user to incorporate filter files not available on the SVO.
        In this case, the filter profile must be a CSV, with two header lines providing the column content
        as well as the units for the wavelength, and the detector type. For example:
                Wavelength, Transmission
                um, energy
                0.5, 0.00
                0.6, 0.10

    outfile -- (default 'filters.hd5') name of output HDF file.

    Notes: The current SVO data only specify the DetectorType if the filter is a photon counter, by setting
    DetectorType = "1". This script reads in the entire file and checks for the occurrence of this line
    to set the detector type in the output file accordingly. The SVO has said that they will improve
    the implementation of this keyword in the future.
    """

    tin = Table.read(infile, format = 'csv', \
                     names = ('column', 'filtername'))
    url = 'http://svo2.cab.inta-csic.es//theory/fps3/fps.php?ID='
    filters = []
    for t in tin:
        try:
            #Look for the filter in the SVO database
            _ = subprocess.call(
                ['curl', '-o', 'temp.vot', url + t['filtername']])
            with open('temp.vot') as f:
                content = f.readlines()
            if any("DetectorType" in c for c in content):
                det_type = 'photon'
            else:
                det_type = 'energy'
            temp = Table.read('temp.vot', format='votable')
            g = pyp.Filter(np.array(temp['Wavelength']), np.array(temp['Transmission']), \
                           name = t['filtername'].replace('/','_'), unit = temp['Wavelength'].unit.name, \
                           dtype = det_type)
        except:
            #Look for the filter on the local machine
            header = np.loadtxt(t['filtername'] + '.csv',
                                max_rows=2,
                                delimiter=',',
                                dtype='str')
            data = np.loadtxt(t['filtername'] + '.csv',
                              skiprows=2,
                              delimiter=',')
            #Note: dtype in the following line is the detector type of the filter
            g = pyp.Filter(data[:, 0], data[:, 1], name = t['filtername'].replace('/', '_'), unit = header[1, 0], \
                           dtype = header[1, 1])
        filters.append(g)
    _ = subprocess.call(['rm', 'temp.vot'])
    h = h5py.File(outfile, 'w')
    h.create_group('filters')
    h.close()
    h = pyp.HDF_Library(source=outfile)
    #Don't try to add a filter if it's already in there
    _, u = np.unique([f.name for f in filters], return_index=True)
    for f in list(np.array(filters)[u]):
        h.add_filter(f)