How can we build a `UnifiedResponse` from a list of `VSOQueryResponseTable` objects?

I need to be able to append the results I get from multiple Fido.search(). I am trying to override __add__ so that I can do something like this:

results1 = Fido.search(...)
results2 = Fido.search(...)
results = results1 + results2

My solution is to iterate over each VSOQueryResponseTable of results1 and results2, and use their as_array() methods to be able to concatenate them using np.concatenate().

What I need to eventually do is to convert all new VSOQueryResponseTable objects into one UnifiedResponse object. I don’t know how to do that.

Is that possible?

Or is there an easier way to merge multiple UnifiedResponse objects?

I appreciate any advice.

1 Like

Hello,

From what I recall, there is no way to formally add responses together.
I don’t think this is the first time this topic has come up, but we have struggled to figure out use cases for this kind of functionally.

Can I ask what your use case is for adding them?

1 Like

My use case is as follows:
I want to sample exactly n observations. When I use the snippet below, I can only specify the time interval and the cadence, and not the variable n:

results = Fido.search(time_q,  # time interval
                      inst_q,  # instrument
                      attrs.Physobs.intensity,  # filter for images
                      attrs.Sample(cadence * u.minute)  # sampling rate
                      )

This limitation, as I understand it, is caused by the fact that instruments are not ideal and many time stamps are being missed.

My workaround is to call the search method multiple times, each time to compensate for the remaining samples. At the end, I need to concatenate all results into one single object of the same type, i.e., UnifiedResponse.

I did this for VSOQueryResponseTable in a wrapper class. It was mainly done thanks to the constructor of VSOQueryResponseTable:

def __add__(self, other: 'VSOSearchResult'):
        # concatenate two 1-d arrays
        new_table = np.concatenate((self.table, other.table))
        vqrt = VSOQueryResponseTable(new_table)
        # sort the new table by 'Start Time'
        sorted_idxs = vqrt.argsort(keys='Start Time')
        return VSOSearchResult(vqrt[sorted_idxs])

I could not, however, figure out how I can do this for UnifiedResponse class.
Any help is much appreciated.

Hello @azim and welcome :smile:

As Nabil said we don’t support adding responses, but there are two different things you can do so you don’t need to.

The first one is that you can just pass multiple UnifiedResponse objects to Fido.fetch and it will download them all.

The second is that you can slice the results object from your original search to get every nth result, so you only need to make one call to Fido.search. See this section of the documentation for some examples: Finding and Downloading Data using Fido — SunPy 4.0.3 documentation but you should be able to do something like results["vso", ::n] to get every nth VSO result, and then just pass that cropped table back into Fido.fetch.

Let us know how it goes and if you have any more questions.

1 Like

Thanks @Cadair and @nabobalis !
I think slicing might be a good workaround for me.