| 1 | #define PY_SSIZE_T_CLEAN |
| 2 | #include <Python.h> |
| 3 | #include "SGP4.h" |
| 4 | #include "structmember.h" |
| 5 | |
| 6 | /* Whether scanf() prefers commas as the decimal point: true means that |
| 7 | we cannot count on the current locale to interpret numbers correctly |
| 8 | when the Vallado C++ code calls `sscanf()`. */ |
| 9 | |
| 10 | static bool switch_locale; |
| 11 | |
| 12 | /* Satrec object that wraps a single raw SGP4 struct, and a Satrec array |
| 13 | that can broadcast into NumPy arrays. */ |
| 14 | |
| 15 | typedef struct { |
| 16 | PyObject_HEAD |
| 17 | elsetrec satrec; |
| 18 | } SatrecObject; |
| 19 | |
| 20 | typedef struct { |
| 21 | PyObject_VAR_HEAD |
| 22 | elsetrec satrec[0]; |
| 23 | } SatrecArrayObject; |
| 24 | |
| 25 | /* Support routine that is used to support NumPy array broadcasting for |
| 26 | both individual satellite objects and also arrays. */ |
| 27 | |
| 28 | static PyObject * |
| 29 | _vectorized_sgp4(PyObject *args, elsetrec *raw_satrec_array, int imax) |
| 30 | { |
| 31 | PyObject *jd_arg, *fr_arg, *e_arg, *r_arg, *v_arg; |
| 32 | Py_buffer jd_buf, fr_buf, e_buf, r_buf, v_buf; |
| 33 | PyObject *rv = NULL; |
| 34 | |
| 35 | // To prepare for "cleanup:" below. |
| 36 | jd_buf.buf = fr_buf.buf = e_buf.buf = r_buf.buf = v_buf.buf = NULL; |
| 37 | |
| 38 | if (!PyArg_ParseTuple(args, "OOOOO:sgp4", |
| 39 | &jd_arg, &fr_arg, &e_arg, &r_arg, &v_arg)) |
| 40 | return NULL; |
| 41 | |
| 42 | if (PyObject_GetBuffer(jd_arg, &jd_buf, PyBUF_SIMPLE)) goto cleanup; |
| 43 | if (PyObject_GetBuffer(fr_arg, &fr_buf, PyBUF_SIMPLE)) goto cleanup; |
| 44 | if (PyObject_GetBuffer(e_arg, &e_buf, PyBUF_WRITABLE)) goto cleanup; |
| 45 | if (PyObject_GetBuffer(r_arg, &r_buf, PyBUF_WRITABLE)) goto cleanup; |
| 46 | if (PyObject_GetBuffer(v_arg, &v_buf, PyBUF_WRITABLE)) goto cleanup; |
| 47 | |
| 48 | if (jd_buf.len != fr_buf.len) { |
| 49 | PyErr_SetString(PyExc_ValueError, "jd and fr must have the same shape"); |
| 50 | goto cleanup; |
| 51 | } |
| 52 | |
| 53 | // This extra block allows the "goto" statements above to jump |
| 54 | // across these further variable declarations. |
| 55 | { |
| 56 | Py_ssize_t jmax = jd_buf.len / sizeof(double); |
| 57 | |
| 58 | if ((r_buf.len != (Py_ssize_t) sizeof(double) * imax * jmax * 3) || |
| 59 | (v_buf.len != (Py_ssize_t) sizeof(double) * imax * jmax * 3) || |
| 60 | (e_buf.len != (Py_ssize_t) sizeof(uint8_t) * imax * jmax)) { |
| 61 | PyErr_SetString(PyExc_ValueError, "bad output array dimension"); |
| 62 | goto cleanup; |
| 63 | } |
| 64 | |
| 65 | double *jd = (double*) jd_buf.buf; |
| 66 | double *fr = (double*) fr_buf.buf; |
| 67 | double *r = (double*) r_buf.buf; |
| 68 | double *v = (double*) v_buf.buf; |
| 69 | uint8_t *e = (uint8_t*) e_buf.buf; |
| 70 | |
| 71 | #pragma omp parallel for |
| 72 | for (Py_ssize_t i=0; i < imax; i++) { |
| 73 | elsetrec &satrec = raw_satrec_array[i]; |
| 74 | for (Py_ssize_t j=0; j < jmax; j++) { |
| 75 | double t = (jd[j] - satrec.jdsatepoch) * 1440.0 |
| 76 | + (fr[j] - satrec.jdsatepochF) * 1440.0; |
| 77 | Py_ssize_t k1 = i * jmax + j; |
| 78 | Py_ssize_t k3 = 3 * k1; |
| 79 | SGP4Funcs::sgp4(satrec, t, r + k3, v + k3); |
| 80 | e[k1] = (uint8_t) satrec.error; |
| 81 | if (satrec.error && satrec.error < 6) { |
| 82 | r[k3] = r[k3+1] = r[k3+2] = v[k3] = v[k3+1] = v[k3+2] = NAN; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | Py_INCREF(Py_None); |
| 89 | rv = Py_None; |
| 90 | cleanup: |
| 91 | if (jd_buf.buf) PyBuffer_Release(&jd_buf); |
| 92 | if (fr_buf.buf) PyBuffer_Release(&fr_buf); |
| 93 | if (r_buf.buf) PyBuffer_Release(&r_buf); |
| 94 | if (v_buf.buf) PyBuffer_Release(&v_buf); |
| 95 | if (e_buf.buf) PyBuffer_Release(&e_buf); |
| 96 | return rv; |
| 97 | } |
| 98 | |
| 99 | /* Details of the "Satrec" satellite object. */ |
| 100 | |
| 101 | static PyObject * |
| 102 | Satrec_twoline2rv(PyTypeObject *cls, PyObject *args) |
| 103 | { |
| 104 | char *string1, *string2, line1[130], line2[130]; |
| 105 | gravconsttype whichconst = wgs72; |
| 106 | double dummy; |
| 107 | |
| 108 | if (!PyArg_ParseTuple(args, |
| 109 | "ss|i:twoline2rv", |
| 110 | &string1, &string2, &whichconst)) |
| 111 | return NULL; |
| 112 | |
| 113 | // Copy both lines, since twoline2rv() might write to both buffers. |
| 114 | strncpy(line1, string1, 130); |
| 115 | strncpy(line2, string2, 130); |
| 116 | |
| 117 | // Truncate the line before any checksum characters, if present; |
| 118 | // otherwise the C++ scanf() interprets each checksum character as |
| 119 | // part of the preceding value. |
| 120 | line1[68] = '\0'; |
| 121 | line2[68] = '\0'; |
| 122 | |
| 123 | /* Allocate the new object. */ |
| 124 | SatrecObject *self = (SatrecObject*) cls->tp_alloc(cls, 0); |
| 125 | if (!self) |
| 126 | return NULL; |
| 127 | |
| 128 | /* Correct for locales that use a comma as the decimal point, since |
| 129 | users report that the scanf() function on macOS is sensitive to |
| 130 | locale when parsing floats. This operation is not thread-safe, |
| 131 | but we have not released the GIL. */ |
| 132 | float f; |
| 133 | sscanf("1,5", "%f", &f); |
| 134 | switch_locale = (f == 1.5); |
| 135 | |
| 136 | char *old_locale = NULL; |
| 137 | if (switch_locale) |
| 138 | old_locale = setlocale(LC_NUMERIC, "C"); |
| 139 | |
| 140 | /* Leading spaces in a catalog number make scanf() in the official |
| 141 | code consume the Classification letter as part of the catalog |
| 142 | number. (The first character of the International Designator |
| 143 | then gets consumed as the Classification instead.) But no |
| 144 | parsing error is reported, which is bad for users, so let's avoid |
| 145 | the situation by adding leading zeros ourselves. */ |
| 146 | for (int i=2; i<7; i++) { |
| 147 | if (line1[i] == ' ') line1[i] = '0'; |
| 148 | if (line2[i] == ' ') line2[i] = '0'; |
| 149 | } |
| 150 | |
| 151 | /* Call the official routine. */ |
| 152 | SGP4Funcs::twoline2rv(line1, line2, ' ', ' ', 'i', whichconst, |
| 153 | dummy, dummy, dummy, self->satrec); |
| 154 | |
| 155 | /* Usability bonus: round the fractional day to exactly the eight |
| 156 | digits specified in the TLE. */ |
| 157 | self->satrec.jdsatepochF = round(self->satrec.jdsatepochF * 1e8) / 1e8; |
| 158 | |
| 159 | /* To avoid having scanf() interpret the "intldesg" as zero or as |
| 160 | several fields, the C++ library changes spaces to periods and |
| 161 | underscores. Let's convert them back to avoid surprising users |
| 162 | and to match our Python implementation. */ |
| 163 | if (self->satrec.intldesg[0] == '.') |
| 164 | self->satrec.intldesg[0] = ' '; |
| 165 | for (int i=1; i<11; i++) |
| 166 | if (self->satrec.intldesg[i] == '_') |
| 167 | self->satrec.intldesg[i] = ' '; |
| 168 | |
| 169 | /* Restore previous locale. */ |
| 170 | if (switch_locale) |
| 171 | setlocale(LC_NUMERIC, old_locale); |
| 172 | |
| 173 | return (PyObject*) self; |
| 174 | } |
| 175 | |
| 176 | static PyObject * |
| 177 | Satrec_sgp4init(PyObject *self, PyObject *args) |
| 178 | { |
| 179 | char satnum_str[6]; |
| 180 | int whichconst; /* "int" rather than "gravconsttype" so we know size */ |
| 181 | int opsmode; /* "int" rather than "char" because "C" needs an int */ |
| 182 | long int satnum; |
| 183 | double epoch, bstar, ndot, nddot; |
| 184 | double ecco, argpo, inclo, mo, no_kozai, nodeo; |
| 185 | |
| 186 | if (!PyArg_ParseTuple(args, "iCldddddddddd:sgp4init", &whichconst, |
| 187 | &opsmode, &satnum, &epoch, &bstar, &ndot, &nddot, |
| 188 | &ecco, &argpo, &inclo, &mo, &no_kozai, &nodeo)) |
| 189 | return NULL; |
| 190 | |
| 191 | // See https://www.space-track.org/documentation#tle-alpha5 |
| 192 | if (satnum < 100000) { |
| 193 | snprintf(satnum_str, 6, "%05ld", satnum); |
| 194 | } else if (satnum > 339999) { |
| 195 | PyErr_SetString(PyExc_ValueError, "satellite number cannot exceed " |
| 196 | "339999, whose Alpha 5 encoding is 'Z9999'"); |
| 197 | return 0; |
| 198 | } else { |
| 199 | char c = 'A' + satnum / 10000 - 10; |
| 200 | if (c > 'I') c++; |
| 201 | if (c > 'O') c++; |
| 202 | satnum_str[0] = c; |
| 203 | snprintf(satnum_str + 1, 5, "%04ld", satnum % 10000); |
| 204 | } |
| 205 | |
| 206 | elsetrec &satrec = ((SatrecObject*) self)->satrec; |
| 207 | |
| 208 | SGP4Funcs::sgp4init((gravconsttype) whichconst, opsmode, satnum_str, epoch, |
| 209 | bstar, ndot, nddot, ecco, argpo, inclo, mo, no_kozai, |
| 210 | nodeo, satrec); |
| 211 | |
| 212 | /* Populate date fields that SGP4Funcs::twoline2rv would set. */ |
| 213 | double whole; |
| 214 | double fraction = modf(epoch, &whole); |
| 215 | double whole_jd = whole + 2433281.5; |
| 216 | |
| 217 | /* Go out on a limb: if `epoch` has no decimal digits past the 8 |
| 218 | decimal places stored in a TLE, then assume the user is trying |
| 219 | to specify an exact decimal fraction. */ |
| 220 | double epoch8 = epoch * 1e8; |
| 221 | if (round(epoch8) == epoch8) { |
| 222 | fraction = round(fraction * 1e8) / 1e8; |
| 223 | } |
| 224 | |
| 225 | satrec.jdsatepoch = whole_jd; |
| 226 | satrec.jdsatepochF = fraction; |
| 227 | |
| 228 | int y, m, d, H, M; |
| 229 | double S, jan0jd, jan0fr /* always comes out 0.0 */; |
| 230 | SGP4Funcs::invjday_SGP4(2433281.5, whole, y, m, d, H, M, S); |
| 231 | SGP4Funcs::jday_SGP4(y, 1, 0, 0, 0, 0.0, jan0jd, jan0fr); |
| 232 | satrec.epochyr = y % 100; |
| 233 | satrec.epochdays = whole_jd - jan0jd + fraction; |
| 234 | |
| 235 | satrec.classification = 'U'; /* default to Unclassified, not '\0' */ |
| 236 | |
| 237 | /* Return true, like sgp4init does; check satrec.error for an error code */ |
| 238 | Py_INCREF(Py_None); |
| 239 | return Py_None; |
| 240 | } |
| 241 | |
| 242 | static PyObject * |
| 243 | Satrec_sgp4_tsince(PyObject *self, PyObject *args) |
| 244 | { |
| 245 | double tsince, r[3], v[3]; |
| 246 | |
| 247 | if (!PyArg_ParseTuple(args, "d:sgp4_tsince", &tsince)) |
| 248 | return NULL; |
| 249 | elsetrec &satrec = ((SatrecObject*) self)->satrec; |
| 250 | SGP4Funcs::sgp4(satrec, tsince, r, v); |
| 251 | if (satrec.error && satrec.error < 6) |
| 252 | r[0] = r[1] = r[2] = v[0] = v[1] = v[2] = NAN; |
| 253 | return Py_BuildValue("i(fff)(fff)", satrec.error, |
| 254 | r[0], r[1], r[2], v[0], v[1], v[2]); |
| 255 | } |
| 256 | |
| 257 | static PyObject * |
| 258 | Satrec_sgp4(PyObject *self, PyObject *args) |
| 259 | { |
| 260 | double jd, fr, r[3], v[3]; |
| 261 | if (!PyArg_ParseTuple(args, "dd:sgp4", &jd, &fr)) |
| 262 | return NULL; |
| 263 | elsetrec &satrec = ((SatrecObject*) self)->satrec; |
| 264 | double tsince = (jd - satrec.jdsatepoch) * 1440.0 |
| 265 | + (fr - satrec.jdsatepochF) * 1440.0; |
| 266 | SGP4Funcs::sgp4(satrec, tsince, r, v); |
| 267 | if (satrec.error && satrec.error < 6) |
| 268 | r[0] = r[1] = r[2] = v[0] = v[1] = v[2] = NAN; |
| 269 | return Py_BuildValue("i(fff)(fff)", satrec.error, |
| 270 | r[0], r[1], r[2], v[0], v[1], v[2]); |
| 271 | } |
| 272 | |
| 273 | static PyObject * |
| 274 | Satrec__sgp4(PyObject *self, PyObject *args) |
| 275 | { |
| 276 | SatrecObject *obj = (SatrecObject*) self; |
| 277 | elsetrec *raw_satrec_array = &(obj->satrec); |
| 278 | Py_ssize_t imax = 1; |
| 279 | return _vectorized_sgp4(args, raw_satrec_array, imax); |
| 280 | } |
| 281 | |
| 282 | static PyMethodDef Satrec_methods[] = { |
| 283 | {"twoline2rv", (PyCFunction)Satrec_twoline2rv, METH_VARARGS | METH_CLASS, |
| 284 | PyDoc_STR("Initialize the record from two lines of TLE text and an optional gravity constant.")}, |
| 285 | {"sgp4init", (PyCFunction)Satrec_sgp4init, METH_VARARGS, |
| 286 | PyDoc_STR("Initialize the record from orbital elements.")}, |
| 287 | {"sgp4", (PyCFunction)Satrec_sgp4, METH_VARARGS, |
| 288 | PyDoc_STR("Given a modified julian date, return position and velocity.")}, |
| 289 | {"_sgp4", (PyCFunction)Satrec__sgp4, METH_VARARGS, |
| 290 | PyDoc_STR("Given an array of modified julian dates, return position and velocity arrays.")}, |
| 291 | {"sgp4_tsince", (PyCFunction)Satrec_sgp4_tsince, METH_VARARGS, |
| 292 | PyDoc_STR("Given minutes since epoch, return position and velocity.")}, |
| 293 | {NULL, NULL} |
| 294 | }; |
| 295 | |
| 296 | #define O(member) offsetof(SatrecObject, satrec.member) |
| 297 | |
| 298 | static PyMemberDef Satrec_members[] = { |
| 299 | /* Listed in the order they appear in a TLE record. */ |
| 300 | |
| 301 | {"operationmode", T_CHAR, O(operationmode), READONLY, |
| 302 | PyDoc_STR("Operation mode: 'a' legacy AFSPC, or 'i' improved.")}, |
| 303 | {"jdsatepoch", T_DOUBLE, O(jdsatepoch), 0, |
| 304 | PyDoc_STR("Julian date of epoch, day number (see jdsatepochF).")}, |
| 305 | {"jdsatepochF", T_DOUBLE, O(jdsatepochF), 0, |
| 306 | PyDoc_STR("Julian date of epoch, fraction of day (see jdsatepoch).")}, |
| 307 | {"classification", T_CHAR, O(classification), 0, |
| 308 | "Usually U=Unclassified, C=Classified, or S=Secret."}, |
| 309 | /* intldesg: inline character array; see Satrec_getset. */ |
| 310 | {"epochyr", T_INT, O(epochyr), 0, |
| 311 | PyDoc_STR("Year of this element set's epoch (see epochdays). Not set by sgp4init().")}, |
| 312 | {"epochdays", T_DOUBLE, O(epochdays), 0, |
| 313 | PyDoc_STR("Day of the year of this element set's epoch (see epochyr). Not set by sgp4init().")}, |
| 314 | {"ndot", T_DOUBLE, O(ndot), READONLY, |
| 315 | PyDoc_STR("Ballistic Coefficient in radians/minute^2.")}, |
| 316 | {"nddot", T_DOUBLE, O(nddot), READONLY, |
| 317 | PyDoc_STR("Second Derivative of Mean Motion in radians/minute^3.")}, |
| 318 | {"bstar", T_DOUBLE, O(bstar), READONLY, |
| 319 | PyDoc_STR("Drag Term in inverse Earth radii.")}, |
| 320 | {"ephtype", T_INT, O(ephtype), 0, |
| 321 | PyDoc_STR("Ephemeris type (should be 0 in published TLEs).")}, |
| 322 | {"elnum", T_LONG, O(elnum), 0, |
| 323 | PyDoc_STR("Element set number.")}, |
| 324 | {"inclo", T_DOUBLE, O(inclo), READONLY, |
| 325 | PyDoc_STR("Inclination in radians.")}, |
| 326 | {"nodeo", T_DOUBLE, O(nodeo), READONLY, |
| 327 | PyDoc_STR("Right ascension of ascending node in radians.")}, |
| 328 | {"ecco", T_DOUBLE, O(ecco), READONLY, |
| 329 | PyDoc_STR("Eccentricity.")}, |
| 330 | {"argpo", T_DOUBLE, O(argpo), READONLY, |
| 331 | PyDoc_STR("Argument of perigee in radians.")}, |
| 332 | {"mo", T_DOUBLE, O(mo), READONLY, |
| 333 | PyDoc_STR("Mean anomaly in radians.")}, |
| 334 | {"no_kozai", T_DOUBLE, O(no_kozai), READONLY, |
| 335 | PyDoc_STR("Mean motion in radians per minute.")}, |
| 336 | {"revnum", T_LONG, O(revnum), 0, |
| 337 | PyDoc_STR("Integer revolution number at the epoch.")}, |
| 338 | |
| 339 | /* For compatibility with the old struct members, also accept the |
| 340 | plain name "no". */ |
| 341 | |
| 342 | {"no", T_DOUBLE, O(no_kozai), READONLY, |
| 343 | PyDoc_STR("Alias for the more carefully named ``no_kozai``.")}, |
| 344 | |
| 345 | /* Derived values that do not appear explicitly in the TLE. */ |
| 346 | |
| 347 | {"method", T_CHAR, O(method), READONLY, |
| 348 | PyDoc_STR("Method, either 'n' near earth or 'd' deep space.")}, |
| 349 | {"error", T_INT, O(error), READONLY, |
| 350 | PyDoc_STR("Error code (1-6) documented in sgp4()")}, |
| 351 | {"a", T_DOUBLE, O(a), READONLY, |
| 352 | PyDoc_STR("semi-major axis")}, |
| 353 | {"altp", T_DOUBLE, O(altp), READONLY, |
| 354 | PyDoc_STR("altitude of perigee")}, |
| 355 | {"alta", T_DOUBLE, O(alta), READONLY, |
| 356 | PyDoc_STR("altitude of perigee")}, |
| 357 | |
| 358 | /* Single averaged mean elements */ |
| 359 | |
| 360 | {"am", T_DOUBLE, O(am), READONLY, |
| 361 | PyDoc_STR("am: Average semi-major axis")}, |
| 362 | {"em", T_DOUBLE, O(em), READONLY, |
| 363 | PyDoc_STR("em: Average eccentricity")}, |
| 364 | {"im", T_DOUBLE, O(im), READONLY, |
| 365 | PyDoc_STR("im: Average inclination")}, |
| 366 | {"Om", T_DOUBLE, O(Om), READONLY, |
| 367 | PyDoc_STR("Om: Average right ascension of ascending node")}, |
| 368 | {"om", T_DOUBLE, O(om), READONLY, |
| 369 | PyDoc_STR("om: Average argument of perigee")}, |
| 370 | {"mm", T_DOUBLE, O(mm), READONLY, |
| 371 | PyDoc_STR("mm: Average mean anomaly")}, |
| 372 | {"nm", T_DOUBLE, O(nm), READONLY, |
| 373 | PyDoc_STR("nm: Average mean motion")}, |
| 374 | |
| 375 | /* Gravity-constant dependent values (initialized by sgp4init() */ |
| 376 | |
| 377 | {"tumin", T_DOUBLE, O(tumin), READONLY, |
| 378 | PyDoc_STR("minutes in one time unit")}, |
| 379 | {"mu", T_DOUBLE, O(mus), READONLY, |
| 380 | PyDoc_STR("Earth gravitational parameter")}, |
| 381 | {"radiusearthkm", T_DOUBLE, O(radiusearthkm), READONLY, |
| 382 | PyDoc_STR("radius of the earth in km")}, |
| 383 | {"xke", T_DOUBLE, O(xke), READONLY, |
| 384 | PyDoc_STR("reciprocal of tumin")}, |
| 385 | {"j2", T_DOUBLE, O(j2), READONLY, |
| 386 | PyDoc_STR("un-normalized zonal harmonic j2 value")}, |
| 387 | {"j3", T_DOUBLE, O(j3), READONLY, |
| 388 | PyDoc_STR("un-normalized zonal harmonic j3 value")}, |
| 389 | {"j4", T_DOUBLE, O(j4), READONLY, |
| 390 | PyDoc_STR("un-normalized zonal harmonic j4 value")}, |
| 391 | {"j3oj2", T_DOUBLE, O(j3oj2), READONLY, |
| 392 | PyDoc_STR("j3 divided by j2")}, |
| 393 | |
| 394 | /* Other convenience variables (some required by propagation.py) */ |
| 395 | |
| 396 | {"t", T_DOUBLE, O(t), READONLY, |
| 397 | PyDoc_STR("Last tsince input to sgp4()")}, |
| 398 | {"mdot", T_DOUBLE, O(mdot), READONLY, |
| 399 | PyDoc_STR("mean anomaly dot (rate)")}, |
| 400 | {"argpdot", T_DOUBLE, O(argpdot), READONLY, |
| 401 | PyDoc_STR("argument of perigee dot (rate)")}, |
| 402 | {"nodedot", T_DOUBLE, O(nodedot), READONLY, |
| 403 | PyDoc_STR("right ascension of ascending node dot (rate)")}, |
| 404 | {"gsto", T_DOUBLE, O(gsto), READONLY, |
| 405 | PyDoc_STR("gsto: greenwich sidereal time")}, |
| 406 | |
| 407 | {NULL} |
| 408 | }; |
| 409 | |
| 410 | #undef O |
| 411 | |
| 412 | static PyObject * |
| 413 | get_intldesg(SatrecObject *self, void *closure) |
| 414 | { |
| 415 | int length = 0; |
| 416 | char *s = self->satrec.intldesg; |
| 417 | while (length <= 7 && s[length]) |
| 418 | length++; |
| 419 | while (length && s[length-1] == ' ') |
| 420 | length--; |
| 421 | return PyUnicode_FromStringAndSize(s, length); |
| 422 | } |
| 423 | |
| 424 | static int |
| 425 | set_intldesg(SatrecObject *self, PyObject *value, void *closure) |
| 426 | { |
| 427 | const char *s = PyUnicode_AsUTF8(value); |
| 428 | if (!s) |
| 429 | return -1; |
| 430 | strncpy(self->satrec.intldesg, s, 10); |
| 431 | self->satrec.intldesg[10] = '\0'; |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* The official TLE code has switched `satnum` to a string, but to avoid |
| 436 | a breaking change we make the string available as `satnum_str` and |
| 437 | still return an integer for `satnum`. */ |
| 438 | static PyObject * |
| 439 | get_satnum(SatrecObject *self, void *closure) |
| 440 | { |
| 441 | char *s = self->satrec.satnum; |
| 442 | long n; |
| 443 | if (strlen(s) < 5 || s[0] <= '9') |
| 444 | n = atol(s); |
| 445 | else if (s[0] <= 'I') |
| 446 | n = (s[0] - 'A' + 10) * 10000 + atol(s + 1); |
| 447 | else if (s[0] <= 'O') |
| 448 | n = (s[0] - 'A' + 9) * 10000 + atol(s + 1); |
| 449 | else |
| 450 | n = (s[0] - 'A' + 8) * 10000 + atol(s + 1); |
| 451 | return PyLong_FromLong(n); |
| 452 | } |
| 453 | |
| 454 | static PyObject * |
| 455 | get_satnum_str(SatrecObject *self, void *closure) |
| 456 | { |
| 457 | return PyUnicode_FromString(self->satrec.satnum); |
| 458 | } |
| 459 | |
| 460 | static int |
| 461 | set_satnum_str(SatrecObject *self, PyObject *value, void *closure) |
| 462 | { |
| 463 | const char *s = PyUnicode_AsUTF8(value); |
| 464 | if (!s) |
| 465 | return -1; |
| 466 | strncpy(self->satrec.satnum, s, 5); |
| 467 | self->satrec.satnum[5] = '\0'; |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static PyGetSetDef Satrec_getset[] = { |
| 472 | {"intldesg", (getter)get_intldesg, (setter)set_intldesg, |
| 473 | PyDoc_STR("International Designator: a string of up to 7 characters" |
| 474 | " from the first line of the TLE that typically provides" |
| 475 | " two digits for the launch year, a 3-digit launch number," |
| 476 | " and one or two letters for which piece of the launch.")}, |
| 477 | {"satnum", (getter)get_satnum, NULL, |
| 478 | PyDoc_STR("Satellite number from characters 3-7 of each TLE line," |
| 479 | " as an integer.")}, |
| 480 | {"satnum_str", (getter)get_satnum_str, (setter)set_satnum_str, |
| 481 | PyDoc_STR("Satellite number from characters 3-7 of each TLE line," |
| 482 | " as a string.")}, |
| 483 | {NULL}, |
| 484 | }; |
| 485 | |
| 486 | static PyTypeObject SatrecType = { |
| 487 | PyVarObject_HEAD_INIT(NULL, 0) |
| 488 | /* See the module initialization function at the bottom of this file. */ |
| 489 | }; |
| 490 | |
| 491 | /* Details of the SatrecArray. */ |
| 492 | |
| 493 | static Py_ssize_t |
| 494 | Satrec_len(PyObject *self) { |
| 495 | return ((SatrecArrayObject*)self)->ob_base.ob_size; |
| 496 | } |
| 497 | |
| 498 | static PySequenceMethods SatrecArray_as_sequence = { |
| 499 | Satrec_len |
| 500 | }; |
| 501 | |
| 502 | static PyObject * |
| 503 | SatrecArray_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 504 | { |
| 505 | PyObject *sequence; |
| 506 | if (!PyArg_ParseTuple(args, "O:SatrecArray", &sequence)) |
| 507 | return NULL; |
| 508 | |
| 509 | Py_ssize_t length = PySequence_Length(sequence); |
| 510 | if (length == -1) |
| 511 | return NULL; |
| 512 | |
| 513 | return type->tp_alloc(type, length); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | static int |
| 518 | SatrecArray_init(SatrecArrayObject *self, PyObject *args, PyObject *kwds) |
| 519 | { |
| 520 | PyObject *sequence; |
| 521 | if (!PyArg_ParseTuple(args, "O:SatrecArray", &sequence)) |
| 522 | return -1; |
| 523 | |
| 524 | Py_ssize_t length = PySequence_Length(sequence); |
| 525 | if (length == -1) |
| 526 | return -1; |
| 527 | |
| 528 | for (Py_ssize_t i=0; i<length; i++) { |
| 529 | PyObject *item = PySequence_GetItem(sequence, i); |
| 530 | if (!item) |
| 531 | return -1; |
| 532 | if (!PyObject_IsInstance(item, (PyObject*) &SatrecType)) { |
| 533 | PyErr_Format(PyExc_ValueError, "every item must be a Satrec," |
| 534 | " but element %d is: %R", i, item); |
| 535 | Py_DECREF(item); |
| 536 | return -1; |
| 537 | } |
| 538 | self->satrec[i] = ((SatrecObject *)item)->satrec; |
| 539 | Py_DECREF(item); |
| 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static PyObject * |
| 545 | SatrecArray_sgp4(PyObject *self, PyObject *args) |
| 546 | { |
| 547 | SatrecArrayObject *satrec_array = (SatrecArrayObject*) self; |
| 548 | elsetrec *raw_satrec_array = &(satrec_array->satrec[0]); |
| 549 | Py_ssize_t imax = satrec_array->ob_base.ob_size; |
| 550 | return _vectorized_sgp4(args, raw_satrec_array, imax); |
| 551 | } |
| 552 | |
| 553 | static PyMethodDef SatrecArray_methods[] = { |
| 554 | {"_sgp4", (PyCFunction)SatrecArray_sgp4, METH_VARARGS, |
| 555 | PyDoc_STR("Given array of minutes since epoch, write" |
| 556 | " positions, velocities, and errors to arrays.")}, |
| 557 | {NULL, NULL} |
| 558 | }; |
| 559 | |
| 560 | static PyTypeObject SatrecArrayType = { |
| 561 | PyVarObject_HEAD_INIT(NULL, sizeof(elsetrec)) |
| 562 | /* See the module initialization function at the bottom of this file. */ |
| 563 | }; |
| 564 | |
| 565 | /* The module that ties it all together. */ |
| 566 | static PyModuleDef module = { |
| 567 | PyModuleDef_HEAD_INIT, |
| 568 | "sgp4.vallado_cpp", |
| 569 | "Official C++ SGP4 implementation.", |
| 570 | -1 |
| 571 | }; |
| 572 | |
| 573 | #include <stdio.h> |
| 574 | |
| 575 | PyMODINIT_FUNC |
| 576 | PyInit_vallado_cpp(void) |
| 577 | { |
| 578 | SatrecType.tp_name = "sgp4.vallado_cpp.Satrec"; |
| 579 | SatrecType.tp_basicsize = sizeof(SatrecObject); |
| 580 | SatrecArrayType.tp_itemsize = 0; |
| 581 | SatrecType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; |
| 582 | SatrecType.tp_doc = "SGP4 satellite record."; |
| 583 | SatrecType.tp_methods = Satrec_methods; |
| 584 | SatrecType.tp_members = Satrec_members; |
| 585 | SatrecType.tp_getset = Satrec_getset; |
| 586 | SatrecType.tp_new = PyType_GenericNew; |
| 587 | |
| 588 | if (PyType_Ready(&SatrecType) < 0) |
| 589 | return NULL; |
| 590 | |
| 591 | SatrecArrayType.tp_name = "sgp4.vallado_cpp.SatrecArray"; |
| 592 | SatrecArrayType.tp_basicsize = sizeof(SatrecArrayObject); |
| 593 | SatrecArrayType.tp_itemsize = sizeof(elsetrec); |
| 594 | SatrecArrayType.tp_as_sequence = &SatrecArray_as_sequence; |
| 595 | SatrecArrayType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; |
| 596 | SatrecArrayType.tp_doc = "SGP4 array of satellites."; |
| 597 | SatrecArrayType.tp_methods = SatrecArray_methods; |
| 598 | SatrecArrayType.tp_init = (initproc) SatrecArray_init; |
| 599 | SatrecArrayType.tp_new = SatrecArray_new; |
| 600 | |
| 601 | if (PyType_Ready(&SatrecArrayType) < 0) |
| 602 | return NULL; |
| 603 | |
| 604 | PyObject *m = PyModule_Create(&module); |
| 605 | if (m == NULL) |
| 606 | return NULL; |
| 607 | |
| 608 | Py_INCREF(&SatrecType); |
| 609 | if (PyModule_AddObject(m, "Satrec", (PyObject *) &SatrecType) < 0) { |
| 610 | Py_DECREF(&SatrecType); |
| 611 | Py_DECREF(m); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | Py_INCREF(&SatrecArrayType); |
| 616 | if (PyModule_AddObject(m, "SatrecArray", (PyObject *) &SatrecArrayType) < 0) { |
| 617 | Py_DECREF(&SatrecArrayType); |
| 618 | Py_DECREF(&SatrecType); |
| 619 | Py_DECREF(m); |
| 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | if (PyModule_AddIntConstant(m, "WGS72", (int)wgs72) || |
| 624 | PyModule_AddIntConstant(m, "WGS72OLD", (int)wgs72old) || |
| 625 | PyModule_AddIntConstant(m, "WGS84", (int)wgs84)) |
| 626 | return NULL; |
| 627 | |
| 628 | return m; |
| 629 | } |