Last active
November 10, 2015 16:52
-
-
Save kwilcox/a485f7ae45ac3e7d8efd to your computer and use it in GitHub Desktop.
netCDF4-python dataset objects getting garbaged collected inside context manager?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env conda-execute | |
# coding=utf-8 | |
""" | |
Testing netCDF4 garbage collection differences between 2.7 and 3.4 | |
**Fails** | |
- python 3.4 / netCDF4-python 1.2.1, 1.2.0, 1.1.9, and 1.1.8 | |
**Passes** | |
- python 3.4 / netCDF4-python 1.1.6 | |
- python 2.7 / netCDF4-python 1.2.1, 1.2.0, 1.1.9, 1.1.8, 1.1.6 | |
""" | |
# conda execute | |
# env: | |
# - python 3.4.* | |
# - netcdf4 1.2.1 | |
# - cf_units 1.0.0 | |
# channels: | |
# - ioos | |
# run_with: python | |
import netCDF4 | |
import cf_units as unit | |
units = [ unit.Unit(x) for x in [ | |
'%', | |
'A', | |
'1e-3', | |
'pascals', | |
'V', | |
'W.m-2', | |
'cm.s-1', | |
'degrees', | |
'1', | |
'ft', | |
'ft.s-1', | |
'ft3.s-1', | |
'hectopascal', | |
'inch_Hg', | |
'inch', | |
'kg.m-2', | |
'kg.m-3', | |
'knot', | |
'm', | |
'm.s-1', | |
'm3.s-1', | |
'mS.cm-1', | |
'millibars', | |
'mg.L-1', | |
'mm', | |
'mmole.m-2', | |
'mile.hour-1', | |
'pH', | |
'.001', | |
'1e-6', | |
's', | |
'degree_Celsius', | |
'degree_Fahrenheit', | |
'microatm', | |
'microg.L-1' | |
]] | |
with netCDF4.Dataset('axiom_cruise_profiles.nc', 'a') as nc: | |
for vname in nc.variables.keys(): | |
v = nc.variables[vname] | |
if not hasattr(v, 'units'): | |
continue | |
try: | |
un = unit.Unit(v.units) | |
except ValueError: | |
continue | |
for oku in units: | |
if un.is_convertible(oku): | |
v[:] = un.convert(v[:], oku) | |
print("Converted {} from {} to {}".format(v.name, v.units, oku.format(unit.UT_NAMES))) | |
v.units = oku.format(unit.UT_NAMES) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment