/** \brief Read TLE data for a given satellite into memory. * \param catnum The catalog number of the satellite. * \param sat Pointer to a valid sat_t structure. * \return 0 if successfull, 1 if an I/O error occurred, * 2 if the TLE data appears to be bad. * */ gint gtk_sat_data_read_sat (gint catnum, sat_t *sat) { guint errorcode = 0; GError *error = NULL; GKeyFile *data; gchar *filename = NULL, *path = NULL; gchar *tlestr1,*tlestr2,*rawtle; /* ensure that sat != NULL */ g_return_val_if_fail (sat != NULL, 1); /* .sat file names */ filename = g_strdup_printf ("%d.sat", catnum); path = sat_file_name_from_catnum (catnum); /* open .sat file */ data = g_key_file_new (); if (!g_key_file_load_from_file (data, path, G_KEY_FILE_KEEP_COMMENTS, &error)) { /* an error occurred */ sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Failed to load data from %s (%s)"), __func__, path, error->message); g_clear_error (&error); errorcode = 1; } else { /* read name, nickname, and website */ sat->name = g_key_file_get_string (data, "Satellite", "NAME", &error); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error reading NAME from %s (%s)"), __func__, path, error->message); g_clear_error (&error); sat->name = g_strdup ("Error"); } sat->nickname = g_key_file_get_string (data, "Satellite", "NICKNAME", &error); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Satellite %d has no NICKNAME"), __func__, catnum); g_clear_error (&error); sat->nickname = g_strdup (sat->name); } sat->website = g_key_file_get_string (data, "Satellite", "WEBSITE", NULL); /* website may be NULL */ /* get TLE data */ tlestr1 = g_key_file_get_string (data, "Satellite", "TLE1", NULL); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error reading TLE line 1 from %s (%s)"), __func__, path, error->message); g_clear_error (&error); } tlestr2 = g_key_file_get_string (data, "Satellite", "TLE2", NULL); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error reading TLE line 2 from %s (%s)"), __func__, path, error->message); g_clear_error (&error); } rawtle = g_strconcat (tlestr1, tlestr2, NULL); if (!Good_Elements (rawtle)) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: TLE data for %d appears to be bad"), __func__, catnum); errorcode = 2; } else { Convert_Satellite_Data (rawtle, &sat->tle); } if (g_key_file_has_key(data, "Satellite", "STATUS",NULL)) sat->tle.status = g_key_file_get_integer (data, "Satellite", "STATUS", NULL); g_free (tlestr1); g_free (tlestr2); g_free (rawtle); /* VERY, VERY important! If not done, some sats will not get initialised, the first time SGP4/SDP4 is called. Consequently, the resulting data will be NAN, INF or similar nonsense. For some reason, not even using g_new0 seems to be enough. */ sat->flags = 0; select_ephemeris (sat); /* initialise variable fields */ sat->jul_utc = 0.0; sat->tsince = 0.0; sat->az = 0.0; sat->el = 0.0; sat->range = 0.0; sat->range_rate = 0.0; sat->ra = 0.0; sat->dec = 0.0; sat->ssplat = 0.0; sat->ssplon = 0.0; sat->alt = 0.0; sat->velo = 0.0; sat->ma = 0.0; sat->footprint = 0.0; sat->phase = 0.0; sat->aos = 0.0; sat->los = 0.0; /* calculate satellite data at epoch */ gtk_sat_data_init_sat (sat, NULL); } g_free (filename); g_free (path); g_key_file_free (data); return errorcode; }
/** \brief Check if satellite is new, if so, add it to local database */ static void check_and_add_sat (gpointer key, gpointer value, gpointer user_data) { new_tle_t *ntle = (new_tle_t *) value; guint *num = user_data; GKeyFile *satdata; GIOChannel *satfile; gchar *cfgstr, *cfgfile; GError *err = NULL; (void) key; /* avoid unused parameter compiler warning */ /* check if sat is new */ if (ntle->isnew) { /* create config data */ satdata = g_key_file_new (); /* store data */ g_key_file_set_string (satdata, "Satellite", "VERSION", "1.1"); g_key_file_set_string (satdata, "Satellite", "NAME", ntle->satname); g_key_file_set_string (satdata, "Satellite", "NICKNAME", ntle->satname); g_key_file_set_string (satdata, "Satellite", "TLE1", ntle->line1); g_key_file_set_string (satdata, "Satellite", "TLE2", ntle->line2); g_key_file_set_integer (satdata, "Satellite", "STATUS", ntle->status); /* create an I/O channel and store data */ cfgfile = sat_file_name_from_catnum (ntle->catnum); if (!gpredict_save_key_file (satdata, cfgfile)){ *num += 1; } /* clean up memory */ g_free (cfgfile); g_key_file_free (satdata); /**** FIXME: NEED TO CREATE COPY of cache */ /* finally, new satellite must be added to proper category */ gchar *catfile; gchar **buff; gint statretval; struct stat temp; buff = g_strsplit (ntle->srcfile, ".", 0); cfgfile = g_strconcat (buff[0], ".cat", NULL); catfile = sat_file_name (cfgfile); /* call stat on file before opening it incase file does not exist and we need to add a group name. */ statretval = stat (catfile,&temp); /* g_io_channel */ satfile = g_io_channel_new_file (catfile, "a", &err); if (err != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Could not open category file file %s (%s)."), __FUNCTION__, cfgfile, err->message); g_clear_error (&err); } else { if (statretval == -1) { /* file did not exist before creating handle */ /* use the file name as the group description */ cfgstr = g_strdup_printf ("%s\n", buff[0]); g_io_channel_write_chars (satfile, cfgstr, -1, NULL, &err); g_free (cfgstr); } cfgstr = g_strdup_printf ("%d\n", ntle->catnum); g_io_channel_write_chars (satfile, cfgstr, -1, NULL, &err); g_io_channel_shutdown (satfile, TRUE, NULL); g_io_channel_unref (satfile); g_free (cfgstr); if (err != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error adding %d to %s (%s)."), __FUNCTION__, ntle->catnum, cfgfile, err->message); g_clear_error (&err); } else { sat_log_log (SAT_LOG_LEVEL_INFO, _("%s: Added satellite %d to %s."), __FUNCTION__, ntle->catnum, cfgfile); } } g_free (catfile); g_free (cfgfile); g_strfreev (buff); } }