def find(self, start: int, stop: int, aei_paths: tuple): """Analyze resonances for pointed half-interval of numbers of asteroids. It gets resonances aggregated to asteroids. Computes resonant phase by orbital elements from prepared aei files of three bodies (asteroid and two planets). After this it finds circulations in vector of resonant phases and solves, based in circulations, libration does exists or no. :param aei_paths: :param start: start point of half-interval. :param stop: stop point of half-interval. It will be excluded. :return: """ orbital_element_sets = None pathbuilder = FilepathBuilder(aei_paths, self._is_recursive, self._clear_s3) filepaths = [pathbuilder.build('%s.aei' % x) for x in self._planets] try: orbital_element_sets = build_bigbody_elements(filepaths) except AEIValueError: logging.error('Incorrect data in %s' % ' or in '.join(filepaths)) exit(-1) aei_getter = AEIDataGetter(pathbuilder, self._clear) classifier = LibrationClassifier(self._is_current, BodyNumberEnum(len(self._planets) + 1)) phase_builder = PhaseBuilder(self._phase_storage) p_bar = None if self._is_verbose: p_bar = ProgressBar((stop + 1 - start), 'Find librations') asteroid_name = None for resonance, aei_data in get_aggregated_resonances(start, stop, False, self._planets, aei_getter): if self._is_verbose and asteroid_name != resonance.small_body.name: p_bar.update() asteroid_name = resonance.small_body.name broken_asteroid_mediator = _BrokenAsteroidMediator(asteroid_name) if broken_asteroid_mediator.check(): continue logging.debug('Analyze asteroid %s, resonance %s' % (asteroid_name, resonance)) resonance_id = resonance.id classifier.set_resonance(resonance) orbital_elem_set_facade = ResonanceOrbitalElementSetFacade(orbital_element_sets, resonance) try: serialized_phases = phase_builder.build(aei_data, resonance_id, orbital_elem_set_facade) except AEIValueError: broken_asteroid_mediator.save() continue classifier.classify(orbital_elem_set_facade, serialized_phases) session.flush() session.commit()
def genres(asteroid_number: int, integers: List[int], filepaths: List[str], planets: Tuple): t1 = aliased(Planet) t2 = aliased(Planet) cond = len(integers) == 3 resonance_cls = ThreeBodyResonance if cond else TwoBodyResonance query = ( session.query(resonance_cls).outerjoin(t1, t1.id == resonance_cls.first_body_id).filter(t1.name == planets[0]) ) if cond: query = query.outerjoin(t2, t2.id == resonance_cls.second_body_id).filter(t2.name == planets[1]) query = query.outerjoin(Asteroid, Asteroid.id == resonance_cls.small_body_id).filter( Asteroid.name == "A%i" % asteroid_number ) resonance = query.first() if not resonance: logging.warning("There is no resonance by pointed filter.") return resonance_id = resonance.id phase_storage = PhaseStorage.file phase_builder = PhaseBuilder(phase_storage) phase_loader = PhaseLoader(phase_storage) phase_cleaner = PhaseCleaner(phase_storage) print("Loading aei files.") builder = FilepathBuilder(filepaths, True) planet_aei_paths = [builder.build("%s.aei" % x) for x in planets] resmaker = ResfileMaker(planets, planet_aei_paths) getter = AEIDataGetter(builder) orbital_element_sets = build_bigbody_elements(planet_aei_paths) orbital_elem_set_facade = ResonanceOrbitalElementSetFacade(orbital_element_sets, resonance) aei_data = getter.get_aei_data(asteroid_number) phase_builder.build(aei_data, resonance_id, orbital_elem_set_facade) phases = phase_loader.load(resonance_id) folder = opjoin(getcwd(), "res") if not exists(folder): mkdir(folder) resmaker.make( phases, aei_data, opjoin(folder, "A%i_%s_%s.res" % (asteroid_number, "_".join(planets), "_".join([str(x) for x in integers]))), ) phase_cleaner.delete(resonance_id)
def plot(start: int, stop: int, phase_storage: PhaseStorage, for_librations: bool, integers: List[str], aei_paths: Tuple[str, ...], is_recursive: bool, planets: Tuple[str], output: str, build_phases: bool): is_s3 = _is_s3(output) is_tar = _is_tar(output) tarf = None s3_bucket_key = None if is_tar: output_dir = opjoin(PROJECT_DIR, '.output') else: output_dir = output if is_s3: if not is_tar: logging.error('You must point tar archive in AWS S3 bucket.') exit(-1) output = opjoin(PROJECT_DIR, os.path.basename(output)) s3_bucket_key = create_aws_s3_key(CONFIG['s3']['access_key'], CONFIG['s3']['secret_key'], CONFIG['s3']['bucket'], output) output_images = opjoin(output_dir, CONFIG['output']['images']) output_res_path = opjoin(output_dir, CONFIG['output']['angle']) output_gnu_path = opjoin(output_dir, CONFIG['output']['gnuplot']) pathbuilder = FilepathBuilder(aei_paths, is_recursive) planet_aei_paths = [pathbuilder.build('%s.aei' % x) for x in planets] resmaker = ResfileMaker(planets, planet_aei_paths) if not os.path.exists(output_images): os.makedirs(output_images) if not os.path.exists(output_gnu_path): os.makedirs(output_gnu_path) phase_builder = PhaseBuilder(phase_storage) orbital_element_sets = None if build_phases: orbital_element_sets = build_bigbody_elements(planet_aei_paths) phase_loader = PhaseLoader(phase_storage) aei_getter = AEIDataGetter(pathbuilder) if is_tar: tarf = tarfile.open(output, 'w') for resonance, aei_data in get_aggregated_resonances(start, stop, for_librations, planets, aei_getter, integers): if build_phases: orbital_elem_set_facade = ResonanceOrbitalElementSetFacade( orbital_element_sets, resonance) phase_builder.build(aei_data, resonance.id, orbital_elem_set_facade) phases = phase_loader.load(resonance.id) apocentric_phases = [cutoff_angle(x + pi) for x in phases] res_filepath = opjoin(output_res_path, 'A%i_%i.res' % (resonance.asteroid_number, resonance.id)) gnu_filepath = opjoin(output_gnu_path, 'A%i_%i.gnu' % (resonance.asteroid_number, resonance.id)) title = 'Asteroid %i %s %s' % (resonance.asteroid_number, str(resonance), ' '.join(planets)) resmaker.make(phases, aei_data, res_filepath) png_path = opjoin(PROJECT_DIR, output_images, 'A%i-res%i%s.png' % ( resonance.asteroid_number, resonance.id, '')) make_plot(res_filepath, gnu_filepath, png_path, title) if is_tar: tarf.add(png_path, arcname=os.path.basename(png_path)) resmaker.make(apocentric_phases, aei_data, res_filepath) png_path = opjoin(PROJECT_DIR, output_images, 'A%i-res%i%s.png' % ( resonance.asteroid_number, resonance.id, '-apocentric')) make_plot(res_filepath, gnu_filepath, png_path, title) if is_tar: tarf.add(png_path, arcname=os.path.basename(png_path)) if is_tar: tarf.close() shutil.rmtree(output_dir, True) if is_s3: s3_bucket_key.set_contents_from_filename(output)