def __init__(self, start, end, near=None): self.start = anytim(start) self.end = anytim(end) self.near = None if near is None else anytim(near) _Range.__init__(self, start, end, self.__class__) Attr.__init__(self)
def test_anytim_ISO(): assert util.anytim('1966-02-03') == LANDING assert ( util.anytim('1966-02-03T20:17:40') == datetime(1966, 2, 3, 20, 17, 40) ) assert ( util.anytim('19660203T201740') == datetime(1966, 2, 3, 20, 17, 40) ) lst = [ ('2007-05-04T21:08:12.999999', datetime(2007, 5, 4, 21, 8, 12, 999999)), ('20070504T210812.999999', datetime(2007, 5, 4, 21, 8, 12, 999999)), ('2007/05/04 21:08:12.999999', datetime(2007, 5, 4, 21, 8, 12, 999999)), ('2007-05-04 21:08:12.999999', datetime(2007, 5, 4, 21, 8, 12, 999999)), ('2007/05/04 21:08:12', datetime(2007, 5, 4, 21, 8, 12)), ('2007-05-04 21:08:12', datetime(2007, 5, 4, 21, 8, 12)), ('2007-05-04 21:08', datetime(2007, 5, 4, 21, 8)), ('2007-05-04T21:08:12', datetime(2007, 5, 4, 21, 8, 12)), ('20070504T210812', datetime(2007, 5, 4, 21, 8, 12)), ('2007-May-04 21:08:12', datetime(2007, 5, 4, 21, 8, 12)), ('2007-May-04 21:08', datetime(2007, 5, 4, 21, 8)), ('2007-May-04', datetime(2007, 5, 4)), ('2007-05-04', datetime(2007, 5, 4)), ('2007/05/04', datetime(2007, 5, 4)), ('20070504_210812', datetime(2007, 5, 4, 21, 8, 12)), ] for k, v in lst: assert util.anytim(k) == v
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" properties = BaseMap.get_properties() properties.update({ 'date': util.anytim(header.get('date_obs')), 'det': header.get('telescop'), 'inst': header.get('telescop'), 'meas': [header.get('energy_l'), header.get('energy_h')], 'obs': header.get('telescop'), 'name': "RHESSI " + str(header.get('energy_l')) + '-' + str(header.get('energy_h')) + ' keV', 'cmap': cm.get_cmap(name = 'rhessi'), 'exptime': (util.anytim(header.get('date_end')) - util.anytim(header.get('date_obs'))).seconds }) return properties
def download(self): """ Function to download LYRA data, and/or set the filename where it can be found""" self.time = anytim(self.time) # date-based subdirectory dateLocation = self.time.strftime('%Y/%m/%d/') # date-based filename dateFilename = self.time.strftime('%Y%m%d-') # extension to the file name extension = '000000_lev'+ str(self.level) + '_' + self.dataType + '.fits' # calculated file name requestedLocation = self.location + dateLocation requestedFile = self.prefix + dateFilename + extension f = os.path.expanduser(self.downloadto) + os.sep + requestedFile isAlreadyThere = os.path.isfile(f) if isAlreadyThere: if self.verbose: print('File '+ f + ' already exists.') self.filename = f else: self.filename = wgetDownload(requestedLocation,requestedFile, self.downloadto)
def __init__(self, a, b = None): if b is None: if isinstance(a[0],str) or isinstance(a[0], float): self.t1 = anytim(a[0]) if isinstance(a[1],str) or isinstance(a[1], float): self.t2 = anytim(a[1]) if type(a[1]) == type(datetime.timedelta(1)): self.t2 = self.t1 + a[1] if isinstance(a[1], int): self.t2 = self.t1 + datetime.timedelta(0,a[1]) else: if isinstance(a,str) or isinstance(a, float): self.t1 = anytim(a) if isinstance(b,str) or isinstance(b, float): self.t2 = anytim(b) if type(b) == type(datetime.timedelta(1)): self.t2 = self.t1 + b if isinstance(b, int): self.t2 = self.t1 + datetime.timedelta(0,b) self.dt = self.t2 - self.t1
def __init__(self,time): self.verbose = False self.filename = None self.data = None self.columns = None self.level = 2 self.dataType = 'std' self.downloadto = os.path.expanduser('~')+os.sep self.location = 'http://proba2.oma.be/lyra/data/bsd/' self.prefix = 'lyra_' self.time = anytim(time) self.tstart = self.time self.tend = self.tstart + datetime.timedelta(days = 1) self.nt = None
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" # Note: Trailing "Z" in date was dropped on 2010/12/07 properties = BaseMap.get_properties() properties.update({ 'date': util.anytim(header['date-obs'][0:22]), 'det': "AIA", 'inst': "AIA", 'meas': header['wavelnth'], 'obs': "SDO", 'name': "AIA %s" % header['wavelnth'], 'cmap': cm.get_cmap(name = 'sdoaia' + str(header['wavelnth'])) }) return properties
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" properties = BaseMap.get_properties() properties.update({ 'date': util.anytim(header.get('date_obs')), 'det': header.get('telescop'), 'inst': header.get('telescop'), 'meas': [header.get('energy_l'), header.get('energy_h')], 'obs': header.get('telescop'), 'name': "RHESSI " + str(header.get('energy_l')) + '-' + str(header.get('energy_h')) + ' keV', 'cmap': cm.get_cmap(name = 'rhessi'), # 'norm': mpl.colors.Normalize(vmin=cls.min(), vmax=cls.max()) }) return properties
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" datestr = "%sT%s" % (header['date_obs'], header['time_obs']) properties = BaseMap.get_properties() properties.update({ "date": util.anytim(datestr), "det": header.get('detector'), "inst": "LASCO", "meas": header.get('wavelnth'), "obs": "SOHO", "name": "LASCO %s" % header.get('detector'), "exptime": header.get("exptime"), }) return properties
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" # Note: Trailing "Z" in date was dropped on 2010/12/07 meas = header['content'].split(" ")[0].lower() properties = BaseMap.get_properties() properties.update({ "date": util.anytim(header.get('date-obs')), "det": "HMI", "inst": "HMI", "meas": meas, "obs": "SDO", "name": "HMI %s" % meas, "exptime": header.get('exptime') }) return properties
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" #date_format = "%Y-%m-%dT%H:%M:%S.%fZ" properties = BaseMap.get_properties() properties.update({ "date": util.anytim(header.get('date_obs')), "det": "EIT", "inst": "EIT", "meas": header.get('wavelnth'), "obs": "SOHO", "name": "EIT %s" % header.get('wavelnth'), "exptime": header.get('exptime'), 'cmap': cm.get_cmap(name='sohoeit' + str(header.get('wavelnth'))), }) return properties
def print_params(t=None): """Print out a summary of Solar ephemeris""" time = util.anytim(t) print('Solar Ephemeris for ' + time.ctime()) print('') print('Distance (AU) = ' + str(sunearth_distance(t))) print('Semidiameter (arc sec) = ' + str(angular_size(t))) print('True (long,lat) in degrees = (' + str(true_longitude(t)) + ',' + str(true_latitude(t)) + ')') print('Apparent (long, lat) in degrees = (' + str(apparent_longitude(t)) + ',' + str(apparent_latitude(t)) + ')') print('True (RA, Dec) = (' + str(true_rightascenscion(t)) + ',' + str(true_declination(t))) print('Apparent (RA, Dec) = (' + str(apparent_rightascenscion(t)) + ',' + str(apparent_declination(t))) print('Heliographic long. and lat of disk center in deg = (' + str(heliographic_solar_center(t)) + ')') print('Position angle of north pole in deg = ' + str(solar_north(t))) print('Carrington Rotation Number = ' + str(carrington_rotation_number(t)))
def get_closest_image(date, observatory, instrument, detector, measurement): """Finds the closest image available for the specified source and date. For more information on what types of requests are available and the expected usage for the response, consult the Helioviewer API documenation: http://helioviewer.org/api Parameters ---------- date : mixed A string or datetime object for the desired date of the image observatory : string The observatory to match instrument : string The instrument to match detector : string The detector to match measurement : string Returns ------- out : dict A dictionary including the following information: filepath filename date scale width height sunCenterX sunCenterY Examples -------- >>> """ # TODO 06/26/2011 Input validation params = { "date": util.anytim(date), "observatory": observatory, "instrument": instrument, "detector": detector, "measurement": measurement } return _request(params)
def get_jp2_image(date, directory=None, **kwargs): """ Downloads the JPEG 2000 that most closely matches the specified time and data source. Parameters ---------- date : mixed A string or datetime object for the desired date of the image Returns ------- string : The filepath to the JP2 image or a URL if the jpip parameter was set to True. """ params = { "action": "getJP2Image", "date": util.anytim(date).strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + "Z" } params.update(kwargs) # Submit request response = urllib2.urlopen(__BASE_API_URL__, urllib.urlencode(params)) # JPIP URL response if 'jpip' in kwargs: return response.read() # JPEG 2000 image response if directory is None: import tempfile directory = tempfile.gettempdir() filename = response.info()['Content-Disposition'][22:-1] filepath = os.path.join(directory, filename) f = open(filepath, 'wb') f.write(response.read()) return filepath
def get_properties(cls, header): """Returns the default and normalized values to use for the Map""" # MDI sometimes has an "60" in seconds field datestr = header['date_obs'] if datestr[17:19] == "60": datestr = datestr[:17] + "30" + datestr[19:] # Determine measurement dpcobsr = header.get('dpc_obsr') meas = "magnetogram" if dpcobsr.find('Mag') != -1 else "continuum" properties = BaseMap.get_properties() properties.update({ "date": util.anytim(datestr), "det": "MDI", "inst": "MDI", "meas": meas, "obs": "SOHO", "name": "MDI %s" % meas, "exptime": header.get('exptime'), "r_sun": header.get('radius') }) return properties
def test_anytim_tuple(): assert util.anytim((1966, 2, 3)) == LANDING
def simulate_raw(params, verbose = False): """ Simulate data in HG' coordinates HG' = HG, except center at wave epicenter """ from sunpy.util import util import datetime from scipy.special import ndtr cadence = params["cadence"] direction = 180.+params["direction"] width_coeff = prep_coeff(params["width"]) wave_thickness_coeff = prep_coeff(params["wave_thickness"]) wave_normalization_coeff = prep_coeff(params["wave_normalization"]) speed_coeff = prep_coeff(params["speed"]) steps = params["max_steps"] lat_min = params["lat_min"] lat_max = params["lat_max"] lat_bin = params["lat_bin"] lon_min = params["lon_min"] lon_max = params["lon_max"] lon_bin = params["lon_bin"] #This roundabout approach recalculates lat_bin and lon_bin to produce #equally sized bins to exactly span the min/max ranges lat_num = int(round((lat_max-lat_min)/lat_bin)) lat_edges, lat_bin = np.linspace(lat_min, lat_max, lat_num+1, retstep=True) lon_num = int(round((lon_max-lon_min)/lon_bin)) lon_edges, lon_bin = np.linspace(lon_min, lon_max, lon_num+1, retstep=True) #Propagates from 90. down to lat_min, irrespective of lat_max p = np.poly1d([speed_coeff[2]/3., speed_coeff[1]/2., speed_coeff[0], -(90.-lat_min)]) #Will fail if wave does not propogate all the way to lat_min #duration = p.r[np.logical_and(p.r.real > 0, p.r.imag == 0)][0] #steps = int(duration/cadence)+1 #if steps > params["max_steps"]: # steps = params["max_steps"] #Maybe used np.poly1d() instead to do the polynomial calculation? time = np.arange(steps)*cadence time_powers = np.vstack((time**0, time**1, time**2)) width = np.dot(width_coeff, time_powers).ravel() wave_thickness = np.dot(wave_thickness_coeff, time_powers).ravel() wave_normalization = np.dot(wave_normalization_coeff, time_powers).ravel() #Propagates from 90., irrespective of lat_max wave_peak = 90.-(p(time)+(90.-lat_min)) out_of_bounds = np.logical_or(wave_peak < lat_min, wave_peak > lat_max) if out_of_bounds.any(): steps = np.where(out_of_bounds)[0][0] wave_maps = [] dict_header = { "CDELT1": lon_bin, "NAXIS1": lon_num, "CRVAL1": lon_min, "CRPIX1": 0.5, #this makes lon_min the left edge of the first bin "CUNIT1": "deg", "CTYPE1": "HG", "CDELT2": lat_bin, "NAXIS2": lat_num, "CRVAL2": lat_min, "CRPIX2": 0.5, #this makes lat_min the left edge of the first bin "CUNIT2": "deg", "CTYPE2": "HG", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": 963.879683, "RSUN_REF": 696000000.0, "DSUN_OBS": 148940609626.98 } header = sunpy.map.MapHeader(dict_header) if verbose: print("Simulating "+str(steps)+" raw maps") for istep in xrange(steps): #Gaussian profile in longitudinal direction #Does not take into account spherical geometry #(i.e., change in area element) if (wave_thickness[istep] <= 0): print("ERROR: wave thickness is non-physical!") z = (lat_edges-wave_peak[istep])/wave_thickness[istep] wave_1d = wave_normalization[istep]*(ndtr(np.roll(z, -1))-ndtr(z))[0:lat_num] wave_1d /= lat_bin wave_lon_min = direction-width[istep]/2 wave_lon_max = direction+width[istep]/2 if (width[istep]< 360.): #Do these need to be np.remainder() instead? wave_lon_min_mod = ((wave_lon_min+180.) % 360.)-180. wave_lon_max_mod = ((wave_lon_max+180.) % 360.)-180. index1 = np.arange(lon_num+1)[np.roll(lon_edges, -1) > min(wave_lon_min_mod, wave_lon_max_mod)][0] index2 = np.roll(np.arange(lon_num+1)[lon_edges < max(wave_lon_min_mod, wave_lon_max_mod)], 1)[0] wave_lon = np.zeros(lon_num) wave_lon[index1+1:index2] = 1. #Possible weirdness if index1 == index2 wave_lon[index1] += (lon_edges[index1+1]-min(wave_lon_min_mod, wave_lon_max_mod))/lon_bin wave_lon[index2] += (max(wave_lon_min_mod, wave_lon_max_mod)-lon_edges[index2])/lon_bin if (wave_lon_min_mod > wave_lon_max_mod): wave_lon = 1.-wave_lon else: wave_lon = np.ones(lon_num) #Could be accomplished with np.dot() without casting as matrices? wave = np.mat(wave_1d).T*np.mat(wave_lon) wave_maps += [sunpy.map.BaseMap(wave, header)] wave_maps[istep].name = "Simulation" wave_maps[istep].date = util.anytim("2011-11-11")+datetime.timedelta(0, istep*cadence) return wave_maps
def test_anytim_int(): assert util.anytim(765548612.0) == datetime(2003, 4, 5, 12, 23, 32) assert util.anytim(1009685652.0) == datetime(2010, 12, 30, 4, 14, 12)
def _a(wlk, root, state, dct): dct["event_starttime"] = anytim(root.start).strftime("%Y-%m-%dT%H:%M:%S") dct["event_endtime"] = anytim(root.end).strftime("%Y-%m-%dT%H:%M:%S") return dct
def solar_cycle_number(t=None): time = util.anytim(t) result = (time.year + 8) % 28 + 1 return result
def _a(wlk, root, state, dct): dct['event_starttime'] = anytim(root.start).strftime('%Y-%m-%dT%H:%M:%S') dct['event_endtime'] = anytim(root.end).strftime('%Y-%m-%dT%H:%M:%S') return dct
def query_legacy(self, tstart=None, tend=None, **kwargs): """ Query data from the VSO mocking the IDL API as close as possible. Either tstart and tend or date_start and date_end or date have to be supplied. Parameters ---------- tstart : datetime.datetime Start of the time-range in which records are searched. tend : datetime.datetime Start of the time-range in which records are searched. date : str (start date) - (end date) start_date : datetime the start date end_date : datetime the end date wave : str (min) - (max) (unit) min_wave : str minimum spectral range max_wave : str maximum spectral range unit_wave : str spectral range units (Angstrom, GHz, keV) extent : str VSO 'extent type' ... (FULLDISK, CORONA, LIMB, etc) physobj : str VSO 'physical observable' provider : str VSO ID for the data provider (SDAC, NSO, SHA, MSU, etc) source : str spacecraft or observatory (SOHO, YOHKOH, BBSO, etc) synonyms : spacecraft, observatory instrument : str instrument ID (EIT, SXI-0, SXT, etc) synonyms : telescope, inst detector : str detector ID (C3, EUVI, COR2, etc.) layout : str layout of the data (image, spectrum, time_series, etc.) level : str level of the data product (numeric range, see below) pixels : str number of pixels (numeric range, see below) resolution : str effective resolution (1 = full, 0.5 = 2x2 binned, etc) numeric range, see below. pscale : str pixel scale, in arcseconds (numeric range, see below) near_time : datetime return record closest to the time. See below. sample : int attempt to return only one record per SAMPLE seconds. See below. Numeric Ranges: - May be entered as a string or any numeric type for equality matching - May be a string of the format '(min) - (max)' for range matching - May be a string of the form '(operator) (number)' where operator is one of: lt gt le ge < > <= >= Examples -------- Query all data from eit between 2010-01-01T00:00 and 2010-01-01T01:00. >>> qr = client.query_legacy( ... datetime(2010, 1, 1), datetime(2010, 1, 1, 1), instrument='eit') Returns ------- out : :py:class:`QueryResult` (enhanced list) of matched items. Return value of same type as the one of :py:class:`VSOClient.query`. """ sdk = lambda key: lambda value: {key: value} ALIASES = { 'wave_min': sdk('wave_wavemin'), 'wave_max': sdk('wave_wavemax'), 'wave_type': sdk('wave_wavetype'), 'wave_unit': sdk('wave_waveunit'), 'min_wave': sdk('wave_wavemin'), 'max_wave': sdk('wave_wavemax'), 'type_wave': sdk('wave_wavetype'), 'unit_wave': sdk('wave_waveunit'), 'wave': _parse_waverange, 'inst': sdk('instrument'), 'telescope': sdk('instrument'), 'spacecraft': sdk('source'), 'observatory': sdk('source'), 'start_date': sdk('time_start'), 'end_date': sdk('time_end'), 'start': sdk('time_start'), 'end': sdk('time_end'), 'near_time': sdk('time_near'), 'date': _parse_date, 'layout': sdk('datatype'), } kwargs.update({'time_start': tstart, 'time_end': tend}) queryreq = self.api.factory.create('QueryRequest') for key, value in kwargs.iteritems(): if key.startswith('time'): value = anytim(value).strftime(TIMEFORMAT) for k, v in ALIASES.get(key, sdk(key))(value).iteritems(): attr = k.split('_') lst = attr[-1] rest = attr[:-1] # pylint: disable=E1103 item = queryreq.block for elem in rest: try: item = item[elem] except KeyError: raise ValueError("Unexpected argument %s." % key) if lst not in item: raise ValueError("Unexpected argument %s." % key) if item[lst]: raise ValueError("Got multiple values for %s." % k) item[lst] = v return QueryResponse.create(self.api.service.Query(queryreq))