Skip to content

Commit

Permalink
Merge pull request #56 from MoonRaker/master
Browse files Browse the repository at this point in the history
Fix: ncss netcdf temp file permission error on Windows
  • Loading branch information
dopplershift committed Oct 19, 2015
2 parents a0ebefb + ba21c67 commit 821106b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions siphon/ncss.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import xml.etree.ElementTree as ET
import atexit
from io import BytesIO
from os import remove
import platform

import numpy as np

Expand Down Expand Up @@ -341,16 +344,35 @@ def parse_xml_dataset(elem, handle_units):
@response_handlers.register('application/x-netcdf')
@response_handlers.register('application/x-netcdf4')
def read_netcdf(data, handle_units): # pylint:disable=unused-argument
with NamedTemporaryFile() as tmp_file:
tmp_file.write(data)
tmp_file.flush()
return Dataset(tmp_file.name, 'r')
ostype = platform.architecture()
if ostype[1].lower() == 'windowspe':
with NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(data)
tmp_file.flush()
atexit.register(deletetempfile, tmp_file.name)
return Dataset(tmp_file.name, 'r')
else:
with NamedTemporaryFile() as tmp_file:
tmp_file.write(data)
tmp_file.flush()
return Dataset(tmp_file.name, 'r')

except ImportError:
import warnings
warnings.warn('netCDF4 module not installed. '
'Will be unable to handle NetCDF returns from NCSS.')


def deletetempfile(fname):
try:
remove(fname)
except OSError:
import warnings
warnings.warn('temporary netcdf dataset file not deleted. '
'to delete temporary dataset file in the future '
'be sure to use dataset.close() when finished.')


# Parsing of CSV data returned from NCSS
@response_handlers.register('text/plain')
def parse_csv_response(data, unit_handler):
Expand Down

0 comments on commit 821106b

Please sign in to comment.