当前位置: 首页>>代码示例>>Python>>正文


Python General.peek方法代码示例

本文整理汇总了Python中General.peek方法的典型用法代码示例。如果您正苦于以下问题:Python General.peek方法的具体用法?Python General.peek怎么用?Python General.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在General的用法示例。


在下文中一共展示了General.peek方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: getHighValues

# 需要导入模块: import General [as 别名]
# 或者: from General import peek [as 别名]
    def getHighValues(self):
        """ Reads the overloaded pixel from the bottom of the mar data.
            Data must be an array of integers so that the high values 
            can inserted. High values are read from the mar345 file and 
            inserted into the array. The values are changed in place. The 
            format of these high values is described at: 
            http://www.mar-usa.com/support/downloads/mar345_formats.pdf """

        # The first thing that has to be done is to pass through the header data.
        # Do do this, ignore everything until the "END OF HEADER" line
        file = open(self.filename, "rb")
        line = file.readline()
        while line != "END OF HEADER\n":
            line = file.readline()

        # Next, there is a bunch of white space and new lines before we get to
        # the packed high data. We need to ignore all the garbage and loop
        # until we get to real data. This part is particularly klugey because
        # the high data can apparently begin with the newline character even though
        # the newline character is also used as part of the garbage. So we have to
        # know when we find a new line whether it is followed by a space (and to keep
        # looking for garbage) or other stuff (in which case it is part of the packed data
        # and we need to make sure to read the newline as part of the high values.)
        # I don't get why they would design the file format like this. It seems particularly
        # silly, but it might be that I am doing something stupid on my end which makes
        # this look messy.
        char = file.read(1)
        while char == " " or char == "\n":
            if char == "\n" and General.peek(file) != " " and General.peek(file) != "\n":
                # if a newline is NOT followed by a space (and not another newline), then the newline is part
                # of the high data, so we need to start reading the real data in
                break

            char = file.read(1)

        # go back one so we get to the beginning of the high data
        file.seek(-1, 1)

        # It is possible that there are no high pixels in the image. When this
        # happens, we arrive immediately at the "CCPR ..." line.
        # When this happens, then exit the function without
        # adding any high pixels to the image.
        peek = General.peekline(file)

        # This little bit is incase there is no peak data but there is a newline preceding the CCP4 line.
        # in this case, we would need to look at the second line to find the CCP4 stuff.
        if peek == "\n":
            peek = General.peeksecondline(file)

        if peek in [
            "CCP4 packed image, X: %04d, Y: %04d\n" % (self.size, self.size),
            "\nCCP4 packed image, X: %04d, Y: %04d\n" % (self.size, self.size),
        ]:
            file.close()
            return

        nRecords = int(self._high / 8.0 + 0.875)
        nPairsPerRecord = 8
        nBytesPerRecord = 64

        # read all the data we need
        next = file.read(nBytesPerRecord * nRecords)

        # unpack it. The 2 is because there are 2 value for each pair
        # The '<' character tells python whether to read in as big or
        # little endian. I am not exactly sure which one I am using but
        # it is the only one that will read the data in properly. Hopefully,
        # no Mar data I am fed will use the old convention because my could
        # would break. Nevertheless, I am pretty sure that my error handling
        # will mean that it crashes which is better then doing the wrong thing.
        highVals = struct.unpack("<" + str(nRecords * nPairsPerRecord * 2) + "i", next)

        if file.readline() != "\n":  # we should have read to the end of the line
            raise Exception(
                "Error: the program was unable to read the high pixels from the mar345 image because after reading all the high pixels, it did not find a newline character."
            )

        if file.readline() != "CCP4 packed image, X: %04d, Y: %04d\n" % (self.size, self.size):
            raise Exception(
                'Error: the program was unable to read the high pixels from the mar345 image because it did not find the "CCCP4 packed image X: XXXX, Y: XXXX" line directly after the high data.'
            )

        file.close()

        for loop in range(nRecords * nPairsPerRecord):
            location = highVals[2 * loop]
            value = highVals[2 * loop + 1]

            if location == 0 and value == 0:
                # These records are padded w/ blank pairs. So if you come across one, just skip it
                continue

            if location < 0 or location >= self.size * self.size:
                raise Exception(
                    "Error: the program was unable to read the high pixels from the mar345 image. The current high value is supposed to be placed in location %d, but values can only be put from %d to %d"
                    % (location, 0, self.size * self.size - 1)
                )
            if value < 65536:
                raise Exception(
                    "Error: the program was unable to read the high pixels from the mar345 image. All high values must be >= 2^16, but the value read is %d"
#.........这里部分代码省略.........
开发者ID:joshualande,项目名称:AreaDiffractionMachine,代码行数:103,代码来源:MarXXXX.py


注:本文中的General.peek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。