Ejemplo n.º 1
0
def test_yeardec():
    for t, r in zip(T, (2013.5, 2013.4986301369863)):
        yeardec = sd.datetime2yeardec(t)

        assert yeardec == approx(r)
        if isinstance(t, datetime.datetime):
            assert sd.yeardec2datetime(yeardec) == t
        else:
            assert sd.yeardec2datetime(yeardec).date() == t
    # %% array
    assert (sd.yeardec2datetime(sd.datetime2yeardec(Tdt)) == T[0]).all()
Ejemplo n.º 2
0
def test_yeardec():
    for t, r in zip(T, (2013.5, 2013.4986301369863)):
        yeardec = sd.datetime2yeardec(t)

        assert_allclose(yeardec, r)
        if isinstance(t, datetime.datetime):
            assert sd.yeardec2datetime(yeardec) == t
        else:
            assert sd.yeardec2datetime(yeardec).date() == t


# %% array
    assert_equal(sd.yeardec2datetime(sd.datetime2yeardec(Tdt)), T[0])
Ejemplo n.º 3
0
def igrf(
    time: datetime,
    glat: Union[np.ndarray, float],
    glon: Union[np.ndarray, float],
    alt_km: Union[np.ndarray, float],
    isv: int = 0,
    itype: int = 1,
    model: int = 12,
) -> xarray.Dataset:
    """
    date: datetime.date or decimal year yyyy.dddd
    glat, glon: geographic Latitude, Longitude
    alt_km: 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(time, (str, date, datetime)):
        yeardec = sciencedates.datetime2yeardec(time)
    elif isinstance(time, float):  # assume decimal year
        yeardec = time
    else:
        raise TypeError(f"unknown time format {type(time)}")

    colat, elon = latlon2colat(glat, glon)

    assert colat.size == elon.size == 1

    alt_km = np.atleast_1d(alt_km)
    Bnorth = np.empty(alt_km.size)
    Beast = np.empty_like(Bnorth)
    Bvert = np.empty_like(Bnorth)
    Btotal = np.empty_like(Bnorth)
    for i, a in enumerate(alt_km):
        if model == 12:
            Bnorth[i], Beast[i], Bvert[i], Btotal[i] = igrf12fort.igrf12syn(
                isv, yeardec, itype, a, colat, elon)
        elif model == 11:
            Bnorth[i], Beast[i], Bvert[i], Btotal[i] = igrf11fort.igrf11syn(
                isv, yeardec, itype, a, colat, elon)
        else:
            raise ValueError(f"unknown IGRF model {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_km},
    )

    decl, incl = mag_vector2incl_decl(mag.north, mag.east, mag.down)

    mag["incl"] = ("alt_km", incl)
    mag["decl"] = ("alt_km", decl)

    return mag
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def test_wmm2015():
    dt = datetime.datetime(2012, 7, 12, 12)

    mag = wmm.wmm(65, 85, 0, datetime2yeardec(dt))

    assert mag.north.item() == approx(9215.692665)
    assert mag.east.item() == approx(2516.0058789)
    assert mag.down.item() == approx(59708.529371)
    assert mag.total.item() == approx(60467.906831)

    assert mag.incl.item() == approx(80.910090)
    assert mag.decl.item() == approx(15.27036)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def gridigrf12(
    t: datetime,
    glat: Union[np.ndarray, float],
    glon: Union[np.ndarray, float],
    alt_km: 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] = igrf12fort.igrf12syn(isv, yeardec, itype,
                                                      alt_km, 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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
def runigrf11(date: datetime,
              glat: float,
              glon: float,
              alt: float,
              isv=0,
              itype=1):
    import igrf11

    yeardec = sciencedates.datetime2yeardec(date)
    colat, elon = latlon2colat(glat, glon)

    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] = igrf11.igrf11syn(
            isv, yeardec, itype, a, colat, elon)

    return Bnorth, Beast, Bdown, Btotal
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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