0

我是 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'

请帮助我处理此代码。谢谢

4

2 回答 2

1

最简单的方法是使用 xarray 和 pandas。

import xarray as xr
import pandas as pd

您首先需要使用 xarray 读取数据:

data = xr.open_dataset(filename)

然后需要将其转换为 pandas 数据集,并重置索引:

data_df = data.to_dataframe().reset_index()

最后,您需要将其保存为 csv:

data_df.to_csv(outfile)
于 2020-06-12T15:40:28.297 回答
1

您可以通过选择以二进制模式打开输出文件'wb'
因此,文件写入函数需要二进制数据,即bytes对象。

但是,当您请求编写 csv 文件的帮助时,我假设您想要编写纯文本数据,因此您只需b在此处删除 for 二进制文件:

with open('output.csv', 'w') as csvFile:
于 2019-05-02T06:36:38.050 回答