def plot_comparison_stars( chartsdir: str, stars: List[StarDescription], stardict: StarDict, jdfilter: List[float], ): # for every selected star make one chart for star in tqdm(stars, desc="Plotting comparison stars", unit="star"): compstars: CompStarData = star.get_metadata("COMPSTARS") compstar_ids = compstars.compstar_ids + [ compstars.extra_id, star.local_id ] labels = compstars.compstar_ids + ["K"] dfs = reading.read_lightcurve_ids(compstar_ids, stardict) # compstars without the 'K' star or check star star.result["compA"] = helper_plot_stars( star, chartsdir, utils.get_star_or_catalog_name(star, suffix="_compstarsA"), dfs[:-1], labels, jdfilter, show_error=True, ) # compstars + 'K' star star.result["compB"] = helper_plot_stars( star, chartsdir, utils.get_star_or_catalog_name(star, suffix="_compstarsB"), dfs, labels + ["V"], jdfilter, show_error=False, )
def load_toml(star, resultdir): starui: utils.StarUI = utils.get_star_or_catalog_name(star) logging.info(f"starui: {starui}") txt_path = ( Path(resultdir) / "phase_selected" / "txt" / f"{starui.filename_no_suff_no_ext}.txt" ) if not os.path.isfile(txt_path): txt_path = ( Path(resultdir) / "phase_selected" / "txt" / f"{star.local_id:05}.txt" ) if not os.path.isfile(txt_path): txt_path = ( Path(resultdir) / "phase_candidates" / "txt" / f"{star.local_id:05}.txt" ) logging.debug("txtpath", txt_path) try: parsed_toml = toml.load(txt_path) except FileNotFoundError: logging.error(f"Could not load txt file with phase information from {txt_path}") return parsed_toml
def plot_merr_vs_jd(chartsdir: str, stars: List[StarDescription], jdfilter): dfs = reading.read_lightcurve_sds(stars) for star, df in tqdm( zip(stars, dfs), desc="Plotting magnitude error vs jd", unit="star", total=len(stars), ): df["floatJD"] = df["JD"].astype(np.float) df = utils.jd_filter_df(df, jdfilter) starui: utils.StarUI = utils.get_star_or_catalog_name(star) fig, ax = get_fig_and_ax() ax.plot(df["floatJD"], df["err"], "*r", markersize=2) ax.set_title("Magnitude error vs JD") plt.xlabel("JD (day)") plt.ylabel("Mag error (mag)") plt.xticks(rotation=25) plt.minorticks_on() plt.autoscale() save_location = Path(chartsdir, f"{starui.filename_no_ext}_merr_vs_jd" + ".png") fig.savefig(save_location) plt.close(fig) star.result["merr_vs_jd"] = save_location
def read_vast_lightcurves( star: StarDescription, compstarproxy, star_result_dict, do_light, do_light_raw, do_phase, do_aavso, aavso_limit, basedir: str, chartsdir: Path, phasedir: Path, aavsodir: Path, jdfilter: List[float] = None, jd_excl_stop: float = None, ): start = timer() if star.local_id not in star_result_dict: star_result_dict[star.local_id] = {} temp_dict = star_result_dict[star.local_id] if star.path == "": logging.debug(f"Path for {star.local_id} is empty") return if not do_light and not do_phase: logging.debug("Nothing to do, no charts or phase needed") logging.debug( f"Reading lightcurves for star {star.local_id} at path {star.path} for {star}..." ) # comp_mags = [x.vmag for x in comparison_stars] try: df = reading.read_lightcurve_vast(star.path) df["floatJD"] = df["JD"].astype(np.float) df = utils.jd_filter_df(df, jdfilter) if df is None or len(df) == 0: logging.info(f"No lightcurve found for {star.path}") return comp_stars = compstarproxy.value filtered_compstars, check_star = do_compstars.filter_comparison_stars( star, comp_stars) comp_stars = None df["realV"], df[ "realErr"] = do_compstars.calculate_ensemble_photometry( df, filtered_compstars, do_compstars.weighted_value_ensemble_method) df = df.dropna(subset=[ "realV", "realErr" ]) # drop rows where the ensemble photometry failed do_calibration.add_catalog_data_to_sd( star, df["realV"].mean(), df["realErr"].mean(), None, "PHOTOMETRY", star.coords, ) starui: utils.StarUI = utils.get_star_or_catalog_name(star, suffix="") period, epoch = determine_period_and_epoch(df, star) # override user calculated epoch with user-supplied epoch if (star.has_metadata("SITE") and star.get_metadata("SITE").epoch is not None): epoch = star.get_metadata("SITE").epoch # filter depending on phase diagram df, points_removed = phase_dependent_outlier_removal(df, period) temp_dict["compstars"] = write_compstars(star, starui.filename_no_ext, phasedir, filtered_compstars, check_star) ymin, ymax, epoch_min, epoch_max, t_start, t_end = *calculate_min_max_epochs( df["floatJD"], df["realV"]), logging.debug( f"Debug for the min/max percentiles: {ymin}, {ymax}, {epoch_min}, {epoch_max}, {t_start}, {t_end}" ) write_toml(starui.filename_no_ext, phasedir, period, epoch, star, points_removed, ymin, ymax, t_start, t_end) if do_phase and "phase" not in star.result: temp_dict["phase"] = plot_phase_diagram(star, df.copy(), phasedir, period=period, epoch=epoch, suffix="") # anova_period, epoch = determine_period_and_epoch(df, star, method=anova_period_calculate) # logging.info(f"anova period is {anova_period}") # temp_dict['phase'] = plot_phase_diagram(star, df.copy(), phasedir, period=anova_period, # epoch=epoch, suffix="b") if do_light and "light" not in star.result: temp_dict["lightpa"] = plot_lightcurve_pa(star, df.copy(), chartsdir, period) temp_dict["lightcont"] = plot_lightcurve_continuous( star, df.copy(), chartsdir) temp_dict['light'] = plot_lightcurve(star, df.copy(), chartsdir) if do_aavso and "aavso" not in star.result: settings = toml.load("settings.txt") temp_dict["aavso"] = do_aavso_report.report( star, df.copy(), filtered_compstars, check_star, target_dir=aavsodir, sitelat=settings["sitelat"], sitelong=settings["sitelong"], sitealt=settings["sitealt"], camera_filter="V", observer=settings["observer"], chunk_size=aavso_limit, ) filtered_compstars = None # logging.error(f"contents of temp_dict is: {temp_dict}") star_result_dict[star.local_id] = temp_dict except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) logging.error(message) import traceback print(traceback.print_exc()) logging.error( f"Exception during read_lightcurve for {star.path}, size JD: {len(df['floatJD'])}," f"size V: {len(df['realV'])}") end = timer() logging.debug(f"Full lightcurve/phase: {end - start}")
def plot_phase_diagram( star: StarDescription, curve: DataFrame, fullphasedir, suffix="", period: Period = None, epoch: str = None, write_plot=True, filter_func=None, ): assert period is not None try: logging.debug( f"Starting plot phase diagram with {star} and {fullphasedir}") starui: utils.StarUI = utils.get_star_or_catalog_name( star, suffix=f"_phase{suffix}") names = utils.get_star_names(star) catalog_title = (f"{names[0]}" if names is not None and names[0] is not star.local_id else "") save_location = Path(fullphasedir, starui.filename_no_ext + ".png") upsilon_match = star.get_metadata("UPSILON") upsilon_text = (upsilon_match.get_upsilon_string() if upsilon_match is not None else "") if curve is None: logging.info("Curve of star {} is None".format(star.local_id)) return t_np = curve["floatJD"] y_np = curve["realV"].to_numpy() dy_np = curve["realErr"].to_numpy() # Epoch centering epoch_location = len(t_np) + np.argmin(abs(t_np - epoch)) if ( epoch is not None and epoch >= np.min(t_np) and epoch <= np.max(t_np)) else None epoch_float = float(epoch) if epoch else None t_np_zeroed = epoch_to_zero_time(epoch_float, t_np) # calculate phase where epoch at t=0 will corresponds to phase 0 phased_t = np.mod(t_np_zeroed / period.period, 1) phased_lc = y_np[:] save_to_disk = False if (save_to_disk): np.save(Path("./", "epoch_t_" + starui.filename_no_ext + ".txt"), t_np) np.save(Path("./", "epoch_y_" + starui.filename_no_ext + ".txt"), y_np) np.save(Path("./", "epoch_tz_" + starui.filename_no_ext + ".txt"), t_np_zeroed) np.save(Path("./", "epoch_pt_" + starui.filename_no_ext + ".txt"), phased_t) np.save(Path("./", "epoch_py_" + starui.filename_no_ext + ".txt"), phased_lc) if filter_func is not None: phased_t, phased_lc = filter_func(phased_t, phased_lc) phased_t_final = np.append(np.subtract(phased_t, 1), phased_t) phased_lc_final = np.append(phased_lc, phased_lc) # error values are clipped to +0.5 and -0.5 phased_err = np.clip(np.append(dy_np, dy_np), -0.5, 0.5) plt_result = _plot_phase_diagram( phased_t_final, phased_lc_final, phased_err, epoch_location, write_plot, save_location, star, catalog_title, period, upsilon_text, ) if not write_plot: return plt_result, t_np, y_np return save_location except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) import traceback print(traceback.print_exc()) logging.error(message) logging.error(f"Error during plot phase: {star.local_id}")
def _plot_lightcurve(star: StarDescription, curve: DataFrame, chartsdir, suffix=f"_light", jd_adjusting_func=None, xlabel='JD', plot_width=20, plot_height=16, plot_dpi=150, markersize=6, errorbars=True, rotate=False, write_plot=True): try: star_id = star.local_id logging.debug(f"Plotting lightcurve for {star_id}") starui: utils.StarUI = utils.get_star_or_catalog_name(star, suffix=suffix) save_location = Path(chartsdir, starui.filename_no_ext + ".png") start = timer() upsilon_match = (star.get_metadata("UPSILON") if star.has_metadata("UPSILON") else None) upsilon_text = (upsilon_match.get_upsilon_string() if upsilon_match is not None else "") end = timer() logging.debug(f"timing upsilon stuff {end - start}") names = utils.get_star_names(star) catalog_title = (f"{names[0]}" if names is not None and names[0] is not star.local_id else "") plot_title = f"{catalog_title}\nStar {star.local_id}" if curve is None: logging.info(f"Curve is None for star {star_id}") return # e.g for phase locked curve if jd_adjusting_func is None: curve["realJD"] = curve["floatJD"] else: curve['realJD'] = jd_adjusting_func(curve['floatJD']) fig = plt.figure(figsize=(plot_width, plot_height), dpi=plot_dpi, facecolor='w', edgecolor='k') if errorbars: plt.errorbar( curve["realJD"], curve["realV"], yerr=curve["realErr"], linestyle="none", marker="o", ecolor="gray", elinewidth=1, ms=markersize, ) else: plt.plot( curve["realJD"], curve["realV"], linestyle="none", linewidth="1", marker="o", ms=markersize, ) plt.xlabel(xlabel, labelpad=TITLE_PAD) curve_max = curve["realV"].max() curve_min = curve["realV"].min() jd_min = curve["realJD"].min() jd_max = curve["realJD"].max() plot_max = curve_max + 0.1 plot_min = curve_min - 0.1 plt.ylim(plot_min, plot_max) plt.xlim(jd_min, jd_max) # plt.xticks(range(math.floor(jd_min), math.ceil(jd_max))) # nop = lambda *a, **k: None # print("limits are here:", star_id, "min:", curve['realJD'].min(), "max", curve['realJD'].max(), # "first 10", curve['realJD'][:10], "first 10 orig:", curve['floatJD'][:10], "len", len(curve['realJD']), # "describe", curve['realJD'].describe()) \ # if curve['realJD'].max() == 0 else nop() plt.gca().invert_yaxis() plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True)) plt.title(plot_title, pad=TITLE_PAD) plt.ticklabel_format(useOffset=False, style="plain") if rotate: plt.xticks(rotation=25) plt.tight_layout() if write_plot: start = timer() fig.savefig(save_location) end = timer() logging.debug(f"timing saving fig {end - start}") plt.close(fig) plt.clf() return save_location return plt, curve["realJD"], curve["realV"] except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) import traceback print(traceback.print_exc()) logging.error(message) logging.error(f"Error during plot lightcurve: {star.local_id}")
def write_selected_files(resultdir: str, vastdir: str, selected_stars: List[StarDescription]): radec_catalog = f"{resultdir}selected_radec.txt" localid_catalog = f"{resultdir}selected_localid.txt" aavso_vsx_catalog = f"{resultdir}aavso_vsx.txt" logging.info( f"Writing {radec_catalog} and {localid_catalog} and {aavso_vsx_catalog} with {len(selected_stars)} stars..." ) sorted_stars = utils.sort_selected(selected_stars) vsx_stars_len = len(utils.get_stars_with_metadata(selected_stars, "VSX")) no_vsx_len = len( utils.get_stars_with_metadata(selected_stars, "SELECTEDTAG", exclude=["VSX"])) def format_float_arg(atoml, arg: str, precision): if arg is None or arg not in atoml: return "" if not isinstance(atoml[arg], float): return atoml[arg] return f"{atoml[arg]:.{precision}f}" def format_float_5(atoml, arg: str): return format_float_arg(atoml, arg, 5) def format_float_2(atoml, arg: str): return format_float_arg(atoml, arg, 2) def format_string(arg: str, atoml): if arg in atoml: return atoml[arg] return "" with open(radec_catalog, "w") as out_radec_catalog, open( localid_catalog, "w") as out_localid_catalog, open(aavso_vsx_catalog, "w") as out_aavso_vsx_catalog: common_prefix = ( f"# resultdir: {resultdir}, vastdir: {vastdir}, vsx stars: {vsx_stars_len}, " f"other stars: {no_vsx_len}\n") common_postfix = (f"minmax,min,max,var_type,period,period_err,epoch\n") out_radec_catalog.write( f"{common_prefix}# our_name,ra,dec,ucac4_name,ucac4_force,{common_postfix}" ) out_localid_catalog.write( f"{common_prefix}# our_name,local_id,ucac4_name,ucac4_force,{common_postfix}" ) out_aavso_vsx_catalog.write( f"{common_prefix}# our_name,ucac4_name,ucac4_ra,ucac4_dec,{common_postfix}" ) for star in sorted_stars: metadata: SiteData = star.get_metadata("SITE") ucac4: CatalogData = utils.get_ucac4_of_sd(star) ucac4_name = "" if not ucac4 else f"{ucac4.name}" ucac4_coords = ( "," if not ucac4 else f"{ucac4.coords.ra.deg:.7f},{ucac4.coords.dec.deg:.7f}") starui: utils.StarUI = utils.get_star_or_catalog_name(star) txt_path = Path( Path(star.result["phase"]).parent, "txt", starui.filename_no_ext + ".txt") try: parsed_toml = toml.load(txt_path) postfix = f"{format_string('minmax', parsed_toml)},{format_float_2(parsed_toml, 'min')},{format_float_2(parsed_toml, 'max')},{metadata.var_type},{format_float_5(parsed_toml, 'period')},{format_float_5(parsed_toml, 'period_err')},{format_string('epoch', parsed_toml)},{metadata.comments}" out_radec_catalog.write( f"{metadata.our_name},{star.coords.ra.deg:.7f},{star.coords.dec.deg:.7f},{ucac4_name},False,{postfix}\n" ) out_localid_catalog.write( f"{metadata.our_name},{star.local_id},{ucac4_name},False,{postfix}\n" ) out_aavso_vsx_catalog.write( f"{metadata.our_name},{ucac4_name},{ucac4_coords},{postfix}\n" ) except FileNotFoundError: logging.error( f"While writing selected files, Could not find {txt_path}")
def run_standard_field_charts( star_descriptions: StarDescriptionList, wcs, fieldchartsdirs, reference_fits_frame, comp_stars: ComparisonStars, ): # setting the font size for titles/axes plt.rcParams.update({"axes.titlesize": "large", "axes.labelsize": "large"}) fits_data, _, _ = reading.get_fits_data(reference_fits_frame) fits_data_blank, _, _ = reading.get_fits_data(reference_fits_frame, blank_data=True) SHOW_UPSILON = False # if SHOW_UPSILON: # candidates = do_calibration.get_candidates(0.5) # TODO hand labeled stars # hand_candidates_descr = do_calibration.get_star_descriptions(init.wwcra_certain_candidates) all_stars_descr = star_descriptions # if SHOW_UPSILON: # big_green = set_custom_label(comparison_ star_descr, 'comp') # small_red = set_custom_label(apass_star_descr, [o.vmag for o in apass_star_descr]) # big_green = set_custom_label(vsx_star_descr, [o.match['catalog_dict']['name'] for o in vsx_star_descr]) # small_red = set_custom_label(hand_candidates_descr, [o.local_id for o in hand_candidates_descr]) # big_green = set_aavso_id_label(vsx_star_descr) # small_red = set_local_id_label(hand_candidates_descr) # all stars get a blank label all_stars_no_label = set_custom_label(all_stars_descr, "") # field chart with all detections logging.info("Plotting field chart with all detected stars...") fig = plot_it( [all_stars_no_label], [4.0], [False], fits_data, wcs, "All detected stars", PADDING, annotate=False, ) save( fig, fieldchartsdirs + "all_detections_{}_stars".format(len(all_stars_no_label)) ) # vsx stars get their aavso id label vsx_descr = utils.get_stars_with_metadata(star_descriptions, "VSX") vsx_labeled = set_aavso_id_label(vsx_descr) # field chart with all vsx stars logging.info("Plotting field chart with all VSX variable stars...") fig = plot_it( [vsx_labeled], [10.0], [False], fits_data, wcs, "All VSX stars", PADDING ) save(fig, fieldchartsdirs + "vsx_stars_{}".format(len(vsx_labeled))) # field chart with all vsx stars without the background logging.info( "Plotting field chart with all VSX variable stars without reference field..." ) fig = plot_it( [vsx_labeled], [10.0], [False], fits_data_blank, wcs, "VSX without background", PADDING, ) save(fig, fieldchartsdirs + "vsx_stars_no_ref_{}".format(len(vsx_labeled))) # field chart with only the background logging.info("Plotting field chart with only the reference field...") fig, _ = get_plot_with_background_data(fits_data, 0, "Reference frame") save(fig, fieldchartsdirs + "only_ref") # candidate stars get their local id label candidate_descr = utils.get_stars_with_metadata( star_descriptions, "CANDIDATE", exclude=["VSX"] ) candidate_labeled = set_local_id_label(candidate_descr) # localid/radec inputfile stars get their local id label selected_desc = utils.get_stars_with_metadata(star_descriptions, "SITE") selected_no_vsx_descr = utils.get_stars_with_metadata( star_descriptions, "SITE", exclude=["VSX"] ) selected_no_vsx_labeled = set_custom_label( selected_no_vsx_descr, [x.get_metadata("SITE").our_name for x in selected_no_vsx_descr], ) selected_count = len(selected_no_vsx_labeled) # field chart with all vsx stars + candidates + radeccatalog logging.info("Plotting field chart with all VSX variable stars + candidate vars...") fig = plot_it( [vsx_labeled, candidate_labeled], [10.0, 5.0], [False, True], fits_data, wcs, "VSX stars + candidate stars", PADDING, ) save( fig, fieldchartsdirs + f"vsx_{len(vsx_labeled)}_and_candidates_{len(candidate_labeled)}", ) # field chart with all vsx stars + localid/radec inputfile logging.info( f"Plotting field chart with all VSX variable stars + {selected_count} selected vars..." ) fig = plot_it( [vsx_labeled, selected_no_vsx_labeled], [10.0, 5.0], [False, True], fits_data, wcs, "VSX stars + selected stars", PADDING, ) save( fig, fieldchartsdirs + "vsx_{}_and_selected_{}".format(len(vsx_labeled), selected_count), ) # compstars get their vmag as label # comp_stars_descr = comp_stars.star_descriptions # comp_stars_labeled = set_custom_label(comp_stars_descr, [x.vmag for x in comp_stars_descr]) logging.info( f"Plotting field chart for each of the {selected_count} selected stars" ) # Plotting finder charts for the site # field charts for each individually selected starfile star for star in tqdm.tqdm(selected_desc, desc="Field chart for each star"): filtered_compstars, check_star = do_compstars.filter_comparison_stars( star, comp_stars ) filtered_compstars_sds = filtered_compstars.star_descriptions check_star_sd = check_star.star_descriptions compstars_labeled = set_custom_label( filtered_compstars_sds, [x.get_metadata("UCAC4").vmag for x in filtered_compstars_sds], ) checkstar_labeled = set_custom_label( check_star_sd, f"Kmag={check_star_sd[0].get_metadata('UCAC4').vmag}" ) starui: utils.StarUI = utils.get_star_or_catalog_name(star, suffix="") fig = plot_it( [[star], vsx_labeled, compstars_labeled, checkstar_labeled], [7.0, 5.0, 3.0, 4.0], [True, False, True, False], fits_data, wcs, f"VSX stars + comp stars + {starui.catalog_name} (star {star.local_id})", PADDING, ) save(fig, f"{fieldchartsdirs}vsx_and_star_{starui.filename_no_ext}") gc.collect()
def block(star: StarDescription, resultdir: str, images_prefix: str, explore: bool): try: is_vsx = star.has_metadata("VSX") starui: utils.StarUI = utils.get_star_or_catalog_name(star, suffix="_phase") txt_path = Path( Path(star.result["phase"]).parent, "txt", starui.filename_no_suff_no_ext + ".txt", ) try: parsed_toml = toml.load(txt_path) except FileNotFoundError: logging.error( f"Could not load txt file with phase information from {txt_path}" ) ucac4_name, ucac4_mag, ucac4_coords, ucac4_colors, ucac4_rgb = get_ucac4_info( star, parsed_toml) name = (f"\n{parsed_toml['our_name']}" if "our_name" in parsed_toml else f"OUR_NAME_{star.local_id}" ) # get the period if it's present, and change -1 to None period = float( parsed_toml['period']) if 'period' in parsed_toml else -1 display_period = f"{period:.6f}" if period > 0 else "None" var_type_raw = get_from_toml('var_type', parsed_toml, UNKNOWN) var_type = f"{var_type_raw}" phase_url = f"{images_prefix}{starui.filename_no_ext}.png" light_url = f"{images_prefix}{starui.filename_no_suff_no_ext}_light.png" if utils.is_var_type_aperiodic(var_type, period) or utils.is_check(var_type): main_url = light_url second_url = phase_url else: main_url = phase_url second_url = light_url epoch = f"{parsed_toml['epoch']}" if 'epoch' in parsed_toml else UNKNOWN vsx_var_flag = f" ({parsed_toml['vsx_var_flag']})" if 'vsx_var_flag' in parsed_toml else "" tomlseparation = parsed_toml[ 'separation'] if 'separation' in parsed_toml else None ucacseparation = star.coords.separation( star.get_metadata("UCAC4").coords).degree if star.has_metadata( "UCAC4") else None realseparation = ucacseparation if ucacseparation is not None else tomlseparation if tomlseparation is not None else None separation = f"<li>separation: +/- {realseparation * 3600:.0f} arcsec</li>" if realseparation is not None else "" var_type_link = f"<a href='https://www.aavso.org/vsx/index.php?view=help.vartype&nolayout=1&abbrev=" \ f"{var_type}'>{var_type}</a>" if var_type != UNKNOWN else var_type mag_range = f"{parsed_toml['range']}" minmax = (f"<li>minmax: {parsed_toml['minmax']}</li>" if "minmax" in parsed_toml else "") vsx_link = ( f'<li><a target="_blank" rel="noopener noreferrer" ' f'href="https://www.aavso.org/vsx/index.php?view=detail.top&oid={starui.extradata["OID"]}"' f">VSX link</a></li>" if is_vsx else "") points_removed = ( f"<li>Outliers removed: {parsed_toml['points_removed']}</li>" if parsed_toml["points_removed"] > 0 else "") optional_compstars = ( f'<a href="{images_prefix}{starui.filename_no_suff_no_ext}_compstarsA.png" ' f'title="Plot of all comparison stars used to measure this star">C</a>, ' f'<a href="{images_prefix}{starui.filename_no_suff_no_ext}_compstarsB.png" ' f'title="Plot of all comparison stars used to measure this star + the star itself">C+V</a>, ' if "compA" in star.result else "") optional_stats = ( f'<li>stats: <a href="{images_prefix}{starui.filename_no_suff_no_ext}_merr_vs_jd.png" ' f'alt="Plot of magnitude error vs JD">merr_vs_jd</a></li>' if "merr_vs_jd" in star.result else "") optional_explore = ( f'<div class="fl w-70 pa2 ba">' f' <img class="special-img-class" src="{second_url}" alt="{second_url}"/>' f'</div>' if explore else "") # show extra phase link if the main image is not a phase diagram and the period is not -1 optional_phase = ( f'<li><a href="{phase_url}" alt="Phase diagram">Phase diagram</a></li>' if utils.is_check(var_type) else "") optional_comments = (f'<li>Comments: {parsed_toml["comments"]}' if "comments" in parsed_toml and explore else "") result = f"""<div class="bb-l b--black-10 w-100"> <div class="fl w-70 pa2 ba"> <img class="special-img-class" src="{main_url}" alt="{main_url}"/> </div>{optional_explore} <div class="fl w-30 pa2 ba"> <ul> <li>{ucac4_name} (mag:{ucac4_mag})</li> <li>name: {name}</li>{ucac4_coords} <li>coords: {utils.get_hms_dms_sober(star.coords)} (ours)</li>{separation}{points_removed} <li>period (d): {display_period}</li>{minmax} <li>mag. range: {mag_range}</li> <li>{ucac4_colors}</li>{optional_comments} <li><a target="_blank" rel="noopener noreferrer" href="https://www.aavso.org/vsx/index.php?view=about.vartypessort">type</a>: {var_type_link}{vsx_var_flag}</li> {vsx_link}<li>epoch: {epoch}</li> <li><a href="{images_prefix}vsx_and_star_{starui.filename_no_suff_no_ext}.png">finder chart</a></li> <li><a href="{images_prefix}{starui.filename_no_suff_no_ext}_ext.txt">observations</a></li> <li>light curve: <a title="Standard plot of julian date on X and magnitude on Y" href="{images_prefix}{starui.filename_no_suff_no_ext}_light.png">Normal</a>, <a title="Lightcurve with empty spaces cut out, taking period into account. If period is 1 day, and we have a gap of 1 day and 5 minutes, we cut out the 1 day and leave the 5 minutes so that the shape of the curve is preserved" href="{images_prefix}{starui.filename_no_suff_no_ext}_lightpa.png">PA</a>, <a title="All observations are plotted sequentially, without taking into account day/time" href="{images_prefix}{starui.filename_no_suff_no_ext}_lightcont.png">Continuous</a></li>{optional_phase} <li>comparison stars: {optional_compstars}<a title="text list of comparison stars" href="{images_prefix}{starui.filename_no_suff_no_ext}_comps.txt">list</a></li>{optional_stats} </ul> </div> </div> """ return result except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) import traceback print(traceback.print_exc()) logging.error(message) logging.error("File not found error in store and curve for star" + star.path) return f'<div class="fl w-100 pa2 ba">Could not load {txt_path}</div>'
def update_img( star: StarDescription, record: ImageRecord, neighbours: List[StarDescription], resultdir: str, platesolved_file: str, ): resultlines = [] fig = plt.figure(figsize=(36, 32), dpi=dpi, facecolor="w", edgecolor="k") wcs = do_calibration.get_wcs(platesolved_file) data, shapex, shapey = reading.get_fits_data(platesolved_file) backgr = data.mean() data = data.reshape(shapex, shapey) data = np.pad( data, (padding, padding), "constant", constant_values=(backgr, backgr) ) starxy = SkyCoord.to_pixel(star.coords, wcs=wcs, origin=0) # add main target add_circle(record.x, record.y, 3, "b") startoml = load_toml(star, resultdir) star.vmag = startoml["vmag"] resultlines.append(log_star(star, -1)) random_offset = False offset1 = 70 offset2 = 10 # add neighbours for idx, nstar in enumerate(neighbours): add_pixels(nstar, wcs, 0) add_circle(nstar.xpos, nstar.ypos, 4, "g") if random_offset: xrandoffset = random.randint(3, 4) yrandoffset = random.randint(2, 3) xsignrand = random.choice([-1.0, 1.0]) ysignrand = random.choice([-1.0, 1.0]) offset1 = xsignrand * xrandoffset offset2 = ysignrand * yrandoffset # https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.arrow.html plt.annotate( f"{idx}", xy=(round(nstar.xpos), round(nstar.ypos)), xycoords="data", xytext=(offset1, offset2), textcoords="offset points", size=NEIGHBOUR_TEXT_SIZE, color="red", arrowprops=dict(arrowstyle="-", color="grey", alpha=0.2), ) resultlines.append(log_star(nstar, idx)) # loading and painting ucac stars radius = 0.08 ucac_stars: List[MinimalStarTuple] = ucac4.get_region_minimal_star_tuples( star.coords.ra.deg, star.coords.dec.deg, radius ) logging.info(f"Looping on {len(ucac_stars)} UCAC4 stars") for ucac_star in ucac_stars: coord = SkyCoord(ucac_star.ra, ucac_star.dec, unit="deg") if star.coords.separation(coord).degree > radius: continue xy = SkyCoord.to_pixel(coord, wcs=wcs, origin=0) x, y = round(xy[0].item(0)), round(xy[1].item(0)) add_circle(x, y, 2, "c") plt.annotate( f"{ucac_star.id[-3:]}", xy=(x, y), xycoords="data", xytext=(2, 2), textcoords="offset points", size=UCAC4_TEXT_SIZE, arrowprops=dict(arrowstyle="-", color="grey", alpha=0.2), ) median = np.median(data) # data = ndimage.interpolation.rotate(data, record.rotation) plt.imshow(data, cmap="gray_r", origin="lower", vmin=0, vmax=min(median * 5, 65536)) starui = utils.get_star_or_catalog_name(star) save_inspect_image = Path( Path(resultdir) / "inspect", f"{starui.filename_no_ext}_{Path(resultdir).name}_inspect.png", ) save_inspect_txt = Path( Path(resultdir) / "inspect", f"{starui.filename_no_ext}_{Path(resultdir).name}_inspect.txt", ) fig.savefig(save_inspect_image) logging.info(f"Saved file as {save_inspect_image}.") write_file(star, save_inspect_txt, resultlines) plt.close(fig) plt.clf()
def report( star: StarDescription, df_curve: DataFrame, comp_stars: ComparisonStars, check_star: ComparisonStars, target_dir: Path, sitelat, sitelong, sitealt, camera_filter=None, observer="RMH", chunk_size=None, ): df = df_curve.sort_values("JD") star_match_ucac4 = (star.get_metadata("UCAC4").name if star.has_metadata("UCAC4") else None) star_match_vsx = star.get_metadata("VSX").name if star.has_metadata( "VSX") else None var_display_name = star_match_ucac4 if star_match_vsx is None else star_match_vsx var_display_name = (var_display_name if var_display_name is not None else f"Star_{star.local_id}") # utils.replace_spaces(f"{star.local_id:05}" if star.aavso_id is None else star.aavso_id) starui = utils.get_star_or_catalog_name(star) earth_location = EarthLocation(lat=sitelat, lon=sitelong, height=sitealt * u.m) logging.debug(f"Starting aavso report with star:{star}") if chunk_size is None: chunk_size = df.shape[0] star_chunks = [ df[i:i + chunk_size] for i in range(0, df.shape[0], chunk_size) ] chunk_counters = 0 kname = check_star.star_descriptions[0].get_metadata("UCAC4").catalog_id notes = f"Standard mag: K = {check_star.comp_catalogmags[0]:.3f}" filterdict = None # Setting up the filter value if camera_filter is None: filterdict = read_camera_filters.read_filters() # print(filterdict) filterlambda = lambda x: filterdict[x] else: filterlambda = lambda x: camera_filter for chunk in star_chunks: chunk_counters += 1 suffix = f"_{chunk_counters}.txt" if len(star_chunks) != 1 else ".txt" filename = Path(target_dir, f"{starui.filename_no_ext}_ext{suffix}") with open(filename, "w") as fp: writer = aavso.ExtendedFormatWriter( fp, observer, location=(sitelat, sitelong, sitealt), software="https://github.com/mrosseel/vast-automation", type="EXTENDED", obstype="CCD", ) for _, row in chunk.iterrows(): # logging.info(row, type(row)) jd = row["JD"] if jd in check_star.observations[0]: # adding an offset of 30 to get instrumental mags to be positive (recommended by aavso) check_mag = f"{check_star.observations[0][jd][0] + 30:.3f}" else: check_mag = "na" writer.addrow({ "name": var_display_name, "date": jd, "magnitude": row["realV"], "magnitude_error": row["realErr"], "filter": filterlambda(row["JD"]), "transformed": "NO", "magnitude_type": "STD", "comparison_name": "ENSEMBLE", "comparison_magnitude": "na", "check_name": kname, "check_magnitude": check_mag, "airmass": calculate_airmass(star.coords, earth_location, row["floatJD"]).value, "group": "na", "chart": "na", "notes": notes, }) writer.flush() return Path(target_dir, f"{starui.filename_no_ext}_ext.txt")