Esempio n. 1
0
def collate(configOpts, fluxtable, hidata, dd0, sdd0, rhoHI, srho, G0, contr):

    print "Re-organizing & tabulating ..."
    print "PDRID, RA, DEC, pRA, pDEC, NHI, sNHI, dd0, sdd0, rhoHI, srho, net flux, sflux, mean_at_r, ntot, sntot"
    data = [[] for dummy in range(20)]
    data_full = configOpts['data_full']
    ntot = np.zeros(np.size(hidata.PDRID))
    sntot = np.zeros(np.size(hidata.PDRID))
      #Add ntot and sntot for later
    with open(data_full, 'w') as fullfile: #no close file needed this way
        for i in range(np.size(hidata.PDRID)):
      #There can be no duplicate PDRIDs in fluxtable or this will yield incorrect results
          #for legibility:
          uv_idx = np.where(fluxtable.PDRID == hidata.PDRID[i])[0][0] #expect only one value - problem if there are more HI PDRIDs than UV PDRIDs
          #hack: 'g' does not take [number] so have to convert again with [0]. Need to 'fix' fluxtable. Same problem with G0
          #print "{0:>3}, {1:12s}, {2:12s}, {3:8g}, {4:6g}, {5:.3f}, {6:f}, {7:.3f}, {8:.3f}, {9:g}, {10:.3g}, {11:g}".format(hidata.PDRID[i], fluxtable.RA[uv_idx], fluxtable.DEC[uv_idx], hidata.NHI[i], hidata.sNHI[i], dd0[i], sdd0[i], rhoHI[i], srho[i], fluxtable.netflux[uv_idx], fluxtable.sflux[uv_idx], fluxtable.mean_at_r[uv_idx])

          #Patch RAs, DECs not written here
          #fullfile.write("{0:>3}, {1:12s}, {2:12s}, {3:8g}, {4:6g}, {5:.3f}, {6:f}, {7:.3f}, {8:.3f}, {9:g}, {10:.3g}, {11:g}\n".format(hidata.PDRID[i], fluxtable.RA[uv_idx], fluxtable.DEC[uv_idx], hidata.NHI[i], hidata.sNHI[i], dd0[i], sdd0[i], rhoHI[i], srho[i], fluxtable.netflux[uv_idx], fluxtable.sflux[uv_idx], fluxtable.mean_at_r[uv_idx]))
          #Fedora bug: need to type cast the variables
          fullfile.write("{0:>3}, {1:12s}, {2:12s}, {3:8g}, {4:6g}, {5:.3f}, {6:f}, {7:.3f}, {8:.3f}, {9:g}, {10:.3g}, {11:g}\n".format(
                int(hidata.PDRID[i]),
                fluxtable.RA[uv_idx], fluxtable.DEC[uv_idx],
                hidata.NHI[i], hidata.sNHI[i],
                dd0[i], sdd0[i], rhoHI[i], srho[i],
                fluxtable.netflux[uv_idx], fluxtable.sflux[uv_idx],
                fluxtable.mean_at_r[uv_idx]))

          for m, n in enumerate((hidata.PDRID[i], fluxtable.RA[uv_idx], fluxtable.DEC[uv_idx], hidata.RA[i], hidata.DEC[i], Rgal[uv_idx], hidata.NHI[i], hidata.sNHI[i], dd0[i], sdd0[i], rhoHI[i], srho[i], fluxtable.netflux[uv_idx], fluxtable.sflux[uv_idx], fluxtable.mean_at_r[uv_idx], fluxtable.aperture[uv_idx], G0[i], contr[i], ntot[i], sntot[i])):
              #print m, n
              data[m].append(n)

    labels = 'PDRID, RA, DEC, pRA, pDEC, Rgal, NHI, sNHI, dd0, sdd0, rhoHI, srho, flux, sflux, mean_at_r, aperture, G0, contr, ntot, sntot'
    data = pdr.records(data, labels) #transform into rec.array
    #print data #instead of the print earlier, for testing
    print "Wrote to file {0}".format(data_full)
    print "... done"
    print

    return data
Esempio n. 2
0
def getHI(configOpts, fluxtable):
    print "Identifying HI patches ..."
    data_hi = configOpts['data_hi']
    hiImage = configOpts['hiImage']
    hi_baselist = "{0}_Regions/{0}_Reg".format(os.path.splitext(hiImage)[0])
    hi_logsbase = "{0}_Regions/{1}".format(os.path.splitext(hiImage)[0], configOpts['hi_logsbase'])
    try:
        dummy = file(data_hi, 'r')
        print "... HI patch data read from file {0}".format(data_hi)
        #Read: HI data (if already there); note that if the read fails, patches will be re-measured also
        columns = np.dtype([('PDRID', 'int',), ('RA', '|S14'), ('DEC', '|S14'), ('NHI', 'float'), ('sNHI', 'float')])
        hidata = pdr.read_array(data_hi, columns)
        print "  {0} records read".format(np.size(hidata))
        #Very simple and incomplete check for enough data:
        if (np.size(np.unique(hidata.PDRID)) != np.size(np.unique(fluxtable.PDRID))):
            print "Fatal error: number of UV and HI PDRIDs do not match. Try regenerating the HI files."
            exit(0)
    except:
        #File does not exist, so process HI postage stamp regions.
        #Must have regions with names matching the PDRIDs
        scale = configOpts['HIscale'] # 2.896e19 for NGC 628 (m?)Jy/beam to cm-2
        hi_bg = configOpts['hiBackground']
        print "Using map scaling of {0} cm-2".format(scale)
        hiresults = [[] for dummy in xrange(5)] #PDRID, ra, dec, NHI, sNHI
        #Warning: up to 999 files
        for i in range(np.size(fluxtable.PDRID)):
            fitsfile = hi_baselist + "{0:03d}.fits".format(int(fluxtable.PDRID[i]))
            print "Calling SExtractor with file {0}".format(fitsfile)

            catfile = pdr.call_SEx(fitsfile)
            if catfile==1:
                print "Fatal error: SExtractor call failed."
                exit(0)
            else:
                logfile = hi_logsbase + "{0:03}.txt".format(int(fluxtable.PDRID[i]))
                patches = pdr.read_secat(catfile, fitsfile, logfile, wcs=True)
                #Patches are not background-subtracted, that will happen now:

                for n in range(len(patches[0])):
                    if patches[2][n] > (hi_bg*2.):
                      #subtract the background and ignore patches that are fainter than hi_bg
                        hiresults[0].append(fluxtable.PDRID[i])
                        hiresults[1].append(patches[0][n])
                        hiresults[2].append(patches[1][n])
                        hiresults[3].append((patches[2][n]-hi_bg)*scale)
                        hiresults[4].append(hi_bg * scale * 0.5) #fix sN_HI to half the background value
                    else:
                        print "Patch too faint: ", patches[2][n]

                if not np.size(np.where(hiresults[0] == fluxtable.PDRID[i])):
                    print "No suitable patches were found for PDRID ", fluxtable.PDRID[i], "; creating empty entry."
                    #With SExtractor, this is unlikely since it does not pick up the faintest patches
                    hiresults[0].append(fluxtable.PDRID[i])
                    hiresults[1].append(0)
                    hiresults[2].append(0)
                    hiresults[3].append(0)
                    hiresults[4].append(0)

        #End for

        #Finally, convert the results into a rec array and save the results
        labels = 'PDRID, RA, DEC, NHI, sNHI'
        hidata = pdr.records(hiresults, labels)
        #Write: HI patches
        hifile = fancyPrint(writeLog=True, logFile=data_hi, verbose=True)
        print "HI patches:"
        #print "#PDRID, RA, DEC, NHI, sNHI"
        hifile.write("#PDRID, RA, DEC, NHI, sNHI\n")
        for n in range(len(hidata)):
            #print type(hidata[n].NHI)
            #Apparently python 2.6 needs explicit conversion from numpy.float32, so workaround
            #fedora bug: changed PDRID:>3 to PDRID:3g
            #print "{0.PDRID:3g}, {0.RA:14s}, {0.DEC:14s}, {1:7.5g}, {2:7.5g}".format(hidata[n], float(hidata[n].NHI), float(hidata[n].sNHI))
            hifile.write("{0.PDRID:3g}, {0.RA:14s}, {0.DEC:14s}, {1:7.5g}, {2:7.5g}\n".format(hidata[n], float(hidata[n].NHI), float(hidata[n].sNHI)))
          #hifile.close()
      #End of try/except

        print

        return hidata