Пример #1
0
    def openFile(self):
        """ the H5 file opening

        :brief: It opens the H5 file
        """

        try:
            self.closeFile()
        except Exception:
            self._streams.warn(
                "TangoDataWriter::openFile() - File cannot be closed")

        wrmodule = self.__setWriter(self.writer)
        self.__nxFile = None
        self.__eFile = None
        self.__initPool = None
        self.__stepPool = None
        self.__finalPool = None
        self.__triggerPools = {}
        self.__currentfileid = 0

        self.__pars = self.__getParams(self.writer)
        pars = dict(self.__pars)
        pars["writer"] = wrmodule
        if os.path.isfile(self.__fileName):
            self.__nxFile = FileWriter.open_file(self.__fileName, False,
                                                 **pars)
        else:
            self.__nxFile = FileWriter.create_file(self.__fileName, **pars)
        self.__fileprefix, self.__fileext = os.path.splitext(
            str(self.__fileName))
        self.__nxRoot = self.__nxFile.root()
        self.__nxRoot.stepsperfile = self.stepsperfile
        self.__nxRoot.currentfileid = self.__currentfileid

        #: element file objects
        self.__eFile = EFile([], None, self.__nxRoot)

        last = self.__eFile
        for gname, gtype in self.__parents:
            last = EGroup({
                "name": gname,
                "type": gtype
            },
                          last,
                          reloadmode=True)
            self.__nxPath.append(last)

        if self.addingLogs:
            name = "nexus_logs"
            if not self.__nxRoot.exists(name):
                ngroup = self.__nxRoot.create_group(name, "NXcollection")
            else:
                ngroup = self.__nxRoot.open(name)
            name = "configuration"
            error = True
            counter = 1
            cname = name
            while error:
                cname = name if counter == 1 else \
                    ("%s_%s" % (name, counter))
                if not ngroup.exists(cname):
                    error = False
                else:
                    counter += 1
            self.__logGroup = ngroup.create_group(cname, "NXcollection")
            vfield = self.__logGroup.create_field("python_version", "string")
            vfield.write(str(sys.version))
            vfield.close()
