Beispiel #1
0
	def __getOutputLocation(self, config):
		try:
			if uf.checkkeys(config, 'OutputLocation'):
				self.outloc = config['OutputLocation']
			else:
				self.outloc = input('Enter the full location to where you would like the output saved: ')
			if self.outloc[-1] != '/':
				self.outloc +='/'
				uf.checkpath(self.outloc)
			else:
				uf.checkpath(self.outloc)
			logger.info('Output location: %s'%self.outloc)
		except KeyboardInterrupt:
			uf.exit(self)
		except SystemExit:
			uf.exit(self)
		except:
			print ('There was a problem with entering the output location. Please try again.\n')
			self.__getOutputLocation(config=False, data=None)
Beispiel #2
0
def main():

    fullcat = ID.inputCatalogue(runtime)

    fullcat.runInput(data)
    fullcat.clean = args.clean
    fullcat.suppress = args.suppress
    fullcat.progress = args.progress
    fullcat.saveprogress = args.saveprogress
    fullcat.multiop = args.multiop
    fullcat.latex = args.latex

    ## create a temp file for all object files
    uf.checkpath('%sstackertemp/' % fullcat.outloc)
    if uf.checkkeys(data, 'BinYN'):
        binyn = data["BinYN"]
    else:
        binyn = raw_input(
            '\nWould you like to stack in various bins? [y/n] ').lower()

    if binyn == 'y':
        bincatalogue = BD.binnedCatalogue(fullcat)
        try:
            bincatalogue.determineBins(data)
        except (SystemExit, KeyboardInterrupt):
            uf.exit(fullcat)
        except:
            logging.warning(
                "Couldn't determine the catalogue for the different bins:",
                exc_info=True)
            uf.earlyexit(fullcat)

        if fullcat.uncert == 'y':
            repeat = 2
        else:
            repeat = 1

        counter = 0
        while counter < repeat:
            mccount = counter
            for m in range(0, len(bincatalogue.binpartindicies) - 1):
                if counter > 0:
                    bincatalogue = pck.load(
                        open(
                            '%sstackertemp/catalogue_obj.pkl' % fullcat.outloc,
                            'rb'))
                else:
                    h = 0
                try:
                    bincatalogue.createCatalogue(bincatalogue.binpartindicies,
                                                 bincatalogue.binnedtable, m,
                                                 counter)
                except KeyboardInterrupt:
                    uf.earlyexit(cat)
                    raise sys.exit()
                except SystemExit:
                    uf.earlyexit(cat)
                    raise sys.exit()
                except:
                    logger.error(
                        'Exception occurred trying to create the binned catalogue.',
                        exc_info=True)
                    uf.earlyexit(cat)
                    raise sys.exit()

                if len(bincatalogue.catalogue) < 2:
                    h = 0
                else:

                    catalogue = pipeline(bincatalogue, config, data, 'bin',
                                         m + 1, mccount)
                    if counter == 0:
                        bincatalogue = bincatalogue + catalogue
                    else:
                        h = 0
            if counter == 0:
                bincatalogue.outcatalogue[
                    'Stellar Mass'].unit = bincatalogue.fullcatalogue[
                        'Stellar Mass'].unit
                bincatalogue.outcatalogue[
                    'Other Data'].unit = bincatalogue.fullcatalogue[
                        'Other Data'].unit
                bincatalogue.outcatalogue[
                    'Integrated Flux'].unit = astun.Jy * astun.km / astun.s

                pck.dump(
                    bincatalogue,
                    open(
                        '%sstackertemp/catalogue_obj.pkl' %
                        bincatalogue.outloc, 'wb'))

                catalogue = uf.maskTable(bincatalogue.outcatalogue)
                catalogue = asttab.unique(catalogue, keys='Object ID')
                catalogue.sort('Bin Number')
                astasc.write(
                    catalogue,
                    bincatalogue.outloc +
                    'Stacked_Catalogue_%s.csv' % bincatalogue.origruntime,
                    format='ecsv')
                fullcat.updateconfig(data, bincatalogue.fullmaxgalw,
                                     bincatalogue.fullrebinstatus,
                                     bincatalogue.fullsmoothtype,
                                     bincatalogue.fullsmoothwin,
                                     bincatalogue.fullfuncnum,
                                     bincatalogue.fulloptnum)
            else:
                h = 0
            saveprogress = None
            counter += 1
    else:
        fullcat.catalogue.add_column(
            asttab.Column(name='Bin Number',
                          data=np.ones(len(fullcat.catalogue), dtype=int)))
        fullcat = pipeline(fullcat, config, data, 'normal')
        try:
            fullcat.updateconfig(data, fullcat.maxgalw.value,
                                 fullcat.rebinstatus, fullcat.smoothtype,
                                 fullcat.smoothwin, fullcat.funcnum,
                                 fullcat.optnum)
            fullcat.outcatalogue['Stellar Mass'].unit = fullcat.catalogue[
                'Stellar Mass'].unit
            fullcat.outcatalogue['Other Data'].unit = fullcat.catalogue[
                'Other Data'].unit
            fullcat.outcatalogue[
                'Integrated Flux'].unit = astun.Jy * astun.km / astun.s
            catalogue = uf.maskTable(fullcat.outcatalogue)
            catalogue = asttab.unique(catalogue, keys='Object ID')
            catalogue.sort('Bin Number')
            astasc.write(catalogue,
                         fullcat.outloc +
                         'Stacked_Catalogue_%s.csv' % fullcat.runtime,
                         format='ecsv')
            logging.info('Written Stacked Catalogue to file.')
        except (SystemExit, KeyboardInterrupt):
            uf.exit(fullcat)
        except:
            logging.warning("Struggled to save the catalogue files.",
                            exc_info=True)
            uf.earlyexit(fullcat)

    ## inform the user that the stacking has finished
    if fullcat.suppress != 'hide':
        outloc = uf.bashfriendlypath(fullcat.outloc)
        os.system('open ' + outloc)
    else:
        h = 0

    print('\nStacker has finished.\n')
    uf.exit(fullcat)
    return
