简体   繁体   English

从 one.netCDF4 文件中提取两个变量到 csv 有什么想法吗?

[英]Any ideas to extract two variables from one netCDF4 file to csv?

I am pretty new to Python and am trying to extract data from a.netCDF4 file.我是 Python 的新手,正在尝试从 .netCDF4 文件中提取数据。 It is a set of meteorological data which contains several variables: temperature, relative humidity, wind speed, wind direction, etc. Each of them has two dimensions: time(4464), and z(7).它是一组气象数据,包含几个变量:温度、相对湿度、风速、风向等。每个变量都有两个维度:时间(4464)和z(7)。 (z is the height of station). (z 是站的高度)。 Each of variables are acquired at the same time(s) and height(s), so the time and z dimension of each variable is the same!每个变量是同时获取的时间和高度,所以每个变量的时间和z维度是相同的!

import netCDF4 as nc
from netCDF4 import num2date
import numpy as np
import os
import pandas as pd

#Open netCDF4 file
input_dir = 'C:/Users/censoring this part' #netCD4 file directory
filename = 'v1.0_201910' #netCD4 file name without extension
ds = nc.Dataset(os.path.join(input_dir,filename+'.nc'))

#Extract variables
F = ds.variables['F'] #Wind speed in m/s
D = ds.variables['D'] #Wind direction in degree


#Get dimensions
time_dim, z_dim = F.get_dims()
time_var = ds.variables[time_dim.name]
times = num2date(time_var[:], time_var.units) #time points
z = ds.variables[z_dim.name][:] #point height

#Extracting file to CSV for wind speed and direction
output_dir = 'C:/Users/censoring this part' #Folder destination for .csv file
output_dest = os.path.join(output_dir, filename+'_F.csv')
print('Writing data in tabular form to destination (this may take some time)...')
times_grid, z_grid = [
    x.flatten() for x in np.meshgrid(times, z, indexing='ij')]
df = pd.DataFrame({
    'time': [t.isoformat() for t in times_grid],
    'height': z_grid[:],
    'wind speed': F[:].flatten()
    'wind direction': D[:].flatten()}) #this part doesn't work
df.to_csv(output_dest, index=False)
print('Successfully done') 

I want to extract wind speed (F) and wind direction (D) to one csv file but it doesn't allow me to do that with my code - same as extracting them first using numpy array separately, which doesn't solve any problem.我想将风速 (F) 和风向 (D) 提取到一个 csv 文件,但它不允许我用我的代码执行此操作 - 与首先分别使用 numpy 数组提取它们一样,这不能解决任何问题. I also want to pick only data from one specific height, and specifying the index of height I want to pick in the code (for example z[0]) doesn't do the job.我还想只从一个特定的高度选择数据,并且在代码中指定我想选择的高度索引(例如 z[0])并不能完成这项工作。 I found out that the errors given always indicate that the array size is not the same so they can't proceed the code, but I don't understand how to resolve this problem.我发现给出的错误总是表明数组大小不一样,所以他们无法继续代码,但我不明白如何解决这个问题。

Any ideas to solve this?有解决这个问题的想法吗? Thank you!谢谢!

Can you check to_pandas() and to_csv()?你能检查一下 to_pandas() 和 to_csv() 吗? Actually there is more explanation someone has already asked convert.netCDF files to csv .实际上还有更多的解释有人已经要求将 convert.netCDF 文件转换为 csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM