def translate_and_query(self, hek_results, limit=None, full_query=False, use_progress_bar=True): """ Translates HEK results, makes a VSO query, then returns the results. Takes the results from a HEK query, translates them, then makes a VSO query, returning the results in a list organized by their corresponding HEK query. Parameters ---------- hek_results: sunpy.net.hek.hek.Response or list of sunpy.-.-.-.Response The results from a HEK query in the form of a list. limit: int An approximate limit to the desired number of VSO results. full_query: Boolean A simple flag that determines if the method is being called from the full_query() method. use_progress_bar: Boolean A flag to turn off the progress bar, defaults to "on" Examples -------- >>> from sunpy.net import hek, hek2vso >>> h = hek.HEKClient() >>> tstart = '2011/08/09 07:23:56' >>> t_end = '2011/08/09 12:40:29' >>> t_event = 'FL' >>> q = h.query(hek.attrs.Time(tstart, tend), hek.attts.EventType(event_type)) >>> h2v = hek2vso.H2VClient() >>> res = h2v.translate_and_query(q) """ if full_query is False: self.quick_clean() self.hek_results = hek_results vso_query = translate_results_to_query(hek_results) result_size = len(vso_query) for query in vso_query: if use_progress_bar: pbar = TTYProgressBar('Querying VSO webservice', result_size) temp = self.vso_client.query(*query) self.vso_results.append(temp) self.num_of_records += len(temp) if limit is not None: if self.num_of_records >= limit: break pbar.poke() if use_progress_bar: pbar.finish() return self.vso_results
def translate_and_query(self, hek_results, limit=None, progress=False): """ Translates HEK results, makes a VSO query, then returns the results. Takes the results from a HEK query, translates them, then makes a VSO query, returning the results in a list organized by their corresponding HEK query. Parameters ---------- hek_results: sunpy.net.hek.hek.Response or list of Responses The results from a HEK query in the form of a list. limit: int An approximate limit to the desired number of VSO results. progress: Boolean A flag to turn off the progress bar, defaults to "off" Examples -------- >>> from sunpy.net import hek, hek2vso >>> h = hek.HEKClient() >>> tstart = '2011/08/09 07:23:56' >>> tend = '2011/08/09 12:40:29' >>> event_type = 'FL' >>> q = h.query(hek.attrs.Time(tstart, tend), hek.attts.EventType(event_type)) >>> h2v = hek2vso.H2VClient() >>> res = h2v.translate_and_query(q) """ vso_query = translate_results_to_query(hek_results) result_size = len(vso_query) if progress: sys.stdout.write('\rQuerying VSO webservice') sys.stdout.flush() pbar = TTYProgressBar(result_size) for query in vso_query: temp = self.vso_client.query(*query) self.vso_results.append(temp) self.num_of_records += len(temp) if limit is not None: if self.num_of_records >= limit: break if progress: pbar.poke() if progress: pbar.finish() return self.vso_results
def _gaussian_fits(self, line_guess=None, *extra_lines, **kwargs): """ Returns an array of fit objects from which parameters can be extracted corresponding to the line guesses provided. Parameters ---------- line_guess and extra_lines: 3-tuples of floats There must be at least one guess, in the format (intensity, position, stddev). The closer these guesses are to the true values the better the fit will be. If left to None, each of the individual spectra will come up with their own guesses. This only works for cubes with clean, single-line spectra. recalc=False: boolean If True, the gaussian fits will be recalculated, even if there's an existing fit for the given wavelengths already in the memo. This keyword should be set to True if changing the amplitude or width of the fit. **kwargs: dict Extra keyword arguments are ultimately passed on to the astropy fitter. """ recalc = kwargs.pop('recalc', False) if line_guess is None: key = 'auto' else: key = tuple([guess[1] for guess in (line_guess, ) + extra_lines]) if recalc or key not in self._memo: gaussian_array = np.empty(self.spectra.shape, dtype=object) bar = PB(self.spectra.shape[0] * self.spectra.shape[1]) drawbar = kwargs.pop('progress_bar', False) for i in range(self.spectra.shape[0]): for j in range(self.spectra.shape[1]): fit = self.spectra[i, j].gaussian_fit(line_guess, *extra_lines, **kwargs) gaussian_array[i, j] = fit if drawbar: bar.poke() self._memo[key] = gaussian_array bar.finish() return gaussian_array else: return self._memo[key]
def _gaussian_fits(self, line_guess=None, *extra_lines, **kwargs): """ Returns an array of fit objects from which parameters can be extracted corresponding to the line guesses provided. Parameters ---------- line_guess and extra_lines: 3-tuples of floats There must be at least one guess, in the format (intensity, position, stddev). The closer these guesses are to the true values the better the fit will be. If left to None, each of the individual spectra will come up with their own guesses. This only works for cubes with clean, single-line spectra. recalc=False: boolean If True, the gaussian fits will be recalculated, even if there's an existing fit for the given wavelengths already in the memo. This keyword should be set to True if changing the amplitude or width of the fit. **kwargs: dict Extra keyword arguments are ultimately passed on to the astropy fitter. """ recalc = kwargs.pop('recalc', False) if line_guess is None: key = 'auto' else: key = tuple([guess[1] for guess in (line_guess,) + extra_lines]) if recalc or key not in self._memo: gaussian_array = np.empty(self.spectra.shape, dtype=object) bar = PB(self.spectra.shape[0] * self.spectra.shape[1]) drawbar = kwargs.pop('progress_bar', False) for i in range(self.spectra.shape[0]): for j in range(self.spectra.shape[1]): fit = self.spectra[i, j].gaussian_fit(line_guess, *extra_lines, **kwargs) gaussian_array[i, j] = fit if drawbar: bar.poke() self._memo[key] = gaussian_array bar.finish() return gaussian_array else: return self._memo[key]
def wait(self, timeout=100, progress=False): """ Wait for result to be complete and return it. """ # Giving wait a timeout somehow circumvents a CPython bug that the # call gets ininterruptible. if progress: with self.lock: self.progress = ProgressBar(self.total, self.total - self.n) self.progress.start() self.progress.draw() while not self.evt.wait(timeout): pass if progress: self.progress.finish() return self.map_
def translate_and_query(self, hek_results, limit=None, progress=False): """ Translates HEK results, makes a VSO query, then returns the results. Takes the results from a HEK query, translates them, then makes a VSO query, returning the results in a list organized by their corresponding HEK query. Parameters ---------- hek_results : `sunpy.net.hek.hek.HEKRow` or `sunpy.net.hek.hek.HEKTable` The results from a HEK query in the form of a list. limit : int An approximate limit to the desired number of VSO results. progress : Boolean A flag to turn off the progress bar, defaults to "off" Examples -------- >>> from sunpy.net import hek, hek2vso >>> h = hek.HEKClient() # doctest: +REMOTE_DATA >>> tstart = '2011/08/09 07:23:56' >>> tend = '2011/08/09 12:40:29' >>> event_type = 'FL' >>> q = h.search(hek.attrs.Time(tstart, tend), hek.attrs.EventType(event_type)) # doctest: +REMOTE_DATA >>> h2v = hek2vso.H2VClient() # doctest: +REMOTE_DATA >>> res = h2v.translate_and_query(q) # doctest: +REMOTE_DATA """ vso_query = translate_results_to_query(hek_results) result_size = len(vso_query) if progress: sys.stdout.write('\rQuerying VSO webservice') sys.stdout.flush() pbar = TTYProgressBar(result_size) for query in vso_query: temp = self.vso_client.search(*query) self.vso_results.append(temp) self.num_of_records += len(temp) if limit is not None: if self.num_of_records >= limit: break if progress: pbar.poke() if progress: pbar.finish() return self.vso_results
class Results(object): """ Returned by VSOClient.get. Use .wait to wait for completion of download. """ def __init__(self, callback, n=0, done=None): self.callback = callback self.n = self.total = n self.map_ = {} self.done = done self.evt = threading.Event() self.errors = [] self.lock = threading.RLock() self.progress = None def submit(self, keys, value): """ Submit Parameters ---------- keys : list names under which to save the value value : object value to save """ for key in keys: self.map_[key] = value self.poke() def poke(self): """ Signal completion of one item that was waited for. This can be because it was submitted, because it lead to an error or for any other reason. """ with self.lock: self.n -= 1 if self.progress is not None: self.progress.poke() if not self.n: if self.done is not None: self.map_ = self.done(self.map_) self.callback(self.map_) self.evt.set() def require(self, keys): """ Require that keys be submitted before the Results object is finished (i.e., wait returns). Returns a callback method that can be used to submit the result by simply calling it with the result. keys : list name of keys under which to save the result """ with self.lock: self.n += 1 self.total += 1 return partial(self.submit, keys) def wait(self, timeout=100, progress=False): """ Wait for result to be complete and return it. """ # Giving wait a timeout somehow circumvents a CPython bug that the # call gets ininterruptible. if progress: with self.lock: self.progress = ProgressBar(self.total, self.total - self.n) self.progress.start() self.progress.draw() while not self.evt.wait(timeout): pass if progress: self.progress.finish() return self.map_ def add_error(self, exception): """ Signal a required result cannot be submitted because of an error. """ self.errors.append(exception) self.poke()