Beispiel #3
0
def stackeverything(pbar,
                    cat,
                    stackobj,
                    binstatus,
                    callno,
                    binno,
                    mccount,
                    fig,
                    ax1,
                    ax2,
                    ax3,
                    ax4,
                    analysisfinal=None):
    stackspectrum = SD.objStack(spec=np.zeros(cat.exspeclen) * cat.stackunit,
                                ref=np.zeros(cat.exspeclen) * cat.stackunit,
                                noise=np.zeros(cat.noiselen),
                                weight=0.,
                                spectral=cat.spectralaxis,
                                sunits=cat.stackunit)
    colours = uf.colourgen(stackobj)
    ## loop through all of spectra listed in the catalogue
    ######################################################################################
    ## Stack the catalogue
    ######################################################################################

    for n in range(0, stackobj):
        spectrum = SD.objSpec()
        col = colours[n]
        cat = spectrum.callModule(cat, n, mccount, axes=[ax1, ax2], fig=fig)
        if spectrum.origspec is None:
            continue
        else:
            stackspectrum = stackspectrum + spectrum
            fig = plotprogress(cat, stackobj, col, stackspectrum, spectrum,
                               fig, ax1, ax2, ax3, ax4, n)
            if mccount == 0:
                frac = int((n + 1.) / stackobj * 100.)
                if frac < 2:
                    frac = 0
                else:
                    h = 0
                pbar = uf.progressbar(frac, pbar)
                if cat.saveprogress == 'save':
                    outloc = cat.outloc + 'progressplots/'
                    uf.checkpath(outloc)
                    if binstatus == 'bin':
                        plt.savefig(
                            outloc + 'bin%i_window_%i_source_%s.png' %
                            (binno, n, str(cat.catalogue['Object ID'][n])),
                            bbox_inches='tight',
                            pad_inches=0.2)
                    else:
                        plt.savefig(outloc + 'window_%i_source_%s.png' %
                                    (n, str(cat.catalogue['Object ID'][n])),
                                    bbox_inches='tight',
                                    pad_inches=0.2)
                else:
                    h = 0
            else:
                h = 0
    ######################################################################################
    ######################################################################################
    # close the progress window before moving on to analysis
    if cat.progress == 'progress' and mccount == 0:
        time.sleep(5)
        plt.close('all')
    elif cat.saveprogress == 'save':
        plt.close('all')
    else:
        cat.progress = None

    ######################################################################################
    ## Abort process if the stack is empty
    ######################################################################################
    if len(stackspectrum.stackrms) == 0 and len(
            cat.outcatalogue) == 0 and cat.uncert == 'n':
        print(
            "\nThere were no spectra to stack or something went wrong. I am going to abort and you need to check that there are spectrum files in the correct folder and that the spectrum file names are correctly entered in to the the catalogue."
        )
        logging.critical(
            'There were no spectra to stack, or something else went wrong. Please check that the spectrum files in specified folder.'
        )
        uf.earlyexit(cat)
    elif len(stackspectrum.stackrms) == 0 and len(
            cat.outcatalogue) != 0 and cat.uncert == 'n':
        print(
            "\nThere were no spectra to stack or something went wrong. I am going to abort and you need to check that there are spectrum files in the correct folder and that the spectrum file names are correctly entered in to the the catalogue."
        )
        return cat
    elif len(stackspectrum.stackrms) == 0 and len(
            cat.outcatalogue) == 0 and cat.uncert == 'y':

        return cat, None
    ######################################################################################
    ## Continue on to analysis
    ######################################################################################
    else:
        analysis = AD.anaData()
        cat = analysis.analyse_data(stackspectrum, cat, mccount)
        ##################################################################################
        ## If Uncertainty needs to run..
        ##################################################################################
        stackspectrum, spectrum = None, None
        if cat.uncert == 'n' or callno == 'firstcall':
            analysisfinal = UA.uncertAnalysis(cat,
                                              analysis,
                                              allspec=[],
                                              allnoise=[],
                                              allrms=[],
                                              middle=[],
                                              allintflux=np.array([[], [], [],
                                                                   [], [], [],
                                                                   [], []]))
            return cat, analysisfinal
        else:
            analysisfinal.updateStackAnalysis(cat, mccount, analysis)
            analysis = None
            return cat
    return cat