Esempio n. 1
0
def _make_res(by_resonance: ResonanceMixin, filepaths: List[str],
              planets: tuple, integers: List[str]):
    asteroid_name = by_resonance.small_body.name
    resonance_id = by_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, by_resonance)

    aei_data = getter.get_aei_matrix(asteroid_name)
    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, '%s_%s_%s.res' % (asteroid_name, '_'.join(planets),
                                  '_'.join([str(x) for x in integers]))
    ))
    phase_cleaner.delete(resonance_id)
Esempio n. 2
0
def _test_phase_correctness(aei_data, facade: ResonanceOrbitalElementSetFacade,
                            phase_values):
    i = 0
    for time, resonant_phase in facade.get_resonant_phases(aei_data):
        assert time == aei_data['Time (years)'][i]
        assert np.isclose(resonant_phase, phase_values[i])
        i += 1

    assert i > 0
Esempio n. 3
0
def _save_resonances_tofile(by_aei_data: List[str], in_file: str,
                            orbital_elem_set: ResonanceOrbitalElementSetFacade) \
        -> List[Dict[str, float]]:
    serialized_phases = []
    with open(in_file, 'w') as f:
        for year, value in orbital_elem_set.get_resonant_phases(by_aei_data):
            serialized_phase = dict(year=year, value=value)
            f.write(str(serialized_phase))
            f.write('\n')
            serialized_phases.append(serialized_phase)
    return serialized_phases
Esempio n. 4
0
def _build_redis_phases(by_aei_data: List[str], in_key: str,
                        orbital_elem_set: ResonanceOrbitalElementSetFacade) \
        -> List[Dict[str, float]]:
    pipe = REDIS.pipeline()
    serialized_phases = []
    for year, value in orbital_elem_set.get_resonant_phases(by_aei_data):
        serialized_phase = dict(year=year, value=value)
        pipe = pipe.rpush(in_key, '%s' % serialized_phase)
        serialized_phases.append(serialized_phase)
    pipe.execute()
    return serialized_phases
Esempio n. 5
0
def plot(asteroids: tuple, 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)
    s3_bucket_key = None

    if is_tar:
        output_dir = opjoin(PROJECT_DIR, '.output')
    else:
        output_dir = output
    out_paths = OutPaths(output_dir)

    s3_bucket_key = None
    if is_s3:
        if not is_tar:
            logging.error('You must point tar archive in AWS S3 bucket.')
            exit(-1)
        s3_bucket_key = _get_folder_on_s3(output)

    pathbuilder = FilepathBuilder(aei_paths, is_recursive)
    planet_aei_paths = [pathbuilder.build('%s.aei' % x) for x in planets]
    resmaker = ResfileMaker(planets, planet_aei_paths)

    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)

    plot_saver = PlotSaver(out_paths, output if is_tar else None,
                           s3_bucket_key)
    builder = _PlotBuilder(phase_loader, plot_saver, resmaker, planets)

    for resonance in get_resonances_by_asteroids(asteroids, for_librations,
                                                 integers, planets):
        aei_data = aei_getter.get_aei_matrix(resonance.small_body.name)
        if build_phases:
            orbital_elem_set_facade = ResonanceOrbitalElementSetFacade(
                orbital_element_sets, resonance)
            phase_builder.build(aei_data, resonance.id,
                                orbital_elem_set_facade)

        builder.build(resonance, aei_data)

    if is_tar:
        shutil.rmtree(out_paths.output_dir, True)
Esempio n. 6
0
    def _find(self, resonances_data: Iterable[ResonanceAeiData], length: int,
              orbital_element_sets: List[OrbitalElementSetCollection]):
        """
        :param resonances_data:
        :param length: used only for progress bar.
        :param orbital_element_sets:
        """
        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(length, 'Find librations')
        asteroid_name = None
        for resonance, aei_data in resonances_data:
            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

            if aei_data.empty:
                broken_asteroid_mediator.save('Has no data in aei file.')

            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
            except AsteroidElementCountException as e:
                broken_asteroid_mediator.save(str(e))
                continue

            classifier.classify(orbital_elem_set_facade, serialized_phases)

        session.flush()
        session.commit()
Esempio n. 7
0
 def build(self, planet_elems: List[OrbitalElementSetCollection])\
         -> ResonanceOrbitalElementSetFacade:
     assert self._resonances is not None
     return ResonanceOrbitalElementSetFacade(planet_elems,
                                             self._resonances[0])
Esempio n. 8
0
def _test_get_resonant_phases_raise(aei_data,
                                    facade: ResonanceOrbitalElementSetFacade):
    with pytest.raises(AsteroidElementCountException):
        for item in facade.get_resonant_phases(aei_data):
            pass