0

I want to open a sounding data generated by myself

I successfully ran the metpy code

df = pd.read_fwf(get_test_data('nov11_sounding.txt', as_file_obj=False), skiprows=5, usecols=[0, 1, 2, 3, 6, 7], names=col_names)

df['u_wind'], df['v_wind'] = mpcalc.wind_components(df['speed'],                                              np.deg2rad(df['direction']))

Now I want to open my file

F = open("ElAlto03012019.dat","r") 
F['u_wind'], F['v_wind'] = mpcalc.wind_components(F['speed'], np.deg2rad(F['direction']))

I expect the program to accept the file, however what I get is:

Traceback (most recent call last):

File "", line 1, in

TypeError: 'file' object has no attribute 'getitem'

Luis
  • 1
  • 2

1 Answers1

0

So the problem is that you're using Python's built-in open function to open your file. This function does not know anything about the structure of your data--it only serves to allow you to read the file by reading chunks of bytes (or sometimes whole lines if it's opened as text).

I don't know what format data you have, but you somehow need to turn the data in the file into numpy arrays for the MetPy functions to be able to understand it. In the original example, Pandas' read_fwf was used to read fixed-width columnar formatted data. Depending on the format of your data, you may be able to use this function, or another like read_csv to read in your data.

DopplerShift
  • 5,472
  • 1
  • 21
  • 20