I am working on trying to get values for a specific latitude and longitude from a netcdf3 file with Python. The model uses X and Y coordinates and an LCC projection.
The netcdf file comes with a set of corresponding 2d lats and longs. I can look up my desired lat and long and find the nearest value of each in the 2d lat long file. I am using scipy.io netcdf and numpy. Dataset is a netcdf object.
lat = 41.050 lon = -125.685 wind_lats = dataset.variables['lat'][:] wind_lons = dataset.variables['lon'][:] lat_idx = np.abs(wind_lats - lat).argmin() lon_idx = np.abs(wind_lons - lon).argmin() lat_idx = np.unravel_index(lat_idx, wind_lats.shape) lon_idx = np.unravel_index(lon_idx, wind_lons.shape) wind_lat_y = lat_idx wind_lat_x = lat_idx wind_lon_y = lon_idx wind_lon_x = lon_idx
This gives me a separate XY pair for the latitude and for the longitude. The variable of the netcdf file I am trying to access has indices time, height, y, and x.
It only uses 1 value for x and 1 for y. I have two values, but I don't know how to go from 2 to 1. Also the netcdf x and y variables are each a 1d array containing the respective x and y values.
The lat_idx and lon_idx after they are unraveled return the indices for the x and y values from the netcdf file. There is about a 100km difference between the x and y values of the lat longs. For example the latitude x value is -2641.2786. The longitude x value is -2543.75. The y values are similarly separated. The x and y values are not actual latitudes and longitudes themselves. The 2d lat long files reference the x and y values. Below is an example of how each of the files is organized:
2d latitude file:
y, x, lat 1959.0411, -3043.5815, 38.9718 1959.0411, -3031.3906, 38.9985 1959.0411, -3019.1997, 39.0251
1d x file:
x, x -3043.5815, -3043.5815 -3031.3906, -3031.3906 -3019.1997, -3019.1997
1d y file:
y, y 1959.0411, 1959.0411 1971.2321, 1971.2322 1983.4230, 1983.4231