Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime to astropy Time: instr #2676

Merged
merged 23 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
# Copy and paste this line for every submodule for which the tests pass
- run: conda install openjpeg
- run: python -m "sunpy.data.sample"
- run: python setup.py test -P instr --online
- run: python setup.py test -P time
- run: python setup.py test -P physics
- run: python setup.py test -P coordinates
Expand Down
24 changes: 12 additions & 12 deletions sunpy/instr/fermi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from collections import OrderedDict
import tempfile

import datetime
import matplotlib.pyplot as plt
import numpy as np
import astropy.units as u
from astropy.time import TimeDelta
from astropy.coordinates import Longitude, Latitude

from sunpy.time import parse_time, TimeRange
Expand All @@ -31,8 +31,8 @@ def download_weekly_pointing_file(date):
Parameters
----------

date : `datetime.datetime`
A datetime object or other date format understood by the parse_time
date : `astropy.time.Time`
A `~astropy.time.Time` object or other date format understood by the parse_time
function.
"""

Expand All @@ -51,7 +51,7 @@ def download_weekly_pointing_file(date):

# find out which mission week corresponds to date
time_diff = date - weekly_file_start
weekdiff = time_diff.days // 7
weekdiff = time_diff.to(u.day).value // 7
week = weekdiff + base_week
# weekstr = ('%03.0f' % week)
weekstr = '{:03.0f}'.format(week)
Expand Down Expand Up @@ -87,8 +87,8 @@ def get_detector_sun_angles_for_time(time, file):
Parameters
----------

time : `datetime.datetime`
A datetime object or other time format understood by the parse_time
time : `astropy.time.Time`
A `~astropy.time.Time` object or other time format understood by the parse_time
function.
file : `str`
A filepath to a Fermi/LAT weekly pointing file (e.g. as obtained by the
Expand Down Expand Up @@ -126,16 +126,16 @@ def get_detector_sun_angles_for_date(date, file):
Parameters
----------

date : `datetime.datetime`
A datetime object or other date format understood by the parse_time
date : `astropy.time.Time`
A `~astropy.time.Time` object or other date format understood by the parse_time
function.
file : `str`
A filepath to a Fermi/LAT weekly pointing file (e.g. as obtained by the
download_weekly_pointing_file function).
"""

date = parse_time(date)
tran = TimeRange(date, date + datetime.timedelta(days=1))
tran = TimeRange(date, date + TimeDelta(1*u.day))
scx, scz, times = get_scx_scz_in_timerange(tran, file)

# retrieve the detector angle information in spacecraft coordinates
Expand Down Expand Up @@ -213,8 +213,8 @@ def get_scx_scz_at_time(time, file):
Parameters
----------

time : `datetime.datetime`
A datetime object or other time format understood by the parse_time function.
time : `astropy.time.Time`
A `~astropy.time.Time` object or other time format understood by the parse_time function.
file : `str`
A filepath to a Fermi/LAT weekly pointing file (e.g. as obtained by the
download_weekly_pointing_file function).
Expand Down Expand Up @@ -313,7 +313,7 @@ def nai_detector_radecs(detectors, scx, scz, time):
scz : array-like
Two-element tuple containing the RA/DEC information of the Fermi
spacecraft Z-axis
time : `datetime.datetime`
time : `astropy.time.Time`
The time corresponding to the input scx and scz values, in a format
understandable by parse_time.

Expand Down
143 changes: 68 additions & 75 deletions sunpy/instr/goes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@
import pandas
import numpy as np
import astropy.units as u
from astropy.time import TimeDelta
from scipy import interpolate
from scipy.integrate import trapz, cumtrapz

from sunpy import sun
from sunpy.net import hek
from sunpy import timeseries
from sunpy.time import parse_time
from sunpy.time import parse_time, Time
from sunpy.util.net import check_download_file
from sunpy.coordinates import get_sunearth_distance
from sunpy.util.config import get_and_create_download_dir
Expand Down Expand Up @@ -133,7 +134,7 @@ def get_goes_event_list(timerange, goes_class_filter=None):

