Differences between revisions 1 and 2
Revision 1 as of 2012-09-13 00:15:44
Size: 3418
Editor: MikhailItkin
Comment:
Revision 2 as of 2012-09-13 00:16:07
Size: 3428
Editor: MikhailItkin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
{{{
Line 134: Line 135:
}}}

   1 #!/usr/bin/env python 
   2 #               use "env" command to make sure you use the right python interpreter
   3 # -*- coding: utf-8 -*-
   4 #               allows usage of non-ASCII characters in scripts
   5 
   6 
   7 # Add general information below
   8 
   9 '''
  10 FILENAME:
  11     ncreader.py
  12 
  13 DESCRIPTION:
  14     Introduction to various python objects
  15     Python Seminar 13.09.2012
  16 
  17 '''
  18 
  19 # for python objects naming conventions and style see PEP8:
  20 #  http://www.python.org/dev/peps/pep-0008/
  21 
  22 # If you use this file as a importable module for other programs than
  23 # the information below is useful to describe 
  24 __author__ = "A. U. Thor"
  25 __version__ = "0.0"
  26 __date__ = "0000/00/00"
  27 
  28 
  29 import sys
  30 import os
  31 from optparse import OptionParser
  32 import Nio
  33 import numpy as np
  34 import matplotlib.pyplot as plt
  35 import pdb
  36 import cPickle as cP
  37 
  38 
  39 
  40 # below is a simple class that provides some convenience functions
  41 class NcReader:
  42         
  43         ''' As an example, this particular Class is basically 
  44                 a set of convenience functions that are meant 
  45                 to speed up some of your routine work with NetCDF files '''
  46         
  47         def __init__(self, file_path):
  48                 ''' Initialize object '''
  49                 self.fpath  = file_path
  50                 self.ncfile = Nio.open_file(self.fpath)
  51                 self.variables = self.ncfile.variables
  52         
  53         def printVars(self):
  54                 ''' prints variables dictionary ''' 
  55                 for i in self.variables: 
  56                         print i
  57         
  58         def plotVar(self, var_name):
  59                 ''' plots single 1d variable '''
  60                 variable = self.ncfile.variables[var_name][:]
  61                 plt.plot(variable)
  62                 plt.show()
  63                 
  64         def plot2dVar(self, *args, **kwargs):
  65                 '''plots multiple 2d variables '''
  66                 
  67                 # args is a tuple
  68                 # kwargs is a keyworded dictionary
  69                 
  70                 for i in args:
  71                         try:
  72                                 var = self.variables[i]
  73                                 print var[:].shape
  74                                 plt.imshow(self.variables[i][0,:])
  75                         except:
  76                                 raise #KeyError("variable %s not found" %i)
  77                         
  78                         if kwargs['colorbar'] == True:
  79                                 plt.colorbar()  
  80                         plt.show()
  81                         
  82 
  83 # "parse_options" is meant to parse command line arguments and options
  84 # there can be unknown number of arguments, but options are predefined
  85 # mandatory arguments are actually options
  86 
  87 def parse_options():
  88     
  89     ''' Parse command line options and arguments '''
  90 
  91     usage = "usage: %prog [options] <input_files>";
  92     version = "%prog 0.1"
  93     parser = OptionParser(usage)
  94     
  95     parser.add_option("-d", "--pdb_switch", dest = 'pdb_switch',
  96                        action = "store_true", default = "False")
  97     parser.add_option("-i", "--input_file", dest = 'input_file')
  98                                                 
  99     (options, args) = parser.parse_args()
 100     
 101     return (options, args)
 102 
 103 
 104 # It is a good practice to separate code execution and objects like classes
 105 # and functions
 106 # Main function is used for executing python code as a standalone program
 107 #  
 108 def main():
 109 
 110     options, arguments = parse_options()
 111 
 112     input_file = options.input_file
 113     
 114     variables = arguments[:]
 115     
 116     a = NcReader(input_file)
 117     a.printVars()
 118     
 119     # keyworded arguments - normally used as options for fuctions
 120     # non keyworded arguments - just arguments
 121     a.plotVar('plev')
 122     a.plot2dVar(*variables, colorbar = True)
 123     
 124     # debugger?
 125     pdb_switch = options.pdb_switch
 126 
 127 
 128 # The following structure is useful if 
 129 # the code to be run as a standalone program AND a module which 
 130 # can be imported in other python scripts
 131 if __name__ == "__main__":
 132     main()

LehreWiki: PythonCourse/PythonLES/SeminarLog5 (last edited 2012-09-13 00:22:20 by MikhailItkin)