0

I have a compressed HDF file (HDF.Z) and would like to open it like that:

from subprocess import Popen, PIPE
f = Popen(['zcat', 'myfile.HDF.Z'], stdout=PIPE).stdout

In order to get the data I need to use pyhdf:

from pyhdf.SD import SD, SDC
mydata = SD(f, SDC.READ)

However, this results in an error message:

*** TypeError: coercing to Unicode: need string or buffer, file found

Is there a way to open this file as a buffer to read it in? Btw: what is a buffer?

5
  • Shouldn't 'myfile.hdf' be 'myfile.hdf.z'? Commented Feb 5, 2014 at 11:15
  • @hjpotter92 yes I changed that in the question. Commented Feb 5, 2014 at 11:17
  • @perreal how do I give it the content of the file? Commented Feb 5, 2014 at 11:17
  • @perreal No. It wants the file name. Commented Feb 5, 2014 at 11:21
  • Since the file is extracted via zcat and opened with Popen I only have the stdout? Commented Feb 5, 2014 at 11:26

1 Answer 1

1

At a short glance, I found no way to make it access an open stream.

You can do the following:

  • Create a temporary file where you unpack the file.
  • Give the name of this temporary file to SD().

Another option is very system dependent: you could take the file handle and do

SD('/dev/fd/%d' % f.fileno(), SDC.READ)

bit this is very platform-dependent (Linux only) and, if SD() does mmap() by any chance, it will fail.

Sign up to request clarification or add additional context in comments.

1 Comment

The temporary unpacking of the file works, although this takes more time (considering the fact that I perform this action on hundreds of files).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.