File size: 3,328 Bytes
21b4b99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import csv

class Streams:

    def __init__(self, streamsDataFile):

        self.tmin =              0
        self.numberOf =          0
        self.streamsData =       []

        self._rawStreamsData =   []
        self._index =            0
        self._length =           0




        with open(streamsDataFile, newline='') as f:
            reader = csv.reader(f)
            for row in reader:
                self._rawStreamsData.append(row)

        if (self._rawStreamsData[0][0].strip() != 'Tmin' or
                [ item.strip() for item in self._rawStreamsData[1] ] != ['CP', 'TSUPPLY', 'TTARGET']):
            raise Exception("""\n[ERROR] Bad formatting in streams data file. \n
                    The first two rows of the streams data file should be: \n
                    `` Tmin, <TMIN VALUE> ''
                    `` CP, TSUPPLY, TTARGET ''\n
                    Where CP is the heat capacity (kW / degC);
                    TSUPPLY is the starting temperature of the given stream (degC);
                    TTARGET is the ending temperature of the given stream (degC);\n""")

        self.createStreams()


    def createStreams(self):
        try:
            self.tmin = float(self._rawStreamsData[0][1])
        except ValueError:
            print("\n[ERROR] Wrong type supplied for Tmin in the streams data file. Perhaps used characters?\n")
            raise
        except IndexError:
            print("\n[ERROR] Missing value for Tmin in the streams data file.\n")
            raise
        except:
            print("\n[ERROR] Unexpected error for Tmin. Try using the supplied streams data file format.\n")
            raise


        for rawStream in self._rawStreamsData[2:]:
            try:
                stream = {}

                if float(rawStream[1]) > float(rawStream[2]):
                    stream["type"] = "HOT"
                else:
                    stream["type"] = "COLD"

                stream["cp"] = float(rawStream[0])
                stream["ts"] = float(rawStream[1])
                stream["tt"] = float(rawStream[2])

                self.streamsData.append(stream)

            except ValueError:
                print("\n[ERROR] Wrong number type supplied in the streams data file. Perhaps used characters?\n")
                raise
            except IndexError:
                print("\n[ERROR] Missing number in the streams data file.\n")
                raise
            except:
                print("\n[ERROR] Unexpected error. Try using the supplied streams data file format.\n")
                raise

        self._length = len(self.streamsData)
        self.numberOf = len(self.streamsData)
        if (self._length < 2):
            raise Exception("\n[ERROR] Need to supply at least 2 streams in the streams data file.\n")


    def __iter__(self):
        return self


    def __next__(self):
        if self._index == self._length:
            self._index = 0
            raise StopIteration
        self._index = self._index + 1

        return self.streamsData[self._index - 1]


    def printTmin(self):
        print(self.tmin)


    def printStreams(self):
        for stream in self.streamsData:
            print(stream)


    def printRawStreams(self):
        for rawStream in self._rawStreamsData:
            print(rawStream)