# Add general information below
FILENAME: DESCRIPTION:
# for python objects naming conventions and style see PEP8: # http://www.python.org/dev/peps/pep-0008/
# If you use this file as a importable module for other programs than # the information below is useful to describe author = "A. U. Thor" version = "0.0" date = "0000/00/00"
import sys import os from optparse import OptionParser import Nio import numpy as np import matplotlib.pyplot as plt import pdb import cPickle as cP
# below is a simple class that provides some convenience functions class NcReader:
As an example, this particular Class is basically
- a set of convenience functions that are meant
to speed up some of your routine work with NetCDF files
- a set of convenience functions that are meant
def init(self, file_path):
Initialize object self.fpath = file_path self.ncfile = Nio.open_file(self.fpath) self.variables = self.ncfile.variables
prints variables dictionary for i in self.variables:
- print i
plots single 1d variable variable = self.ncfile.variables[var_name][:] plt.plot(variable) plt.show()
plots multiple 2d variables # args is a tuple # kwargs is a keyworded dictionary for i in args:
- try:
- var = self.variables[i] print var[:].shape plt.imshow(self.variables[i][0,:])
raise #KeyError("variable %s not found" %i)
- plt.colorbar()
- try:
# "parse_options" is meant to parse command line arguments and options # there can be unknown number of arguments, but options are predefined # mandatory arguments are actually options
def parse_options():
Parse command line options and arguments
usage = "usage: %prog [options] <input_files>"; version = "%prog 0.1" parser = OptionParser(usage) parser.add_option("-d", "--pdb_switch", dest = 'pdb_switch',
- action = "store_true", default = "False")
# It is a good practice to separate code execution and objects like classes # and functions # Main function is used for executing python code as a standalone program # def main():
- options, arguments = parse_options() input_file = options.input_file variables = arguments[:]
a = NcReader(input_file) a.printVars() # keyworded arguments - normally used as options for fuctions # non keyworded arguments - just arguments a.plotVar('plev') a.plot2dVar(*variables, colorbar = True) # debugger? pdb_switch = options.pdb_switch
# The following structure is useful if # the code to be run as a standalone program AND a module which # can be imported in other python scripts if name == "main":
- main()