def init1(self): moon = Moon(self.planetx + self.radCurve, self.planety + self.radCurve) planet = Planet(self.planetx, self.planety, "earth", 100) ship = Ship(self.planetx + self.r * cos(self.theta), self.planety + self.r * sin(self.theta)) self.planetGroup = pygame.sprite.Group(planet) #c self.shipGroup = pygame.sprite.Group(ship) #c self.moonGroup = pygame.sprite.Group(moon) #c
def create_moons(moon_text) -> List[Moon]: moons = [] for moon in moons_text: parts = moon.strip('<>').split(',') t = [] for i, part in enumerate(parts): dim, val = part.split('=') t.append(int(val)) moons.append(Moon(*t)) return moons
def level1init(self): self.marsHit = False self.screen1 = True self.playing = True self.onScreen = True self.marsx = self.width * 3 / 5 - 50 self.marsy = self.height / 2 self.mars = Planet(self.marsx, self.marsy, "mars", 100, 30) self.marsGroup = pygame.sprite.Group(self.mars) Ship.init() Moon.init() self.planetx, self.planety = self.width / 5, self.height - self.height / 3, self.r = 120 self.radCurve = 550 self.moonAngle = 80 self.moonx, self.moony = self.width, self.height self.init1() self.m = 0 self.gameDisplay = pygame.display.set_mode((self.width, self.height)) self.starImage = pygame.transform.rotate( pygame.transform.scale( pygame.image.load('images/stars.png').convert_alpha(), (self.width, self.height)), 0)
def partOne(): numbers = fileToList("input.txt") # Make my 4 moon objects Moons = [] TIME = 0 for coor in numbers: Moons.append(Moon(coor[0], coor[1], coor[2])) while TIME < 1000: Moons = Gravity(Moons) Moons = Velocity(Moons) TIME += 1 TotalEnergy = FindTotalEnergy(Moons) print("The total energy after {} steps is: {}".format(TIME, TotalEnergy))
def partTwo(): def _lcm(a, b): return a * b // math.gcd(a, b) numbers = fileToList("input.txt") # Make my 4 moon objects Moons = [] # Make a dictionary for the moon times # Period of all the moons XPeriod = 0 YPeriod = 0 ZPeriod = 0 TIME = 1 for coor in numbers: Moons.append(Moon(coor[0], coor[1], coor[2])) Moons = SetAllKeys(Moons) print("IO starting") Moons[0].PrintValues() StartingX = [Moons[0].x, Moons[1].x, Moons[2].x, Moons[3].x] StartingY = [Moons[0].y, Moons[1].y, Moons[2].y, Moons[3].y] StartingZ = [Moons[0].z, Moons[1].z, Moons[2].z, Moons[3].z] while True: Moons = Gravity(Moons) Moons = Velocity(Moons) TIME += 1 if [Moons[0].x, Moons[1].x, Moons[2].x, Moons[3].x ] == StartingX and XPeriod == 0: XPeriod = TIME print(XPeriod) if [Moons[0].y, Moons[1].y, Moons[2].y, Moons[3].y ] == StartingY and YPeriod == 0: YPeriod = TIME print(YPeriod) if [Moons[0].z, Moons[1].z, Moons[2].z, Moons[3].z ] == StartingZ and ZPeriod == 0: ZPeriod = TIME print(ZPeriod) if XPeriod > 0 and YPeriod > 0 and ZPeriod > 0: answer = _lcm(_lcm(XPeriod, YPeriod), ZPeriod) print("Answer to part two is: ", answer) return 0
def get_system_energy(moons): total = 0 for moon in moons: total += moon.get_total_energy() return total # TEST 1 print('TEST 1') raw_moons = '<x=-1, y=0, z=2>\n\ <x=2, y=-10, z=-7>\n\ <x=4, y=-8, z=8>\n\ <x=3, y=5, z=-1>' moon_data_set = [ Moon.parse_moon(str(i), raw_moon) for i, raw_moon in enumerate(raw_moons.split('\n')) ] moon_data_set = update_steps(moon_data_set, steps=10) test_energy = get_system_energy(moon_data_set) expected_energy = 179 print( 'Testing get_system_energy (1)', 'RIGHT' if expected_energy == test_energy else f'WRONG!! Expected {expected_energy} but was {test_energy}') # TEST 2 print('TEST 2') raw_moons = '<x=-8, y=-10, z=0>\n\ <x=5, y=5, z=10>\n\ <x=2, y=-7, z=3>\n\
def getopsimouts(inputobs_file , override_config_file=None, outfile=None, verbose=False): """ Obtains the output of opsimObs for interesting quantities based on the input file. This function is used in creating the ouputs converted into the simlib file. TODO: Actually this function should go into makelsstsims rather than here. """ config = setDefaultConfigs(override_config_file) # Set up a skypos object to hold site information and provide ra/dec -> alt/az/airmass translations. skypos = SkyPos() skypos.setSite(lat=config['latitude'], lon=config['longitude'], height=config['height'], pressure=config['pressure'], temperature=config['temperature'], relativeHumidity=config['relativeHumidity'], lapseRate=config['lapseRate']) # Set up a Weather object to read the site weather data. t = time.time() weather = Weather() weather.readWeather(config) dt, t = dtime(t) if verbose: print '# Reading weather required %.2f seconds' %(dt) # Set up a Downtime object to read the downtime data. downtime = Downtime() downtime.readDowntime(config) dt, t = dtime(t) if verbose: print '# Reading downtime required %.2f seconds' %(dt) # Read observations. obs = ObsFields() obs.readInputObs(inputobs_file, config) nobs = len(obs.ra) dt, t = dtime(t) if verbose: print '# Reading %d input observations required %.2f seconds' %(nobs, dt) # Check timing of input observations. if config['check_observations']: obs.checkInputObs(config) dt, t = dtime(t) if verbose: print '# Checking %d input observations required %.2f seconds' %(nobs, dt) print '# Did not check input observations for minimum required timing separation!' # Calculate alt/az/airmass for all fields. obs.getAltAzAirmass(skypos) dt, t = dtime(t) if verbose: print '# Calculating alt/az/airmass for %d input observations required %.2f seconds' %(nobs, dt) # Calculate weather (cloud/seeing) for all fields. dt, t = dtime(t) obs.getObsWeather(weather, config) dt, t = dtime(t) if verbose: print '# Getting weather information for %d observations required %.2f seconds' %(nobs, dt) # Check downtime status for these observations obs.getDowntime(downtime, config) # Calculate position of sun at the times of these observations. sun = Sun() dt, t = dtime(t) sun.calcPos(obs.mjd) sun.getAltAz(skypos) dt, t = dtime(t) if verbose: print '# Calculating sun position at %d times required %.2f seconds' %(nobs, dt) # Calculate the position, phase and altitude of the Moon. moon = Moon() moon.calcPos(obs.mjd, config) moon.getAltAz(skypos) dt, t = dtime(t) if verbose: print '# Calculating moon position at %d times required %.2f seconds' %(nobs, dt) # Will clean this up and put into classes as time is available. # Calculate the sky brightness. skybright = SkyBright(model='Perry', solar_phase='ave') sky = numpy.zeros(len(obs.mjd), 'float') filterlist = numpy.unique(obs.filter) for f in filterlist: condition = (obs.filter == f) skybright.setSkyBright(obs.alt[condition], obs.az[condition], moon.alt[condition], moon.az[condition], moon.phase[condition], bandpass = f) sky[condition] = skybright.getSkyBright() """ for i in range(len(obs.mjd)): # Calculate sky brightness for each observation. skybright.setSkyBright(obs.alt[i], obs.az[i], moon.alt[i], moon.az[i], moon.phase[i], bandpass=obs.filter[i]) sky[i] = skybright.getSkyBright() """ # Add modification to match 'skybrightness_modified' (which is brighter in twilight) sky = numpy.where(sun.alt > -18, 17, sky) dt, t = dtime(t) if verbose: print '# Calculating the sky brightness for %d observations required %.2f seconds' %(nobs, dt) # Calculate the 5-sigma limiting magnitudes. maglimit = numpy.zeros(len(obs.mjd), 'float') m5 = m5calculations() # Read the throughput curves for LSST (needed to determine zeropoints). m5.setup_Throughputs(verbose=False) # Swap 'y4' for 'y' (or other default y value). tmp_filters = m5.check_filter(obs.filter) # Determine the unique exposure times, as have to set up dark current, etc. based on this for telescope ZP's. exptimes = numpy.unique(obs.exptime) for expT in exptimes: condition = [obs.exptime == expT] # Calculate telescope zeropoints. # Calculate actual open-sky time, after accounting for readout time, number of exposures per visit, shutter time, etc. opentime = ((expT - config['readout_time']*config['nexp_visit'] - config['nexp_visit']* config['add_shutter']*config['shutter_time']) / float(config['nexp_visit'])) print "# Calculating depth for %d exposures of %.2f open shutter time" %(config['nexp_visit'], opentime) m5.setup_values(expTime=opentime, nexp=config['nexp_visit']) # Calculate 5sigma limiting magnitudes. maglimit[condition] = m5.calc_maglimit(obs.seeing[condition], sky[condition], tmp_filters[condition], obs.airmass[condition], snr=5.0) dt, t = dtime(t) if verbose: print '# Calculating the m5 limit for %d observations required %.2f seconds' %(nobs, dt) # Print out interesting values. obs.printObs(sun, moon, sky, maglimit, config, outfile)
def parse_moon(string): m = re.search(r'<x=(-?\d+), y=(-?\d+), z=(-?\d+)>', string) x, y, z = int(m.group(1)), int(m.group(2)), int(m.group(3)) return Moon(x, y, z)
def run(inputobs_file, config): # Set up a skypos object to hold site information and provide ra/dec -> alt/az/airmass translations. skypos = SkyPos() skypos.setSite(lat=config['latitude'], lon=config['longitude'], height=config['height'], pressure=config['pressure'], temperature=config['temperature'], relativeHumidity=config['relativeHumidity'], lapseRate=config['lapseRate']) # Set up a Weather object to read the site weather data. t = time.time() weather = Weather() weather.readWeather(config) dt, t = dtime(t) print '# Reading weather required %.2f seconds' %(dt) # Set up a Downtime object to read the downtime data. downtime = Downtime() downtime.readDowntime(config) dt, t = dtime(t) print '# Reading downtime required %.2f seconds' %(dt) # Read observations. obs = ObsFields() obs.readInputObs(inputobs_file, config) nobs = len(obs.ra) dt, t = dtime(t) print '# Reading %d input observations required %.2f seconds' %(nobs, dt) # Check timing of input observations. if config['check_observations']: obs.checkInputObs(config) dt, t = dtime(t) print '# Checking %d input observations required %.2f seconds' %(nobs, dt) else: print '# Did not check input observations for minimum required timing separation!' # Calculate alt/az/airmass for all fields. obs.getAltAzAirmass(skypos) dt, t = dtime(t) print '# Calculating alt/az/airmass for %d input observations required %.2f seconds' %(nobs, dt) # Calculate weather (cloud/seeing) for all fields. dt, t = dtime(t) obs.getObsWeather(weather, config) dt, t = dtime(t) print '# Getting weather information for %d observations required %.2f seconds' %(nobs, dt) # Check downtime status for these observations obs.getDowntime(downtime, config) # Calculate position of sun at the times of these observations. sun = Sun() dt, t = dtime(t) sun.calcPos(obs.mjd) sun.getAltAz(skypos) dt, t = dtime(t) print '# Calculating sun position at %d times required %.2f seconds' %(nobs, dt) # Calculate the position, phase and altitude of the Moon. moon = Moon() moon.calcPos(obs.mjd, config) moon.getAltAz(skypos) dt, t = dtime(t) print '# Calculating moon position at %d times required %.2f seconds' %(nobs, dt) # Will clean this up and put into classes as time is available. # Calculate the sky brightness. skybright = SkyBright(model='Perry', solar_phase='ave') sky = numpy.zeros(len(obs.mjd), 'float') filterlist = numpy.unique(obs.filter) for f in filterlist: condition = (obs.filter == f) skybright.setSkyBright(obs.alt[condition], obs.az[condition], moon.alt[condition], moon.az[condition], moon.phase[condition], bandpass = f) sky[condition] = skybright.getSkyBright() # Add modification to match 'skybrightness_modified' (which is brighter in twilight) sky = numpy.where(sun.alt > -18, 17, sky) dt, t = dtime(t) print '# Calculating the sky brightness for %d observations required %.2f seconds' %(nobs, dt) # Calculate the 5-sigma limiting magnitudes. maglimit = numpy.zeros(len(obs.mjd), 'float') m5 = m5calculations() # Read the throughput curves for LSST (needed to determine zeropoints). m5.setup_Throughputs(verbose=False) # Swap 'y4' for 'y' (or other default y value). tmp_filters = m5.check_filter(obs.filter) # Determine the unique exposure times, as have to set up dark current, etc. based on this for telescope ZP's. exptimes = numpy.unique(obs.exptime) for expT in exptimes: condition = [obs.exptime == expT] # Calculate telescope zeropoints. # Calculate actual open-sky time, after accounting for readout time, number of exposures per visit, shutter time, etc. opentime = ((expT - config['readout_time']*config['nexp_visit'] - config['nexp_visit']* config['add_shutter']*config['shutter_time']) / float(config['nexp_visit'])) print "# Calculating depth for %d exposures of %.2f open shutter time" %(config['nexp_visit'], opentime) m5.setup_values(expTime=opentime, nexp=config['nexp_visit']) # Calculate 5sigma limiting magnitudes. maglimit[condition] = m5.calc_maglimit(obs.seeing[condition], sky[condition], tmp_filters[condition], obs.airmass[condition], snr=5.0) dt, t = dtime(t) print '# Calculating the m5 limit for %d observations required %.2f seconds' %(nobs, dt) # Print out interesting values. obs.printObs(sun, moon, sky, maglimit, config)