Sateesh K
Sateesh K

Reputation: 1091

Selecting the All Coordinate values using xarray

I am using xarray to read the data from netcdf files.

I am exporting the data for any given data variable to CSV format.

Here is a sample data variable

stratiform_snowfall_rate    (time, grid_latitude, grid_longitude) float32
Dimensions  = ('time', 'grid_latitude', 'grid_longitude')
No.of Dimensions  = 3
DataType    = float32
Shape       = (36, 548, 421)

I can get the individual values from the DataArray.

However, I want a unique set of values for each Coordinate.

i.e I want all the latitude longitude and time values for which I have the snowfall_rate data.

There should be 36 Unique time values, 548 latitude values, and 421 longitude values.

Thanks Sateesh

Upvotes: 0

Views: 742

Answers (1)

Sateesh K
Sateesh K

Reputation: 1091

Thanks everyone. I am able to read the coordinate values.

Here is a sample ex:

dataArray = xr.DataArray(
    np.random.randn(4, 6),
    dims=("x", "y"),
    coords={
        "x": [-3.2, 2.1, 5.3, 6.5],
        "y": pd.date_range("2009-01-05", periods=6, freq="M"),
    },
)

for i in range(len(dataArray.x)):
    for j in range(len(dataArray.y)):
        rowDict = {}
        rowDict['x'] = dataArray.coords['x'].values[i]
        rowDict['y'] = pd.to_datetime(str(dataArray.coords['y'].values[j])).strftime('%Y.%m.%d %H.%M.%S')
        rowDict['val'] = dataArray.data[i, j]
        print(rowDict)

And I am getting the output as :

{'x': -3.2, 'y': '2009.01.31 00.00.00', 'val': 1.1672918170123077}
{'x': -3.2, 'y': '2009.02.28 00.00.00', 'val': -0.45000573967001745}
{'x': -3.2, 'y': '2009.03.31 00.00.00', 'val': -0.3070507637309568}
{'x': -3.2, 'y': '2009.04.30 00.00.00', 'val': -0.16715901864646582}
{'x': -3.2, 'y': '2009.05.31 00.00.00', 'val': 1.6700002681882784}
{'x': -3.2, 'y': '2009.06.30 00.00.00', 'val': 0.2303405222447574}
{'x': 2.1, 'y': '2009.01.31 00.00.00', 'val': -0.10394183859025365}
{'x': 2.1, 'y': '2009.02.28 00.00.00', 'val': -0.6399654408544677}
{'x': 2.1, 'y': '2009.03.31 00.00.00', 'val': -0.7322155474326156}
{'x': 2.1, 'y': '2009.04.30 00.00.00', 'val': 0.4443732649765692}
{'x': 2.1, 'y': '2009.05.31 00.00.00', 'val': 0.32141177764826817}
{'x': 2.1, 'y': '2009.06.30 00.00.00', 'val': 0.990286185910961}
{'x': 5.3, 'y': '2009.01.31 00.00.00', 'val': -1.0176437927897097}
{'x': 5.3, 'y': '2009.02.28 00.00.00', 'val': -0.23011079013897445}
{'x': 5.3, 'y': '2009.03.31 00.00.00', 'val': -0.17623947662095518}
{'x': 5.3, 'y': '2009.04.30 00.00.00', 'val': -1.567830106123095}
{'x': 5.3, 'y': '2009.05.31 00.00.00', 'val': 0.3510704631729717}
{'x': 5.3, 'y': '2009.06.30 00.00.00', 'val': -1.619554124545969}
{'x': 6.5, 'y': '2009.01.31 00.00.00', 'val': -0.7614693432763142}
{'x': 6.5, 'y': '2009.02.28 00.00.00', 'val': 1.1021880682514598}
{'x': 6.5, 'y': '2009.03.31 00.00.00', 'val': -0.9944031641204935}
{'x': 6.5, 'y': '2009.04.30 00.00.00', 'val': 0.4910807257668136}
{'x': 6.5, 'y': '2009.05.31 00.00.00', 'val': 1.5922216407449592}
{'x': 6.5, 'y': '2009.06.30 00.00.00', 'val': -0.41559207408354637}

Upvotes: 0

Related Questions