import matplotlib.pyplot as plt
import numpy as np

"""
Explanation: Today was quite an informal meeting and since all the attendants were beginners to Python, we talked about basics.

Launching Python on Grace using Anaconda
Login to Grace and load Anaconda module
Create your environment
Switch to your newly created environment


Python basics: getting familiar with numpy, scipy
Opening a NetCDF file
netcdf4
xray


Question about interpolation

Launching Python on Grace using Anaconda
About a half of people that completed the survey use Python on a remote server and today there were questions about it too. Below is the essential set of commands to create your own Python ecosystem on Grace using Anaconda software.
<div class="alert alert-warning", style="font-size: 120%">
The instructions below have been summarised in a script which is available here: <a href=https://github.com/ueapy/grace-python#option-a-anaconda-python-environment-on-grace>grace-python</a>.
</div>

Login to Grace and load Anaconda module
After you log in to Grace, you should load the Anaconda module by typing the following command:
module load python/anaconda/2.3.0
Note that I'm using Python 2 here, but Anaconda with Python 3 is also available on Grace (you can find that module using the command module avail)
Create your environment
To be able to manage your Python distribution without calling IT, it's better to create an Anaconda environment in your home directory:
conda create -n myenv python anaconda
where myenv is just a name of the environment (could be anything, such as my_awesome_environment).
Now, typing
conda list
will print out the environments available to you. In my case the result was
myenv                    /gpfs/home/abc12xyz/.conda/envs/myenv
root                  *  /gpfs/grace/anaconda/2.3.0
Switch to your newly created environment
source activate myenv
This command appended the correct path to the system path. 
(!) One thing that you should do is to clear standard environment variables:
unset PYTHONHOME PYTHONPATH
If you decided to stick with Anaconda, put the previous line in your .bashrc file.
Your Anaconda ecosystem is ready to use. Among many cool commands you can use conda install package_name to install a package.
Example
To install xray: conda install xray
To install iris from SciTools channel (because iris is not in the default conda channel): conda install -c scitools iris
To test it, launch python (or better, ipython) and type import package_name.
Python basics: getting familiar with numpy, scipy

Scipy Lectures - a short and very good tutorial with many useful examples
Matlab users can find a lot of useful tips here: Numpy for Matlab users
Stick to standard aliases for imported modules and sub-modules, such as:
End of explanation
"""


import netCDF4 as nc

"""
Explanation: Opening a NetCDF file
We had a brief look at two packages that are widely used to work with data in NetCDF format: netcdf4 and xray.
netcdf4
Import the module (note the upper case)
End of explanation
"""


ds = nc.Dataset('../data/data.nc')

"""
Explanation: Open the file as a Dataset:
End of explanation
"""


ds.variables.keys()

"""
Explanation: Print all variables:
End of explanation
"""


ds.dimensions.keys()

"""
Explanation: Interrogate the dimensions of the dataset:
End of explanation
"""


u = ds.variables['u']
print('Name of u: {}'.format(u.long_name))
print('Units of u: {}'.format(u.units))
print('Shape of u: {}'.format(u.shape))
print('First element of u array: {}'.format(u[0,0,0,0]))

"""
Explanation: Get values of u variable:
End of explanation
"""


import xray

"""
Explanation: xray
End of explanation
"""


xds = xray.open_dataset('../data/data.nc')

"""
Explanation: Open the same file as a xray Dataset:
End of explanation
"""


print(xds)

"""
Explanation: Neatly print out all the dataset info:
End of explanation
"""


u = xds.u
print(u)

"""
Explanation: Select a variable and get its metadata:
End of explanation
"""


HTML(html)

"""
Explanation: Question about interpolation
During today's meeting a question was asked about interpolating gridded data (2D) to a finer grid. Luckily, there is a scipy function with a simple example:
scipy.interpolate.interp2d
End of explanation
"""


