Example #1
0
 def __getSpectraLocation(self, config):
     try:
         if uf.checkkeys(config, 'SpectrumLocation'):
             self.specloc = config['SpectrumLocation']
         else:
             self.specloc = input(
                 'Please enter the full path of the spectra location: ')
         if self.specloc[-1] != '/':
             self.specloc += '/'
         else:
             h = 0
         if not os.path.exists(self.specloc):
             print(
                 '\nThe spectrum location you have entered does not exist. Please try again.\n'
             )
             self.__getSpectraLocation(config=None)
         else:
             print('\nThe spectrum location exists :).\n')
     except KeyboardInterrupt:
         raise uf.exit(self)
     except SystemExit:
         uf.exit(self)
     except:
         logger.error(
             'There was an issue with the entered spectrum folder path.',
             exc_info=True)
         uf.earlyexit(self)
Example #2
0
 def __getStackUnits(self, config):
     try:
         if uf.checkkeys(config, 'StackFluxUnit'):
             choice = config['StackFluxUnit']
         else:
             print(
                 "\nWhat units do you want your final stacked profile to have?\n\t1. Jansky (Jy)\n\t2. Solar Masses (Msun)\n\t3. Gas Fraction (Msun/Msun)\n\t4. Solar Masses (Msun), stack in Jy [recommend for clusters]"
             )
             choice = eval(
                 input('Please enter the number of the applicable units: '))
             print(type(choice))
         if (type(choice) != int):
             print('Please only select one unit.')
             self.__getStackUnits(None)
         else:
             options = [astun.Jy, uf.msun, uf.gasfrac, astun.Jy]
             self.stackunit = options[int(choice - 1)]
             if choice == 4:
                 self.cluster = True
             else:
                 pass
     except KeyboardInterrupt:
         raise uf.exit(self)
     except SystemExit:
         raise uf.exit(self)
     except:
         logger.error('There was an input error with your unit selection.',
                      exc_info=True)
         uf.earlyexit(self)
     return
Example #3
0
 def __getWeightingScheme(self, config):
     try:
         if uf.checkkeys(config, 'WeightOption'):
             self.weighting = str(config['WeightOption'])
         else:
             print(
                 '\nThe following are options can be used to weight each spectrum:\n\t1. w = 1. [default]\n\t2. w = 1/rms \n\t3. w = 1/rms^2 \n\t4. w = dl^2/rms^2'
             )
             ## TODO: add in option for individual weights from catalogue as well as custom weighting function
             weights = input(
                 'Please enter the number of the scheme you would like to use: '
             )
             self.weighting = weights[0]
         wghts = {
             '1': 'w = 1',
             '2': 'w = 1/rms',
             '3': 'w = 1/rms^2',
             '4': 'w = dl^2/rms^2'
         }
         logger.info('Weighting factor: %s' % wghts[self.weighting])
     except KeyboardInterrupt:
         raise uf.exit(self)
     except SystemExit:
         raise uf.exit(self)
     except:
         logger.error('Could not identify the weighting scheme.',
                      exc_info=True)
         uf.earlyexit(self)
Example #4
0
 def __getCosmology(self, config):
     try:
         if uf.checkkeys(config, 'H0') and uf.checkkeys(config, 'Om0'):
             H0 = config['H0']
             Om0 = config['Om0']
         else:
             print('\nWe assume a Flat Lambda CDM universe')
             H0 = input(
                 'Please enter the Hubble constant (H0) in units of km/s/Mpc: '
             )
             Om0 = input('Please enter the Omega matter (Om0): ')
         self.cosmology = astcos.FlatLambdaCDM(H0, Om0)
         logger.info(
             'Cosmology: H0 = %.1f %s, Om0 = %.3f' %
             (self.cosmology.H0.value, self.cosmology.H0.unit.to_string(),
              self.cosmology.Om0))
     except KeyboardInterrupt:
         raise uf.exit(self)
     except SystemExit:
         raise uf.exit(self)
     except:
         logger.error('There was a problem with entering the cosmology.',
                      exc_info=True)
         uf.earlyexit(self)
     return
Example #5
0
	def __getFluxUnits(self, config):
		try:
			options = [astun.Jy, astun.mJy, astun.uJy]
			if uf.checkkeys(config, 'SpectrumFluxUnit'):
				choice = config['SpectrumFluxUnit']
			else:
				print ("\nWhat are the flux denisty units of your spectra?\n\t1. Jansky (Jy)\n\t2. Milli Jansky (mJy)\n\t3. Micro Jansky (uJy)\n\t4. Jansky/beam (Jy/beam)\n\t5. Milli Jansky/beam (mJy/beam)\n\t6. Micro Jansky/beam (uJy/beam)")
				choice = input('Please enter the number of the applicable units: ')
			if type(choice) != int:
				print ('Please only select one unit.')
				self.__getFluxUnits(None)
			elif choice in [1,2,3]:
				self.fluxunit = options[int(choice-1)]
				self.convfactor = 1
			else:
				self.fluxunit = options[int(choice-4)]
				if uf.checkkeys(config, 'BeamSize') and uf.checkkeys(config, 'PixelSize'):
					pxsize = config['PixelSize']
					beamsize = config['BeamSize']
				else:
					pxsize = input('Please enter pixel size in arcseconds: ')
					beamsize = input('Please enter fwhm of beam (in arcseconds): ')
				self.convfactor = ( 2.*np.pi*beamsize*beamsize/( 2.*np.sqrt(2.*np.log(2) ) )**2  )/pxsize**2
		except KeyboardInterrupt:
			raise KeyboardInterrupt
		except SystemExit:
			uf.exit(self)
		except:
			logger.error('There was an input error with your unit selection', exc_info=True)
			uf.earlyexit(self)
		return
Example #6
0
 def __getMaxLenSpectrum(self, config):
     try:
         if uf.checkkeys(config, 'StackedSpectrumLength'):
             maxstackw = config['StackedSpectrumLength'] * astun.km / astun.s
         else:
             maxstackw = input(
                 'Please enter the maximum length of the stacked spectrum in km/s: '
             ) * astun.km / astun.s
         if maxstackw < 2.5 * self.maxgalw:
             print(
                 'The stacked spectrum length is too short - we will not be able to have very good noise calculations, etc. Please try again.\n'
             )
             self.__getMaxLenSpectrum(config=None)
         else:
             self.maxstackw = maxstackw
         logger.info(
             'Length of stacked spectrum: %i %s' %
             (self.maxstackw.value, self.maxstackw.unit.to_string()))
     except (KeyboardInterrupt, SystemExit):
         uf.exit(self)
     except:
         logger.error(
             'Could not determine the maximum length of the stacked spectrum.',
             exc_info=True)
         uf.earlyexit(self)
Example #7
0
	def __getClusterStack(self, config):
		try:
			vals = {'t': True, 'f': False}
			if uf.checkkeys(config, 'ClusterStack'):
				val = config['ClusterStack'] 
				val = val[0].lower()
				self.cluster = vals[val]
			else:
				val = input('Are you stacking a galaxy cluster [True/False]: ') 
				val = val.lower()
				self.cluster = vals[val]

			if self.cluster == True:
				if uf.checkkeys(config, 'ClusterRedshift'):
					val = config['ClusterRedshift']
					if type(val) != float:
						val = eval(val)
					else: pass
					self.clusterZ = val
				else:
					val = eval(input('Enter the cluster redshift: '))
					self.clusterZ = val
				self.clusterDL = self.cosmology.luminosity_distance(self.clusterZ)
				logger.info('Stacking galaxy cluster at z = %.4f (Dl = %.1f Mpc)'%(self.clusterZ, self.clusterDL.value))
			else:
				logger.info('No galaxy cluster in this sample.')
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			raise uf.exit(self)
		except:
			logger.error("Did not understand if there is a galaxy cluster in this sample.", exc_info=True)
			uf.earlyexit(self)
