Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level2File: 'IndexError: tuple index out of range' (older L2 data) #3719

Open
msw17002 opened this issue Dec 20, 2024 · 4 comments
Open

Level2File: 'IndexError: tuple index out of range' (older L2 data) #3719

msw17002 opened this issue Dec 20, 2024 · 4 comments
Labels
Type: Bug Something is not working like it should

Comments

@msw17002
Copy link

What went wrong?

Hello,

The file I'm using can be obtained by the following Linux command, 'wget https://noaa-nexrad-level2.s3.amazonaws.com/2005/01/21/KBOX/KBOX20050121_010932.gz'

The script;

radar = Level2File("KBOX20050121_010932.gz")
try:
    cent_lon = radar.sweeps[0][0][1].lon
    cent_lat = radar.sweeps[0][0][1].lat
except:
    lat,lon,ele, = get_nexrad_location("KBOX") #this is taken from https://raw.githubusercontent.com/ARM-DOE/pyart/refs/heads/main/pyart/io/nexrad_common.py
sweep = 0 #base
print(radar.sweeps[sweep][0][4][b'REF'][0])

The problem;

File "/storage/orig/web_backup/kmz/MikesToolz/L2-BR/parallel/./test.py", line 48, in <module>
   print(radar.sweeps[sweep][0][4][b'REF'][0])
IndexError: tuple index out of range

I'd like to ask, how would I rewrite the script to index the correct tuples to retrieve base reflectivity?

This script works for newer L2 NEXRAD files, just not this one.

Operating System

Linux

Version

1.6.2

Python Version

3.9.19

Code to Reproduce

radar = Level2File("KBOX20050121_010932.gz")
try:
    cent_lon = radar.sweeps[0][0][1].lon
    cent_lat = radar.sweeps[0][0][1].lat
except:
    lat,lon,ele, = get_nexrad_location("KBOX") #this is taken from https://raw.githubusercontent.com/ARM-DOE/pyart/refs/heads/main/pyart/io/nexrad_common.py
sweep = 0 #base
print(radar.sweeps[sweep][0][4][b'REF'][0])

Errors, Traceback, and Logs

File "/storage/orig/web_backup/kmz/MikesToolz/L2-BR/parallel/./test.py", line 48, in <module>
    print(radar.sweeps[sweep][0][4][b'REF'][0])
IndexError: tuple index out of range
@msw17002 msw17002 added the Type: Bug Something is not working like it should label Dec 20, 2024
@dopplershift
Copy link
Member

This should work:

ref = radar.sweeps[0][0][1]['REF'][1]

@msw17002
Copy link
Author

Hello again, Ryan.

Based on the online example,
radar.sweeps[0][0][4][b'REF'][0]
and what you suggest,
radar.sweeps[0][0][1]['REF'][1]

It'd be great if MetPy could create a dynamic function that selects the correct index based on a user specified sweep and field ('REF') for all older/newer files. Besides that, could you please elaborate what the 3rd (example - '[4]') and 5th (example - '[0]') tuple index denote? Could I simply write script that indexes the final option of both tuples ('[-1]')?

So for example,
radar.sweeps[0][0][-1]['REF'][-1]

Thanks again!

@dopplershift
Copy link
Member

The current NEXRAD interface is pretty low level and maps very directly to the layout of the files; an improved interface has been on the wishlist for awhile #49 but has never reached the top of the priority list. You may also want to look into PyART or xradar for improved interfaces; the latter can provide an xarray dataset/datatree view of NEXRAD data.

The basic interface with .sweeps is the first index gets the sweep (collection of radials), the next index selects a particular radial. For newer ("message 31") data, this radial has a set of headers, then a dictionary of moment data; these pieces are arranged as a tuple, with the dictionary as the last item, so yes -1 should work there. For each moment, it's a tuple of (header, data), so -1 should work there as well.

Not sure why I didn't suggest that or write the docs with that. PRs updating are welcome.

@msw17002
Copy link
Author

I initially looked at PyART, but it's near impossible to create an application with the library. There are too many scripts to pack into an executable... It is possible with MetPy, which is why I continue to use it. I may look into xradar although I didn't know about it until now.

Is this correct for "message 31" data?
index 1: sweep [n; '0' denotes base]
index 2: radial [set to 0; unsure of why?]
index 3: radial [-1; locating the tuple that possesses data for sweep 'n'. and radial '0' ... should be -1. If not, find a method to locate 'data']
index 4: moment [-1: index the data tuple of moment which should be set to -1, as well]

For index 2 (radial header), why is '0' selected? Does this correspond to sweep? So if sweep == 0, index 2 == 0.

Also, if the file doesn't contain "message 31" data, how would the indices change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something is not working like it should
Projects
None yet
Development

No branches or pull requests

2 participants