Skip to content

Instantly share code, notes, and snippets.

@dstuebe
Last active December 19, 2015 16:49
Show Gist options
  • Save dstuebe/5986519 to your computer and use it in GitHub Desktop.
Save dstuebe/5986519 to your computer and use it in GitHub Desktop.
Pretty crazy test file for netCDF
from netCDF4 import Dataset
import numpy
rootgrp = Dataset('test.nc','w')
### Create some groups
group1 = rootgrp.createGroup('g1')
group2 = rootgrp.createGroup('g2')
group3 = rootgrp.createGroup('really_long group_name')
# a nested group
group2_1 = group2.createGroup('g2_1')
### Create some dimensions
# unlimited
## Spaces are not acceptable in netcdf Java dimension names
#dim1 = rootgrp.createDimension('bad name', None)
dim1 = rootgrp.createDimension('bad_name', None)
dim2 = rootgrp.createDimension('good_name', None)
# limited
dim3 = rootgrp.createDimension('funny_dim_name?!@#$%^&*()_-+""{}', 3)
dim4 = rootgrp.createDimension('dim4', 4)
# in other groups
g1_dim1 = group1.createDimension('same_dim', 1)
g1_dim2 = group1.createDimension('diff_dim', None)
# Should be allowed to use the same name in a different part of the tree?
g2_1_dim1 = group2_1.createDimension('same_dim', 3)
g2_1_dim2 = group2_1.createDimension('diff_dim', None)
### Create some attributes
att_dict = {
'foo' : 'bar',
'foo_byte' : numpy.iinfo(numpy.int8).max,
'foo_short' : numpy.iinfo(numpy.int16).max,
'foo_int' : numpy.iinfo(numpy.int32).max,
'foo_float' : numpy.float32(numpy.pi),
'foo_long' : numpy.iinfo(numpy.int64).max,
'foo_double' : numpy.float64(numpy.pi),
'foo funny name': 'fun string?!@#$%^&*()_-+""{}'
}
rootgrp.setncatts(att_dict)
#create some attributes in a group
group3.setncatts(att_dict)
### Create some variables (with atts)
def add_vars_to_grp(grp,types, **kwargs):
v = grp.createVariable(kwargs.get('var1','var1'),numpy.int8)
v[:] = numpy.int8(8)
v.foo = 'bar'
v = grp.createVariable(kwargs.get('var2','var2'),numpy.int8, (dim3._name,), fill_value=5)
v[:] = numpy.int8(8)
v.foo = 'bar'
v = grp.createVariable(kwargs.get('var3','var3'),numpy.int8, (dim1._name,dim4._name,))
v[:] = numpy.arange(8,dtype=numpy.int8).reshape(2,4)
v.foo = 'bar'
v = grp.createVariable(kwargs.get('var4','var4'),'S1', (dim1._name,dim4._name,))
#v[:] = numpy.ndarray(8,dtype='S1').reshape(2,4)
v[:] = 'a'
v.foo = 'bar'
for num,type in enumerate(types):
default_name = 'var{}'.format(num+5)
print default_name
v = grp.createVariable(kwargs.get(default_name,default_name),type, (dim4._name,))
try:
v[:] = numpy.iinfo(type).max
continue
except ValueError:
pass
try:
v[:] = numpy.pi
continue
except IndexError:
pass
v[0] = 'c'
v[1] = 'h'
v[2] = 'a'
v[3] = 'r'
types = [numpy.int8,numpy.int16,numpy.int32,numpy.int64,numpy.float32,numpy.float64,numpy.string_]
add_vars_to_grp(rootgrp,types)
add_vars_to_grp(group1,types,var1='foobar',var2='moobar',var3='long funny name!@#$%^&*()_=+""{}')
add_vars_to_grp(group2_1,types)
# Make a var using nested dimensions...
v = group2_1.createVariable('crazy_dims',numpy.int8, (dim1._name,dim2._name,dim3._name,g2_1_dim1._name), fill_value=5)
rootgrp.close()
<?xml version="1.0" encoding="UTF-8"?>
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:/Users/dstuebe/Documents/Dev/code/petulant-bear/test.nc">
<dimension name="bad_name" length="2" isUnlimited="true" />
<dimension name="good_name" length="0" isUnlimited="true" />
<dimension name="funny_dim_name?!@#$%^&amp;*()_-+&quot;&quot;{}" length="3" />
<dimension name="dim4" length="4" />
<attribute name="foo_double" type="double" value="3.141592653589793" />
<attribute name="foo_funny_name" value="fun string?!@#$%^&amp;*()_-+&quot;&quot;{}" />
<attribute name="foo_float" type="float" value="3.1415927" />
<attribute name="foo_long" type="long" value="9223372036854775807" />
<attribute name="foo_byte" type="long" value="127" />
<attribute name="foo_short" type="long" value="32767" />
<attribute name="foo_int" type="long" value="2147483647" />
<attribute name="foo" value="bar" />
<variable name="var1" shape="" type="byte">
<attribute name="foo" value="bar" />
</variable>
<variable name="var2" shape="funny_dim_name?!@#$%^&amp;*()_-+&quot;&quot;{}" type="byte">
<attribute name="foo" value="bar" />
<attribute name="_FillValue" type="byte" value="5" />
</variable>
<variable name="var3" shape="bad_name dim4" type="byte">
<attribute name="foo" value="bar" />
<attribute name="_ChunkSize" type="int" value="1 4" />
</variable>
<variable name="var4" shape="bad_name dim4" type="char">
<attribute name="foo" value="bar" />
<attribute name="_ChunkSize" type="int" value="1 4" />
</variable>
<variable name="var5" shape="dim4" type="byte" />
<variable name="var6" shape="dim4" type="short" />
<variable name="var7" shape="dim4" type="int" />
<variable name="var8" shape="dim4" type="long" />
<variable name="var9" shape="dim4" type="float" />
<variable name="var10" shape="dim4" type="double" />
<variable name="var11" shape="dim4" type="String" />
<group name="g1">
<dimension name="same_dim" length="1" />
<dimension name="diff_dim" length="0" isUnlimited="true" />
<variable name="foobar" shape="" type="byte">
<attribute name="foo" value="bar" />
</variable>
<variable name="moobar" shape="funny_dim_name?!@#$%^&amp;*()_-+&quot;&quot;{}" type="byte">
<attribute name="_FillValue" type="byte" value="5" />
<attribute name="foo" value="bar" />
</variable>
<variable name="long_funny_name!@#$%^&amp;*()_=+&quot;&quot;{}" shape="bad_name dim4" type="byte">
<attribute name="foo" value="bar" />
<attribute name="_ChunkSize" type="int" value="1 4" />
</variable>
<variable name="var4" shape="bad_name dim4" type="char">
<attribute name="foo" value="bar" />
<attribute name="_ChunkSize" type="int" value="1 4" />
</variable>
<variable name="var5" shape="dim4" type="byte" />
<variable name="var6" shape="dim4" type="short" />
<variable name="var7" shape="dim4" type="int" />
<variable name="var8" shape="dim4" type="long" />
<variable name="var9" shape="dim4" type="float" />
<variable name="var10" shape="dim4" type="double" />
<variable name="var11" shape="dim4" type="String" />
</group>
<group name="g2">
<group name="g2_1">
<dimension name="same_dim" length="3" />
<dimension name="diff_dim" length="0" isUnlimited="true" />
<variable name="var1" shape="" type="byte">
<attribute name="foo" value="bar" />
</variable>
<variable name="var2" shape="funny_dim_name?!@#$%^&amp;*()_-+&quot;&quot;{}" type="byte">
<attribute name="_FillValue" type="byte" value="5" />
<attribute name="foo" value="bar" />
</variable>
<variable name="var3" shape="bad_name dim4" type="byte">
<attribute name="foo" value="bar" />
<attribute name="_ChunkSize" type="int" value="1 4" />
</variable>
<variable name="var4" shape="bad_name dim4" type="char">
<attribute name="foo" value="bar" />
<attribute name="_ChunkSize" type="int" value="1 4" />
</variable>
<variable name="var5" shape="dim4" type="byte" />
<variable name="var6" shape="dim4" type="short" />
<variable name="var7" shape="dim4" type="int" />
<variable name="var8" shape="dim4" type="long" />
<variable name="var9" shape="dim4" type="float" />
<variable name="var10" shape="dim4" type="double" />
<variable name="var11" shape="dim4" type="String" />
<variable name="crazy_dims" shape="bad_name good_name funny_dim_name?!@#$%^&amp;*()_-+&quot;&quot;{} same_dim" type="byte">
<attribute name="_FillValue" type="byte" value="5" />
<attribute name="_ChunkSize" type="int" value="1 1 3 3" />
</variable>
</group>
</group>
<group name="really_long_group_name">
<attribute name="foo_double" type="double" value="3.141592653589793" />
<attribute name="foo_funny_name" value="fun string?!@#$%^&amp;*()_-+&quot;&quot;{}" />
<attribute name="foo_float" type="float" value="3.1415927" />
<attribute name="foo_long" type="long" value="9223372036854775807" />
<attribute name="foo" value="bar" />
<attribute name="foo_byte" type="long" value="127" />
<attribute name="foo_short" type="long" value="32767" />
<attribute name="foo_int" type="long" value="2147483647" />
</group>
</netcdf>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment