# NetCDF: Why store coordinates as matrix

I am reading the documentation of Python library xarray

After having opened multiple NetCDFs and explored their contents, I see that some files store their coordinates as a matrix and some store their coordinates as a single variable.

For example variables as a matrix:

``````import numpy
lat = numpy.array([[-6.91,-6.91],[-6.90,-6.90]])
``````

or for example as a one dimensional list:

``````lat = [-6.91,-6.90]
``````

When setting the coordinates of a NetCDF using xarray,

It doesn't recognize the matrix of `lat` as a dimension (as shown by the lack of the asterisk)

What is the benefit of storing it as a 2D matrix?

• One question per post please :) – bugmenot123 Aug 8 '19 at 11:51

I think one of the most evident advantages is indexing. Consider the following example where you have data and both longitude and latitude stored in 2D arrays:

``````data = np.random.randint(100, 1000, size=(4, 4))

data
[[176, 479, 713, 973],
[992, 259, 969, 355],
[182, 139, 633, 938],
[761, 911, 124, 855]]

x = np.linspace(-76, -74.5, 4)
y = np.linspace(-5.0, -6.5, 4)
lon, lat = np.meshgrid(x, y)

lon
[[-76. , -75.5, -75. , -74.5],
[-76. , -75.5, -75. , -74.5],
[-76. , -75.5, -75. , -74.5],
[-76. , -75.5, -75. , -74.5]])

lat
[[-5. , -5. , -5. , -5. ],
[-5.5, -5.5, -5.5, -5.5],
[-6. , -6. , -6. , -6. ],
[-6.5, -6.5, -6.5, -6.5]]
``````

Now, you want to get the latitude and longitude for all those values in data that are lower than 500. Having this information stored as 2D arrays makes it easier to index them and get the values:

``````lon[data < 500]
[-76. , -75.5, -75.5, -74.5, -76. , -75.5, -75. ]

lat[data < 500]
[-5. , -5. , -5.5, -5.5, -6. , -6. , -6.5]
``````

Furthermore, it makes easier to index the data based on latitude and longitude values. Suppose you want all values that are east of -75.5 and north of -6.

``````data[(lon > -75.5) & (lat > -6)]
[713, 973, 969, 355]
``````