使用NetCDF文件转置xray数据集

时间:2014-10-21 20:59:45

标签: python multidimensional-array transpose python-xarray

我正在试验xray库(Python中的N-D标记数组和数据集)。我使用转置来更改索引顺序,但结果没有变化。

以下代码段访问NetCDF文件并分配给xray数据集,提取数据子集,创建Pandas DataFrame,并将结果输出到CSV文件。

接下来,转换xray数据集的尺寸,并提取子集,创建DataFrame和输出CSV的相同过程。结果是一样的。

import pandas as pd
import xray

# access NetCDF over HTTP
ds = xray.open_dataset('http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/noaa.oisst.v2/sst.mnmean.nc')

# Extract subset of data  using indexes: time, lat, lon
sst = ds['sst'][133:157, 80:100, 180:260]

# Convert to Dataframe
df = sst.to_dataframe()

# Outut to csv format
df.to_csv('c:/dev/sst1.csv', mode='w')

'''
lat lon time    sst
9.5 180.5   1993-01-01 00:00:00 26.799999401
9.5 180.5   1993-02-01 00:00:00 27.0699993949
9.5 180.5   1993-03-01 00:00:00 27.1199993938
9.5 180.5   1993-04-01 00:00:00 27.379999388
9.5 180.5   1993-05-01 00:00:00 27.8499993775
9.5 180.5   1993-06-01 00:00:00 28.1699993704
9.5 180.5   1993-07-01 00:00:00 28.2799993679
9.5 180.5   1993-08-01 00:00:00 28.7999993563
9.5 180.5   1993-09-01 00:00:00 29.2099993471
9.5 180.5   1993-10-01 00:00:00 29.2199993469
9.5 180.5   1993-11-01 00:00:00 28.7099993583
9.5 180.5   1993-12-01 00:00:00 28.0799993724
9.5 180.5   1994-01-01 00:00:00 27.7999993786
9.5 180.5   1994-02-01 00:00:00 27.649999382
9.5 180.5   1994-03-01 00:00:00 27.7599993795
9.5 180.5   1994-04-01 00:00:00 28.1099993717
9.5 180.5   1994-05-01 00:00:00 28.3799993657
9.5 180.5   1994-06-01 00:00:00 28.3099993672
9.5 180.5   1994-07-01 00:00:00 28.3599993661
9.5 180.5   1994-08-01 00:00:00 29.1899993476
9.5 180.5   1994-09-01 00:00:00 29.6899993364
9.5 180.5   1994-10-01 00:00:00 29.4799993411
9.5 180.5   1994-11-01 00:00:00 29.0999993496
9.5 180.5   1994-12-01 00:00:00 28.4199993648
9.5 181.5   1993-01-01 00:00:00 26.8399994001
9.5 181.5   1993-02-01 00:00:00 27.1399993934
9.5 181.5   1993-03-01 00:00:00 27.1399993934
...
'''

# Transpose dimensions
ds_T = ds.transpose('lon', 'lat', 'time', 'nbnds')

# Extract subset the data  using indexes: lon, lat, time
sst = ds_T['sst'][180:260, 80:100, 133:157]

# Convert to Dataframe
df = sst.to_dataframe()

# Outut to csv format
df.to_csv('c:/dev/sst2.csv', mode='w')

'''
lat lon time    sst
9.5 180.5   1993-01-01 00:00:00 26.799999401
9.5 180.5   1993-02-01 00:00:00 27.0699993949
9.5 180.5   1993-03-01 00:00:00 27.1199993938
9.5 180.5   1993-04-01 00:00:00 27.379999388
9.5 180.5   1993-05-01 00:00:00 27.8499993775
9.5 180.5   1993-06-01 00:00:00 28.1699993704
9.5 180.5   1993-07-01 00:00:00 28.2799993679
9.5 180.5   1993-08-01 00:00:00 28.7999993563
9.5 180.5   1993-09-01 00:00:00 29.2099993471
9.5 180.5   1993-10-01 00:00:00 29.2199993469
9.5 180.5   1993-11-01 00:00:00 28.7099993583
9.5 180.5   1993-12-01 00:00:00 28.0799993724
9.5 180.5   1994-01-01 00:00:00 27.7999993786
9.5 180.5   1994-02-01 00:00:00 27.649999382
9.5 180.5   1994-03-01 00:00:00 27.7599993795
9.5 180.5   1994-04-01 00:00:00 28.1099993717
9.5 180.5   1994-05-01 00:00:00 28.3799993657
9.5 180.5   1994-06-01 00:00:00 28.3099993672
9.5 180.5   1994-07-01 00:00:00 28.3599993661
9.5 180.5   1994-08-01 00:00:00 29.1899993476
9.5 180.5   1994-09-01 00:00:00 29.6899993364
9.5 180.5   1994-10-01 00:00:00 29.4799993411
9.5 180.5   1994-11-01 00:00:00 29.0999993496
9.5 180.5   1994-12-01 00:00:00 28.4199993648
9.5 181.5   1993-01-01 00:00:00 26.8399994001
9.5 181.5   1993-02-01 00:00:00 27.1399993934
9.5 181.5   1993-03-01 00:00:00 27.1399993934
...
'''

1 个答案:

答案 0 :(得分:2)

我已经在xray邮件列表上回答了这个问题,但简而言之,这是一个错误:https://github.com/xray/xray/issues/260

它在最新的X射线版本(0.3.1)中修复。