Beispiel #1
0
def write_singleRTS_from_RTS_sources(RTS_sources,outname,name='combined_name'):
    '''Takes a list RTS_sources containg RTS_source classes, and writes
    them out into a single SOURCE RTS srclist of name outname'''
    with open(outname,'w+') as outfile:
        write_git_header(outfile)

        for source_ind,source in enumerate(RTS_sources):
            for comp_ind,comp_info in enumerate(source.component_infos):
                if source_ind == 0 and comp_ind == 0:
                    outfile.write('SOURCE %s %s %s\n' %(name,source.ras[comp_ind],source.decs[comp_ind]))
                else:
                    outfile.write('COMPONENT %s %s\n' %(source.ras[comp_ind],source.decs[comp_ind]))

                for line in source.flux_lines[comp_ind]:
                    outfile.write(line+'\n')

                if comp_info.comp_type == 'GAUSSIAN':
                    outfile.write('GAUSSIAN %s %s %s\n' %(comp_info.pa,comp_info.major,comp_info.minor))

                elif comp_info.comp_type == 'SHAPELET':
                    outfile.write('SHAPELET2 %s %s %s\n' %(comp_info.pa,comp_info.major,comp_info.minor))

                    for line in comp_info.shapelet_coeffs:
                        outfile.write(line+'\n')

                if source_ind == 0 and comp_ind == 0:
                    pass
                else:
                    outfile.write('ENDCOMPONENT\n')

        outfile.write('ENDSOURCE')
Beispiel #2
0
def write_woden_from_RTS_sources(RTS_sources,outname):
    """
    Takes a list of :class:`RTS_source` classes and uses the to write a WODEN
    style srclist called `outname`

    Parameters
    ----------
    RTS_sources : list
        A list of :class:`RTS_source` s to write out to a WODEN style srclist
    outname : string
        Path to save the output WODEN srclist text file to

    """

    all_comp_types = []
    all_shape_coeffs = 0
    for source in RTS_sources:
        for comp in source.component_infos:
            all_comp_types.append(comp.comp_type)
            all_shape_coeffs += len(comp.shapelet_coeffs)

    all_comp_types = array(all_comp_types)

    num_point = len(where(all_comp_types == 'POINT')[0])
    num_gauss = len(where(all_comp_types == 'GAUSSIAN')[0])
    num_shape = len(where(all_comp_types == 'SHAPELET')[0])

    with open(outname,'w+') as outfile:
        write_git_header(outfile)
        outfile.write('SOURCE %s P %d G %d S %d %d\n' %(RTS_sources[0].name,
                                num_point,num_gauss,num_shape,all_shape_coeffs))

        for source in RTS_sources:
            for comp_ind,comp_info in enumerate(source.component_infos):
                outfile.write('COMPONENT %s %s %s\n' %(comp_info.comp_type,source.ras[comp_ind],source.decs[comp_ind]))

                for line in source.flux_lines[comp_ind]:
                    outfile.write(line+'\n')

                if comp_info.comp_type == 'GAUSSIAN':
                    ##RTS gaussians are std dev, WODEN are FWHM
                    outfile.write('GPARAMS %s %.10f %.10f\n' %(comp_info.pa,float(comp_info.major)/rts_factor,float(comp_info.minor)/rts_factor))

                elif comp_info.comp_type == 'SHAPELET':
                    outfile.write('SPARAMS %s %s %s\n' %(comp_info.pa,comp_info.major,comp_info.minor))

                    for line in comp_info.shapelet_coeffs:
                        outfile.write('S'+line+'\n')
                outfile.write('ENDCOMPONENT\n')

        outfile.write('ENDSOURCE')
Beispiel #3
0
def write_singleRTS_from_RTS_sources(RTS_sources,outname,name='combined_name'):
    '''Takes a list RTS_sources containg RTS_source classes, and writes
    them out into a single SOURCE RTS srclist of name outname'''

    """
    Takes a list of :class:`RTS_source` classes and uses the to write a RTS
    style srclist called `outname`, combining all sources in `RTS_sources` into
    a single :class:`RTS_source`

    Parameters
    ----------
    RTS_sources : list
        A list of :class:`RTS_source` s to write out to a WODEN style srclist
    outname : string
        Path to save the output WODEN srclist text file to
    name : string
        Name for the single output RTS source

    """

    with open(outname,'w+') as outfile:
        write_git_header(outfile)

        for source_ind,source in enumerate(RTS_sources):
            for comp_ind,comp_info in enumerate(source.component_infos):
                if source_ind == 0 and comp_ind == 0:
                    outfile.write('SOURCE %s %s %s\n' %(name,source.ras[comp_ind],source.decs[comp_ind]))
                else:
                    outfile.write('COMPONENT %s %s\n' %(source.ras[comp_ind],source.decs[comp_ind]))

                for line in source.flux_lines[comp_ind]:
                    outfile.write(line+'\n')

                if comp_info.comp_type == 'GAUSSIAN':
                    outfile.write('GAUSSIAN %s %s %s\n' %(comp_info.pa,comp_info.major,comp_info.minor))

                elif comp_info.comp_type == 'SHAPELET':
                    outfile.write('SHAPELET2 %s %s %s\n' %(comp_info.pa,comp_info.major,comp_info.minor))

                    for line in comp_info.shapelet_coeffs:
                        outfile.write(line+'\n')

                if source_ind == 0 and comp_ind == 0:
                    pass
                else:
                    outfile.write('ENDCOMPONENT\n')

        outfile.write('ENDSOURCE')
