def test_SphToCart(self): """SphToCart should known known output (regression)""" invals = [[0, 0, 5], [90, 0, 5], [90, 90, 5], [90, 180, 5]] ans = [[5.0, 0.0, 0.0], [3.061616997868383e-16, 0.0, 5.0], [1.8746997283273223e-32, 3.061616997868383e-16, 5.0], [-3.061616997868383e-16, 3.7493994566546446e-32, 5.0]] for i, val in enumerate(invals): vec3 = Lgm_Vector.SphToCart(*val) for j, val2 in enumerate(vec3.tolist()): self.assertAlmostEqual(ans[i][j], val2) # test an input check self.assertRaises(ValueError, Lgm_Vector.SphToCart, [1] * 2, [2] * 3, [3] * 2) # test putting in lists ans_tst = Lgm_Vector.SphToCart( zip(*invals)[0], zip(*invals)[1], zip(*invals)[2]) for i, v1 in enumerate(ans_tst): for j, v2 in enumerate(v1.tolist()): self.assertAlmostEqual(ans[i][j], ans_tst[i].tolist()[j])
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()