def test_CartToSph(self): """CartToSph shuld give known output for known input""" np.testing.assert_array_equal(Lgm_Vector.CartToSph(1, 0, 0), (0.0, 0.0, 1.0)) np.testing.assert_array_equal( Lgm_Vector.CartToSph([1, 1], [0, 0], [0, 0]), [[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]])
def getLatLonRadfromTLE(epochs, TLEpath, options): """ Reads Latitude and Longitude from TLE. Parameters ========== epochs : List of Ticktock objects. TLEpath : Path to TLE file. options : optparse.Values Organized options from the command line. Returns ======= testlat, testlon, testrad : list, list, list Latitude, Longitude, and Radius, respectively, each being a list of float values. """ # now do Mike's setup for getting coords from TLE using SGP4 pos_in = [0, 0, 0] s = _SgpInfo() TLEs = _SgpTLE() # loop over all times testlat = np.asarray(epochs).copy() testlat.fill(0) testlon = testlat.copy() testrad = testlat.copy() testtdiff = testlat.copy() print('Fetching TLEs & converting for range {0} to {1}'.format( epochs[0].isoformat(), epochs[-1].isoformat())) for idx, c_date in enumerate(epochs): #print('Doing {0}'.format(c_date)) # put into JD as SGP4 needs serial time c = Lgm_init_ctrans(0) # now do Mike's setup for getting coords from TLE using SGP4 dstr = int(c_date.strftime('%Y%j')) + c_date.hour / 24.0 + \ c_date.minute / 1440.0 + c_date.second / 86400.0 globstat = os.path.join(TLEpath, '*.txt') TLEfiles = glob.glob(globstat) if not TLEfiles: raise IOError( 'No TLE files found in {0}. Aborting...'.format(TLEpath)) Line0, Line1, Line2 = fTLE.findTLEinfiles( TLEfiles, ParseMethod='UseSatelliteNumber', TargetEpoch=dstr, SatelliteNumber=options.SatNum, Verbose=False, PurgeDuplicates=True) # print("{0}\n{1}\n{2}\n\n".format(Line0,Line1,Line2)) nTLEs = c_int(0) LgmSgp_ReadTlesFromStrings(Line0, Line1, Line2, pointer(nTLEs), pointer(TLEs), 1) LgmSgp_SGP4_Init(pointer(s), pointer(TLEs)) date = Lgm_CTrans.dateToDateLong(c_date) utc = Lgm_CTrans.dateToFPHours(c_date) JD = Lgm_JD(c_date.year, c_date.month, c_date.day, utc, LGM_TIME_SYS_UTC, c) # Set up the trans matrices Lgm_Set_Coord_Transforms(date, utc, c) # get SGP4 output, needs minutes-since-TLE-epoch tsince = (JD - TLEs.JD) * 1440.0 LgmSgp_SGP4(tsince, pointer(s)) pos_in[0] = s.X pos_in[1] = s.Y pos_in[2] = s.Z Pin = Lgm_Vector.Lgm_Vector(*pos_in) Pout = Lgm_Vector.Lgm_Vector() Lgm_Convert_Coords(pointer(Pin), pointer(Pout), TEME_TO_GEO, c) PoutPy = Pout.tolist() PoutPy[0] /= WGS84_A PoutPy[1] /= WGS84_A PoutPy[2] /= WGS84_A nlat, nlon, nrad = Lgm_Vector.CartToSph(*PoutPy) testlat[idx] = nlat testlon[idx] = nlon testrad[idx] = nrad testtdiff[idx] = tsince / 1440.0 return testlat, testlon, testrad
def coordTrans(pos_in, time_in, in_sys, out_sys, de_eph=False): ''' Convert coordinates between almost any system using LanlGeoMag Parameters ---------- position : list a three element vector of positions in input coord system time : datetime a datimetime object representing the time at the desired conversion system_in : str a string giving the acronym for the input coordinate system system_out : str a string giving the acronym for the desired output coordinate system de_eph : bool or int (optional) a boolean stating whether JPL DE421 is to be used for Sun, etc. Returns ------- out : list 3-element list of the converted coordinate Examples -------- >>> from lgmpy import magcoords >>> import datetime >>> magcoords.coordTrans([-4,0,0], datetime.datetime(2009,1,1),'SM','GSM') [-3.60802691..., 2.5673907444...e-16, -1.72688788616...] >>> magcoords.coordTrans([-3.608026916281573, 2.5673907444456745e-16, -1.7268878861662329], datetime.datetime(2009,1,1),'GSM','SM') [-3.99999999..., 4.0592529337...e-16, 8.8817841970...3e-16] TODO ---- extend interface to get necessary args from a MagModel or cTrans structure ''' # change datetime to Lgm Datelong and UTC ct = Lgm_CTrans.Lgm_CTrans(0) if de_eph: Lgm_Set_CTrans_Options(LGM_EPH_DE, LGM_PN_IAU76, pointer(ct)) try: datelong = Lgm_CTrans.dateToDateLong(time_in) utc = Lgm_CTrans.dateToFPHours(time_in) Lgm_Set_Coord_Transforms( datelong, utc, pointer(ct)) # don't need pointer as it is one except AttributeError: raise (TypeError("Date must be a datetime object")) try: conv_val = trans_dict[in_sys] * 100 + trans_dict[out_sys] except KeyError: raise KeyError( 'One of the specified coordinate systems is not recognised') ## do this as WGS uses Cartesian but needs to be converted from desired spherical input if 'WGS84' in in_sys: XYZ = Lgm_Vector.SphToCart(*pos_in) SPH = Lgm_Vector.Lgm_Vector(XYZ.x, XYZ.y, XYZ.z) Pout = _doConversion(SPH, conv_val, cTrans=ct) else: Pout = _doConversion(pos_in, conv_val, ct) if 'WGS84' in out_sys: nlat, nlon, nrad = Lgm_Vector.CartToSph(*Pout.tolist()) Pout = Lgm_Vector.Lgm_Vector(nlat, nlon, nrad) return Pout.tolist()