@author: stuart """ import os import tempfile import datetime import astropy.table import astropy.time import astropy.units as u import pytest from sunpy.time import parse_time from sunpy.net.jsoc import JSOCClient, JSOCResponse from sunpy.net.vso.vso import Results import sunpy.net.jsoc.attrs as attrs client = JSOCClient() def test_jsocresponse_double(): j1 = JSOCResponse(table=astropy.table.Table(data=[[1, 2, 3, 4]])) j1.append(astropy.table.Table(data=[[1, 2, 3, 4]])) assert isinstance(j1, JSOCResponse) assert all(j1.table == astropy.table.vstack([ astropy.table.Table(data=[[1, 2, 3, 4]]), astropy.table.Table(data=[[1, 2, 3, 4]]) ])) def test_jsocresponse_single(): j1 = JSOCResponse(table=None) assert len(j1) == 0
def main(): path = Path('~/sunpy/data/jsocflare/').expanduser() files = glob.glob(str(path / '*.fits')) # requestid = 'JSOC_20180831_1097' requestid = None if not files: if requestid: c = JSOCClient() filesd = c.get_request(requestid, path=str(path), overwrite=False).wait() files = [] for f in filesd.values(): files.append(f['path']) else: results = Fido.search( a.jsoc.Time('2017-09-06T12:00:00', '2017-09-06T12:02:00'), a.jsoc.Series('aia.lev1_euv_12s'), a.jsoc.Segment('image'), a.jsoc.Notify("*****@*****.**")) print(results) files = Fido.fetch(results, path=str(path)) files.sort() files = np.array(files) # For each image get: # the index inds = [] # the time times = [] # the dt from the first image seconds = [] # the wavelength waves = [] for i, filepath in enumerate(files): with fits.open(filepath) as hdul: header = hdul[1].header time = parse_time(header['DATE-OBS']) if i == 0: root_header = header start_time = time inds.append(i) times.append(time) seconds.append((time - start_time).total_seconds()) waves.append(header['WAVELNTH']) # Construct an array and sort it by wavelength and time arr = np.array((inds, seconds, waves)).T sorter = np.lexsort((arr[:, 1], arr[:, 2])) # Using this double-sorted array get the list indicies list_sorter = np.array(arr[sorter][:, 0], dtype=int) # Calculate the desired shape of the output array n_waves = len(list(set(waves))) shape = (n_waves, len(files) // n_waves) # Construct a 2D array of filenames cube = files[list_sorter].reshape(shape) # Extract a list of coordinates in time and wavelength # this assumes all wavelength images are taken at the same time time_coords = np.array([t.isoformat() for t in times])[list_sorter].reshape(shape)[0, :] wave_coords = np.array(waves)[list_sorter].reshape(shape)[:, 0] smap0 = sunpy.map.Map(files[0]) spatial = map_to_transform(smap0) timemodel = generate_lookup_table(lookup_table=seconds[:shape[1]] * u.s) wavemodel = generate_lookup_table(lookup_table=waves[:shape[0]] * u.AA) hcubemodel = spatial & timemodel & wavemodel wave_frame = cf.SpectralFrame(axes_order=(3, ), unit=u.AA, name="wavelength", axes_names=("wavelength", )) time_frame = cf.TemporalFrame(axes_order=(2, ), unit=u.s, reference_time=Time(time_coords[0]), name="time", axes_names=("time", )) sky_frame = cf.CelestialFrame(axes_order=(0, 1), name='helioprojective', reference_frame=smap0.coordinate_frame, axes_names=("helioprojective longitude", "helioprojective latitude")) sky_frame = cf.CompositeFrame([sky_frame, time_frame, wave_frame]) detector_frame = cf.CoordinateFrame(name="detector", naxes=4, axes_order=(0, 1, 2, 3), axes_type=("pixel", "pixel", "pixel", "pixel"), axes_names=("x", "y", "time", "wavelength"), unit=(u.pix, u.pix, u.pix, u.pix)) wcs = gwcs.wcs.WCS(forward_transform=hcubemodel, input_frame=detector_frame, output_frame=sky_frame) print(repr(wcs)) print(wcs(*[1 * u.pix] * 4, with_units=True)) ea = references_from_filenames(cube, relative_to=str(path)) tree = { 'gwcs': wcs, 'dataset': ea, } with asdf.AsdfFile(tree) as ff: # ff.write_to("test.asdf") filename = str(path / "aia_{}.asdf".format(time_coords[0])) ff.write_to(filename) print("Saved to : {}".format(filename)) # import sys; sys.exit(0) from dkist.dataset import Dataset ds = Dataset.from_directory(str(path)) print(repr(ds)) print(repr(ds.wcs)) print(ds.wcs(*[1 * u.pix] * 4, with_units=True))
def test_can_handle_query_no_series(): assert not JSOCClient._can_handle_query(a.Time("2020/01/02", "2020/01/03")) assert not JSOCClient._can_handle_query(a.Wavelength(17.1 * u.nm)) assert JSOCClient._can_handle_query(a.jsoc.Series("hmi.M_45s"))
def client(): return JSOCClient()
#!/bin/env python import sunpy from sunpy.net.jsoc import JSOCClient from sunpy.net.vso import VSOClient print(f"Updating the attrs json files using sunpy {sunpy.__version__}...") print("Updating VSO json...") VSOClient.create_parse_vso_values() print("Updating JSOC json...\nThis may take some time...") JSOCClient.create_parse_jsoc_values() print("Done. Don't forget to update the doctests.")