Reputation: 101
I am experimenting with xray library (N-D labeled arrays and datasets in Python). I am using transpose to change index order, but results in no change.
The following code segment accesses NetCDF file and assigns to an xray Dataset, extracts a subset of data, creates a Pandas DataFrame, and outputs results to CSV file.
Next, the dimensions of the xray Dataset are transposed and same process of extracting subset, create DataFrame and output CSV. The result is identical.
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
...
'''
Upvotes: 2
Views: 553
Reputation: 9613
I answered this question on the xray mailing list already, but in brief, this is a bug: https://github.com/xray/xray/issues/260
It is fixed in the latest xray release (0.3.1).
Upvotes: 2