def loadDeck(nameDck,eraseBeginComment=True,eliminateComments=True): """ Parameters ---------- nameDck : str name of the TRNSYS deck to be loaded eraseBeginComment : bool True will delete all lines starting with *, !, and blank, but also the comments *********anyComment********* False will delete all lines starting with !, and blank, but keep the ones starting with ** Return ------ lines : str list of lines obateined form the deck without the comments """ infile = open(nameDck, 'r') lines = infile.readlines() # skypChar = None #['*'] #This will eliminate the lines starting with skypChar if (eraseBeginComment == True): skypChar = ['*', '!', ' \n'] # ['*'] #This will eliminate the lines starting with skypChar else: skypChar = ['!', ' \n'] # ['*'] #This will eliminate the lines starting with skypChar replaceChar = None #[',','\''] #This characters will be eliminated, so replaced by nothing linesChanged = spfUtils.purgueLines(lines, skypChar, replaceChar, removeBlankLines=True) # Only one comment is erased, so that if we hve ! comment1 ! comment2 only the commen2 will be erased if (eliminateComments == True): linesChanged = spfUtils.purgueComments(linesChanged, ['!']) return linesChanged
def loadDeck(self, _path, _name): nameOneDck = _path + "\%s.%s" % (_name, self.extOneSheetDeck) infile = open(nameOneDck, 'r') lines = infile.readlines() replaceChar = None self.linesChanged = spfUtils.purgueLines(lines, self.skypChar, replaceChar, removeBlankLines=True) if (self.eliminateComments == True): self.linesChanged = spfUtils.purgueComments( self.linesChanged, ['!']) infile.close() return lines[0:3] #only returns the caption with the info of the file
def loadFile(self, skypChar=None, replaceChar=None, skypedLines=0, splitArgument=None, verbose=True, indexToRead=1): if verbose: print("%s" % self.doubleLine) print("Reading the file: %s " % self.fileNameWithExtension) infile = open(self.fileNameWithExtension, "r") lines = infile.readlines()[skypedLines:] if skypChar != None or replaceChar != None: if verbose: print("%s" % self.doubleLine) print("Purgin the input file") lines = spfUtils.purgueLines( lines, skypChar, replaceChar, removeBlankLines=self.removeBlankLines, removeBlankSpaces=True ) if skypChar != None and self.removeComments: lines = spfUtils.purgueComments(lines, skypChar) if self.parseFileCreated: parsedFile = "%s.parse.dat" % self.fileName outfile = open(parsedFile, "w") outfile.writelines(lines) outfile.close() self.numberOfVariables = len(lines) self.variables = np.arange(self.numberOfVariables, dtype=float) self.namesVariables = np.arange(self.numberOfVariables, dtype=object) for i in range(self.numberOfVariables): split = lines[i].split(splitArgument) self.namesVariables[i] = split[0] # print split[0] self.variables[i] = split[indexToRead] infile.close()
def readFile(self, path, name, inputs, parseFileCreated=True, controlDataType=True): skypChar = "#" configFile = os.path.join(path, name) infile = open(configFile, "r") lines = infile.readlines() lines = processFiles.purgueLines(lines, skypChar, None, removeBlankLines=True, removeBlankSpaces=False) lines = processFiles.purgueComments(lines, skypChar) if "calcMonthly" not in inputs: inputs["calcMonthly"] = [] if "calcMonthlyTest" not in inputs: inputs["calcMonthlyTest"] = [] if "calcMonthlyMax" not in inputs: inputs["calcMonthlyMax"] = [] if "calcMonthlyMin" not in inputs: inputs["calcMonthlyMin"] = [] if "calcHourly" not in inputs: inputs["calcHourly"] = [] if "calcMonthlyFromHourly" not in inputs: inputs["calcMonthlyFromHourly"] = [] if "calcDaily" not in inputs: inputs["calcDaily"] = [] if "calc" not in inputs: inputs["calc"] = [] if "calcTest" not in inputs: inputs["calcTest"] = [] if "calcCumSumHourly" not in inputs: inputs["calcCumSumHourly"] = [] if "calcHourlyTest" not in inputs: # dirty trick to calculate after calcCumSumHourly DC inputs["calcHourlyTest"] = [] if "calcTimeStep" not in inputs: inputs["calcTimeStep"] = [] if "calcTimeStepTest" not in inputs: inputs["calcTimeStepTest"] = [] if "calcCumSumTimeStep" not in inputs: inputs["calcCumSumTimeStep"] = [] if parseFileCreated: parsedFile = "%s.parse.dat" % configFile outfile = open(parsedFile, "w") outfile.writelines(lines) outfile.close() for i in range(len(lines)): if lines[i][-1:] == "\n": lines[i] = lines[i][0:-1] splitLine = lines[i].split() if splitLine[0] == "bool": inputs[splitLine[1]] = self.str2bool(splitLine[2]) elif splitLine[0] == "int": inputs[splitLine[1]] = int(splitLine[2]) elif splitLine[0] == "string": if len(splitLine) != 3: splitString = "" for i in range(len(splitLine) - 2): if i == 0: splitString += splitLine[i + 2][1:] splitString += " " elif i == len(splitLine) - 3: splitString += splitLine[i + 2][:-1] else: splitString += splitLine[i + 2] splitString += " " inputs[splitLine[1]] = splitString # raise ValueError("Error in string : %s"%lines[i]) else: inputs[splitLine[1]] = splitLine[2][1:-1] # I delete the " elif splitLine[0] == "stringArray": if splitLine[1] not in inputs.keys(): inputs[splitLine[1]] = [] newElement = [] for i in range(len(splitLine) - 2): strEl = splitLine[i + 2][1:-1] # I delete the " newElement.append(strEl) inputs[splitLine[1]].append(newElement) # if len(inputs[splitLine[1]])==1: # inputs[splitLine[1]]=inputs[splitLine[1]][0] elif splitLine[0] == "calcMonthly": inputs["calcMonthly"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcMonthlyTest": inputs["calcMonthlyTest"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcMonthlyMax": inputs["calcMonthlyMax"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcMonthlyMin": inputs["calcMonthlyMin"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calc": inputs["calc"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcHourly": inputs["calcHourly"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcMonthlyFromHourly": inputs["calcMonthlyFromHourly"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcDaily": inputs["calcDaily"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcHourlyTest": inputs["calcHourlyTest"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcTimeStep": inputs["calcTimeStep"].append(" ".join(splitLine[1:])) elif splitLine[ 0] == "calcTimeStepTest": # dirty trick to have it after the calcCumSumTimeStep DC inputs["calcTimeStepTest"].append(" ".join(splitLine[1:])) # elif (splitLine[0] == "calcHourlyTest"): # inputs["calcHourlyTest"].append(" ".join(splitLine[1:])) elif splitLine[0] == "calcCumSumHourly": if len(splitLine) == 2: inputs["calcCumSumHourly"].append(splitLine[1]) else: inputs["calcCumSumHourly"].append(splitLine[1:]) elif splitLine[0] == "calcCumSumTimeStep": if len(splitLine) == 2: inputs["calcCumSumTimeStep"].append(splitLine[1]) else: inputs["calcCumSumTimeStep"].append(splitLine[1:]) elif splitLine[0] == "calcTest": inputs["calcTest"].append(" ".join(splitLine[1:])) else: if controlDataType: raise ValueError("type of data %s unknown" % splitLine[0]) else: pass return lines
def loadFile(self, skypChar=None, replaceChar=None, skypedLines=0, splitArgument=None, readLabels=True, verbose=True, cutBlank=False, typeVariables="float", fmt='', dateExt=''): if (verbose): print("%s" % self.doubleLine) print("Reading the file: %s " % self.fileNameWithExtension) infile = open(self.fileNameWithExtension, 'r') lines = infile.readlines()[skypedLines:] #remove last line if its only a \n line if (cutBlank == True): #File used to rad until a black line is found. #I want to avoid reading the max min etc... from TRNSYS hourly data j = len(lines) - 1 for i in range(len(lines)): if (verbose): print("lines:%s size:%d" % (lines[i], len(lines[i]))) try: if (lines[i].split("\t")[1] == "Maximum Instantaneous Values"): j = i - 2 if (verbose): print("BREAK j:%d lines:%s\n" % (j, lines[j])) break except: if (verbose): print("BLANK LINE:%d lines:%s\n" % (i, lines[i])) j = i break lines = lines[0:j] size = len(lines) - 1 if (len(lines[size]) == 1): lines = lines[:-1] size = len(lines) - 1 if (skypChar != None or replaceChar != None): if (verbose): print("%s" % self.doubleLine) print("Purgin the input file") lines = spfUtils.purgueLines( lines, skypChar, replaceChar, removeBlankLines=self.removeBlankLines) if (skypChar != None and self.removeComments): lines = spfUtils.purgueComments(lines, skypChar) if (self.parseFileCreated): parsedFile = "%s.parse.dat" % self.fileName outfile = open(parsedFile, 'w') outfile.writelines(lines) outfile.close() self.numberOfVariables = len(lines[0].split(splitArgument)) # self.numberOfDataPoints = len(lines)-1 if (readLabels == True): self.numberOfDataPoints = len(lines) - 1 else: self.numberOfDataPoints = len(lines) #NumPy array if (typeVariables == "float"): self.variables = np.arange( self.numberOfVariables * self.numberOfDataPoints, dtype=float).reshape(self.numberOfVariables, self.numberOfDataPoints) else: self.variables = np.arange( self.numberOfVariables * self.numberOfDataPoints, dtype=object).reshape(self.numberOfVariables, self.numberOfDataPoints) self.namesVariables = np.arange(self.numberOfVariables, dtype=object) if (verbose): print("%s" % self.doubleLine) print("numberOfDataPoints:%d numberOfVariables:%d" % (self.numberOfDataPoints, self.numberOfVariables)) if (verbose): print("%s" % self.doubleLine) print("Copying Data from file to variables array") startIndex = 0 if (readLabels == True): splittedLine = lines[0].split(splitArgument) for i in range(self.numberOfVariables): self.namesVariables[i] = splittedLine[i] startIndex = 1 endingOfLastName = self.namesVariables[self.numberOfVariables - 1][-1:] if (endingOfLastName == "\n" or endingOfLastName == "\t"): # I erase the last two characters self.namesVariables[self.numberOfVariables - 1] = self.namesVariables[ self.numberOfVariables - 1][:-1] k = 0 for i in range(len(lines)): if (i >= startIndex): splittedLine = lines[i].split(splitArgument) for j in range(self.numberOfVariables): # print "j:%d var:%s" % (j,splittedLine[j]) try: # [self.numberOfVariables][self.numberOfDataPoints] self.variables[j][i - startIndex] = splittedLine[j] except: try: self.variables[j][i - startIndex] = time.mktime( datetime.datetime.strptime( dateExt + splittedLine[j], fmt).timetuple()) except: if (self.printWarning == True and j >= 1): # print "i:%d j:%d len(lines):%d startIndex:%d"%(i,j,len(lines),startIndex) print( "LoadBaseNumpy loadFile Not controlled number :", splittedLine[j], " for column j: ", j + 1, " line: ", i) self.variables[j][i - startIndex] = 0.0 k = k + 1 if (verbose): print("%s" % self.doubleLine) print("End of copying Data") infile.close()