3
$\begingroup$

Hi I need For each Lat and Long value also the Alt value! However, I am having problems extracting these values from a Geotiff and wonder if this data is publicly available in Excel or CSV file format? Unfortunately I have not found anything myself!

my data: https://wetransfer.com/downloads/8e5ba7aa4cd8c406068a60543b353b0420220622084939/d3c744 For people who know Python:

import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import pandas as pd

data = Dataset("C:/Users/Oliver Weisser/Desktop/Bachelor/Programm/Daten/Daten/ETOPO1_Bed_g_gdal.grd",'r')
print(data.variables.keys())

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]

lon = np.linspace(lon_range[0],lon_range[1],dimension[0])
lat = np.linspace(lat_range[0],lat_range[1],dimension[1])

topo = np.reshape(z, (lat_num, lon_num))


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

Source: ETOPO1 region selection (in python)

This is the code I have written so far, maybe someone will find a bug there

$\endgroup$
1
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$
    – gerrit
    Jun 24 at 8:04

1 Answer 1

4
$\begingroup$

So I hope I finally understood what your problem really was and this gives you the height information at your specific lat lon values from your csv data.

import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt

csv_data = np.loadtxt('1994_12_O18grid.csv',skiprows=1,delimiter=',')

num_el = csv_data[:,0]
lat = csv_data[:,1]
lon = csv_data[:,2]
value = csv_data[:,3]

data = Dataset("ETOPO1_Bed_g_gdal.grd",'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)
for i in range(len(num_el)):  #in this loop we search for the height values for the specific lat and lon values from your csv
    desired_lat_idx = np.abs(etopo_lat - lat[i]).argmin()
    desired_lon_idx = np.abs(etopo_lon - lon[i]).argmin()
    height[i] = topo[desired_lat_idx,desired_lon_idx]

plt.figure()
height[height<0]=0 # if you want to disregard values below 0
plt.imshow(np.reshape(height,(180,360))) # if the resolution of your csv data changes from 1 degree to something different you need to chance the values in the reshape. For simplicity I hardcoded the numbers.

The searching for the height levels is not that efficient but I didnt have the time to do a proper numpy style solution.

$\endgroup$
6
  • $\begingroup$ I will have a detailed look at it! but it looks very promising! Thank you very much for your time and for answering me so patiently! $\endgroup$
    – Weiss
    Jun 22 at 12:08
  • 1
    $\begingroup$ You are welcome, maybe as an advice in the future try to better formulate your issues and questions. Then people can help you more quickly :) $\endgroup$
    – smichel
    Jun 22 at 12:13
  • 1
    $\begingroup$ OK I will do that! You may notice that I am new to coding and still need some help :-) $\endgroup$
    – Weiss
    Jun 22 at 12:14
  • $\begingroup$ Yeah, no worries. Everyone started at one point. $\endgroup$
    – smichel
    Jun 22 at 12:25
  • $\begingroup$ Hi @smichel I'm afraid I don't quite understand the probgram and I still want to ask how I can extract the data from this figure that I have a table that looks like this: Long/lat/Value(CSV)/Altitude? If something is unclear, I will try to ask the question again in more detail in a new question. $\endgroup$
    – Weiss
    Jun 23 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.