for r in result:
goes_event = {
'event_date': parse_time(r['event_starttime']).date().strftime(
'event_date': parse_time(r['event_starttime']).strftime(
'%Y-%m-%d'),
'start_time': parse_time(r['event_starttime']),
'peak_time': parse_time(r['event_peaktime']),
Expand Down Expand Up @@ -230,21 +231,21 @@ def calculate_temperature_em(goests, abundances="coronal",
>>> goests = ts.TimeSeries(GOES_XRS_TIMESERIES) # doctest: +REMOTE_DATA
>>> goests.data # doctest: +REMOTE_DATA
xrsa xrsb
2011-06-06 23:59:59.961999 1.000000e-09 1.887100e-07
2011-06-07 00:00:02.008999 1.000000e-09 1.834600e-07
2011-06-07 00:00:04.058999 1.000000e-09 1.860900e-07
2011-06-07 00:00:06.104999 1.000000e-09 1.808400e-07
2011-06-07 00:00:08.151999 1.000000e-09 1.860900e-07
2011-06-06 23:59:59.961999893 1.000000e-09 1.887100e-07
2011-06-07 00:00:02.008999944 1.000000e-09 1.834600e-07
2011-06-07 00:00:04.058999896 1.000000e-09 1.860900e-07
2011-06-07 00:00:06.104999900 1.000000e-09 1.808400e-07
2011-06-07 00:00:08.151999950 1.000000e-09 1.860900e-07
...

>>> goests_new = calculate_temperature_em(goests) # doctest: +REMOTE_DATA
>>> goests_new.data # doctest: +REMOTE_DATA
xrsa xrsb temperature em
2011-06-06 23:59:59.961999 1.000000e-09 1.887100e-07 3.503510 2.190626e+48
2011-06-07 00:00:02.008999 1.000000e-09 1.834600e-07 3.534262 2.055847e+48
2011-06-07 00:00:04.058999 1.000000e-09 1.860900e-07 3.518700 2.122771e+48
2011-06-07 00:00:06.104999 1.000000e-09 1.808400e-07 3.550100 1.990333e+48
2011-06-07 00:00:08.151999 1.000000e-09 1.860900e-07 3.518700 2.122771e+48
2011-06-06 23:59:59.961999893 1.000000e-09 1.887100e-07 3.503510 2.190626e+48
2011-06-07 00:00:02.008999944 1.000000e-09 1.834600e-07 3.534262 2.055847e+48
2011-06-07 00:00:04.058999896 1.000000e-09 1.860900e-07 3.518700 2.122771e+48
2011-06-07 00:00:06.104999900 1.000000e-09 1.808400e-07 3.550100 1.990333e+48
2011-06-07 00:00:08.151999950 1.000000e-09 1.860900e-07 3.518700 2.122771e+48
...

"""
Expand Down Expand Up @@ -294,7 +295,7 @@ def _goes_chianti_tem(longflux: u.W/u.m/u.m, shortflux: u.W/u.m/u.m, satellite=8
correct calibration of data.
Default=8

date : `datetime.datetime` or `str`
date : `astropy.time.Time` or `str`
Date when observations made. Important for correctcalibration.
Default=today

Expand Down Expand Up @@ -388,7 +389,7 @@ def _goes_chianti_tem(longflux: u.W/u.m/u.m, shortflux: u.W/u.m/u.m, satellite=8
# PREPARE DATA
# GOES 6 long channel flux before 1983-Jun-28 must be corrected by a
# factor of 4.43/5.32
if date < datetime.datetime(1983, 6, 28) and satellite == 6:
if date < parse_time((1983, 6, 28)) and satellite == 6:
longflux_corrected = longflux*(4.43/5.32)
else:
longflux_corrected = longflux
Expand Down Expand Up @@ -805,23 +806,23 @@ def calculate_radiative_loss_rate(goests, force_download=False,
>>> goests = ts.TimeSeries(GOES_XRS_TIMESERIES) # doctest: +REMOTE_DATA
>>> goests.data # doctest: +REMOTE_DATA
xrsa xrsb
2011-06-06 23:59:59.961999 1.000000e-09 1.887100e-07
2011-06-07 00:00:02.008999 1.000000e-09 1.834600e-07
2011-06-07 00:00:04.058999 1.000000e-09 1.860900e-07
2011-06-07 00:00:06.104999 1.000000e-09 1.808400e-07
2011-06-07 00:00:08.151999 1.000000e-09 1.860900e-07
2011-06-07 00:00:10.201999 1.000000e-09 1.808400e-07
2011-06-06 23:59:59.961999893 1.000000e-09 1.887100e-07
2011-06-07 00:00:02.008999944 1.000000e-09 1.834600e-07
2011-06-07 00:00:04.058999896 1.000000e-09 1.860900e-07
2011-06-07 00:00:06.104999900 1.000000e-09 1.808400e-07
2011-06-07 00:00:08.151999950 1.000000e-09 1.860900e-07
2011-06-07 00:00:10.201999903 1.000000e-09 1.808400e-07
...

>>> goests_new = calculate_radiative_loss_rate(goests) # doctest: +REMOTE_DATA
>>> goests_new.data # doctest: +REMOTE_DATA
xrsa xrsb temperature em rad_loss_rate
2011-06-06 23:59:59.961999 1.000000e-09 1.887100e-07 3.503510 2.190626e+48 1.781001e+19
2011-06-07 00:00:02.008999 1.000000e-09 1.834600e-07 3.534262 2.055847e+48 1.660031e+19
2011-06-07 00:00:04.058999 1.000000e-09 1.860900e-07 3.518700 2.122771e+48 1.719931e+19
2011-06-07 00:00:06.104999 1.000000e-09 1.808400e-07 3.550100 1.990333e+48 1.601718e+19
2011-06-07 00:00:08.151999 1.000000e-09 1.860900e-07 3.518700 2.122771e+48 1.719931e+19
2011-06-07 00:00:10.201999 1.000000e-09 1.808400e-07 3.550100 1.990333e+48 1.601718e+19
2011-06-06 23:59:59.961999893 1.000000e-09 1.887100e-07 3.503510 2.190626e+48 1.781001e+19
2011-06-07 00:00:02.008999944 1.000000e-09 1.834600e-07 3.534262 2.055847e+48 1.660031e+19
2011-06-07 00:00:04.058999896 1.000000e-09 1.860900e-07 3.518700 2.122771e+48 1.719931e+19
2011-06-07 00:00:06.104999900 1.000000e-09 1.808400e-07 3.550100 1.990333e+48 1.601718e+19
2011-06-07 00:00:08.151999950 1.000000e-09 1.860900e-07 3.518700 2.122771e+48 1.719931e+19
2011-06-07 00:00:10.201999903 1.000000e-09 1.808400e-07 3.550100 1.990333e+48 1.601718e+19
...

"""
Expand Down Expand Up @@ -883,7 +884,7 @@ def _calc_rad_loss(temp: u.MK, em: u.cm**-3, obstime=None, force_download=False,
at the same times corresponding to the temperatures in temp.
Must be same length as temp. Units=[cm**-3]

obstime : (optional) array-like of `datetime.datetime` objects
obstime : (optional) array-like of `~sunpy.time.parse_time` parsable objects
Array of measurement times to which temperature and
emission measure values correspond. Must be same length
as temp and em. If this keyword is set, the integrated
Expand Down Expand Up @@ -991,23 +992,14 @@ def _calc_rad_loss(temp: u.MK, em: u.cm**-3, obstime=None, force_download=False,
if len(obstime) != n:
raise IOError("obstime must have same number of elements as "
"temp and em.")
if type(obstime) == pandas.DatetimeIndex:
obstime = obstime.to_pydatetime
if any(type(obst) == str for obst in obstime):
parse_time(obstime)
if not all(type(obst) == datetime.datetime for obst in obstime):
raise TypeError("obstime must be an array-like whose elements are"
" convertible to datetime objects.")

obstime = parse_time(obstime)

# Check elements in obstime in chronological order
chrono_check = obstime-np.roll(obstime, 1)
chrono_check = chrono_check[1:]
if not all(chrono_check > datetime.timedelta(0)):
raise ValueError(
"Elements of obstime must be in chronological order.")
_assert_chrono_order(obstime)
# Next, get measurement times in seconds from time of first
# measurement.
obstime_seconds = np.array([(ot-obstime[0]).total_seconds()
for ot in obstime], dtype="float64")
obstime_seconds = (obstime - obstime[0]).sec
# Finally, integrate using trapezoid rule
rad_loss_int = trapz(rad_loss.value, obstime_seconds)
rad_loss_int = u.Quantity(rad_loss_int, unit=rad_loss.unit*u.s)
Expand Down Expand Up @@ -1063,22 +1055,22 @@ def calculate_xray_luminosity(goests):
>>> goests = ts.TimeSeries(GOES_XRS_TIMESERIES) # doctest: +REMOTE_DATA
>>> goests.data # doctest: +REMOTE_DATA
xrsa xrsb
2011-06-06 23:59:59.961999 1.000000e-09 1.887100e-07
2011-06-07 00:00:02.008999 1.000000e-09 1.834600e-07
2011-06-07 00:00:04.058999 1.000000e-09 1.860900e-07
2011-06-07 00:00:06.104999 1.000000e-09 1.808400e-07
2011-06-07 00:00:08.151999 1.000000e-09 1.860900e-07
2011-06-06 23:59:59.961999893 1.000000e-09 1.887100e-07
2011-06-07 00:00:02.008999944 1.000000e-09 1.834600e-07
2011-06-07 00:00:04.058999896 1.000000e-09 1.860900e-07
2011-06-07 00:00:06.104999900 1.000000e-09 1.808400e-07
2011-06-07 00:00:08.151999950 1.000000e-09 1.860900e-07
...


>>> goests_new = calculate_xray_luminosity(goests) # doctest: +REMOTE_DATA
>>> goests_new.data # doctest: +REMOTE_DATA
xrsa xrsb luminosity_xrsa luminosity_xrsb
2011-06-06 23:59:59.961999 1.000000e-09 1.887100e-07 2.896209e+14 5.465435e+16
2011-06-07 00:00:02.008999 1.000000e-09 1.834600e-07 2.896209e+14 5.313384e+16
2011-06-07 00:00:04.058999 1.000000e-09 1.860900e-07 2.896209e+14 5.389555e+16
2011-06-07 00:00:06.104999 1.000000e-09 1.808400e-07 2.896209e+14 5.237503e+16
2011-06-07 00:00:08.151999 1.000000e-09 1.860900e-07 2.896209e+14 5.389555e+16
2011-06-06 23:59:59.961999893 1.000000e-09 1.887100e-07 2.896209e+14 5.465435e+16
2011-06-07 00:00:02.008999944 1.000000e-09 1.834600e-07 2.896209e+14 5.313384e+16
2011-06-07 00:00:04.058999896 1.000000e-09 1.860900e-07 2.896209e+14 5.389555e+16
2011-06-07 00:00:06.104999900 1.000000e-09 1.808400e-07 2.896209e+14 5.237503e+16
2011-06-07 00:00:08.151999950 1.000000e-09 1.860900e-07 2.896209e+14 5.389555e+16
...

"""
Expand Down Expand Up @@ -1122,12 +1114,12 @@ def _goes_lx(longflux, shortflux, obstime=None, date=None):
Array containing the observed GOES/XRS short channel flux.
Units=[W/m**2]

obstime : (optional) array-like of `datetime.datetime` objects
obstime : (optional) array-like of `~sunpy.time.parse_time` parsable objects
Measurement times corresponding to each flux measurement.
Assumes each pair of 0.5-4 and 1-8 angstrom flux measurements
were taken simultaneously.

date : (optional) `datetime.datetime` object or valid date string.
date : (optional) `astropy.time.Time` object or valid date string.
Date at which measurements were taken. This is used to
calculate the Sun-Earth distance.
Default=None implies Sun-Earth distance is set to 1AU.
Expand Down Expand Up @@ -1155,16 +1147,16 @@ def _goes_lx(longflux, shortflux, obstime=None, date=None):
Examples
--------
>>> from sunpy.instr.goes import _goes_lx
>>> from datetime import datetime
>>> from astropy.time import Time
>>> from astropy.units.quantity import Quantity
>>> longflux = Quantity([7e-6,7e-6,7e-6,7e-6,7e-6,7e-6], unit='W/m**2')
>>> shortflux = Quantity([7e-7,7e-7,7e-7,7e-7,7e-7,7e-7], unit='W/m**2')
>>> obstime = np.array([datetime(2014,1,1,0,0,0),
... datetime(2014,1,1,0,0,2),
... datetime(2014,1,1,0,0,4),
... datetime(2014,1,1,0,0,6),
... datetime(2014,1,1,0,0,8),
... datetime(2014,1,1,0,0,10),], dtype=object)
>>> obstime = Time(['2014-1-1T0:0:0',
... '2014-1-1T0:0:2',
... '2014-1-1T0:0:4',
... '2014-1-1T0:0:6',
... '2014-1-1T0:0:8',
... '2014-1-1T0:0:10'])
>>> lx_out = _goes_lx(longflux, shortflux, obstime) # doctest: +REMOTE_DATA
>>> lx_out["longlum"] # doctest: +REMOTE_DATA
<Quantity [1.96860565e+18, 1.96860565e+18, 1.96860565e+18, 1.96860565e+18,
Expand All @@ -1190,23 +1182,16 @@ def _goes_lx(longflux, shortflux, obstime=None, date=None):
if not len(longflux) == len(shortflux) == len(obstime):
raise ValueError("longflux, shortflux, and obstime must all have "
"same number of elements.")
if type(obstime) == pandas.DatetimeIndex:
obstime = obstime.to_pydatetime
if any(type(obst) == str for obst in obstime):
parse_time(obstime)
if not all(type(obst) == datetime.datetime for obst in obstime):
raise TypeError("obstime must be an array-like whose elements are"
" convertible to datetime objects.")

obstime = parse_time(obstime)

# Check elements in obstime in chronological order
chrono_check = obstime-np.roll(obstime, 1)
chrono_check = chrono_check[1:]
if not all(chrono_check > datetime.timedelta(0)):
raise ValueError(
"Elements of obstime must be in chronological order.")
_assert_chrono_order(obstime)

# Next, get measurement times in seconds from time of first
# measurement.
obstime_seconds = np.array([(ot-obstime[0]).total_seconds()
for ot in obstime], dtype="float64")
obstime_seconds = (obstime - obstime[0]).sec

# Finally, integrate using trapezoid rule
longlum_int = trapz(longlum.value, obstime_seconds)
longlum_int = u.Quantity(longlum_int, unit=longlum.unit*u.s)
Expand Down Expand Up @@ -1245,7 +1230,7 @@ def _calc_xraylum(flux: u.W/u.m/u.m, date=None):
flux : `~astropy.units.Quantity`
Containing the observed solar flux. Units=[W/m**2]

date : (optional) `datetime.datetime` object or valid date string
date : (optional) `astropy.time.Time` object or valid date string
Used to calculate a more accurate Sun-Earth distance based on
Earth's orbit at that date. If date is None, Sun-Earth
distance is set to 1AU.
Expand Down Expand Up @@ -1369,3 +1354,11 @@ def flux_to_flareclass(goesflux: u.watt/u.m**2):
str_class = conversion_dict.get(u.Quantity(10 ** decade, "W/m**2"))
goes_subclass = 10 ** -decade * goesflux.to('W/m**2').value
return "{0}{1:.3g}".format(str_class, goes_subclass)


def _assert_chrono_order(obstime):
chrono_check = np.array(obstime) - np.roll(obstime, 1)
chrono_check = chrono_check[1:]
if not all(val > TimeDelta(0*u.day) for val in chrono_check):
raise ValueError(
"Elements of obstime must be in chronological order.")
2 changes: 1 addition & 1 deletion sunpy/instr/iris/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def SJI_to_cube(filename, start=0, stop=None, hdu=0):
# Set the date/time

for i, m in enumerate(iris_cube):
m.meta['DATE-OBS'] = splits[i].center.isoformat()
m.meta['DATE-OBS'] = splits[i].center.iso

return iris_cube
Loading