netCDF_example.py :  » Database » PyTables » tables-2.1.2 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyTables 
PyTables » tables 2.1.2 » examples » netCDF_example.py
### bogus example to illustrate the use of tables.netcdf3
### Author: Jeff Whitaker

import tables.netcdf3 as NetCDF
import time

history = 'Created ' + time.ctime(time.time())
file = NetCDF.NetCDFFile('test.h5', 'w', history=history)
file.createDimension('level', 12)
file.createDimension('time', None)
file.createDimension('lat', 90)
print '**dimensions**'
print file.dimensions

times = file.createVariable('time','d',('time',))
levels = file.createVariable('level','i',('level',))
latitudes = file.createVariable('latitude','f',('lat',))
temp = file.createVariable('temp','f',('time','level','lat',))
# try this to see how much smaller the file gets.
#temp = file.createVariable('temp','f',('time','level','lat',),least_significant_digit=1)
pressure = file.createVariable('pressure','i',('level','lat',))
print '**variables**'
print file.variables

file.description = 'bogus example to illustrate the use of tables.netcdf3'
file.source = 'PyTables Users Guide'
latitudes.units = 'degrees north'
pressure.units = 'hPa'
temp.units = 'K'
times.units = 'days since January 1, 2005'
times.scale_factor = 1
print '**global attributes**'
for name in file.ncattrs():
    print 'Global attr', name, '=', getattr(file,name)

import numpy
levels[:] = numpy.arange(12)+1
latitudes[:] = numpy.arange(-89,90,2)
for lev in levels[:]:
    pressure[:,:] = 1000.-100.*lev
print 'levels = ',levels[:]
print 'latitudes =\n',latitudes[:]
for n in range(10):
    times.append(n)
print 'times = ',times[:]
print 'temp.shape before sync = ',temp.shape
file.sync()
print 'temp.shape after sync = ',temp.shape
for n in range(10):
    temp[n] = 10.*numpy.random.random_sample(pressure.shape)
    print 'time, min/max temp, temp[n,0,0] = ',times[n],min(temp[n].flat),max(temp[n].flat),temp[n,0,0]
# print a summary of the file contents
print file

# Check conversions between netCDF <--> HDF5 formats
if NetCDF.ScientificIONetCDF_imported:
    scale_factor = {'temp': 1.75e-4}
    add_offset = {'temp': 5.}
    file.h5tonc('test.nc',packshort=True,scale_factor=scale_factor,add_offset=add_offset)
    file.close()
    history = 'Convert from netCDF ' + time.ctime(time.time())
    file = NetCDF.NetCDFFile('test2.h5', 'w', history=history)
    nobjects, nbytes = file.nctoh5('test.nc',unpackshort=True)
    print nobjects,' objects converted from netCDF, totaling',nbytes,'bytes'
    temp = file.variables['temp']
    times = file.variables['time']
    print 'temp.shape after h5 --> netCDF --> h5 conversion = ',temp.shape
    for n in range(10):
        print 'time, min/max temp, temp[n,0,0] = ',times[n],min(temp[n].flat),max(temp[n].flat),temp[n,0,0]
    file.close()
else:
    print 'skipping netCDF <--> h5 conversion since Scientific.IO.NetCDF not available'
    file.close()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.