Exemple #1
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
Exemple #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
Exemple #3
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)
Exemple #4
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
Exemple #5
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)
Exemple #6
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)
Exemple #7
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)
Exemple #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
Exemple #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)
Exemple #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)
Exemple #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)
Exemple #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)
Exemple #13
0
 def __getSpectrum(self, cat, n, runno):
     self.objid = cat.catalogue['Object ID'][n]
     filexist = os.path.isfile(cat.specloc + cat.catalogue['Filename'][n])
     if filexist == True:
         try:
             # print(cat.constan['Object ID'][n])
             data = astasc.read(cat.specloc + cat.catalogue['Filename'][n],
                                data_start=cat.rowstart)
             checkNAN, cat = self.__getSpecNaNOK(
                 (data[data.colnames[cat.speccol[0]]]).astype(np.float_),
                 cat)
             checkZ, cat = self.__getSpectrumZOK(cat, n, runno)
             checkDV, cat = self.__getSpectrumDVOK(
                 (data[data.colnames[cat.speccol[0]]]).astype(np.float_),
                 cat, n)
             checklen, cat = self.__getSpectrumlenOK(
                 (data[data.colnames[cat.speccol[1]]]).astype(np.float_),
                 cat, n)
             if cat.stackunit == uf.gasfrac:
                 checkSM, cat = self.__getStellarMass(cat, n)
             else:
                 checkSM = True
             if checkDV and checkZ and checklen and checkSM and checkNAN:
                 spec = data[data.colnames[
                     cat.speccol[1]]] * cat.fluxunit / cat.convfactor
                 spec[np.isnan(spec)] = 0.
                 self.origspec = spec.to(astun.Jy)
                 if cat.veltype == 'optical':
                     self.spectralspec = data[data.colnames[
                         cat.speccol[0]]].data * cat.spectralunit
                 else:
                     self.spectralspec = data[data.colnames[cat.speccol[
                         0]]].data * (1. + self.redshift) * cat.spectralunit
                 return cat
             else:
                 self.status = 'incomplete'
                 return cat
         except KeyboardInterrupt:
             logger.error('Encountered an exception:', exc_info=True)
             uf.earlyexit(self)
             raise KeyboardInterrupt
         except Exception as e:
             logger.error('Encountered an exception:', exc_info=True)
             self.status = 'incomplete'
         return cat
     else:
         logger.warning(
             'Spectrum ID: %s has no corresponding spectrum file.' %
             str(cat.catalogue['Object ID'][n]))
         self.status = 'incomplete'
         return cat
Exemple #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)
Exemple #15
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)
Exemple #16
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)
Exemple #17
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)
Exemple #18
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
Exemple #19
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)
Exemple #20
0
 def __getSpectrum(self, cat, n, runno):
     self.objid = cat.catalogue['Object ID'][n]
     filexist = os.path.isfile(cat.specloc + cat.catalogue['Filename'][n])
     if filexist == True:
         try:
             data = astasc.read(cat.specloc + cat.catalogue['Filename'][n],
                                data_start=cat.rowstart)
             checkZ, cat = self.__getSpectrumZOK(cat, n, runno)
             checkDV, cat = self.__getSpectrumDVOK(
                 data[data.colnames[cat.speccol[0]]], cat, n)
             checklen, cat = self.__getSpectrumlenOK(
                 data[data.colnames[cat.speccol[1]]], cat, n)
             if cat.stackunit == uf.gasfrac:
                 checkSM, cat = self.__getStellarMass(cat, n)
             else:
                 checkSM = True
             if checkDV and checkZ and checklen and checkSM:
                 ################################
                 # for testing puposes only
                 #------------------------------------------------------#
                 # random gaussian noise
                 #------------------------------------------------------#
                 # s = 0.6*np.max(data[1])#abs(np.random.normal(0,scale=np.max(data[1]), size=1))
                 # data[1] += np.random.normal(0, s, size=len(data[1]))
                 #------------------------------------------------------#
                 # making absorption spectra
                 #------------------------------------------------------#
                 # data[1] = data[1]*(-1)
                 #------------------------------------------------------#
                 # coarsening resolution
                 #------------------------------------------------------#
                 # self.origspec = self.__smoothData(data[1])*cat.fluxunit/cat.convfactor
                 ################################
                 spec = data[data.colnames[
                     cat.speccol[1]]] * cat.fluxunit / cat.convfactor
                 spec[np.isnan(spec)] = 0.
                 self.origspec = spec.to(astun.Jy)
                 if cat.veltype == 'optical':
                     self.spectralspec = data[data.colnames[
                         cat.speccol[0]]].data * cat.spectralunit
                 else:
                     self.spectralspec = data[data.colnames[cat.speccol[
                         0]]].data * (1. + self.redshift) * cat.spectralunit
                 return cat
             else:
                 self.status = 'incomplete'
                 return cat
         except KeyboardInterrupt:
             logger.error('Encountered an exception:', exc_info=True)
             uf.earlyexit(self)
             raise KeyboardInterrupt
         except Exception as e:
             print(cat.specloc + cat.catalogue['Filename'][n])
             logger.error('Encountered an exception:', exc_info=True)
             self.status = 'incomplete'
         return cat
     else:
         logger.warning(
             'Spectrum ID: %s has no corresponding spectrum file.' %
             str(cat.catalogue['Object ID'][n]))
         self.status = 'incomplete'
         return cat