Example #8
0
    def __getSpectralUnits(self, config):
        try:
            if uf.checkkeys(config, 'SpectralAxisUnit') and uf.checkkeys(
                    config, 'VelocityType'):
                choice = config['SpectralAxisUnit']
                self.veltype = config['VelocityType']
            else:
                print(
                    "\nWhat are the units of spectral axis of your spectra?\n\t1. Hertz (Hz)\n\t2. Kilo Hertz (kHz)\n\t3. Mega Hertz (MHz)\n\t4. Metres/Second (m/s)\n\t5. Kilometres/Second (km/s)\n\t6. Radio velocity (km/s)"
                )
                choice = input(
                    'Please enter the number of the applicable units: ')
                if int(choice) in [4, 5]:
                    self.veltype = input(
                        'Please enter the velocity type [optical/radio]: '
                    ).lower()
            options = [
                astun.Hz, astun.kHz, astun.MHz, astun.m / astun.s,
                astun.km / astun.s
            ]
            self.spectralunit = options[int(choice) - 1]

        except KeyboardInterrupt:
            raise KeyboardInterrupt
        except SystemExit:
            uf.exit(self)
        except:
            logger.error('There was an input error with your unit selection.',
                         exc_info=True)
            uf.earlyexit(self)
        return
Example #9
0
    def runInput(self, config):

        try:
            self.__callCriticalUser(config)
            self.__callOptionalUser(config)
            self.__calcPrivate()
            self.__checkConfig(config)
        except (SystemExit, KeyboardInterrupt):
            uf.exit(self)
        except:
            logger.error('Encountered an exception:', exc_info=True)
            uf.earlyexit(self)
Example #10
0
	def __getSpectrumDelimiter(self, config):
		try: 
			if uf.checkkeys(config, 'RowDelimiter'): 
				self.rowdelim = config['RowDelimiter']
			else:
				self.rowdelim = input('Please enter the delimiter used in the spectra files (this must be the same for all spectra): ')
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			uf.exit(self)
		except:
			logger.error('There was an error with the row delimiter.', exc_info=True)
			uf.earlyexit(self)
Example #11
0
	def __getChannelWidth(self, config):
		try:
			if uf.checkkeys(config, 'ChannelWidth'):
				self.channelwidth = config['ChannelWidth'] * self.spectralunit
			else:
				self.channelwidth = input('Please enter the channel width (in the same units as the spectral axis): ') * self.spectralunit
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			uf.exit(self)
		except:
			logger.error('There was an input error with your channel width.', exc_info=True)
			uf.earlyexit(self)
Example #12
0
	def __getMaxRedshift(self, config):
		try:
			if uf.checkkeys(config, 'z_max'):
				self.max_redshift = config["z_max"]
			else:
				self.max_redshift = input('\nPlease enter the maximum redshift value of your sample: ')
		except KeyboardInterrupt:
			uf.earlyexit(self)
		except SystemExit:
			uf.exit(self)
		except:
			logger.critical("There was an error with the maximum redshift value.", exc_info=True)
			uf.earlyexit(self)
Example #13
0
	def __getSpectrumRowStart(self, config):
		try:
			if uf.checkkeys(config, 'FirstRowInSpectrum'):
				self.rowstart = int(config['FirstRowInSpectrum'])
			else:
				self.rowstart = input('Please enter the row number corresponding to the start of the data in the spectrum files (this number should be the same for every spectrum file).\n(Please note: row numbering starts at 1 - the first row of information is row 1): ')
				self.rowstart = int(self.rowstart)
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			uf.exit(self)
		except:
			logger.error('There was an error with the number of the first row in the spectrum files.', exc_info=True)
			uf.earlyexit(self)