Пример #2
0
    def test_xmlAttrSpectrum(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        xml = """<definition>
  <group type="NXentry" name="entry1">
    <group type="NXinstrument" name="instrument">
      <group type="NXdetector" name="detector">
        <attribute type="NX_FLOAT" name="spectrum_float">
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="STEP"/>
           2.344e-4 234.34 -34.4e+3  -0.34
        </attribute>
        <attribute type="NX_INT32" name="init_spectrum_int32">
          <dimensions rank="1" />
          <strategy mode="INIT"/>
            1 2 3 4 5
        </attribute>
        <attribute type="NX_BOOLEAN" name="spectrum_bool">
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="STEP"/>
          True False 1  0
        </attribute>

      </group>
      <field type="NX_FLOAT" name="counter">
        <attribute type="NX_FLOAT32" name="spectrum_float32">
          <dimensions rank="1" />
          <strategy mode="STEP"/>
           2.344e-4 234.34 -34.4e+3  -0.34
        </attribute>
        <attribute type="NX_UINT64" name="final_spectrum_uint64">
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
          <strategy mode="FINAL"/>
            1 2 3 4 5
        </attribute>
        <attribute type="NX_BOOLEAN" name="final_spectrum_bool">
          <dimensions rank="1" />
          <strategy mode="FINAL"/>
          True False 1  0
        </attribute>
        1.2
      </field>
    </group>
  </group>
</definition>
"""

        #        <attribute type="NX_CHAR" name="flag_spectrum_string">
        #          <dimensions rank="1">
        #            <dim value="8" index="1"/>
        #          </dimensions>
        #        </attribute>

        spec = [2.344e-4, 234.34, -34.4e+3, -0.34]
        ins = [1, 2, 3, 4, 5]
        ls = [True, False, True, False]

        tdw = self.openWriter(fname, xml)
        for i in range(3):
            self.record(tdw, '{ }')

        self.closeWriter(tdw)

        # check the created file
        FileWriter.writer = H5PYWriter
        f = FileWriter.open_file(fname, readonly=True)
        det, field = self._sc.checkAttributeTree(f, fname, 3, 3)
        self._sc.checkSpectrumAttribute(det,
                                        "spectrum_float",
                                        "float64",
                                        spec,
                                        error=1.e-14)
        self._sc.checkSpectrumAttribute(det, "init_spectrum_int32", "int32",
                                        ins)
        self._sc.checkSpectrumAttribute(det, "spectrum_bool", "bool", ls)
        self._sc.checkSpectrumAttribute(field,
                                        "spectrum_float32",
                                        "float32",
                                        spec,
                                        error=1.e-5)
        self._sc.checkSpectrumAttribute(field, "final_spectrum_uint64",
                                        "uint64", ins)
        self._sc.checkSpectrumAttribute(field, "final_spectrum_bool", "bool",
                                        ls)
        # NOT SUPPORTED BY PNINX
        # self._sc.checkSpectrumAttribute(field, "flag_spectrum_string", "string",
        # logical)

        f.close()
        os.remove(fname)
Пример #3
0
    def test_xmlImage(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        xml = """<definition>
  <group type="NXentry" name="entry1">
    <group type="NXinstrument" name="instrument">
      <group type="NXdetector" name="detector">
        <field units="" type="NX_INT" name="pco_int">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP"/>
          11 12
          21 22
          31 32
        </field>

        <field units="" type="NX_INT8" name="pco_int8">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" grows="2"/>
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_INT16" name="pco_int16">
          <dimensions rank="2" />
          <strategy mode="STEP" compression="true" grows="3"/>
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_INT32" name="pco_int32">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true"  grows="2"
 shuffle="false" />
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_INT64" name="pco_int64">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true" rate="3"/>
          11 12
          21 22
          31 32
        </field>

        <field units="" type="NX_UINT" name="pco_uint">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP"/>
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_UINT8" name="pco_uint8">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" grows="3"/>
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_UINT16" name="pco_uint16">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true"/>
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_UINT32" name="pco_uint32">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true"  grows="2"
 shuffle="false" />
          11 12
          21 22
          31 32
        </field>
        <field units="" type="NX_UINT64" name="pco_uint64">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true" rate="3"  grows="3"/>
          11 12
          21 22
          31 32
        </field>



        <field units="" type="NX_FLOAT" name="pco_float">
          <dimensions rank="2">
            <dim value="2" index="1"/>
            <dim value="3" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true" rate="3"/>
          -3.43 -3.3e-1 53.1
          3.43e+02 3.13e-1 53
        </field>
        <field units="" type="NX_FLOAT32" name="pco_float32">
          <dimensions rank="2">
            <dim value="2" index="1"/>
            <dim value="3" index="2"/>
          </dimensions>
          <strategy mode="STEP" compression="true" grows="2" shuffle="true"/>
          -3.43 -3.3e-1 53.1
          3.43e+02 3.13e-1 53
        </field>
        <field units="" type="NX_FLOAT64" name="pco_float64">
          <dimensions rank="2" />
          <strategy mode="STEP" grows="3"/>
          -3.43 -3.3e-1 53.1
          3.43e+02 3.13e-1 53
        </field>
        <field units="" type="NX_NUMBER" name="pco_number">
          <dimensions rank="2">
            <dim value="2" index="1"/>
            <dim value="3" index="2"/>
          </dimensions>
          <strategy mode="STEP"  grows = "1" />
          -3.43 -3.3e-1 53.1
          3.43e+02 3.13e-1 53
        </field>



        <field units="" type="NX_DATE_TIME" name="time">
          <strategy mode="STEP" compression="true" rate="3"/>
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="4" index="2"/>
          </dimensions>
          Let's us check it,
          and test it here.
          , . : ;
        </field>
        <field units="" type="ISO8601" name="isotime">
          <strategy mode="STEP" compression="true" grows="2" shuffle="true"/>
          <dimensions rank="2" />
          Let's us check it,
          and test it here.
          , . : ;
        </field>
        <field units="" type="NX_CHAR" name="string_time">
          <strategy mode="STEP" grows="2"/>
          <dimensions rank="2"/>
          Let's us check it,
          and test it here.
          , . : ;
        </field>

        <field units="" type="NX_BOOLEAN" name="flags">
          <strategy mode="STEP"/>
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="4" index="2"/>
          </dimensions>
         True False 1 0
         FALSE TRUE false true
         0 1 1 0
        </field>

        <field units="" type="NX_BOOLEAN" name="flags_dim">
          <strategy mode="STEP"/>
          <dimensions rank="2" />
         True False 1 0
         FALSE TRUE false true
         0 1 1 0
        </field>




      </group>
    </group>
  </group>
</definition>
"""

        image = [[11, 12], [21, 22], [31, 32]]
        fimage = [[-3.43, -3.3e-1, 53.1], [3.43e+02, 3.13e-1, 53]]
        simage = [["Let's", "us", "check", "it,"],
                  ["and", "test", "it", "here."], [",", ".", ":", ";"]]
        bimage = [[True, False, True, False], [False, True, False, True],
                  [False, True, True, False]]

        tdw = self.openWriter(fname, xml)

        for c in range(3):
            self.record(tdw, '{}')

        self.closeWriter(tdw)

        # check the created file

        FileWriter.writer = H5PYWriter
        f = FileWriter.open_file(fname, readonly=True)
        det = self._sc.checkFieldTree(f, fname, 19)
        self._sc.checkXMLImageField(det, "pco_int", "int64", "NX_INT", image)
        self._sc.checkXMLImageField(det, "pco_int8", "int8", "NX_INT8", image)
        self._sc.checkXMLImageField(det, "pco_int16", "int16", "NX_INT16",
                                    image)
        self._sc.checkXMLImageField(det, "pco_int32", "int32", "NX_INT32",
                                    image)
        self._sc.checkXMLImageField(det, "pco_int64", "int64", "NX_INT64",
                                    image)
        self._sc.checkXMLImageField(det, "pco_uint", "uint64", "NX_UINT",
                                    image)
        self._sc.checkXMLImageField(det, "pco_uint8", "uint8", "NX_UINT8",
                                    image)
        self._sc.checkXMLImageField(det, "pco_uint16", "uint16", "NX_UINT16",
                                    image)
        self._sc.checkXMLImageField(det, "pco_uint32", "uint32", "NX_UINT32",
                                    image)
        self._sc.checkXMLImageField(det, "pco_uint64", "uint64", "NX_UINT64",
                                    image)

        self._sc.checkXMLImageField(det,
                                    "pco_float",
                                    "float64",
                                    "NX_FLOAT",
                                    fimage,
                                    error=1.0e-14)
        self._sc.checkXMLImageField(det,
                                    "pco_float32",
                                    "float32",
                                    "NX_FLOAT32",
                                    fimage,
                                    error=1.0e-5)
        self._sc.checkXMLImageField(det,
                                    "pco_float64",
                                    "float64",
                                    "NX_FLOAT64",
                                    fimage,
                                    error=1.0e-14)
        self._sc.checkXMLImageField(det,
                                    "pco_number",
                                    "float64",
                                    "NX_NUMBER",
                                    fimage,
                                    error=1.0e-14)

        self._sc.checkXMLImageField(det, "flags", "bool", "NX_BOOLEAN", bimage)
        self._sc.checkXMLImageField(det, "time", "string", "NX_DATE_TIME",
                                    simage)
        self._sc.checkXMLImageField(det, "string_time", "string", "NX_CHAR",
                                    simage)
        self._sc.checkXMLImageField(det, "isotime", "string", "ISO8601",
                                    simage)
        self._sc.checkXMLImageField(det, "flags_dim", "bool", "NX_BOOLEAN",
                                    bimage)

        f.close()
        os.remove(fname)
Пример #4
0
    def test_xmlSpectrum(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        xml = """<definition>
  <group type="NXentry" name="entry1">
    <group type="NXinstrument" name="instrument">
      <group type="NXdetector" name="detector">
        <field units="" type="NX_INT" name="mca_int">
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
            1 2 3 4 5
        </field>
        <field units="" type="NX_INT8" name="mca_int8">
          <strategy mode="INIT" compression="true"/>
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
            1 2 3 4 5
        </field>
        <field units="" type="NX_INT16" name="mca_int16">
          <dimensions rank="1" />
            1 2 3 4 5
        </field>
        <field units="" type="NX_INT32" name="mca_int32">
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
            1 2 3 4 5
        </field>
        <field units="" type="NX_INT64" name="mca_int64">
          <dimensions rank="1"/>
            1 2 3 4 5
        </field>

        <field units="" type="NX_UINT" name="mca_uint">
          <strategy mode="INIT" compression="true" rate="2"/>
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
            1 2 3 4 5
        </field>
        <field units="" type="NX_UINT8" name="mca_uint8">
          <dimensions rank="1"/>
            1 2 3 4 5
        </field>
        <field units="" type="NX_UINT16" name="mca_uint16">
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
            1 2 3 4 5
        </field>
        <field units="" type="NX_UINT32" name="mca_uint32">
          <dimensions rank="1">
          </dimensions>
            1 2 3 4 5
        </field>
        <field units="" type="NX_UINT64" name="mca_uint64">
          <strategy mode="INIT" compression="true" rate="5" shuffle="false"/>
          <dimensions rank="1">
            <dim value="5" index="1"/>
          </dimensions>
            1 2 3 4 5
        </field>

        <field units="" type="NX_INT64" name="mca_int64_dim">
          <dimensions rank="1"/>
            1 2 3 4 5
        </field>



        <field units="" type="NX_FLOAT" name="mca_float">
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="INIT" compression="true" rate="3"/>
           2.344e-4 234.34 -34.4e+3  -0.34
        </field>
        <field units="" type="NX_FLOAT32" name="mca_float32">
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="INIT" compression="true" grows="2" shuffle="true"/>
           2.344e-4 234.34 -34.4e+3  -0.34
        </field>
        <field units="" type="NX_FLOAT64" name="mca_float64">
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="INIT" grows="2"/>
           2.344e-4 234.34 -34.4e+3  -0.34
        </field>
        <field units="" type="NX_NUMBER" name="mca_number">
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="INIT" />
           2.344e-4 234.34 -34.4e+3  -0.34
        </field>

        <field units="" type="NX_FLOAT" name="mca_float_dim">
          <dimensions rank="1"/>
          <strategy mode="FINAL" />
           2.344e-4 234.34 -34.4e+3  -0.34
        </field>

        <field units="" type="NX_DATE_TIME" name="time">
          <strategy mode="INIT" compression="true" rate="3"/>
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          Let's us check it,.
        </field>



        <field units="" type="ISO8601" name="isotime">
          <strategy mode="STEP" compression="true" grows="2" shuffle="true"/>
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          Let's us check it,.
        </field>
        <field units="" type="NX_CHAR" name="string_time">
          <strategy mode="STEP" grows="2"/>
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          Let's us check it,.
        </field>



        <field units="" type="NX_BOOLEAN" name="flags">
          <strategy mode="STEP"/>
          <dimensions rank="1">
            <dim value="4" index="1"/>
          </dimensions>
          <strategy mode="STEP" />
          True False 1  0
        </field>

        <field units="" type="NX_BOOLEAN" name="flags_dim">
          <dimensions rank="1" />
          <strategy mode="STEP" />
          True False 1  0
        </field>

        <field units="" type="NX_CHAR" name="string_time_dim">
          <strategy mode="STEP" grows="2"/>
          <dimensions rank="1"/>
          Let's us check it,.
        </field>








      </group>
    </group>
  </group>
</definition>
"""

        spec = [2.344e-4, 234.34, -34.4e+3, -0.34]
        string = ["Let's", "us", "check", "it,."]

        tdw = self.openWriter(fname, xml)

        for c in range(3):
            self.record(tdw, '{}')

        self.closeWriter(tdw)

        # check the created file

        FileWriter.writer = H5PYWriter
        f = FileWriter.open_file(fname, readonly=True)
        det = self._sc.checkFieldTree(f, fname, 22)
        self._sc.checkXMLSpectrumField(det,
                                       "mca_int",
                                       "int64",
                                       "NX_INT", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_INT",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det, "mca_int8", "int8", "NX_INT8",
                                       [1, 2, 3, 4, 5])
        self._sc.checkXMLSpectrumField(det,
                                       "mca_int16",
                                       "int16",
                                       "NX_INT16", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_INT16",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det,
                                       "mca_int32",
                                       "int32",
                                       "NX_INT32", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_INT32",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det,
                                       "mca_int64",
                                       "int64",
                                       "NX_INT64", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_INT64",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det, "mca_uint", "uint64", "NX_UINT",
                                       [1, 2, 3, 4, 5])
        self._sc.checkXMLSpectrumField(det,
                                       "mca_uint8",
                                       "uint8",
                                       "NX_UINT8", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_UINT8",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det,
                                       "mca_uint16",
                                       "uint16",
                                       "NX_UINT16", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_UINT16",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det,
                                       "mca_uint32",
                                       "uint32",
                                       "NX_UINT32", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_UINT32",
                                           "units": ""
                                       })
        self._sc.checkXMLSpectrumField(det, "mca_uint64", "uint64",
                                       "NX_UINT64", [1, 2, 3, 4, 5])
        self._sc.checkXMLSpectrumField(det,
                                       "mca_int64_dim",
                                       "int64",
                                       "NX_INT64", [1, 2, 3, 4, 5],
                                       attrs={
                                           "type": "NX_INT64",
                                           "units": ""
                                       })

        self._sc.checkXMLSpectrumField(det,
                                       "mca_float",
                                       "float64",
                                       "NX_FLOAT",
                                       spec,
                                       error=1.0e-14)
        self._sc.checkXMLSpectrumField(det,
                                       "mca_float_dim",
                                       "float64",
                                       "NX_FLOAT",
                                       spec,
                                       error=1.0e-14)
        self._sc.checkXMLSpectrumField(det,
                                       "mca_float32",
                                       "float32",
                                       "NX_FLOAT32",
                                       spec,
                                       error=1.0e-5)
        self._sc.checkXMLSpectrumField(det,
                                       "mca_float64",
                                       "float64",
                                       "NX_FLOAT64",
                                       spec,
                                       error=1.0e-14)
        self._sc.checkXMLSpectrumField(det,
                                       "mca_number",
                                       "float64",
                                       "NX_NUMBER",
                                       spec,
                                       error=1.0e-14)

        self._sc.checkXMLSpectrumField(det, "flags", "bool", "NX_BOOLEAN",
                                       [True, False, True, False])
        self._sc.checkXMLSpectrumField(det, "time", "string", "NX_DATE_TIME",
                                       string)
        self._sc.checkXMLSpectrumField(det, "string_time", "string", "NX_CHAR",
                                       string)
        self._sc.checkXMLSpectrumField(det, "isotime", "string", "ISO8601",
                                       string)
        self._sc.checkXMLSpectrumField(det, "string_time_dim", "string",
                                       "NX_CHAR", string)

        f.close()
        os.remove(fname)
Пример #5
0
    def test_xmlAttrScalar(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        xml = """<definition>
  <group type="NXentry" name="entry1">
    <group type="NXinstrument" name="instrument">
      <group type="NXdetector" name="detector">
        <attribute type="NX_FLOAT" name="scalar_float">
            -1.3e-1
        </attribute>
        <attribute type="NX_CHAR" name="scalar_string">
          Let's go.
        </attribute>
        <attribute type="NX_INT" name="scalar_int">
          -12
        </attribute>
        <attribute type="NX_BOOLEAN" name="flag">
           True
        </attribute>
      </group>
      <field type="NX_FLOAT" name="counter">
        <attribute type="NX_FLOAT32" name="scalar_float32">
            -1.3e-1
        </attribute>
        <attribute type="NX_CHAR" name="scalar_string">
          Let's go.
        </attribute>
        <attribute type="NX_INT8" name="scalar_int8">
          -12
        </attribute>
        1.2
      </field>
    </group>
  </group>
</definition>
"""
        fls = -1.3e-1
        ins = -12
        sts = "Let's go."
        ls = True

        tdw = self.openWriter(fname, xml)
        for i in range(3):
            self.record(tdw, '{}')

        self.closeWriter(tdw)

        # check the created file
        FileWriter.writer = H5PYWriter
        f = FileWriter.open_file(fname, readonly=True)
        det, field = self._sc.checkAttributeTree(f, fname, 4, 3)
        self._sc.checkScalarAttribute(det,
                                      "scalar_float",
                                      "float64",
                                      fls,
                                      error=1.e-14)
        self._sc.checkScalarAttribute(det, "scalar_string", "string", sts)
        self._sc.checkScalarAttribute(det, "scalar_int", "int64", ins)
        self._sc.checkScalarAttribute(det, "flag", "bool", ls)
        self._sc.checkScalarAttribute(field,
                                      "scalar_float32",
                                      "float32",
                                      fls,
                                      error=1.e-6)
        self._sc.checkScalarAttribute(field, "scalar_string", "string", sts)
        self._sc.checkScalarAttribute(field, "scalar_int8", "int8", ins)

        f.close()
        os.remove(fname)
Пример #6
0
    def test_xmlAttrImage(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        xml = """<definition>
  <group type="NXentry" name="entry1">
    <group type="NXinstrument" name="instrument">
      <group type="NXdetector" name="detector">
        <attribute type="NX_FLOAT" name="image_float">
          <dimensions rank="2">
            <dim value="2" index="1"/>
            <dim value="3" index="2"/>
          </dimensions>
          <strategy mode="STEP"/>
          -3.43 -3.3e-1 53.1
          3.43e+02 3.13e-1 53
        </attribute>

        <attribute type="NX_INT" name="image_int">
          <dimensions rank="2" />
          <strategy mode="FINAL"/>
          11 12
          21 22
          31 32
        </attribute>

        <attribute type="NX_INT32" name="image_int32">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP"/>
          11 12
          21 22
          31 32
        </attribute>



        <attribute type="NX_BOOLEAN" name="image_bool">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="4" index="2"/>
          </dimensions>
          <strategy mode="STEP"/>
         True False 1 0
         FALSE TRUE false true
         0 1 1 0
        </attribute>

      </group>
      <field type="NX_FLOAT" name="counter">
        <attribute type="NX_FLOAT32" name="image_float32">
          <dimensions rank="2" />
          <strategy mode="STEP"/>
          -3.43 -3.3e-1 53.1
          3.43e+02 3.13e-1 53
        </attribute>

        <attribute type="NX_UINT32" name="image_uint32">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="2" index="2"/>
          </dimensions>
          <strategy mode="STEP"/>
          11 12
          21 22
          31 32
        </attribute>

        <attribute type="NX_UINT64" name="image_uint64">
          <dimensions rank="2"/>
          <strategy mode="FINAL"/>
          11 12
          21 22
          31 32
        </attribute>


        <attribute type="NX_BOOLEAN" name="image_bool">
          <dimensions rank="2">
            <dim value="3" index="1"/>
            <dim value="4" index="2"/>
          </dimensions>
          <strategy mode="INIT"/>
         True False 1 0
         FALSE TRUE false true
         0 1 1 0
        </attribute>

        1.2
      </field>
    </group>
  </group>
</definition>
"""

        #        <attribute type="NX_CHAR" name="flag_spectrum_string">
        #          <dimensions rank="1">
        #            <dim value="8" index="1"/>
        #          </dimensions>
        #          <strategy mode="STEP"/>
        #        </attribute>

        image = [[11, 12], [21, 22], [31, 32]]
        fimage = [[-3.43, -3.3e-1, 53.1], [3.43e+02, 3.13e-1, 53]]
        bimage = [[True, False, True, False], [False, True, False, True],
                  [False, True, True, False]]

        tdw = self.openWriter(fname, xml)
        for i in range(3):
            self.record(tdw, '{ }')

        self.closeWriter(tdw)

        # check the created file
        FileWriter.writer = H5PYWriter
        f = FileWriter.open_file(fname, readonly=True)
        det, field = self._sc.checkAttributeTree(f, fname, 4, 4)
        self._sc.checkImageAttribute(det,
                                     "image_float",
                                     "float64",
                                     fimage,
                                     error=1.e-14)
        self._sc.checkImageAttribute(det, "image_int", "int64", image)
        self._sc.checkImageAttribute(det, "image_bool", "bool", bimage)
        self._sc.checkImageAttribute(det, "image_int32", "int32", image)
        self._sc.checkImageAttribute(field,
                                     "image_float32",
                                     "float32",
                                     fimage,
                                     error=1.e-5)
        self._sc.checkImageAttribute(field, "image_uint32", "uint32", image)
        self._sc.checkImageAttribute(field, "image_uint64", "uint64", image)
        self._sc.checkImageAttribute(field, "image_bool", "bool", bimage)
        # STRING NOT SUPPORTED BY PNINX

        f.close()
        os.remove(fname)
Пример #7
0
    def test_xmlScalar(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        xml = """<definition>
  <group type="NXentry" name="entry1">
    <group type="NXinstrument" name="instrument">
      <group type="NXdetector" name="detector">
        <field units="m" type="NX_INT" name="counter">
          -12
        </field>
        <field units="m" type="NX_INT8" name="counter8">
          -12
        </field>
        <field units="m" type="NX_INT16" name="counter16">
          -12
        </field>
        <field units="m" type="NX_INT32" name="counter32">
          -12
        </field>
        <field units="m" type="NX_INT64" name="counter64">
          -12
        </field>
        <field units="m" type="NX_UINT" name="ucounter">
          12
        </field>
        <field units="m" type="NX_POSINT" name="pcounter">
          12
        </field>
        <field units="m" type="NX_UINT8" name="ucounter8">
          12
        </field>
        <field units="m" type="NX_UINT16" name="ucounter16">
          12
        </field>
        <field units="m" type="NX_UINT32" name="ucounter32">
          12
        </field>
        <field units="m" type="NX_UINT64" name="ucounter64">
          12
        </field>
        <field units="m" type="NX_FLOAT" name="float">
         -12.3
        </field>
        <field units="m" type="NX_FLOAT32" name="float32">
         -12.3
        </field>
        <field units="m" type="NX_FLOAT64" name="float64">
         -12.3
        </field>
        <field units="m" type="NX_NUMBER" name="number">
         -12.3
        </field>

        <field units="m" type="NX_DATE_TIME" name="time">
             string, string
        </field>
        <field units="m" type="ISO8601" name="isotime">
             string, string
        </field>
        <field units="m" type="NX_CHAR" name="string_time">
             string, string
        </field>
        <field units="m" type="NX_BOOLEAN" name="flags">
          True
        </field>


      </group>
    </group>
  </group>
</definition>
"""

        uc = 12
        mc = -12
        fc = -12.3
        string = "string, string"
        tdw = self.openWriter(fname, xml)

        # flip = True
        for c in range(4):
            self.record(tdw, '{ }')

        self.closeWriter(tdw)

        # check the created file

        FileWriter.writer = H5PYWriter
        f = FileWriter.open_file(fname, readonly=True)
        det = self._sc.checkFieldTree(f, fname, 19)
        self._sc.checkXMLScalarField(det,
                                     "counter",
                                     "int64",
                                     "NX_INT",
                                     mc,
                                     attrs={
                                         "type": "NX_INT",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "counter8",
                                     "int8",
                                     "NX_INT8",
                                     mc,
                                     attrs={
                                         "type": "NX_INT8",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "counter16",
                                     "int16",
                                     "NX_INT16",
                                     mc,
                                     attrs={
                                         "type": "NX_INT16",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "counter32",
                                     "int32",
                                     "NX_INT32",
                                     mc,
                                     attrs={
                                         "type": "NX_INT32",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "counter64",
                                     "int64",
                                     "NX_INT64",
                                     mc,
                                     attrs={
                                         "type": "NX_INT64",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "ucounter",
                                     "uint64",
                                     "NX_UINT",
                                     uc,
                                     attrs={
                                         "type": "NX_UINT",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "ucounter8",
                                     "uint8",
                                     "NX_UINT8",
                                     uc,
                                     attrs={
                                         "type": "NX_UINT8",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "ucounter16",
                                     "uint16",
                                     "NX_UINT16",
                                     uc,
                                     attrs={
                                         "type": "NX_UINT16",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "ucounter32",
                                     "uint32",
                                     "NX_UINT32",
                                     uc,
                                     attrs={
                                         "type": "NX_UINT32",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "ucounter64",
                                     "uint64",
                                     "NX_UINT64",
                                     uc,
                                     attrs={
                                         "type": "NX_UINT64",
                                         "units": "m"
                                     })

        self._sc.checkXMLScalarField(det,
                                     "float",
                                     "float64",
                                     "NX_FLOAT",
                                     fc,
                                     1.0e-14,
                                     attrs={
                                         "type": "NX_FLOAT",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "float64",
                                     "float64",
                                     "NX_FLOAT64",
                                     fc,
                                     1.0e-14,
                                     attrs={
                                         "type": "NX_FLOAT64",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "float32",
                                     "float32",
                                     "NX_FLOAT32",
                                     fc,
                                     1.0e-06,
                                     attrs={
                                         "type": "NX_FLOAT32",
                                         "units": "m"
                                     })
        self._sc.checkXMLScalarField(det,
                                     "number",
                                     "float64",
                                     "NX_NUMBER",
                                     fc,
                                     1.0e-14,
                                     attrs={
                                         "type": "NX_NUMBER",
                                         "units": "m"
                                     })

        self._sc.checkXMLStringScalarField(det,
                                           "time",
                                           "string",
                                           "NX_DATE_TIME",
                                           string,
                                           attrs={
                                               "type": "NX_DATE_TIME",
                                               "units": "m"
                                           })
        self._sc.checkXMLStringScalarField(det,
                                           "isotime",
                                           "string",
                                           "ISO8601",
                                           string,
                                           attrs={
                                               "type": "ISO8601",
                                               "units": "m"
                                           })
        self._sc.checkXMLStringScalarField(det,
                                           "string_time",
                                           "string",
                                           "NX_CHAR",
                                           string,
                                           attrs={
                                               "type": "NX_CHAR",
                                               "units": "m"
                                           })
        self._sc.checkXMLScalarField(det,
                                     "flags",
                                     "bool",
                                     "NX_BOOLEAN",
                                     True,
                                     attrs={
                                         "type": "NX_BOOLEAN",
                                         "units": "m"
                                     })

        f.close()
        os.remove(fname)
Пример #8
0
    def test_scanRecord_nexuspath(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun)
        data = '{"exp_c01":' + str(self._counter[0]) + \
            ', "p09/mca/exp.02":' + str(self._mca1) + '  }'
        jdata = '{"data": {"exp_c01":' + str(self._counter[0]) + \
            ', "p09/mca/exp.02":' + str(self._mca1) + '  } }'
        xmlfile = '%s/%s%s.xml' % (os.getcwd(), self.__class__.__name__, fun)
        jsonfile = '%s/%s%s.json' % (os.getcwd(), self.__class__.__name__, fun)
        nxpath = "/entry1:NXentry"

        commands = [
            [
                'nxsfromxml', '--h5py', '-p',
                "%s:/%s" % (fname, nxpath), '--nrecords', '2', '-j', jsonfile,
                '--time', '0.5', '-v', self._scanXmlpart
            ],
            [
                'nxsfromxml', '--h5py', '--parent',
                "%s:/%s" % (fname, nxpath), '--nrecords', '2', '--json-file',
                jsonfile, '-x', xmlfile
            ],
            [
                'nxsfromxml', '--h5py', '-p',
                "%s:/%s" % (fname, nxpath), '-n', '2', '-d', data, '--verbose',
                '--xml-file', xmlfile
            ],
            [
                'nxsfromxml', '--h5py', '--parent',
                "%s:/%s" % (fname, nxpath), '-n', '2', '--data', data, '-t',
                '0', self._scanXmlpart
            ],
        ]
        for ci, cmd in enumerate(commands):
            try:

                with open(xmlfile, "w") as text_file:
                    text_file.write(self._scanXmlpart)
                with open(jsonfile, "w") as text_file:
                    text_file.write(jdata)
                vl, er = self.runtest(cmd)
                if PYTG_BUG_213:
                    self.assertTrue(er)
                else:
                    print(er)
                    self.assertEqual('', er)
                if ci % 2:
                    self.assertEqual('', vl)
                else:
                    self.assertTrue(vl)

                # check the created file

                from nxstools import filewriter as FileWriter
                FileWriter.writer = H5PYWriter
                f = FileWriter.open_file(fname, readonly=True)
                f = f.root()
                self.assertEqual(5, len(f.attributes))
                self.assertEqual(f.attributes["file_name"][...], fname)
                self.assertTrue(f.attributes["NX_class"][...], "NXroot")
                self.assertEqual(f.size, 2)

                en = f.open("entry1")
                self.assertTrue(en.is_valid)
                self.assertEqual(en.name, "entry1")
                self.assertEqual(len(en.attributes), 1)
                self.assertEqual(en.size, 1)

                at = en.attributes["NX_class"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "NX_class")
                self.assertEqual(at[...], "NXentry")

                ins = en.open("instrument")
                self.assertTrue(ins.is_valid)
                self.assertEqual(ins.name, "instrument")
                self.assertEqual(len(ins.attributes), 2)
                self.assertEqual(ins.size, 1)

                at = ins.attributes["NX_class"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "NX_class")
                self.assertEqual(at[...], "NXinstrument")

                at = ins.attributes["short_name"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "short_name")
                self.assertEqual(at[...], "scan instrument")

                det = ins.open("detector")
                self.assertTrue(det.is_valid)
                self.assertEqual(det.name, "detector")
                self.assertEqual(len(det.attributes), 1)
                self.assertEqual(det.size, 2)

                at = det.attributes["NX_class"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "NX_class")
                self.assertEqual(at[...], "NXdetector")

                cnt = det.open("counter1")
                self.assertTrue(cnt.is_valid)
                self.assertEqual(cnt.name, "counter1")
                self.assertTrue(hasattr(cnt.shape, "__iter__"))
                self.assertEqual(len(cnt.shape), 1)
                self.assertEqual(cnt.shape, (2, ))
                self.assertEqual(cnt.dtype, "float64")
                self.assertEqual(cnt.size, 2)
                value = cnt.read()
                #            value = cnt[:]
                for i in range(len(value)):
                    self.assertEqual(self._counter[0], value[i])

                self.assertEqual(len(cnt.attributes), 4)

                at = cnt.attributes["nexdatas_strategy"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "nexdatas_strategy")
                self.assertEqual(at[...], "STEP")

                at = cnt.attributes["type"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "type")
                self.assertEqual(at[...], "NX_FLOAT")

                at = cnt.attributes["units"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "units")
                self.assertEqual(at[...], "m")

                at = cnt.attributes["nexdatas_source"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")

                mca = det.open("mca")
                self.assertTrue(mca.is_valid)
                self.assertEqual(mca.name, "mca")

                self.assertTrue(hasattr(cnt.shape, "__iter__"))
                self.assertEqual(len(mca.shape), 2)
                self.assertEqual(mca.shape, (2, 2048))
                self.assertEqual(mca.dtype, "float64")
                self.assertEqual(mca.size, 4096)
                value = mca.read()
                for i in range(len(value[0])):
                    self.assertEqual(self._mca1[i], value[0][i])
                for i in range(len(value[0])):
                    self.assertEqual(self._mca1[i], value[1][i])

                self.assertEqual(len(mca.attributes), 4)

                at = cnt.attributes["nexdatas_strategy"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "nexdatas_strategy")
                self.assertEqual(at[...], "STEP")

                at = mca.attributes["type"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "type")
                self.assertEqual(at[...], "NX_FLOAT")

                at = mca.attributes["units"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")
                self.assertEqual(at.name, "units")
                self.assertEqual(at[...], "")

                at = mca.attributes["nexdatas_source"]
                self.assertTrue(at.is_valid)
                self.assertTrue(hasattr(at.shape, "__iter__"))
                self.assertEqual(len(at.shape), 0)
                self.assertEqual(at.shape, ())
                self.assertEqual(at.dtype, "string")

                f.close()

            finally:

                if os.path.isfile(fname):
                    os.remove(fname)
                if os.path.isfile(xmlfile):
                    os.remove(xmlfile)
                if os.path.isfile(jsonfile):
                    os.remove(jsonfile)