Sorting a TDSCatalog List for Loops and Animations

110 Views Asked by At

I'm working on making animated radar loops using siphon and I am seeing that siphon isn't extracting the data in time/file order. (Example below).

from siphon.cdmr         import Dataset
from siphon.radarserver  import get_radarserver_datasets, RadarServer
from siphon.catalog      import TDSCatalog
from datetime            import datetime

radar_station = "TWX"

ds  = get_radarserver_datasets('http://thredds.ucar.edu/thredds/')
url = ds['NEXRAD Level III Radar from IDD'].follow().catalog_url
rs  = RadarServer(url)

query = rs.query()

query.stations(radar_station).time_range(datetime(2021,11,10,22,0,0),
                                         datetime(2021,11,10,23,0,0)).variables('N0Q')

rs.validate_query(query)

catalog = rs.get_catalog(query)

list(catalog.datasets.values())

yields...

[Level3_TWX_N0Q_20211110_2233.nids,
 Level3_TWX_N0Q_20211110_2202.nids,
 Level3_TWX_N0Q_20211110_2255.nids,
 Level3_TWX_N0Q_20211110_2300.nids,
 Level3_TWX_N0Q_20211110_2241.nids,
 Level3_TWX_N0Q_20211110_2223.nids,
 Level3_TWX_N0Q_20211110_2212.nids,
 Level3_TWX_N0Q_20211110_2245.nids,
 Level3_TWX_N0Q_20211110_2216.nids,
 Level3_TWX_N0Q_20211110_2240.nids,
 Level3_TWX_N0Q_20211110_2258.nids,
 Level3_TWX_N0Q_20211110_2226.nids,
 Level3_TWX_N0Q_20211110_2250.nids,
 Level3_TWX_N0Q_20211110_2232.nids,
 Level3_TWX_N0Q_20211110_2248.nids,
 Level3_TWX_N0Q_20211110_2207.nids,
 Level3_TWX_N0Q_20211110_2236.nids,
 Level3_TWX_N0Q_20211110_2238.nids,
 Level3_TWX_N0Q_20211110_2209.nids,
 Level3_TWX_N0Q_20211110_2211.nids,
 Level3_TWX_N0Q_20211110_2246.nids,
 Level3_TWX_N0Q_20211110_2228.nids,
 Level3_TWX_N0Q_20211110_2252.nids,
 Level3_TWX_N0Q_20211110_2219.nids,
 Level3_TWX_N0Q_20211110_2230.nids,
 Level3_TWX_N0Q_20211110_2256.nids,
 Level3_TWX_N0Q_20211110_2205.nids,
 Level3_TWX_N0Q_20211110_2218.nids,
 Level3_TWX_N0Q_20211110_2253.nids,
 Level3_TWX_N0Q_20211110_2200.nids,
 Level3_TWX_N0Q_20211110_2204.nids,
 Level3_TWX_N0Q_20211110_2235.nids,
 Level3_TWX_N0Q_20211110_2243.nids,
 Level3_TWX_N0Q_20211110_2221.nids,
 Level3_TWX_N0Q_20211110_2214.nids,
 Level3_TWX_N0Q_20211110_2225.nids]

tacking a .sort() at the end of the list yields the following error:

TypeError: '<' not supported between instances of 'Dataset' and 'Dataset'

Is there way to sort the resulting TDS catalogs to feed into resources like matplotlib.animation.FuncAnimation?

Cheers-n-Thanks,

Bill

2

There are 2 best solutions below

2
On BEST ANSWER

The solution here is not to sort the values from the list of datasets, but sort the names (keys):

datasets = list(catalog.datasets)
datasets.sort()

or really easier:

datasets = sorted(catalog.datasets)

The canonical way to loop over the sorted order would be:

for name in sorted(catalog.datasets):
    ds = catalog.datasets[name]
0
On

You can instead use the list of the dataset names from catalog.datasets to sort successfully,

...

for file in sorted(catalog.datasets):
    ds = catalog.datasets[file].remote_access()

...