Example #14
0
	def __getMaxGalaxyWidth(self, config):
		try:
			if uf.checkkeys(config, 'GalaxyWidth'):
				self.maxgalw = config['GalaxyWidth'] * astun.km/astun.s
			else:
				self.maxgalw = input('Enter maximum galaxy width in km/s: ') * astun.km/astun.s
			logger.info('Maximum galaxy width = %i %s'%(self.maxgalw.value, self.maxgalw.unit.to_string()))
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			raise uf.exit(self)
		except:
			logger.error("Did not understand the maximum galaxy width.", exc_info=True)
			uf.earlyexit(self)
Example #15
0
	def __getUncertType(self, config):
		try:
			if uf.checkkeys(config, 'UncertaintyMethod'):
				self.uncerttype = config['UncertaintyMethod']
			else:
				untyp = input('Which method of redshift uncertainties should I use? [dagjk/redshift] ')
				uncertop = { 'r': 'redshift', 'd': 'dagjk' }
				self.uncerttype = uncertop[(untyp.lower())[0]]
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			raise uf.exit(self)
		except: 
			logger.error('There was a problem determining whether or not to perform an uncertainty analysis', exc_info=True)
			uf.earlyexit(self)
Example #16
0
	def __getSpectrumColumns(self, config):
		try:
			if uf.checkkeys(config, 'SpectrumColumns'):
				self.speccol = config['SpectrumColumns']
			else:
				self.speccol = input("Please enter the column numbers of the spectral axis and flux (the column number of the first column is 0).\nThe numbers should be separated by a ',' e.g. 0,1: ")
			if len(self.speccol) != 2:
				print ('You need to enter two column numbers. Please try again.\n')
				self.__getSpectrumColumns(config=False, data=None)
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit():
			raise uf.exit(self)
		except:
			logger.error('There was an error with the spectrum column numbers.', exc_info=True)
			uf.earlyexit(self)
Example #17
0
    def display(self, cat, allflux, extra=None, hide=None):
        if cat.uncert == 'y':
            uncert = True
        else:
            uncert = False
        try:
            self.__read_in_data(cat)
        except (SystemExit, KeyboardInterrupt):
            uf.exit(cat)
        except:
            logger.critical('Encountered an error reading in the FITS data.',
                            exc_info=True)
            uf.earlyexit(cat)

        try:
            self.__readTextData()
        except (SystemExit, KeyboardInterrupt):
            uf.exit(cat)
        except:
            logger.critical('Encountered an error reading in the text data.',
                            exc_info=True)
            uf.earlyexit(cat)

        try:
            self.__printStatistics(uncert, cat, self.spec, self.specuncert,
                                   self.spectralaxis)
        except (SystemExit, KeyboardInterrupt):
            uf.exit(cat)
        except:
            logger.critical('Encountered an error printing the results.',
                            exc_info=True)
            uf.earlyexit(cat)

        if cat.suppress == 'hide':
            logger.info(
                'Final plots are not displayed to screen. Check saved data.')
            return
        else:
            try:
                self.__displayplots(cat,
                                    uncert,
                                    allflux=allflux,
                                    spec=self.spec,
                                    spectral=self.spectralaxis,
                                    refspec=self.refspec,
                                    specuncert=self.specuncert,
                                    extra=extra)
            except (SystemExit, KeyboardInterrupt):
                uf.exit(cat)
            except:
                logger.critical(
                    'Encountered an error trying to display the plots:',
                    exc_info=True)
                uf.earlyexit(cat)
        return
Example #18
0
	def __getUncertAn(self, config):
		try:
			if uf.checkkeys(config, 'UncertaintyYN'):
				self.uncert = config['UncertaintyYN']
			else:
				self.uncert = input('Should I calculate uncertainties? ')

			if self.uncert.lower() == 'n':
				self.mc = 1
			else:
				self.mc = self.mc
		except KeyboardInterrupt:
			raise uf.exit(self)
		except SystemExit:
			raise uf.exit(self)
		except: 
			logger.error('There was a problem determining whether or not to perform an uncertainty analysis.')
			uf.earlyexit(self)
Example #19
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)
Example #20
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