def igrf(t: datetime, glat: Union[np.ndarray, float], glon: Union[np.ndarray, float], alt: Union[np.ndarray, float], isv: int = 0, itype: int = 1, model: str = '12') -> xarray.Dataset: """ date: datetime.date or decimal year yyyy.dddd glat, glon: geographic Latitude, Longitude alt: altitude [km] above sea level for itype==1 isv: 0 for main geomagnetic field itype: 1: altitude is above sea level """ # decimal year if isinstance(t, (date, datetime)): yeardec: float = sciencedates.datetime2yeardec(t) elif isinstance(yeardec, float): # assume decimal year pass else: raise TypeError(f'unknown yeardec type {type(yeardec)}') colat, elon = latlon2colat(glat, glon) assert colat.size == elon.size == 1 alt = np.atleast_1d(alt) Bnorth = np.empty(alt.size) Beast = np.empty_like(Bnorth) Bvert = np.empty_like(Bnorth) Btotal = np.empty_like(Bnorth) for i, a in enumerate(alt): if model == '12': Bnorth[i], Beast[i], Bvert[i], Btotal[i] = igrf12.igrf12syn( isv, yeardec, itype, a, colat, elon) # noqa E128 elif model == '11': import igrf11 Bnorth[i], Beast[i], Bvert[i], Btotal[i] = igrf11.igrf11syn( isv, yeardec, itype, a, colat, elon) # noqa E128 else: raise ValueError('unknown IGRF model {}'.format(model)) # %% assemble output mag = xarray.Dataset( { 'north': ('alt_km', Bnorth), 'east': ('alt_km', Beast), 'down': ('alt_km', Bvert), 'total': ('alt_km', Btotal) }, coords={'alt_km': alt}) decl, incl = mag_vector2incl_decl(mag.north, mag.east, mag.down) mag['incl'] = ('alt_km', incl) mag['decl'] = ('alt_km', decl) return mag
def gridigrf12(dtime,isv,itype,alt,glat,glon): yeardec = datetime2yeardec(dtime) colat,elon = latlon2colat(glat,glon) x = empty(colat.size); y = empty_like(x); z = empty_like(x); f=empty_like(x) for i,(clt,eln) in enumerate(nditer((colat,elon))): x[i],y[i],z[i],f[i] = igrf12.igrf12syn(isv, yeardec, itype, alt, clt, eln) return x.reshape(colat.shape), y.reshape(colat.shape), z.reshape(colat.shape),f.reshape(colat.shape), yeardec
def runigrf12(dtime,isv,itype,alt,glat,glon): yeardec = datetime2yeardec(dtime) colat,elon = latlon2colat(glat,glon) assert colat.size==elon.size==1 alt = atleast_1d(alt) x = empty(alt.size); y = empty_like(x); z = empty_like(x); f=empty_like(x) for i,a in enumerate(alt): x[i],y[i],z[i],f[i] = igrf12.igrf12syn(isv, yeardec, itype, a, colat, elon) return x,y,z,f
def gridigrf12(t: datetime, glat: Union[np.ndarray, float], glon: Union[np.ndarray, float], alt: Union[np.ndarray, float], isv: int = 0, itype: int = 1) -> xarray.Dataset: glat = np.atleast_1d(glat) glon = np.atleast_1d(glon) yeardec = sciencedates.datetime2yeardec(t) colat, elon = latlon2colat(glat.ravel(), glon.ravel()) x = np.empty(colat.size) y = np.empty_like(x) z = np.empty_like(x) f = np.empty_like(x) for i, (clt, eln) in enumerate(zip(colat, elon)): x[i], y[i], z[i], f[i] = igrf12.igrf12syn(isv, yeardec, itype, alt, clt, eln) # %% assemble output if glat.ndim == 2 and glon.ndim == 2: # assume meshgrid coords = {'glat': glat[:, 0], 'glon': glon[0, :]} elif glat.ndim == 1 and glon.ndim == 1: coords = {'glat': glat, 'glon': glon} else: raise ValueError(f'glat/glon shapes: {glat.shape} {glon.shape}') mag = xarray.Dataset(coords=coords, attrs={'time': t, 'isv': isv}) mag['north'] = (('glat', 'glon'), x.reshape(glat.shape)) mag['east'] = (('glat', 'glon'), y.reshape(glat.shape)) mag['down'] = (('glat', 'glon'), z.reshape(glat.shape)) mag['total'] = (('glat', 'glon'), f.reshape(glat.shape)) decl, incl = mag_vector2incl_decl(mag.north, mag.east, mag.down) mag['incl'] = (('glat', 'glon'), incl) mag['decl'] = (('glat', 'glon'), decl) return mag
def gridigrf12(date: datetime, glat: float, glon: float, alt: float, isv: int = 0, itype: int = 1): yeardec = sciencedates.datetime2yeardec(date) colat, elon = latlon2colat(glat.ravel(), glon.ravel()) x = np.empty(colat.size) y = np.empty_like(x) z = np.empty_like(x) f = np.empty_like(x) for i, (clt, eln) in enumerate(zip(colat, elon)): x[i], y[i], z[i], f[i] = igrf12.igrf12syn(isv, yeardec, itype, alt, clt, eln) return x.reshape(glat.shape), y.reshape(glat.shape), z.reshape( glat.shape), f.reshape(glat.shape), yeardec
def runigrf12(date: datetime, glat: float, glon: float, alt: float, isv: int = 0, itype: int = 1): """ date: datetime.date or decimal year yyyy.dddd glat, glon: geographic Latitude, Longitude alt: altitude [km] above sea level for itype==1 isv: 0 for main geomagnetic field itype: 1: altitude is above sea level """ # decimal year if isinstance(date, (datetime.date, datetime.datetime)): yeardec = sciencedates.datetime2yeardec(date) elif isinstance(yeardec, float): # assume decimate year pass else: raise TypeError(f'unknown yeardec type {type(yeardec)}') colat, elon = latlon2colat(glat, glon) assert colat.size == elon.size == 1 alt = np.atleast_1d(alt) Bnorth = np.empty(alt.size) Beast = np.empty_like(Bnorth) Bdown = np.empty_like(Bnorth) Btotal = np.empty_like(Bnorth) for i, a in enumerate(alt): Bnorth[i], Beast[i], Bdown[i], Btotal[i] = igrf12.igrf12syn( isv, yeardec, itype, a, colat, elon) return Bnorth, Beast, Bdown, Btotal