def power(gcomp, lmin, lmax, rparam=1.0, separated=False): m,l=newleg.degrees(lmax,start=1) powers=rparam**(2*l[l >= lmin]+4)*(l[l >= lmin] + 1)*gcomp[:, numpy.where(l >= lmin)]**2 if separated: return powers.sum(axis=1) else: return powers.sum(axis=2).sum(axis=1)
def magnetic_north(gcoefs, order, south=True, debug=False): northlons = [] northlats = [] startingpoint_north = (numpy.pi, numpy.pi / 2) if south: southlons = [] southlats = [] startingpoint_south = (numpy.pi, numpy.pi / 2) m, l = newleg.degrees(order, start=1) max_index = len(m) def inc_value(coords, gcomp): x, y, z = xyzfieldv2( gcomp[:max_index], numpy.array([coords[0]]), numpy.array([coords[1]]), regular=False, order=order ) inc = numpy.arctan2(z, numpy.sqrt(x ** 2 + y ** 2)) return -inc for gcomp in gcoefs: sol_north = scipy.optimize.minimize(inc_value, startingpoint_north, args=(gcomp), method="Powell") startingpoint_north = sol_north.x north_lon, north_lat = numpy.rad2deg(sol_north.x[0]), 90 - numpy.rad2deg(sol_north.x[1]) northlons.append(north_lon) northlats.append(north_lat) if south: sol_south = scipy.optimize.minimize( lambda x, g: -inc_value(x, g), startingpoint_south, args=(gcomp), method="Powell", options={disp: debug} ) startingpoint_south = sol_south.x south_lon, south_lat = numpy.rad2deg(sol_south.x[0]), 90 - numpy.rad2deg(sol_south.x[1]) southlons.append(south_lon) southlats.append(south_lat) if debug: print(north_lon, north_lat) if south: return northlons, northlats, southlons, southlats else: return northlons, northlats
def condition_array_xyz(thetav, phiv, order=13): mv,lv=newleg.degrees(order, start=1) leg,dleg=newleg.legendre(scipy.cos(thetav), order) cossin = numpy.zeros((len(phiv),order*(order+2))) cossin[:, mv >= 0] = numpy.cos(phiv[:, numpy.newaxis] @ abs(mv[numpy.newaxis, :]))[:, mv >= 0] cossin[:, mv < 0] = numpy.sin(phiv[:, numpy.newaxis] @ abs(mv[numpy.newaxis, :]))[:, mv < 0] sinmcos=numpy.zeros((len(phiv),order*(order+2))) sinmcos[:, mv >= 0] = numpy.sin(phiv[:, numpy.newaxis] @ abs(mv[numpy.newaxis, :]))[:, mv >= 0] sinmcos[:, mv < 0] = -numpy.cos(phiv[:, numpy.newaxis] @ abs(mv[numpy.newaxis, :]))[:, mv < 0] costhetav = numpy.cos(thetav) sinthetav = numpy.sin(thetav) Ax = cossin.transpose() * dleg * (-sinthetav) Ay = abs(mv[:, numpy.newaxis]) * leg * sinmcos.transpose() / sinthetav Az = -(lv + 1)[:, numpy.newaxis] * leg * cossin.transpose() return Ax, Ay, Az
def xyzfieldv2(gcoefs, phi, theta, rparam=1.0, order=13, regular=False): mv, lv = newleg.degrees(order, start=1) legv, dlegv = newleg.legendre(scipy.cos(theta), order) x = numpy.zeros_like(theta) y = x.copy() z = x.copy() for m, l, g, leg, dleg in zip(mv, lv, gcoefs, legv, dlegv): rparamexp = rparam ** (l + 2) m_abs = abs(m) cossin = scipy.cos(m_abs * phi) if m >= 0 else scipy.sin(m_abs * phi) sinmcos = scipy.sin(m_abs * phi) if m >= 0 else -scipy.cos(m_abs * phi) x += rparamexp * (g * cossin) * dleg * (-scipy.sin(theta)) y += rparamexp * (g * sinmcos) * m_abs * leg / (scipy.sin(theta)) z -= rparamexp * (l + 1) * (g * cossin) * leg return x, y, z