Xarray#

What do you need to do if you want to work with Xarray and Numpy?

# your code here

Data Structures: Data Array#

Create an Xarray DataArray containing 100 equally spaced numbers between 0 adn \(2\pi\). The name of the data array should be "x" and the dimension along which these numbers are enumerated should also be called "x".

# your code here

Calculate the cosine of this array and assign it to a new variable. The result will be a new Xarray data array. Make sure the name of the new array is "cosx".

# your code here

Create an Xarray DataArray containing 100 equally spaced numbers between 0 adn \(2\pi\). The name of the data array should be "y" and the dimension along which these numbers are enumerated should also be called "y".

# your code here

Now, create an array called "siny" out of the data array "y".

# your code here

Multiply cosx with siny and inspect the different parts of the resulting data array. Do we have everything we need to fully understand the data?

# your code here

We’re missing coordinates. Let’s add the data array x as coordinate "x" to the cosine and the data array y as coordinate "y" to the sine. Then calculate the product of the cosine and the sine again.

# your code here

Now, visualize the resulting data array.

# your code here

Data Structures: Dataset#

Now, create an Xarray Dataset containing three data variables: cosx, siny, and the product of the two. What happened to the coordinates of the underlying data arrays?

# your code here

Finally, create the Dataset above from code alone: Define all data variables as data arrays and also explicitly add coordinates.

# your code here

Slicing / Selection#

From the above dataset select the part representing every second y value (based on the indes) and x values with \(x\in[0.5, 4.5]\).

# your code here

Now calculate the standard deviation in the x-direction and the mean in the y-direction of this subset dataset and of the complete one. Discuss the following observations:

  • What data structure results from calculating the means and standard deviations?

  • Do the values what reflect what you expected?

# your code here

Input-Output#

Now, write the dataset from above into a netCDF file. (Hint, you can use the .to_netcdf() method for this.) Let’s call the file "cos_sin_xy.nc".

# your code here

Inspect the netCDF file with the Unix command-line tool ncdump. You can create a cell with the following command in it:

!ncdump -h cos_sin_xy.nc

This will start a Unix sub shell and run the command ncdump -h <filename> where the -h flag indicates you only want to see the header of the file.

Interpret the output.

# your code here

Finally, read the file again into an Xarray dataset using the xr.open_dataset() function.

# your code here