I am pretty new to Python and am trying to extract data from a.netCDF4 file. 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). (z is the height of station). 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!
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. 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. 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()? Actually there is more explanation someone has already asked convert.netCDF files to csv .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.