示例#1
0
    def preload(self, filename, takeSugestions):
        self.takeSugestions = takeSugestions
        sugestedStart = datetime.now()
        sugestedEnd = self.defaultEpoch
        stationList = []

        error = []

        # Some initialization
        if self.filename is not None:
            raise Exception(
                "Cannot pre-load two different files (current one is %s)" %
                self.filename)

        print("Analysing ... ", file=sys.stderr)
        fd = open(filename)
        for line in fd:
            line = line.strip()
            if not line or line[0] == "#": continue

            try:
                (stationCode, start, end) = self.__analyseLine__(line.split())
            except Exception as e:
                error.append(str(e))
                continue

            if not stationCode: continue
            if stationCode not in stationList:
                stationList.append(stationCode)

            sugestedStart = min(sugestedStart, start)
            if end and sugestedEnd:
                sugestedEnd = max(sugestedEnd, end)
            else:
                sugestedEnd = None
        fd.close()

        if len(error):
            raise Exception("\n".join(error))

        print(" Loaded %d different stations" % len(stationList),
              file=sys.stderr)
        if takeSugestions:
            print(" Taking suggestion start date of %s " %
                  formatDate(self.startDate),
                  file=sys.stderr)
            self.startDate = sugestedStart
            print(" Taking suggestion end date of %s " %
                  formatDate(self.endDate),
                  file=sys.stderr)
            self.endDate = sugestedEnd

        self.filename = filename
        self.stationList = stationList
        print("Done.", file=sys.stderr)
示例#2
0
    def __convertHeader__(self, line, fdo):

        # Split line
        items = line.split()

        if not self.takeSugestions:
            if self.nat.hasStart:
                print(" Using start from attribute.", file=sys.stderr)
                self.startDate = self.nat.startDate
            if self.nat.hasEnd:
                print(" Using end from attribute.", file=sys.stderr)
                self.endDate = self.nat.endDate

        nCode = items[2].strip()
        if nCode != self.networkCode:
            raise Exception("Wrong network code found: %s != %s" %
                            (self.networkCode, nCode))

        fdo.write(
            "Nw: %s %s %s" %
            (nCode, formatDate(self.startDate), formatDate(self.endDate)) +
            "\n")

        self.nat.dump(fdo)
示例#3
0
    def __convertLine__(self, line, fdo, atFront):
        lnfmt = self.__fmtline__()

        # Split line
        items = line.split()

        try:
            code = items.pop(0)
        except Exception as e:
            raise Exception("Missing Code on %s" % line)

        if code not in self.stationList:
            raise Exception("Unknow station code $s" % code)

        try:
            hummanStr(items.pop(0))
        except Exception as e:
            raise Exception("Missing Gain on %s" % line)

        try:
            datalogger = items.pop(0)
        except Exception as e:
            raise Exception("Missing Datalogger on %s" % line)

        try:
            sensor = items.pop(0)
        except Exception as e:
            raise Exception("Missing Sensor on %s" % line)

        try:
            gaind = items.pop(0)
            if float(gaind) != 1.0:
                if not self.inst:
                    raise Exception(
                        "Instrument database needed to convert gain")
                try:
                    dte = self.inst.dls[str(datalogger).split("%")[0]]
                except Exception as e:
                    print(e, file=sys.stderr)
                    raise Exception("Datalogger %s not found" %
                                    str(datalogger).split("%")[0])
                datalogger += "%%%s" % (float(dte.gain) * float(gaind))
                print(
                    " Converting gain multiplier to real gain using instrument DB on %s"
                    % code,
                    file=sys.stderr)
        except Exception as e:
            raise Exception("Missing Gain on %s (%s)" % (line, str(e)))

        try:
            channel = items.pop(0)
        except Exception as e:
            raise Exception("Missing Channel on %s" % line)

        try:
            latitude = items.pop(0)
        except Exception as e:
            raise Exception("Missing Latitude on %s" % line)

        try:
            longitude = items.pop(0)
        except Exception as e:
            raise Exception("Missing Longitude on %s" % line)
        try:
            elevation = items.pop(0)
        except Exception as e:
            raise Exception("Missing Elevation on %s" % line)

        try:
            depth = items.pop(0)
        except Exception as e:
            raise Exception("Missing Depth on %s" % line)

        #Orientation
        try:
            float(depth)
            orientation = "ZNE"
        except:
            orientation = "Z"
            (depth, a1, a2) = depth.split("/")

            a1n = float(a1)
            if a1n == 0.0:
                orientation += "1"
            else:
                orientation += "1(0.0,%s)" % a1

            a2n = float(a2)
            if a2n == 90.0:
                orientation += "2"
            else:
                orientation += "2(0.0,%s)" % a2

        # Start
        try:
            start = items.pop(0)
        except Exception:
            raise Exception("Missing Start on %s" % line)

        try:
            start = parseDate(start)
        except Exception as e:
            raise Exception("Invalide Start date: %s (%s) on %s" %
                            (start, e, line))

        #End
        try:
            end = items.pop(0)
        except:
            end = ""

        try:
            end = parseDate(end)
        except Exception as e:
            raise Exception("Invalide End date: %s (%s) on %s" %
                            (end, e, line))

        [place, country] = self.sat.parseStationLine(line.split())
        description = "%s/%s" % (place, country)

        ## Prepare necessary output
        if not atFront:
            self.sma.dump(fdo, code)
            self.sat.dump(fdo, code)

        for (start, end) in self.sma.getMappings(code, start, end):
            fdo.write(lnfmt %
                      (code, quote(description), datalogger, sensor, channel,
                       orientation, latitude, longitude, elevation, depth,
                       formatDate(start), formatDate(end)) + "\n")

        return code
