Former-commit-id:2b462d8665
[formerly133dc97f67
] [formerlya02aeb236c
] [formerlya02aeb236c
[formerly9f19e3f712
]] [formerly2b462d8665
[formerly133dc97f67
] [formerlya02aeb236c
] [formerlya02aeb236c
[formerly9f19e3f712
]] [formerly06a8b51d6d
[formerlya02aeb236c
[formerly9f19e3f712
] [formerly06a8b51d6d
[formerly 64fa9254b946eae7e61bbc3f513b7c3696c4f54f]]]]] Former-commit-id:06a8b51d6d
Former-commit-id:2c3569dd39
[formerly9bb8decbcf
] [formerly8e80217e59
] [formerlye2ecdcfe33
[formerly377dcd10b9
] [formerly8e80217e59
[formerly3360eb6c5f
]]] Former-commit-id:e2ecdcfe33
[formerly377dcd10b9
] Former-commit-id:e2ecdcfe33
Former-commit-id:7dbd17a5aa
61 lines
1.1 KiB
Python
Executable file
61 lines
1.1 KiB
Python
Executable file
from Scientific.N import *
|
|
from Scientific.IO.NetCDF import *
|
|
import time
|
|
|
|
|
|
def getUserName():
|
|
try:
|
|
import os, pwd, string
|
|
except ImportError:
|
|
return 'unknown user'
|
|
pwd_entry = pwd.getpwuid(os.getuid())
|
|
name = string.strip(string.splitfields(pwd_entry[4], ',')[0])
|
|
if name == '':
|
|
name = pwd_entry[0]
|
|
return name
|
|
|
|
#
|
|
# Creating a file
|
|
#
|
|
file = NetCDFFile('test.nc', 'w', 'Created ' + time.ctime(time.time())
|
|
+ ' by ' + getUserName())
|
|
|
|
file.title = "Just some useless junk"
|
|
file.version = 42
|
|
|
|
file.createDimension('xyz', 3)
|
|
file.createDimension('n', 20)
|
|
file.createDimension('t', None) # unlimited dimension
|
|
|
|
foo = file.createVariable('foo', Float, ('n', 'xyz'))
|
|
foo[:,:] = 0.
|
|
foo[0,:] = [42., 42., 42.]
|
|
foo[:,1] = 1.
|
|
foo.units = "arbitrary"
|
|
print foo[0]
|
|
print foo.dimensions
|
|
|
|
bar = file.createVariable('bar', Int, ('t', 'n'))
|
|
for i in range(10):
|
|
bar[i] = i
|
|
print bar.shape
|
|
|
|
print file.dimensions
|
|
print file.variables
|
|
|
|
file.close()
|
|
|
|
#
|
|
# Reading a file
|
|
#
|
|
file = NetCDFFile('test.nc', 'r')
|
|
|
|
print file.dimensions
|
|
print file.variables
|
|
|
|
foo = file.variables['foo']
|
|
foo_array = foo[:]
|
|
foo_units = foo.units
|
|
print foo[0]
|
|
|
|
file.close()
|