Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)