示例#4
0
    def __analyseLine__(self, items):
        inputLine = " ".join(items)
        if len(items) < 4:
            raise Exception("Invalid items count on line %s" % inputLine)

        if len(items) <= 5:
            netCode = items[2]
            if netCode != self.networkCode:
                raise Exception(
                    "Tab file (%s) doesn't match class (%s) -- %s" %
                    (netCode, self.networkCode, inputLine))
            return [None, None, None]
        else:
            if len(items) < 6:
                raise Exception("Invalid Station line %s" % inputLine)

            stationCode = items.pop(0)
            code = len(stationCode)
            self.code = max(self.code, code)

            description = len(quote(hummanStr(items.pop(0))))
            self.description = max(self.description, description)

            datalogger = len(items.pop(0))
            self.datalogger = max(self.datalogger, datalogger)

            sensor = len(items.pop(0))
            self.sensor = max(self.sensor, sensor)

            # Gain
            gaind = items.pop(0)
            if float(gaind) != 1.0:
                self.datalogger = max(self.datalogger, datalogger + len(gaind))

            channel = len(items.pop(0))
            self.channel = max(self.channel, channel)

            latitude = len(items.pop(0))
            self.latitude = max(self.latitude, latitude)

            longitude = len(items.pop(0))
            self.longitude = max(self.longitude, longitude)

            elevation = len(items.pop(0))
            self.elevation = max(self.elevation, elevation)

            #Orientation
            depth = items.pop(0)
            try:
                float(depth)
                orientation = "ZNE"
            except:
                orientation = "Z"
                (depth, a1, a2) = depth.split("/")

                a1n = float(a1)
                a2n = float(a2)

                orientation += "1"
                if a1n != 0.0: orientation += "(0.0,%s)" % a1

                orientation += "2"
                if a2n != 90.0: orientation += "(0.0,%s)" % a1

            orientation = len(orientation)
            self.orientation = max(self.orientation, orientation)

            depth = len(depth)
            self.depth = max(self.depth, depth)

            # Start
            try:
                start = parseDate(items.pop(0))
                self.start = max(self.start, len(formatDate(start)))
            except:
                raise Exception("Invalid Station line start date %s" %
                                inputLine)

            # End
            try:
                end = parseDate(items.pop(0))
            except:
                end = parseDate("")
                pass
            self.end = max(self.end, len(formatDate(end)))

            return [stationCode, start, end]