9#include "supernovaecomponent.h"
15#include "kstarslite.h"
18#include "kstars_debug.h"
19#include "ksnotification.h"
20#include "kstarsdata.h"
22#include "skylabeler.h"
24#include "skypainter.h"
25#include "auxiliary/filedownloader.h"
26#include "projections/projector.h"
27#include "auxiliary/kspaths.h"
29#include <QtConcurrent>
30#include <QJsonDocument>
39const QString SupernovaeComponent::tnsDataFilename(
"tns_public_objects.csv");
40const QString SupernovaeComponent::tnsDataFilenameZip(
"tns-daily.csv.gz");
41const QString SupernovaeComponent::tnsDataUrl(
42 "https://indilib.org/jdownloads/kstars/tns-daily.csv.gz");
48 connect(Options::self(), &Options::SupernovaDownloadUrlChanged,
this,
49 &SupernovaeComponent::loadData);
50 connect(Options::self(), &Options::MagnitudeLimitShowSupernovaeChanged,
this,
51 &SupernovaeComponent::loadData);
60 for (
auto so : m_ObjectList)
63 so->updateCoords(num);
64 so->EquatorialToHorizontal(data->
lst(), data->
geo()->
lat());
70 return Options::showSupernovae();
73void SupernovaeComponent::loadData()
76 objectNames(SkyObject::SUPERNOVA).
clear();
77 objectLists(SkyObject::SUPERNOVA).
clear();
83 io::CSVReader<26, io::trim_chars<
' '>, io::double_quote_escape<
',',
'\"'>,
85 in(sFileName.toLocal8Bit());
87 const char *line = in.next_line();
90 qCritical() <<
"file is empty\n";
94 std::string id,
name, ra_s, dec_s,
type;
96 std::string host_name;
98 std::string reporting_group, disc_datasource, classifying_group, assoc_group,
99 disc_internal_name, disc_instrument, classifying_instrument;
100 int tns_at, is_public;
101 std::string end_prop_period;
102 double discovery_mag;
103 std::string discovery_filter, discovery_date_s,
sender, remarks,
104 discovery_bibcode, classification_bibcode, ext_catalog;
107 id, name, ra_s, dec_s, type, redshift, host_name, host_redshift,
108 reporting_group, disc_datasource, classifying_group, assoc_group,
109 disc_internal_name, disc_instrument, classifying_instrument, tns_at,
110 is_public, end_prop_period, discovery_mag, discovery_filter, discovery_date_s,
111 sender, remarks, discovery_bibcode, classification_bibcode, ext_catalog))
113 auto discovery_date =
121 QString(discovery_date_s.c_str()), redshift, discovery_mag, discovery_date);
123 objectNames(SkyObject::SUPERNOVA).append(
QString(
name.c_str()));
126 objectLists(SkyObject::SUPERNOVA)
127 .append(QPair<QString, const SkyObject *>(qname, sup));
130 m_DataLoading =
false;
133 catch (io::error::can_not_open_file &ex)
135 qCCritical(KSTARS) <<
"could not open file " << sFileName.toLocal8Bit() <<
"\n";
138 catch (std::exception &ex)
140 qCCritical(KSTARS) <<
"unknown exception happened:" << ex.what() <<
"\n";
150 double rBest = maxrad;
152 for (
auto &so : m_ObjectList)
154 double r = so->angularDistanceTo(p).Degrees();
169 double lgmin = log10(MINZOOM);
170 double lgz = log10(Options::zoomFactor());
172 return 14.0 + 2.222 * (lgz - lgmin) +
173 2.222 * log10(
static_cast<double>(Options::starDensity()));
181 else if (!m_DataLoaded)
185 m_DataLoading =
true;
186#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
196 double refage = Options::supernovaDetectionAge();
197 bool hostOnly = Options::supernovaeHostOnly();
198 bool classifiedOnly = Options::supernovaeClassifiedOnly();
200 for (
auto so : m_ObjectList)
203 float mag = sup->
mag();
204 float age = sup->getAgeDays();
207 if (mag >
float(Options::magnitudeLimitShowSupernovae()))
218 if (mag > maglim && Options::limitSupernovaeByZoom())
222 if (classifiedOnly && type ==
"")
230void SupernovaeComponent::notifyNewSupernovae()
239 if (sup->getMagnitude() >
float(Options::magnitudeLimitAlertSupernovae()))
248 if (!latestList.
empty())
250 NotifyUpdatesUI * ui =
new NotifyUpdatesUI(0);
251 ui->addItems(latestList);
262 delete (downloadJob);
263 downloadJob =
new FileDownloader();
264 QString url = Options::supernovaDownloadUrl();
270 qInfo() <<
"fetching data from web: " << url <<
"\n";
271 downloadJob->setProgressDialogEnabled(
true,
i18n(
"Supernovae Update"),
272 i18n(
"Downloading Supernovae updates..."));
274 QObject::connect(downloadJob, SIGNAL(downloaded()),
this, SLOT(downloadReady()));
279 downloadJob->get(
QUrl(url));
286 qInfo() <<
"fetching data from local file at: " << fname <<
"\n";
289 qInfo() <<
"copy returned: " << res <<
"\n";
297void SupernovaeComponent::unzipData()
308 auto fhz = gzopen(ifpath.c_str(),
"rb");
309 auto fout = fopen(ofpath.c_str(),
"wb");
313 printf(
"Error: Failed to gzopen %s\n", ifpath.c_str());
316 unsigned char unzipBuffer[8192];
317 unsigned int unzippedBytes;
320 unzippedBytes = gzread(fhz, unzipBuffer, 8192);
321 if (unzippedBytes > 0)
323 fwrite(unzipBuffer, 1, unzippedBytes, fout);
334void SupernovaeComponent::downloadReady()
345 downloadJob->deleteLater();
348void SupernovaeComponent::downloadError(
const QString &errorString)
350 KSNotification::error(
i18n(
"Error downloading supernova data: %1", errorString));
351 downloadJob->deleteLater();
const CachingDms * lat() const
There are several time-dependent values used in position calculations, that are not specific to an ob...
KStarsData is the backbone of KStars.
void setFullTimeUpdate()
The Sky is updated more frequently than the moon, which is updated more frequently than the planets.
static KStarsLite * Instance()
KStarsData * data() const
static KStars * Instance()
KStarsData * data() const
An abstract parent class, to be inherited by SkyComponents that store a QList of SkyObjects.
void appendListObject(SkyObject *object)
Add an object to the Object list.
SkyComposite is a kind of container class for SkyComponent objects.
Provides all necessary information about an object in the sky: its coordinates, name(s),...
Draws things on the sky, without regard to backend.
virtual bool drawSupernova(Supernova *sup)=0
Draw a Supernova.
Represents the supernova object.
QString getHostGalaxy() const
void slotTriggerDataFileUpdate()
This initiates updating of the data file.
void draw(SkyPainter *skyp) override
SkyObject * objectNearest(SkyPoint *p, double &maxrad) override
Find the SkyObject nearest the given SkyPoint.
static float zoomMagnitudeLimit()
void update(KSNumbers *num=nullptr) override
Update the sky positions of this component.
An angle, stored as degrees, but expressible in many ways.
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QString filePath(const QString &fileName) const const
bool copy(const QString &fileName, const QString &newName)
void append(QList< T > &&value)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
QString right(qsizetype n) const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
std::string toStdString() const const
QTextStream & dec(QTextStream &stream)
QFuture< T > run(Function function,...)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)