Exemplo n.º 1
0
    except (OverflowError,AttributeError,ValueError):
        if VERBOSE: print data
        print name, 'had infinities (or something in plotting went wrong)'
        return "-1, -1,-1",[],None


def calc((i, (sample, flux, err, nm, bss, mds, disp, dust_corr, verbose, res, scales, nps, logf))):
    logf=sys.stdout
    print >>logf,"\n\nreading in measurements ",i+1
    fluxi={}#np.zeros((len(bss[0]),nm),float)
    for k in bss[0].iterkeys():
        print >>logf,'{0:15} '.format(k),
        print >>logf,'{0:0.2} +/- {1:0.2}'.format(flux[k][i],err[k][i])
        fluxi[k]=flux[k][i]*np.ones(len(sample[i]))+err[k][i]*sample[i]
        warnings.filterwarnings("ignore")
    success=metallicity.calculation(scales[i],fluxi,nm,mds,nps,logf,disp=disp, dust_corr=dust_corr,verbose=verbose)
    if success==-1:
        print >>logf, "MINIMUM REQUIRED LINES: '[OII]3727','[OIII]5007','[NII]6584','[SII]6717'"
        
    for key in scales[i].mds.iterkeys():
        if key in res.keys():
            res[key][i] = scales[i].mds[key]
            if res[key][i] is None:
                res[key][i] = [float('NaN')]*len(sample)
    return res

##############################################################################
##  The main function. takes the flux and its error as input. 
##  filename - a string 'filename' common to the three flux files
##  flux - np array of the fluxes
##  err - the flux errors, must be the same dimension as flux
Exemplo n.º 2
0
Arquivo: mcz.py Projeto: zpace/pyMCZ
    def sample(self, nsample=1000, from_estimate=False, test=False):

        if (nsample == 1) and (from_estimate == False):
            raise ValueError('for nsample = 1, use .estimate() method!')
        elif (1 < nsample < 100) and (test == False):
            raise ValueError('need at least 100 samples!')
        elif (1 < nsample < 100) and (test == True):
            print 'not enough samples, remember to only ' + \
                'use this as a test-bed!'

        # increasing sample by 10% to ensure
        # robustness against rejected samples
        nsample = int(nsample)

        # set up a dictionary to store tables of relevant data for each spaxel
        res_d = {}

        tfcnames = [
            k for k in self.flux.colnames
            if len(self.flux[k]) > np.isnan(self.flux[k]).sum()
        ]
        self.tfcnames = tfcnames

        #looping over nm measurements
        pbar = ProgressBar(widgets=[Percentage(), Bar(),
                                    ETA()],
                           maxval=self.nm).start()

        for i in range(self.NM0, self.nm):
            blockPrint()
            galnum = self.flux['galnum'][i]
            fr = self.flux[i]
            er = self.err[i]

            fluxi = {
                k: np.random.normal(fr[k], er[k], nsample)
                for k in fr.colnames
                if ((k != 'galnum') and (~np.isnan(fr[k])))
            }

            # set up a table for a given galnum
            res_d[galnum] = t.Table()
            # add a column for flux information
            for n in tfcnames:
                if (n != 'galnum'):
                    if (np.isnan(self.flux[n]).sum() != len(self.flux[n])):
                        res_d[galnum][n] = fluxi[n]
                        res_d[galnum][n].unit = u.Unit('1e-17 erg cm^-2 s^-1')

            scales = ms.diagnostics(nsample, None, self.nps)

            with warnings.catch_warnings():
                warnings.simplefilter('ignore')
                success = metallicity.calculation(scales,
                                                  fluxi,
                                                  self.nm,
                                                  'all',
                                                  1,
                                                  self.logf,
                                                  disp=self.verbose,
                                                  dust_corr=self.dust_corr,
                                                  verbose=self.verbose)
            if success == -1:
                raise ValueError('MINIMUM REQUIRED LINES:  [OII]3727 ' + \
                    '& [OIII] + 5007, or [NII]6584, and Ha & Hb if ' + \
                    'you want dereddening')

            for k, v in scales.mds.iteritems():
                if type(v) == np.ndarray:
                    if np.isnan(v).sum() != len(v):
                        res_d[galnum][k] = v

            enablePrint()
            pbar.update(i)

        pbar.finish()

        self.res_d = res_d
        self.nsample = nsample
        self.Zdiags = res_d[galnum].colnames
