1

Hi I am trying to extract data from a netCDF file, but the data is upside down. How can I reverse the database: enter image description here

The data I want to extract is the height data from the (netcdf) at the points I have in the CSV file. my Data:

import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Path, PathPatch

csv_data = np.loadtxt('CSV with target coordinates',skiprows=1,delimiter=',')
num_el = csv_data[:,0]
lat = csv_data[:,1]
lon = csv_data[:,2]
value = csv_data[:,3]

data = Dataset("elevation Data",'r')
lon_range = data.variables['x_range'][:]
lat_range = data.variables['y_range'][:]
topo_range = data.variables['z_range'][:]
spacing = data.variables['spacing'][:]
dimension = data.variables['dimension'][:]
z = data.variables['z'][:]
lon_num =  dimension[0]
lat_num =  dimension[1]

etopo_lon = np.linspace(lon_range[0],lon_range[1],dimension[0])
etopo_lat = np.linspace(lat_range[0],lat_range[1],dimension[1])
topo = np.reshape(z, (lat_num, lon_num))

height = np.empty_like(num_el)
desired_lat_idx = np.empty_like(num_el)
desired_lon_idx = np.empty_like(num_el)
for i in range(len(num_el)): 
    tmp_lat = np.abs(etopo_lat - lat[i]).argmin()
    tmp_lon = np.abs(etopo_lon - lon[i]).argmin()
    desired_lat_idx[i] = tmp_lat
    desired_lon_idx[i] = tmp_lon
    height[i] = topo[tmp_lat,tmp_lon]
height[height<-10]=0 

print(len(desired_lat_idx))
print(len(desired_lon_idx))
print(len(height))


dfl= pd.DataFrame({
    'Latitude' : lat.reshape(-1),
    'Longitude': lon.reshape(-1),
    'Altitude': height.reshape(-1)
});
print(dfl)

# but the Lat should not be changed here (the dfl must be correct)
df =dfl
lat=np.array(df['Latitude'])
lon=np.array(df['Longitude'])
val=np.array(df['Altitude'])

m = basemap.Basemap(projection='robin', lon_0=0, lat_0=0, resolution='l',area_thresh=1000)
m.drawcoastlines(color = 'black')
x,y = m(lon,lat)
colormesh= m.contourf(x,y,val,100, tri=True,  cmap = 'terrain')
plt.colorbar(location='bottom',pad=0.04,fraction=0.06)
plt.show()

I have already tried:

lat = csv_data[:,1]
lat= lat*(-1)

But this didn´t work

12
  • data.iloc[::-1], or data.reindex(index=data.index[::-1]), you can find more in there: stackoverflow.com/questions/20444087/… Commented Jun 24, 2022 at 7:40
  • no I already tried this and this dosn´t work either! However nothing I tried produced a error Commented Jun 24, 2022 at 7:41
  • is there any chance that I can run your entire code? right now I dont have csv Commented Jun 24, 2022 at 7:46
  • Yeah I can send You the Data Commented Jun 24, 2022 at 7:46
  • 1
    I tried your example and it worked Commented Jun 24, 2022 at 8:17

1 Answer 1

1

It's a plotting artifact().

Just do:

colormesh= m.contourf(x,y[::-1],val,100, tri=True,  cmap = 'terrain')

y[::-1] will reverse the order of the y latitude elements (as opposed to the land-mass outlines; and while keeping the x longitude coordinates the same) and hence flip them.

I've often had this problem with plotting numpy image data in the past.

Your raw CSV data are unlikely to be flipped themselves (why would they be?). You should try sanity-checking them [I am not a domain expert I'm afraid]! Overlaying an actual coordinate grid can help with this.

Another way to do it is given here: Reverse Y-Axis in PyPlot

You could also therefore just do

ax = plt.gca()
ax.invert_yaxis()
Sign up to request clarification or add additional context in comments.

4 Comments

you think my databsae is right?
I mean this should be right so that I can calculate with these data
Hi do you have a Idea according this question: stackoverflow.com/questions/72742539/…
I'm afraid not, perhaps try asking another question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.