Example #1
0
    def loadSources(self):
        self.sheets["sources"] = self.createSheet("sources")
        sourcedb = Sourcedb(self.edb)

        page = self.getPage(
            ["read", self.domain, self.edb.user, self.edb.name, "sourcedb"])
        #filename="/local_disk/dvlp/airviro/loairviro/test/TR_ships.out"

        substEmis = {}
        substAlob = {}
        subgrpEmis = {}
        subgrpAlob = {}
        activityEmis = {}
        activityAlob = {}
        srcAlobs = {}

        #Reading sources
        batchInd = 0
        while sourcedb.read(fileObject=page, accumulate=True, batchSize=1):
            self.updateProgressDlg("Loading point and area sources",
                                   "read batch %i" % batchInd)
            #print "Read batch %i" %batchInd
            batchInd += 1

        self.updateProgressDlg("Loading point and area sources",
                               "writing sources to sheet")
        #To present sources in a table with a nice header, it is necessary
        #to list all substances, alobs, subgroups, emfacs and variables that are
        #used in the edb. This is because each alob, substance etc. should be
        #shown only once in the header
        for src in sourcedb.sources:
            #Accumulate all alobs into a list
            for alob in src.ALOBOrder:
                srcAlobs[alob] = None

            #store all substances and their alobs in a dict
            for substInd, emis in src.subst_emis:
                if substInd not in substEmis:
                    substEmis[substInd] = {"alob": {}}
                for alob in emis.ALOBOrder:
                    substEmis[substInd]["alob"][alob] = None

            #store all substance groups and their alobs in a dict
            for subgrpInd, emis in src.subgrp_emis.items():
                if subgrpInd not in subgrpEmis:
                    subgrpEmis[subgrpInd] = {"alob": {}}
                for alob in emis.ALOBOrder:
                    subgrpEmis[subgrpInd]["alob"][alob] = None

            #Accumulate all activities and included alobs
            for emfacInd, emis in src.activity_emis.items():
                if emfacInd not in activityEmis:
                    activityEmis[emfacInd] = {"alob": {}, "var": {}}
                for varInd, varVal in emis["VARLIST"]:
                    #vars should also be indexed
                    activityEmis[emfacInd]["var"][varInd] = None
                for alob in emis.ALOBOrder:
                    activityEmis[emfacInd]["alob"][alob] = None

        #Writing header
        header0 = ()
        header = ()
        srcAlobInd = {}

        for parName in src.parOrder:
            header += (parName, )
        header0 += ("Static parameters", )
        header0 += (len(header) - len(header0)) * ("", )
        alobKeys = srcAlobs.keys()
        alobKeys.sort()
        for alobKey in alobKeys:
            header += (alobKey, )
        if src["ALOB"] > 0:
            header0 += ("ALOBs", )
            header0 += (len(header) - len(header0)) * ("", )

        for substInd in substEmis:
            substName = self.subdb.substNames[substInd]
            header += ("Emission", "Time variation", "Unit", "Macro",
                       "Activity code")
            header0 += ("Substance:", substName)
            header0 += (len(header) - len(header0)) * ("", )
            alobKeys = substEmis[substInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                row += [src.ALOB.get(alobKey, "")]

            for alob in alobKeys:
                header += (alob, )
            if len(alobKeys) > 0:
                header0 += ("ALOBs", )
                header0 += (len(header) - len(header0)) * ("", )

        for subgrpInd in subgrpEmis:
            subgrp = self.subgrpdb.subgrps[subgrpInd]
            header += ("Activity", "Time variation", "Unit", "Activity code")
            header0 += ("Substance group:", subgrp.name)
            header0 += (len(header) - len(header0)) * ("", )

            alobKeys = subgrpEmis[subgrpInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                header += (alobKey, )
            if subgrpEmis[subgrpInd]["alob"] > 0:
                header0 += ("ALOBs", )
                header0 += (len(header) - len(header0)) * ("", )

        for emfacInd in activityEmis:
            emfac = self.emfacdb[emfacInd]
            header += ("Time variation", )
            for varInd, var in emfac.vars.items():
                header += (var.name, )
            header0 += ("Emfac:", emfac.name)
            header0 += (len(header) - len(header0)) * ("", )

            header += ("Activity code", )
            alobKeys = activityEmis[emfacInd]["alob"].keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                header += (alobKey, )
            if len(alobKeys):
                header0 += ("ALOBs", )
                header0 += (len(header) - len(header0)) * ("", )
        header0 += (len(header) - len(header0)) * ("", )

        firstCol = 0
        for colInd, val in enumerate(header0[1:]):
            if val != "":
                bottomCellRange = self.sheets[
                    "sources"].getCellRangeByPosition(firstCol, 1, colInd - 2,
                                                      1)

                bottomBorder = bottomCellRange.BottomBorder
                bottomBorder.OuterLineWidth = 30
                bottomCellRange.BottomBorder = bottomBorder
                firstCol = colInd - 2

        out = [header0, header]
        for src in sourcedb.sources:
            row = []
            for par in src.parOrder:
                row += [unicode(src[par])]

            #Write alobs for sources
            alobKeys = srcAlobs.keys()
            alobKeys.sort()
            for alobKey in alobKeys:
                row += [src.ALOB.get(alobKey, "")]

            #write substance emissions with alobs
            for substInd in substEmis:
                alobKeys = substEmis[substInd]["alob"].keys()
                alobKeys.sort()
                if substInd in src.subst_emis:
                    emis = src.subst_emis[substInd]
                    row += [
                        emis["EMISSION"], emis["TIMEVAR"], emis["UNIT"],
                        emis["MACRO"], emis["ACTCODE"]
                    ]
                    for alobKey in alobKeys:
                        row += [emis.ALOB.get(alobKey, "")]
                else:
                    row += ["", "", "", "", ""]  #empty cells for substance
                    row += [""] * len(alobKeys)

            #write substance group emissions with alobs
            for subgrpInd in subgrpEmis:
                alobKeys = subgrpEmis[subgrpInd]["alob"].keys()
                alobKeys.sort()
                if subgrpInd in src.subst_emis:
                    emis = src.subgrp_emis[subgrpInd]
                    row += [
                        emis["ACTIVITY"], emis["TIMEVAR"], emis["UNIT"],
                        emis["ACTCODE"]
                    ]
                    for alobKey in alobKeys:
                        row += [emis.ALOB.get(alobKey, "")]
                else:
                    row += ["", "", "", ""]  #empty cells for substance group
                    row += [""] * len(alobKeys)

            #write emfac emissions with variables and alobs
            for emfacInd in activityEmis:
                alobKeys = activityEmis[emfacInd]["alob"].keys()
                alobKeys.sort()
                varKeys = activityEmis[emfacInd]["var"].keys()
                varKeys.sort()
                if emfacInd in src.activity_emis:
                    emis = src.activity_emis[emfacInd]
                    row += [emis["TIMEVAR"]]
                    varVals = [var[1] for var in emis["VARLIST"]]
                    row += varVals
                    row += [emis["ACTCODE"]]
                    for alobKey in alobKeys:
                        row += [emis.ALOB.get(alobKey, "")]
                else:
                    row += 2 * [""] + [""] * len(varKeys) + [""
                                                             ] * len(alobKeys)
            out.append(tuple(row))
        cellRange = self.sheets["sources"].getCellRangeByPosition(
            0, 0,
            len(header) - 1,
            len(out) - 1)
        cellRange.setDataArray(tuple(out))