def test_position_from_radec(): # Only a couple of minimal tests, since the routine is deprecated. p = api.position_from_radec(0, 0) assert length_of(p.position.au - [1, 0, 0]) < 1e-16 p = api.position_from_radec(6, 0) assert length_of(p.position.au - [0, 1, 0]) < 1e-16
def test_constellations(): ts = load.timescale(builtin=True) t = ts.utc(2000) lookup = load_constellation_map() assert lookup(position_from_radec(0, 0)) == 'Psc' assert lookup(position_from_radec(360, 90)) == 'UMi' # (4.65, 0) used to be in Orion, but, precession assert lookup(position_from_radec(4.65, 0)) == 'Eri' assert lookup(position_from_radec(4.75, 0.3)) == 'Ori' # Test vectorization. assert (lookup(position_from_radec([4.65, 4.75], [0, 0.3])) == ['Eri', 'Ori']).all()
def test_position_from_radec(): p = api.position_from_radec(0, 0) assert length_of(p.position.au - [1, 0, 0]) < 1e-16 p = api.position_from_radec(6, 0) assert length_of(p.position.au - [0, 1, 0]) < 1e-16 p = api.position_from_radec(12, 90, 2) assert length_of(p.position.au - [0, 0, 2]) < 2e-16 ts = api.load.timescale(builtin=True) epoch = ts.tt_jd(api.B1950) p = api.position_from_radec(0, 0, epoch=epoch) assert length_of(p.position.au - [1, 0, 0]) > 1e-16 ra, dec, distance = p.radec(epoch=epoch) assert abs(ra.hours) < 1e-12 assert abs(dec.degrees) < 1e-12 assert abs(distance.au - 1) < 1e-16
def fix_dso_constellation(): constell_dict = {} constellation_at = load_constellation_map() for co in Constellation.query.all(): constell_dict[co.iau_code.upper()] = co.id try: for dso in DeepskyObject.query.all(): const_code = constellation_at( position_from_radec(dso.ra / np.pi * 12.0, dso.dec / np.pi * 180.0)) dso.constellation_id = constell_dict[ const_code.upper()] if const_code else None db.session.add(dso) db.session.commit() except KeyError as err: print('\nKey error: {}'.format(err)) db.session.rollback() except IntegrityError as err: print('\nIntegrity error {}'.format(err)) db.session.rollback() print('')
def import_hnsky(hnsky_dso_file): from sqlalchemy.exc import IntegrityError constellation_at = load_constellation_map() constell_dict = {} for co in Constellation.query.all(): constell_dict[co.iau_code.upper()] = co.id hnd_file = open(hnsky_dso_file, 'r', encoding='ISO-8859-1') lines = hnd_file.readlines()[2:] hnd_file.close() existing_dsos = {} for dso in DeepskyObject.query.filter_by().all(): existing_dsos[dso.name] = dso dso_set = set() master_dso_map = {} pref_cats = [None] * 1000 cat_codes = {} catalogs = Catalogue.query.filter(Catalogue.id<1000).order_by(Catalogue.id) for cat in catalogs: pref_cats[cat.id-1] = [] cat_codes[cat.id-1] = cat.code dso_count = 0 other_dsos = [] try: for line in lines: if len(line) == 0: continue items = line.split(',') ra = 2.0 * np.pi * float(items[0])/864000.0 dec = np.pi * float(items[1])/(324000.0 * 2.0) const_code = constellation_at(position_from_radec(ra / np.pi * 12.0, dec / np.pi * 180.0)) constellation_id = constell_dict[const_code.upper()] if const_code else None str_mag = items[2].strip() mag = float(str_mag)/10.0 if str_mag else 100.0 brightness = None if len(items) > 5: str_brightness = items[5].strip() try: brightness = float(str_brightness)/10.0 if str_brightness else 100.0 except (ValueError, TypeError): pass obj_types = items[4].split('/') obj_type = obj_types[0].strip() indx = obj_types[0].find('[') if indx>0: obj_type = obj_type[:indx] indx = obj_types[0].find(';') if indx>0: obj_type = obj_type[:indx] indx1 = items[4].find('[') indx2 = items[4].find(']') obj_subtype = items[4][indx1+1:indx2] if indx1<indx2 else None # remove uncertainty flag if obj_type.endswith('?') and len(obj_type) > 1: obj_type = obj_type[:-1] obj_type = dso_type_map.get(obj_type, None) if obj_type is None: print('No type {}'.format(obj_types[0].strip())) print(line) continue rlong = None str_length = items[6].strip() if len(items) > 6 else None if str_length: try: rlong = float(str_length) * 6 except (ValueError, TypeError): pass rshort = None str_width = items[7].strip() if len(items) > 7 else None if str_width: try: rshort = float(str_width) * 6 except (ValueError, TypeError): pass position_angle = None str_pos_angle = items[8].strip() if len(items) > 8 else None if str_pos_angle: try: position_angle = float(str_pos_angle) except (ValueError, TypeError): pass names = items[3].split('/') master_dso = None child_dsos = [] master_cat_prio = None for name1 in names: for name in name1.split(';'): name = name.strip() if name.startswith('PN_'): name = name[3:] if name.startswith('A66_'): name = 'Abell' + name[4:] if name.startswith('PK_'): name = 'PK' + _denormalize_pk_name(name[3:]) if name.startswith('Arp_'): name = 'Arp' + name[4:] if name.startswith('NGC') or name.startswith('IC') or name.startswith('UGC'): if name.endswith('A'): name = name[:-1] elif name.endswith('-1') or name.endswith('_1'): name = name[:-2] if name in dso_set: continue dso_set.add(name) cat = get_catalog_from_dsoname(name) if cat: dso = existing_dsos.get(name, None) if dso is None: dso = DeepskyObject() dso.name = name dso.type = obj_type dso.subtype = obj_subtype dso.ra = ra dso.dec = dec dso.constellation_id = constellation_id dso.catalogue_id = cat.id dso.major_axis = rlong dso.minor_axis = rshort dso.position_angle = position_angle dso.mag = mag dso.surface_bright = brightness dso.common_name = None cat_prio = cat_priorities.get(cat.code, 1000) if (master_cat_prio is not None) and cat_prio < master_cat_prio: child_dsos.append(master_dso) master_dso = dso master_cat_prio = cat_prio elif master_dso: child_dsos.append(dso) else: master_dso = dso master_cat_prio = cat_prio if cat.id < 1000: pref_cats[cat.id-1].append(dso) else: other_dsos.append(dso) dso_count += 1 else: print('Not found {}'.format(name)) for child_dso in child_dsos: master_dso_map[child_dso.name] = master_dso # Sort dso in catalog list according object number in catalog for i in range(1000): dso_list = pref_cats[i] if not dso_list or i not in cat_codes: continue ccl = len(cat_codes[i]) if cat_codes[i] in { 'Sh2' }: # skip '-' character in case of Sh2 object ID ccl += 1 pos = int(log(len(dso_list), 10)) + 1 # add 0 before dso ID (in catalog) dso_list.sort(key=lambda x: (('0' * (pos - (len(x.name) - ccl))) + x.name[ccl:]) if len(x.name) - ccl < pos else x.name[ccl:]) line_cnt = 1 # Import master DSO from preferred catalogs for i in range(1000): dso_list = pref_cats[i] if dso_list and i in cat_codes: line_cnt = _save_dso_list(dso_count, line_cnt, dso_list, master_dso_map, True) # Import child DSO from preferred catalogs line_cnt = _save_dso_list(dso_count, line_cnt, other_dsos, master_dso_map, True) for i in range(1000): dso_list = pref_cats[i] if dso_list and i in cat_codes: line_cnt = _save_dso_list(dso_count, line_cnt, dso_list, master_dso_map, False) line_cnt = _save_dso_list(dso_count, line_cnt, other_dsos, master_dso_map, False) db.session.commit() except KeyError as err: print('\nKey error: {}'.format(err)) db.session.rollback() except IntegrityError as err: print('\nIntegrity error {}'.format(err)) db.session.rollback() print('') # finish on new line
def import_carbon_stars(carbon_stars_data_file): sf = open(carbon_stars_data_file, 'r') lines = sf.readlines() sf.close() constellation_at = load_constellation_map() constell_dict = {} for co in Constellation.query.all(): constell_dict[co.iau_code] = co.id try: editor_user = User.get_editor_user() star_list = StarList.query.filter_by(name='carbon-stars').first() if star_list: star_list.name = 'carbon-stars' star_list.long_name = 'Carbon Stars' star_list.update_by = editor_user.id star_list.create_date = datetime.now() star_list.star_list_items[:] = [] star_list.star_list_descriptions[:] = [] else: star_list = StarList(name='carbon-stars', long_name='Carbon Stars', create_by=editor_user.id, update_by=editor_user.id, create_date=datetime.now(), update_date=datetime.now()) db.session.add(star_list) db.session.flush() base_name = os.path.basename(carbon_stars_data_file) descr_list = _load_descriptions( os.path.dirname(carbon_stars_data_file), base_name[:-len('.txt')], star_list, editor_user) for descr in descr_list: db.session.add(descr) db.session.flush() row_count = len(lines) row_id = 0 for line in lines: row_id += 1 progress(row_id, row_count, 'Importing carbon stars catalog') str_hd = line[0:7].strip() hd = int(str_hd) if str_hd else None var_id = line[7:17].strip() ra = int(line[17:23].strip()) * np.pi / 12.0 + int( line[23:26].strip()) * np.pi / (12.0 * 60.0) + float( line[26:31].strip()) * np.pi / (12 * 60.0 * 60) dec = int(line[34:35] + '1') * ( int(line[35:37].strip()) * np.pi / 180.0 + int(line[38:40].strip()) * np.pi / (180.0 * 60.0) + int(line[41:43].strip()) * np.pi / (180.0 * 60.0 * 60.0)) mag = float(line[43:50].strip()) sp_type = line[50:69].strip() max_min = line[69:82].strip() min = None max = None if max_min: sep_index = max_min.find('-') if sep_index >= 0: min = float(max_min[0:sep_index]) max = float(max_min[sep_index + 1:]) star = None if hd is not None: star = Star.query.filter_by(hd=hd).first() if not star and var_id: star = Star.query.filter_by(var_id=var_id).first() if not star: star = Star() star.src_catalogue = 'carbon_stars' constellation = constellation_at( position_from_radec(ra / np.pi * 12.0, dec / np.pi * 180.0)) star.hd = hd star.var_id = var_id star.ra = ra star.dec = dec star.mag = mag star.mag_max = max star.mag_min = min star.constellation_id = constell_dict[ constellation] if constellation else None if sp_type: star.sp_type = sp_type db.session.add(star) db.session.flush() item = StarListItem.query.filter_by(star_list_id=star_list.id, star_id=star.id).first() if not item: item = StarListItem( star_list_id=star_list.id, star_id=star.id, item_id=row_id, create_by=editor_user.id, create_date=datetime.now(), ) db.session.add(item) db.session.commit() except KeyError as err: print('\nKey error: {}'.format(err)) db.session.rollback() except IntegrityError as err: print('\nIntegrity error {}'.format(err)) db.session.rollback() print('') # finish on new line
def import_vic(vic_data_file): """Import data from VIC catalog.""" from sqlalchemy.exc import IntegrityError constellation_at = load_constellation_map() constell_dict = {} for co in Constellation.query.all(): constell_dict[co.iau_code] = co.id row_count = sum(1 for line in open(vic_data_file)) - 1 with open(vic_data_file) as csvfile: reader = csv.DictReader(csvfile, delimiter=';') catalogue_id = Catalogue.get_catalogue_id_by_cat_code('VIC') row_id = 0 try: for row in reader: row_id += 1 progress(row_id, row_count, 'Importing VIC catalogue') dso_name = 'VIC' + str(row_id) c = DeepskyObject.query.filter_by(name = dso_name).first() if c is None: c = DeepskyObject() ra_ang = Angle(hours=tuple(map(float, row['RA'].split(',')))) if len(row['RA']) > 0 else None dec_ang = Angle(degrees=tuple(map(float, row['Dec'].split(',')))) if len(row['Dec']) > 0 else None constellation = constellation_at(position_from_radec(ra_ang.radians / np.pi * 12.0, dec_ang.radians / np.pi * 180.0)) c.name = dso_name c.type = 'AST' c.ra = ra_ang.radians if ra_ang else None c.dec = dec_ang.radians if dec_ang else None c.constellation_id = constell_dict[constellation] if constellation else None c.catalogue_id = catalogue_id c.major_axis = vic2int(row['length']) / 10 * 60.0 c.minor_axis = vic2int(row['width']) / 10 * 60.0 c.position_angle = vic2int(row['orient']) / 10 c.mag = vic2int(row['mag']) / 10 c.surface_bright = vic2int(row['brightness']) / 10 c.hubble_type = None c.c_star_u_mag = None c.c_star_b_mag = None c.c_star_v_mag = None c.identifiers = None c.common_name = row['name'].strip() c.descr = None db.session.add(c) db.session.commit() except KeyError as err: print('\nKey error: {}'.format(err)) db.session.rollback() except IntegrityError as err: print('\nIntegrity error {}'.format(err)) db.session.rollback() print('') # finish on new line
"name": almanac.MOON_PHASES[phase_identifier], "datetime": phase_time.utc_datetime() }) # Rising and setting next_day = observation_datetime + timedelta(days=1) next_day_time = timescale.utc(next_day) rs_almanac = almanac.risings_and_settings(eph, moon, observer_topos) rs_times, rs_moments = almanac.find_discrete(observation_time, next_day_time, rs_almanac) # Constellation constellation_at = load_constellation_map() moon_position = position_from_radec(ra.hours, dec.degrees) current_constellation = constellation_at(moon_position) # JSON build data = detailled_coordinates(ra, dec, alt, az, dist) for rs_time, rs_moment in zip(rs_times, rs_moments): rs_apparent = observer_location.at(rs_time).observe(moon).apparent() rs_ra, rs_dec, rs_dist = rs_apparent.radec() rs_alt, rs_az, _ = rs_apparent.altaz() rs_coordinates = detailled_coordinates(rs_ra, rs_dec, rs_alt, rs_az, rs_dist) moment_name = "rises" if rs_moment else "sets" data[moment_name] = {**rs_coordinates, "datetime": rs_time.utc_datetime()}