Beispiel #4
0
def save_srclist(save_tag=None, nmax=None, n1s=None, n2s=None, fitted_coeffs=None,
    b1=None, b2=None, fitted_model=None, ra_cent=None, dec_cent=None, freq=None,
    pa=0.0, pix_area=None,  rts_srclist=True):
    '''Take the fitted parameters and creates an RTS/WODEN style srclist with them'''

    all_flux = sum(fitted_model)
    print('TOTAL FLUX in convolved model is %.2f' %all_flux)

    ##This scaling removes pixel effects, and sets the model to sum to one -
    ##this way when the RTS creates the model and multiplies by the reported
    ##flux density we get the correct answer
    scale = 1 / (pix_area*all_flux)

    ##Scale to arcmin or deg
    major, minor = (b1 / D2R)*60, (b2 / D2R)*60
    pa /= D2R

    if rts_srclist:
        outfile = open('srclist-rts_%s.txt' %(save_tag),'w+')
    else:
        outfile = open('srclist-woden_%s.txt' %(save_tag),'w+')


    write_git_header(outfile)

    if rts_srclist:
        outfile.write('SOURCE %s %.6f %.6f\n' %(save_tag[:16],ra_cent/15.0,dec_cent))
        outfile.write("FREQ %.5e %.5f 0 0 0\n" %(freq,all_flux))
        outfile.write("SHAPELET2 %.8f %.8f %.8f\n" %(pa,major,minor))

        for index,coeff in enumerate(fitted_coeffs):
            outfile.write("COEFF %.1f %.1f %.12f\n" %(n1s[index],n2s[index],coeff * scale))

        outfile.write('ENDSOURCE\n')

    else:
        outfile.write('SOURCE %s P 0 G 0 S 1 %d\n' %(save_tag,len(fitted_coeffs)))
        outfile.write('COMPONENT SHAPELET %.6f %.6f\n' %(ra_cent/15.0,dec_cent))
        outfile.write("FREQ %.5e %.5f 0 0 0\n" %(freq,all_flux))
        outfile.write("SPARAMS %.8f %.8f %.8f\n" %(pa,major,minor))

        for index,coeff in enumerate(fitted_coeffs):
            outfile.write("SCOEFF %.1f %.1f %.12f\n" %(n1s[index],n2s[index],coeff * scale))
        outfile.write('ENDCOMPONENT\n')
        outfile.write('ENDSOURCE\n')

    outfile.close()
Beispiel #5
0
    def save_srclist(self,
                     save_tag='shapelet',
                     rts_srclist=True,
                     woden_srclist=True):
        """
        Uses the best fitted parameters and creates an RTS/WODEN style
        srclist with them, saved as text files

        Parameters
        ----------
        save_tag : string
            A tag to add into the file name to save the plot to
        rts_srclist : bool
            If True, save a sky model compatible with the RTS (Mitchell et al, 2008)
        woden_srclist : bool
            If True, save a sky model compatible with WODEN (Line et al, 2020)

        """

        all_flux = sum(self.fit_data)
        print('Total flux in convolved model is %.2f' % all_flux)

        ##This scaling removes pixel effects, and sets the model to sum to one -
        ##this way when the RTS creates the model and multiplies by the reported
        ##flux density we get the correct answer
        scale = 1 / (self.fits_data.pix_area_rad * all_flux)

        ##Scale to arcmin or deg
        major, minor = (self.best_b1 / D2R) * 60, (self.best_b2 / D2R) * 60
        pa = self.shpcoord.pa / D2R

        if rts_srclist:
            with open(
                    'srclist-rts_%s_nmax%03d_p%03d.txt' %
                (save_tag, self.nmax, int(self.model_percentage)),
                    'w+') as outfile:
                write_git_header(outfile)
                outfile.write('SOURCE %s %.6f %.6f\n' %
                              (save_tag[:16], self.shpcoord.ra_cent / 15.0,
                               self.shpcoord.dec_cent))
                outfile.write("FREQ %.5e %.5f 0 0 0\n" %
                              (self.fits_data.freq, all_flux))
                outfile.write("SHAPELET2 %.8f %.8f %.8f\n" %
                              (pa, major, minor))

                for index, coeff in enumerate(self.fitted_coeffs):
                    outfile.write("COEFF %.1f %.1f %.12f\n" %
                                  (self.fit_n1s[index], self.fit_n2s[index],
                                   coeff * scale))

                outfile.write('ENDSOURCE\n')

        if woden_srclist:
            with open(
                    'srclist-woden_%s_nmax%03d_p%03d.txt' %
                (save_tag, self.nmax, int(self.model_percentage)),
                    'w+') as outfile:
                write_git_header(outfile)
                outfile.write('SOURCE %s P 0 G 0 S 1 %d\n' %
                              (save_tag, len(self.fitted_coeffs)))
                outfile.write(
                    'COMPONENT SHAPELET %.6f %.6f\n' %
                    (self.shpcoord.ra_cent / 15.0, self.shpcoord.dec_cent))
                outfile.write("FREQ %.5e %.5f 0 0 0\n" %
                              (self.fits_data.freq, all_flux))
                outfile.write("SPARAMS %.8f %.8f %.8f\n" % (pa, major, minor))

                for index, coeff in enumerate(self.fitted_coeffs):
                    outfile.write("SCOEFF %.1f %.1f %.12f\n" %
                                  (self.fit_n1s[index], self.fit_n2s[index],
                                   coeff * scale))
                outfile.write('ENDCOMPONENT\n')
                outfile.write('ENDSOURCE\n')