我是Python的新手,正在尝试将netCDF文件解析为columanar格式的.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'
请帮助我处理此代码。谢谢
答案 0 :(得分:1)
通过选择'wb'
以二进制模式打开输出文件。
因此,文件写入功能需要二进制数据,即bytes
对象。
但是,当您请求编写csv文件的帮助时,我假设您想写入纯文本数据,因此您只需在此处删除二进制文件的b
:
with open('output.csv', 'w') as csvFile:
答案 1 :(得分:1)
最简单的方法是使用xarray和pandas。
import xarray as xr
import pandas as pd
您首先需要使用xarray读取数据:
data = xr.open_dataset(filename)
然后将其转换为熊猫数据集,并重新设置索引:
data_df = data.to_dataframe().reset_index()
最后,您需要将其另存为csv:
data_df.to_csv(outfile)