簡體   English   中英

將數組值提取到 CSV 文件中

[英]Extract array values into a CSV file

嗨,我有來自該網站的以下代碼: https ://earthscience.stackexchange.com/questions/23947/world-elevation-data-as-csv

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_data(lat/long/value).csv',skiprows=1,delimiter=',')
num_el = csv_data[:,0]
lat = csv_data[:,1]
lon = csv_data[:,2]
value = csv_data[:,3]

data = Dataset("elevation_data.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)): 
    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]
height[height<0]=0 

dfl= pd.DataFrame({
    'Latitude' : desired_lat_idx.reshape(-1),
    'Longitude': desired_lon_idx.reshape(-1),
    'Altitude': height.reshape(-1)
});
dfl.to_csv(path)

有了這個,我可以提取文件中的高程數據(附加到另一個 CSV 文件中的緯度/經度數據。現在我想編寫一個新的 CSV 文件,其結構如下長/緯度/值/高度。我用剪斷的方法試過了

dfl= pd.DataFrame({
    'Latitude' : desired_lat_idx.reshape(-1),
    'Longitude': desired_lon_idx.reshape(-1),
    'Altitude': height.reshape(-1)
});
dfl.to_csv(path)

但我得到了錯誤

>>> dfl= pd.DataFrame({
...     'Latitude' : desired_lat_idx.reshape(-1),
...     'Longitude': desired_lon_idx.reshape(-1),
...     'Altitude': height.reshape(-1)
... });
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\UsersName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\frame.py", line 637, in __init__
    mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
  File "C:\UsersName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\internals\construction.py", line 502, in dict_to_mgr
    return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
  File "C:\UsersNameAppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\internals\construction.py", line 120, in arrays_to_mgr
    index = _extract_index(arrays)
  File "C:\UsersName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\internals\construction.py", line 674, in _extract_index
    raise ValueError("All arrays must be of the same length")
ValueError: All arrays must be of the same length
>>>

我怎樣才能解決這個問題?

在此處輸入圖像描述

desired_lat_idxdesired_lon_idx將在每個循環中被覆蓋,因此您將為它們中的每一個都有一個值(來自len(num_el)的最后一個循環的值,而您的高度將具有與num_el相同的長度,因為您更改了索引i處的值在每個循環中嘗試打印出所需的desired_lat_idx 、所需的desired_lon_idxheightlen以檢查我是否正確。

嘗試:

height = np.empty_like(num_el)
for i in range(len(num_el)): 
    lat_idx = np.abs(etopo_lat - lat[i]).argmin()
    lon_idx = np.abs(etopo_lon - lon[i]).argmin()
    height[i] = topo[lat_idx,lon_idx]
height[height<0]=0 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM