Difference between revisions of "Python/SettingPaths"

From mn/geo/geoit
Jump to: navigation, search
(Customizing Ipython)
Line 1: Line 1:
===Setting up Python Paths===
+
=== Setting up Python Paths ===
  
 
Python is a programming language that comes with 'batteries included', meaning there are many existing tools (called modules) for your use. For meteorological and climate data analysis, many of the tools have significant library dependencies, and it can be a challenge to build the modules (for instance netcdf requires the zlib compression libraries for HDF4/5 while grib requires the jasper libraries for compression).
 
Python is a programming language that comes with 'batteries included', meaning there are many existing tools (called modules) for your use. For meteorological and climate data analysis, many of the tools have significant library dependencies, and it can be a challenge to build the modules (for instance netcdf requires the zlib compression libraries for HDF4/5 while grib requires the jasper libraries for compression).
Line 13: Line 13:
 
This file will add a PYTHONPATH environment variable, but will also set the GRIB paths and add to your own $PATH. Read the header of the file for further information.
 
This file will add a PYTHONPATH environment variable, but will also set the GRIB paths and add to your own $PATH. Read the header of the file for further information.
  
===Customizing Ipython (OLD VERSION)===
+
=== Customizing Ipython (OLD VERSION) ===
 +
 
 
Ipython is a powerful interpreter for Python. It is highly recommended to use this for developing and testing code snippets while you are writing your modules and scripts.
 
Ipython is a powerful interpreter for Python. It is highly recommended to use this for developing and testing code snippets while you are writing your modules and scripts.
  
Once you are logged on to a server and have sourced the file above, you should be able to import the modules. A handy shortcut is to customize your Ipython environment and automatically import some of these modules. To do this, you simply need to edit the file: <code>~/.ipython/ipy_user_conf.py</code> in your $HOME directory. In the <code>main</code> function, you can add some import statements in the following way:
+
Once you are logged on to a server and have sourced the file above, you should be able to import the modules. A handy shortcut is to customize your Ipython environment and automatically import some of these modules. To do this, you simply need to edit the file: <code>~/.ipython/ipy_user_conf.py</code> in your $HOME directory. In the <code>main</code> function, you can add some import statements in the following way: <source lang="py">
<source lang='py'>
 
 
# Most of your config files and extensions will probably start with this import
 
# Most of your config files and extensions will probably start with this import
  
Line 25: Line 25:
  
 
def main():
 
def main():
    ip.ex('import numpy as np')
+
ip.ex('import numpy as np')
    ip.ex('import matplotlib.pyplot as plt')
+
ip.ex('import matplotlib.pyplot as plt')
    ip.ex('from mpl_toolkits.basemap import shiftgrid, addcyclic, Basemap')
+
ip.ex('from mpl_toolkits.basemap import shiftgrid, addcyclic, Basemap')
    ip.ex('from netCDF4 import Dataset as NetCDFFile')
+
ip.ex('from netCDF4 import Dataset as NetCDFFile')
    ip.ex('plt.ion()') #make sure interactive plotting is turned on
+
ip.ex('plt.ion()') #make sure interactive plotting is turned on
  
 
#the config files will close with a call to main()
 
#the config files will close with a call to main()
Line 35: Line 35:
  
 
</source>
 
</source>
 +
 +
[[Category:Tools]][[Category:Software]][[Category:Python]]

Revision as of 17:34, 30 January 2015

Setting up Python Paths

Python is a programming language that comes with 'batteries included', meaning there are many existing tools (called modules) for your use. For meteorological and climate data analysis, many of the tools have significant library dependencies, and it can be a challenge to build the modules (for instance netcdf requires the zlib compression libraries for HDF4/5 while grib requires the jasper libraries for compression).

Many of the modules have been built already and will work for the main transport group servers: billy, sjanten, tyr, tor, nordre, etc.

In order to use these modules, you need to add the appropriate python path to you shell.

The easiest way to accomplish this is to add the following line somewhere in your .bashrc file::

   source /xnilu_wrk/flex_wrk/.flexpartrc

This file will add a PYTHONPATH environment variable, but will also set the GRIB paths and add to your own $PATH. Read the header of the file for further information.

Customizing Ipython (OLD VERSION)

Ipython is a powerful interpreter for Python. It is highly recommended to use this for developing and testing code snippets while you are writing your modules and scripts.

Once you are logged on to a server and have sourced the file above, you should be able to import the modules. A handy shortcut is to customize your Ipython environment and automatically import some of these modules. To do this, you simply need to edit the file: ~/.ipython/ipy_user_conf.py in your $HOME directory. In the main function, you can add some import statements in the following way:
# Most of your config files and extensions will probably start with this import

import IPython.ipapi
ip = IPython.ipapi.get()
 

def main():
 ip.ex('import numpy as np')
 ip.ex('import matplotlib.pyplot as plt')
 ip.ex('from mpl_toolkits.basemap import shiftgrid, addcyclic, Basemap')
 ip.ex('from netCDF4 import Dataset as NetCDFFile')
 ip.ex('plt.ion()') #make sure interactive plotting is turned on

#the config files will close with a call to main()
main()