File: test_SD.py

package info (click to toggle)
python-hdf4 0.11.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,328 kB
  • sloc: ansic: 9,252; python: 3,445; makefile: 192
file content (72 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python

import numpy as np
import os
import pyhdf.SD
import shutil
import tempfile
from numpy.testing import assert_array_equal
from pathlib import Path
from pyhdf.SD import SDC

def test_long_varname():
    sds_name = 'a'*255

    temp = tempfile.mkdtemp(prefix='pyhdf_')
    try:
        path = os.path.join(temp, "test.hdf")

        # create a file with a long variable name
        sd = pyhdf.SD.SD(path, SDC.WRITE|SDC.CREATE|SDC.TRUNC)
        sds = sd.create(sds_name, SDC.FLOAT32, (3,))
        sds[:] = range(10, 13)
        sds.endaccess()
        sd.end()

        # check we can read the variable name
        sd = pyhdf.SD.SD(path)
        sds = sd.select(sds_name)
        name, _, _, _, _ = sds.info()
        sds.endaccess()
        sd.end()
        assert sds_name == name
    finally:
        shutil.rmtree(temp)

def test_negative_int8():
    temp = tempfile.mkdtemp(prefix='pyhdf_')
    try:
        path = os.path.join(temp, "test.hdf")

        sd = pyhdf.SD.SD(path, SDC.WRITE|SDC.CREATE|SDC.TRUNC)
        data = np.zeros(shape=(20,20), dtype=np.int8)
        sds = sd.create("testsds", SDC.INT8, data.shape)
        sds.setfillvalue(-1)
        assert sds.getfillvalue() == -1

        sds.setrange(-50, -30)
        min, max = sds.getrange()
        assert min == -50
        assert max == -30

        attr = sds.attr("testattr")
        attr.set(SDC.INT8, -1)
        assert attr.get() == -1

        dim = sds.dim(0)
        scale = [-1]*20
        dim.setscale(SDC.INT8, scale)
        assert_array_equal(dim.getscale(), scale)

        sds[:,:] = -40
        sd.end()
    finally:
        shutil.rmtree(temp)

def test_char():
    with tempfile.TemporaryDirectory() as temp_dir:
        hdf_file = str(Path(temp_dir) / "test.hdf")
        sd = pyhdf.SD.SD(hdf_file, SDC.WRITE | SDC.CREATE)
        sds = sd.create("test_sds", SDC.CHAR, [5])
        sds[:] = "ABCDE"
        assert_array_equal(sds[:], np.array(list("ABCDE"), "S2"))