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

Weiss
  • 176
  • 2
  • 16
  • `data.iloc[::-1]`, or `data.reindex(index=data.index[::-1])`, you can find more in there: https://stackoverflow.com/questions/20444087/right-way-to-reverse-a-pandas-dataframe – demetere._ Jun 24 '22 at 07:40
  • no I already tried this and this dosn´t work either! However nothing I tried produced a error – Weiss Jun 24 '22 at 07:41
  • is there any chance that I can run your entire code? right now I dont have csv – demetere._ Jun 24 '22 at 07:46
  • Yeah I can send You the Data – Weiss Jun 24 '22 at 07:46
  • demetredzmanashvili@gmail.com send it there – demetere._ Jun 24 '22 at 07:47
  • Oh I put a link in my question :) – Weiss Jun 24 '22 at 07:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245873/discussion-between-weiss-and-demetre-dzmanashvili). – Weiss Jun 24 '22 at 07:50
  • Does this answer your question? [Plotting data on map in Mercator projector looks flipped 90 deg latitude](https://stackoverflow.com/questions/60902229/plotting-data-on-map-in-mercator-projector-looks-flipped-90-deg-latitude) – jtlz2 Jun 24 '22 at 07:57
  • @Weiss BTW it can be dangerous to post a raw data file / people might be suspicious of malware etc.. You could just include a few ASCII lines from your CSV in the question, those few data points being able to distinguish up from down when the outlines are overlaid :) – jtlz2 Jun 24 '22 at 08:05
  • @Weiss Let us know how you get on! – jtlz2 Jun 24 '22 at 08:12
  • 1
    I tried your example and it worked – Weiss Jun 24 '22 at 08:17
  • Brilliant, happy to help! :) Good luck with the analysis! – jtlz2 Jun 24 '22 at 08:18

1 Answers1

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()
jtlz2
  • 7,700
  • 9
  • 64
  • 114