def getRowsAndFileReader(self, slacNum):
     sessionCSV = self.fileFormatter.format(CM_SLAC=slacNum)
     rows = open(sessionCSV).readlines()
     # Reversing to get in chronological order (the program appends the most
     # recent sessions to the end of the file)
     rows.reverse()
     compatibleNext(reader([rows.pop()]))
     fileReader = reader(rows)
     return fileReader, rows
    def getRowsAndFileReader(self, slacNum, kind=None, cavNum=None):
        # type: (int, str, int) -> Tuple

        sessionCSV = self.genSessionFile(slacNum, kind, cavNum)
        rows = open(compatibleMkdirs(sessionCSV)).readlines()
        # Reversing to get in chronological order (the program appends the most
        # recent sessions to the end of the file)
        rows.reverse()
        compatibleNext(reader([rows.pop()]))
        fileReader = reader(rows)
        return fileReader, rows
    def __init__(self, inputFile):
        # type: (str) -> None

        self.inputFile = inputFile

        self.csvReader = reader(open(inputFile))
        self.header = compatibleNext(self.csvReader)
        self.slacNumIdx = self.header.index("SLAC Cryomodule Number")

        self.cryModManager = CryModDataManager(self)
        self.cavManager = CavityDataManager(self)

        self.figStartIdx = 1

        # A dict of dicts where the format is:
        #   {[SLAC cryomodule number]:
        #       {[line number from record-keeping CSV]: [DataSession object]}}
        self.dataSessions = {}

        # A dict of the form {[SLAC cryomodule number]: [Cryomodule Object]}
        self.cryoModules = {}  # type: Dict[int, Cryomodule]

        # We store the JT Valve position from the first time that we run
        # getRefValveParams (in Container) so that we don't have to rerun that
        # function every time we get new data (each call takes 2 hours)
        self.valveParams = None  # type: Optional[ValveParams]
    def addDataSession(self,
                       slacNum,
                       container,
                       refGradVal=None,
                       calibSession=None):
        # type: (int, Cryomodule, float, CalibDataSession) -> CalibDataSession

        indices = self.idxMap[slacNum]

        fileReader, rows = self.getRowsAndFileReader(slacNum, kind="calib")

        # Unclear if this is actually necessary, but the idea is to have the
        # output of json.dumps be ordered by index number
        options = OrderedDict()

        for row in fileReader:
            # if (len(options) + 1) % 10 == 0:
            #     printOptions(options)
            #     showMore = isYes("Search for more options? ")
            #     if not showMore:
            #         break

            addOption(csvRow=row,
                      lineNum=fileReader.line_num,
                      indices=indices,
                      options=options)

        selection = getSelection(duration=5,
                                 suffix="calibration",
                                 options=options)

        if selection != max(options):

            calibRow = compatibleNext(reader([rows[selection - 1]]))

            if not container:
                container = Cryomodule(slacNum,
                                       calibRow[indices["jlabNumIdx"]])

            refHeatLoad = float(calibRow[indices["refHeatIdx"]])
            refHeatLoadAct = float(calibRow[indices["refHeatActIdx"]])

            return container.addDataSessionFromRow(calibRow,
                                                   indices,
                                                   refHeatLoad,
                                                   refHeatLoadAct,
                                                   kind="calib")

        else:
            if not container:
                container = Cryomodule(
                    slacNum,
                    getNumInputFromLst("JLab cryomodule"
                                       " number: ", [2, 3], int))

            (calibSession, self.parent.valveParams) = container.runCalibration(
                self.parent.valveParams)

            return calibSession
    def addDataSession(self,
                       slacNum,
                       container,
                       refGradVal=None,
                       calibSession=None):
        # type: (int, Cavity, float, CalibDataSession) -> Q0DataSession

        indices = self.idxMap[slacNum]

        fileReader, rows = self.getRowsAndFileReader(slacNum)

        # Unclear if this is actually necessary, but the idea is to have the
        # output of json.dumps be ordered by index number
        options = OrderedDict()

        for row in fileReader:

            # We could theoretically have hundreds of results, and that seems
            # like a seriously unnecessary number of options to show. This
            # asks the user if they want to keep searching for more every 10
            # hits
            if (len(options) + 1) % 10 == 0:
                printOptions(options)
                showMore = isYes("Search for more options? ")
                if not showMore:
                    break

            grad = float(row[indices["gradIdx"]])
            cavNum = int(row[indices["cavNumIdx"]])

            # The files are per cryomodule, so there's a lot of different
            # cavities in the file. We check to make sure that we're only
            # presenting the options for the requested cavity at the requested
            # gradient (by just skipping the irrelevant ones)
            if (grad != refGradVal) or (cavNum != container.cavNum):
                continue

            addOption(csvRow=row,
                      lineNum=fileReader.line_num,
                      indices=indices,
                      options=options)

        selection = getSelection(duration=2,
                                 suffix="Q0 Measurement",
                                 options=options)

        # If using an existing data session
        if selection != max(options):
            selectedRow = compatibleNext(reader([rows[selection - 1]]))
            refHeatLoad = float(selectedRow[indices["refHeatIdx"]])
            refHeatLoadAct = float(selectedRow[indices["refHeatActIdx"]])
            return container.addDataSessionFromRow(selectedRow, indices,
                                                   refHeatLoad, refHeatLoadAct,
                                                   calibSession, refGradVal)

        else:
            (Q0Sess, self.parent.valveParams) = container.runQ0Meas(
                refGradVal, calibSession, self.valveParams)
            return Q0Sess
 def populate(fileObj, header=None):
     if not header:
         csvReader = reader(fileObj)
         header = compatibleNext(csvReader)
     indices = {}
     for key, column in self.idxKeys:
         indices[key] = header.index(column)
     self.idxMap[slacNum] = indices
 def populate(self, fileObj, slacNum, cavNum=None, header=None, kind=None):
     # type: (TextIO, int, int, List, str) -> None
     if not header:
         csvReader = reader(fileObj)
         header = compatibleNext(csvReader)
     indices = {}
     for key, column in (self.idxKeys if kind != "q0" else self.q0IdxKeys):
         indices[key] = header.index(column)
     if kind == "calib":
         self.idxMap[slacNum] = {cavNum: indices}
     else:
         self.q0IdxMap[slacNum] = {cavNum: indices}
    def getRowAndHeatLoad(self, slacNum, idx):
        # type: (int, int) -> Tuple[List[str], float, float]

        sessionCSV = self.fileFormatter.format(CM_SLAC=slacNum)
        row = open(sessionCSV).readlines()[idx - 1]

        selectedRow = compatibleNext(reader([row]))
        refHeatLoad = float(selectedRow[self.idxMap[slacNum]["refHeatIdx"]])
        refHeatLoadAct = float(
            selectedRow[self.idxMap[slacNum]["refHeatActIdx"]])

        return selectedRow, refHeatLoad, refHeatLoadAct