我是 Python 新手,并试图将 netCDF 文件解析为列格式的 .CSV,因此我可以将该数据加载到 RDBMS 中以用于其他报告目的。请参考下面的详细信息。
我的 netCDF 文件的截图:
dimensions:
time = UNLIMITED ; // (36 currently)
grid_latitude = 548 ;
grid_longitude = 421 ;
time_0 = UNLIMITED ; // (3 currently)
pressure = 3 ;
time_1 = UNLIMITED ; // (3 currently)
bnds = 2 ;
pressure_0 = 2 ;
pressure_1 = 3 ;
dim0 = UNLIMITED ; // (3 currently)
grid_longitude_0 = 421 ;
grid_latitude_0 = 547 ;
time_3 = UNLIMITED ; // (3 currently)
variables:
float stratiform_snowfall_rate(time, grid_latitude, grid_longitude) ;
stratiform_snowfall_rate:_FillValue = -1.073742e+09f ;
string stratiform_snowfall_rate:long_name = "stratiform_snowfall_rate" ;
string stratiform_snowfall_rate:units = "kg m-2 s-1" ;
string stratiform_snowfall_rate:um_stash_source = "m01s04i204" ;
string stratiform_snowfall_rate:grid_mapping = "rotated_latitude_longitude" ;string stratiform_snowfall_rate:coordinates = "forecast_period forecast_reference_time" ;int rotated_latitude_longitude ;
我的代码:
from netCDF4 import Dataset, num2date
filename ='prods_op_mogreps-uk_20140717_03_11_015.nc'
nc = Dataset(filename, 'r', Format='NETCDF4')
ncv = nc.variables
lats = nc.variables['grid_latitude'][:]
lons = nc.variables['grid_longitude'][:]
sfc= nc.variables['stratiform_snowfall_rate'][:]
times = nc.variables['time'][:]
units = nc.variables['time'].units
dates = num2date (times[:], units=units, calendar='365_day')
header = ['Latitude', 'Longitude']
for d in dates:
header.append(d)
import csv
with open('output.csv', 'wb') as csvFile:
outputwriter = csv.writer(csvFile, delimiter=',')
for time_index, time in enumerate(times): # pull the dates out for the header
t = num2date(time, units = units, calendar='365_day')
header.append(t)
outputwriter.writerow(header)
for lat_index, lat in enumerate(lats):
content = lat
#print lat_index
for lon_index, lon in enumerate(lons):
content.append(lon)
#print lon_index
for time_index, time in enumerate(times): # for a date
# pull out the data
data = sfc[time_index,lat_index,lon_index]
content.append(data)
outputwriter.writerow(content)
csvFile.close()
nc.close()
我收到以下错误:
TypeError Traceback (most recent call last) <ipython-input-41-b4b3b888999f> in <module> 4 t = num2date(time, units = units, calendar='365_day') 5 header.append(t) ----> 6 outputwriter.writerow(header) 7 for lat_index, lat in enumerate(lats): 8 content = lat TypeError: a bytes-like object is required, not 'str'
请帮助我处理此代码。谢谢