Exemplo n.º 3
0
        return "-1\t -1\t -1", [], None


def calc((i, (sample, flux, err, nm, bss, mds, disp, dust_corr,
              verbose, res, scales, nps, logf))):
    logf = sys.stdout
    logf.write("\n\nreading in measurements %d\n"%(i + 1))
    fluxi = {}  # np.zeros((len(bss[0]),nm),float)
    for k in bss[0].iterkeys():
        logf.write('{0:15} '.format(k))
        logf.write('{0:0.2} +/- {1:0.2}\n'.format(flux[k][i], err[k][i]))
        #print >> logf, '{0:15} '.format(k),
        #print >> logf, '{0:0.2} +/- {1:0.2}'.format(flux[k][i], err[k][i])
        fluxi[k] = flux[k][i] * np.ones(len(sample[i])) + err[k][i] * sample[i]
        warnings.filterwarnings("ignore")
    success = metallicity.calculation(scales[i], fluxi, nm, mds, nps, logf, disp=disp, dust_corr=dust_corr, verbose=verbose)
    if success == -1:
        logf.write("MINIMUM REQUIRED LINES: '[OII]3727','[OIII]5007','[NII]6584','[SII]6717'\n")
        #print >> logf, "MINIMUM REQUIRED LINES: '[OII]3727','[OIII]5007','[NII]6584','[SII]6717'"

    for key in scales[i].mds.iterkeys():
        if key in res.keys():
            res[key][i] = scales[i].mds[key]
            if res[key][i] is None:
                res[key][i] = [float('NaN')] * len(sample)
    return res


##############################################################################
##  The main function. takes the flux and its error as input.
##  filename - a string 'filename' common to the three flux files
Exemplo n.º 4
0
Arquivo: mcz.py Projeto: zpace/pyMCZ
    def sample(self, nsample=1000, from_estimate=False, test=False):

        if (nsample == 1) and (from_estimate == False):
            raise ValueError("for nsample = 1, use .estimate() method!")
        elif (1 < nsample < 100) and (test == False):
            raise ValueError("need at least 100 samples!")
        elif (1 < nsample < 100) and (test == True):
            print "not enough samples, remember to only " + "use this as a test-bed!"

        # increasing sample by 10% to ensure
        # robustness against rejected samples
        nsample = int(nsample)

        # set up a dictionary to store tables of relevant data for each spaxel
        res_d = {}

        tfcnames = [k for k in self.flux.colnames if len(self.flux[k]) > np.isnan(self.flux[k]).sum()]
        self.tfcnames = tfcnames

        # looping over nm measurements
        pbar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=self.nm).start()

        for i in range(self.NM0, self.nm):
            blockPrint()
            galnum = self.flux["galnum"][i]
            fr = self.flux[i]
            er = self.err[i]

            fluxi = {
                k: np.random.normal(fr[k], er[k], nsample)
                for k in fr.colnames
                if ((k != "galnum") and (~np.isnan(fr[k])))
            }

            # set up a table for a given galnum
            res_d[galnum] = t.Table()
            # add a column for flux information
            for n in tfcnames:
                if n != "galnum":
                    if np.isnan(self.flux[n]).sum() != len(self.flux[n]):
                        res_d[galnum][n] = fluxi[n]
                        res_d[galnum][n].unit = u.Unit("1e-17 erg cm^-2 s^-1")

            scales = ms.diagnostics(nsample, None, self.nps)

            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                success = metallicity.calculation(
                    scales,
                    fluxi,
                    self.nm,
                    "all",
                    1,
                    self.logf,
                    disp=self.verbose,
                    dust_corr=self.dust_corr,
                    verbose=self.verbose,
                )
            if success == -1:
                raise ValueError(
                    "MINIMUM REQUIRED LINES:  [OII]3727 "
                    + "& [OIII] + 5007, or [NII]6584, and Ha & Hb if "
                    + "you want dereddening"
                )

            for k, v in scales.mds.iteritems():
                if type(v) == np.ndarray:
                    if np.isnan(v).sum() != len(v):
                        res_d[galnum][k] = v

            enablePrint()
            pbar.update(i)

        pbar.finish()

        self.res_d = res_d
        self.nsample = nsample
        self.Zdiags = res_d[galnum].colnames