Exemple #21
0
    def callModule(self, cat, n, runno, axes=None, fig=None):
        try:
            cat = self.__getSpectrum(cat, n, runno)

            if self.origspec is None:
                return cat
            else:
                self.__calcShift(cat)
                if cat.cluster == True:
                    self.dl = cat.clusterDL
                else:
                    self.dl = cat.cosmology.luminosity_distance(self.redshift)

                if uf.msun == cat.stackunit:
                    self.massspec, self.refmassspec = self.calcMassConversion(
                        cat, self.shiftorigspec, self.shiftrefspec,
                        self.redshift, self.randredshift, self.dl, self.dvkms)
                    self.rms, self.noisespec, self.noisespecmass = self.__calcNoiseRMS(
                        self.shiftorigspec, cat, self.massspec)
                    self.extendspec, self.extendrefspec = self.__calcExtendSpectrum(
                        cat, self.massspec, self.refmassspec,
                        self.noisespecmass)
                    self.__calcWeights(cat, n)
                elif astun.Jy == cat.stackunit:
                    self.rms, self.noisespec = self.__calcNoiseRMS(
                        self.shiftorigspec, cat)
                    self.extendspec, self.extendrefspec = self.__calcExtendSpectrum(
                        cat, self.shiftorigspec, self.shiftrefspec,
                        self.noisespec)
                    self.__calcWeights(cat, n)
                elif uf.gasfrac == cat.stackunit:
                    self.massspec, self.refmassspec = self.calcMassConversion(
                        cat, self.shiftorigspec, self.shiftrefspec,
                        self.redshift, self.randredshift, self.dl, self.dvkms)
                    # self.massspec, self.refmassspec = self.calcMassConversion(cat, self.shiftorigspec, self.shiftrefspec, 0.0231, self.randredshift, 100*astun.Mpc, self.dvkms)
                    self.rms, self.noisespec, self.noisespecmass = self.__calcNoiseRMS(
                        self.shiftorigspec, cat, self.massspec)
                    self.extendspec, self.extendrefspec = self.__calcExtendSpectrum(
                        cat, self.massspec, self.refmassspec,
                        self.noisespecmass)
                    self.__calcWeights(cat, n)
                else:
                    logger.error(
                        "Something went wrong with deciding the stack unit and how to stack"
                    )
                    uf.earlyexit(cat)
                    raise sys.exit()

                startind = int(len(self.shiftorigspec) / 2 - cat.stackmask / 2)
                endind = int(
                    len(self.shiftorigspec) / 2 + 1 + cat.stackmask / 2)

                intfluxspec = (self.shiftorigspec * self.dvkms).to(
                    astun.Jy * astun.km / astun.s)
                intflux = np.sum(intfluxspec.value[startind:endind])

                if runno == 0:
                    cat.outcatalogue.add_row([
                        cat.catalogue['Bin Number'][n], self.objid,
                        cat.catalogue['Filename'][n],
                        cat.catalogue['Redshift'][n],
                        cat.catalogue['Redshift Uncertainty'][n],
                        cat.catalogue['Stellar Mass'][n],
                        cat.catalogue['Other Data'][n], intflux, self.weight
                    ])
                else:
                    h = 0
        except KeyboardInterrupt:
            logger.error("Encountered exception.", exec_info=True)
            uf.earlyexit(cat)
            raise KeyboardInterrupt
        except SystemExit:
            logger.error("Encountered exception.", exec_info=True)
            uf.earlyexit(cat)
            raise sys.exit()
        return cat
Exemple #22
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
Exemple #23
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
Exemple #24
0
    def __get_astropy_catalogue(self, config):
        print("Initialising the catalogue file.")
        logger.info('Initialising the catalogue file.')

        if uf.checkkeys(config, 'CatalogueFilename'):
            self.catfilename = config['CatalogueFilename']
        else:
            self.catfilename = input(
                'Enter location and filename of catalogue file: ')
        while not os.path.isfile(self.catfilename):
            print(
                'The catalogue filename and location you have given does not exist.\n'
            )
            self.catfilename = input(
                'Enter location and filename of catalogue file: ')
        try:
            filetable = astasc.read(
                self.catfilename
            )  # [, format='csv'] took out 10/01/19 when line crashed with .dat on JD system
        except KeyboardInterrupt:
            uf.earlyexit(self)
            raise sys.exit()
        except:
            print(
                "Did not recognise the catalogue data type. Please check that your catalogue file is in the same format as the example catalogue file."
            )
            logger.critical("Did not recognise the catalogue file data type.")
            raise sys.exit()

        if uf.checkkeys(config, 'CatalogueColumnNumbers'):
            catcols = config['CatalogueColumnNumbers']
        else:
            catcols = None
            filetablenames = filetable.colnames
            print('\nThe following columns are available:')
            for u in range(len(filetablenames)):
                print('%i: %s' % (u, filetablenames[u]))

        colnames = [
            'Object ID', 'Filename', 'Redshift', 'Redshift Uncertainty',
            'Stellar Mass', 'Other Data'
        ]
        catalogue = asttab.Table(names=['Dud'],
                                 dtype=['f8'],
                                 masked=True,
                                 data=[np.zeros(len(filetable))])

        for o in range(len(colnames)):
            try:
                self.__fill_catalogue(catalogue, filetable, colnames[o], o,
                                      catcols)
            except KeyboardInterrupt:
                uf.earlyexit(self)
            except SystemExit():
                uf.earlyexit(self)
                raise sys.exit()
            except:
                print(
                    'There was problem with entering the column number, please try again.\n'
                )
                self.__fill_catalogue(catalogue, filetable, colnames[o], o,
                                      None)
        catalogue.remove_column('Dud')

        ## check units of the stellar mass
        if True in catalogue['Stellar Mass'].mask:
            if self.stackunit == uf.gasfrac:
                print(
                    "\nTo stack in gas fraction, the catalogue file must include the Stellar Mass for each profile."
                )
                logger.info(
                    'To stack in gas fraction, the catalogue file must include the Stellar Mass for each profile.'
                )
                uf.earlyexit(self)
        else:
            checkval = str(int(catalogue['Stellar Mass'][0]))
            if len(checkval) < 3:
                catalogue['Stellar Mass'].unit = astun.dex * uf.msun
            else:
                catalogue['Stellar Mass'].unit = uf.msun

            self.avesm = np.mean(catalogue['Stellar Mass'].data
                                 ) * catalogue['Stellar Mass'].unit

        uniquecatalogue = asttab.unique(catalogue, keys='Object ID')
        self.catalogue = uniquecatalogue
        self.medianz = np.median(self.catalogue['Redshift'].data)
        logger.info('Catalogue has been read in.')

        return