Updated some reference state functions, does not work properly at the moment.

This commit is contained in:
Jorrit Wronski
2014-09-23 19:07:55 +02:00
parent 320907b946
commit 580fbd15d8
27 changed files with 262 additions and 246 deletions

View File

@@ -1,3 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
from __future__ import division, print_function
import numpy as np
@@ -11,10 +13,73 @@ from matplotlib.ticker import MaxNLocator
from matplotlib.backends.backend_pdf import PdfPages
import itertools
import matplotlib
import csv
from CoolProp.BibtexParser import BibTeXerClass
from warnings import warn
# See: https://docs.python.org/2/library/csv.html#csv-examples
import csv, codecs, cStringIO
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
"""
A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
class SolutionDataWriter(object):
"""
A base class that defines all the variables needed
@@ -1126,26 +1191,26 @@ class SolutionDataWriter(object):
return rst
def table_div(self, max_cols, header_flag=1, indent=2):
out = ""
out = u""
for i in range(indent):
out += " "
out += u" "
if header_flag == 1:
style = "="
style = u"="
else:
style = "-"
style = u"-"
for max_col in max_cols:
out += max_col * style + " "
out += "\n"
out += max_col * style + u" "
out += u"\n"
return out
def normalize_row(self, row, max_cols, indent=2):
r = ""
r = u""
for i in range(indent):
r += " "
r += u" "
for i, max_col in enumerate(max_cols):
r += row[i] + (max_col - len(row[i]) + 1) * " "
return r + "\n"
r += row[i] + (max_col - len(row[i]) + 1) * u" "
return r + u"\n"
def writeTextToFile(self, path,text):
@@ -1153,19 +1218,21 @@ class SolutionDataWriter(object):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
with open(path, 'w') as f:
f.write(text)
f.write(text.encode('utf-8'))
return True
def writeTxtTableToFile(self, path,table,head=""):
if not head == "":
return self.writeTextToFile(path+".txt", head+"\n\n"+self.make_table(table))
def writeTxtTableToFile(self, path,table,head=u""):
if not head == u"":
return self.writeTextToFile(path+".txt", head+u"\n\n"+self.make_table(table))
return self.writeTextToFile(path+".txt", self.make_table(table))
def writeCsvTableToFile(self, path,table):
if not os.path.exists(os.path.dirname(path+".csv")):
os.makedirs(os.path.dirname(path+".csv"))
with open(path+".csv", 'wb') as f:
writer = csv.writer(f)
#writer = csv.writer(f)
writer = UnicodeWriter(f)
writer.writerows(table)
return True
@@ -1203,20 +1270,20 @@ class SolutionDataWriter(object):
# link = "{0}".format(text)
# pass
# TODO: Fix this!
link = ":download:`{0}<{1}>`".format(text,target)
link = u":download:`{0}<{1}>`".format(text,target)
return link
def m(self, math):
text = ":math:`{0}`".format(math)
text = u":math:`{0}`".format(math)
return text
def c(self, number):
#text = "{0:5.2f} |degC|".format(self.checkForNumber(number)-273.15)
text = "{0:5.2f}".format(self.checkForNumber(number)-273.15)
text = u"{0:5.2f}".format(self.checkForNumber(number)-273.15)
return text
def x(self, number):
text = "{0:3.2f}".format(self.checkForNumber(number))
text = u"{0:3.2f}".format(self.checkForNumber(number))
return text
@@ -1229,9 +1296,9 @@ class SolutionDataWriter(object):
if np.any(xmin>0.0) and np.any(xmax<1.0): use_x = True
else: use_x = False
header = ['Name', 'Description', 'Reference', \
self.m('T_{min}')+" (|degC|)", self.m('T_{max}')+" (|degC|)"]
if use_x: header.extend([self.m('x_{min}'), self.m('x_{max}')])
header = [u'Name', u'Description', u'Reference', \
self.m(u'T_\\text{min}')+u" (°C)", self.m(u'T_\\text{max}')+u" (°C)"]
if use_x: header.extend([self.m(u'x_\\text{min}'), self.m(u'x_\\text{max}')])
testTable = []
testTable.append(header) # Headline

View File

@@ -82,7 +82,7 @@
[
-9.073173e-07,
4.118599e-06,
-4.391163e-06,
-4.391164e-06,
0.000000e+00,
0.000000e+00,
0.000000e+00

View File

@@ -80,7 +80,7 @@
0.000000e+00
],
[
3.270564e-08,
3.270563e-08,
-2.261472e-07,
3.368862e-07,
0.000000e+00,

View File

@@ -76,8 +76,8 @@
],
[
-4.117538e-10,
3.193137e-09,
-3.138206e-09,
3.193145e-09,
-3.138209e-09,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -160,9 +160,9 @@
0.000000e+00
],
[
-1.548862e-10,
8.615137e-10,
-8.067025e-11,
-1.548841e-10,
8.615147e-10,
-8.067086e-11,
0.000000e+00,
0.000000e+00,
0.000000e+00

View File

@@ -86,9 +86,9 @@
0.000000e+00
],
[
-6.379647e-08,
-6.379657e-08,
3.363858e-07,
-3.132405e-07,
-3.132406e-07,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -124,8 +124,8 @@
],
[
-2.058397e-06,
-3.348859e-10,
5.467784e-10,
-3.348864e-10,
5.467785e-10,
0.000000e+00,
0.000000e+00,
0.000000e+00

View File

@@ -45,7 +45,7 @@
[
1.059366e-09,
-2.212375e-09,
2.522431e-10,
2.522430e-10,
0.000000e+00,
0.000000e+00,
0.000000e+00

View File

@@ -16,7 +16,7 @@
1.000000e-03
],
[
4.945495e-17
4.945410e-17
],
[
4.446923e-20

View File

@@ -16,7 +16,7 @@
1.000000e-03
],
[
3.613874e-17
3.613676e-17
],
[
3.832824e-20

View File

@@ -16,7 +16,7 @@
1.000000e-03
],
[
9.106767e-17
9.107021e-17
],
[
1.979940e-20

View File

@@ -33,7 +33,7 @@
-2.269000e+00
],
[
-1.492754e-13
-1.493085e-13
],
[
1.637145e-16
@@ -65,7 +65,7 @@
2.000000e+00
],
[
1.348892e-13
1.348812e-13
],
[
-2.528359e-16

View File

@@ -160,7 +160,7 @@
0.000000e+00
],
[
3.074006e-05,
3.074005e-05,
4.644860e-04,
-4.128601e-04,
0.000000e+00,

View File

@@ -80,7 +80,7 @@
0.000000e+00
],
[
-1.261406e-06,
-1.261407e-06,
1.595687e-04,
-2.874999e-04,
0.000000e+00,

View File

@@ -41,9 +41,9 @@
0.000000e+00
],
[
-3.201909e-14,
-8.491526e-14,
1.265223e-12,
-3.116864e-14,
-8.496811e-14,
1.264271e-12,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -78,9 +78,9 @@
0.000000e+00
],
[
1.986411e-11,
-3.429773e-10,
7.354027e-10,
1.984946e-11,
-3.372422e-10,
7.093277e-10,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -130,9 +130,9 @@
0.000000e+00
],
[
5.248779e-11,
-7.030447e-10,
1.861376e-09,
5.482548e-11,
-6.893357e-10,
1.956098e-09,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -167,9 +167,9 @@
0.000000e+00
],
[
-1.262879e-15,
-4.101099e-13,
1.456601e-12,
-9.388323e-15,
-4.140820e-13,
1.454213e-12,
0.000000e+00,
0.000000e+00,
0.000000e+00

View File

@@ -39,8 +39,8 @@
],
[
5.963914e-14,
-1.171232e-12,
3.544925e-12,
-1.171287e-12,
3.544939e-12,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -75,9 +75,9 @@
0.000000e+00
],
[
2.382838e-11,
-4.371017e-10,
2.078221e-09,
2.382339e-11,
-4.369353e-10,
2.078946e-09,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -129,7 +129,7 @@
[
-7.407941e-11,
1.506720e-09,
-5.254342e-09,
-5.256161e-09,
0.000000e+00,
0.000000e+00,
0.000000e+00
@@ -164,9 +164,9 @@
0.000000e+00
],
[
1.144571e-14,
-5.037253e-13,
2.031073e-12,
1.141968e-14,
-5.033025e-13,
2.031147e-12,
0.000000e+00,
0.000000e+00,
0.000000e+00

View File

@@ -80,7 +80,7 @@
0.000000e+00
],
[
7.584600e-07,
7.584599e-07,
-1.681891e-06,
8.927989e-07,
0.000000e+00,

View File

@@ -80,7 +80,7 @@
0.000000e+00
],
[
-8.851911e-08,
-8.851905e-08,
8.435685e-07,
-1.848113e-06,
0.000000e+00,

View File

@@ -33,10 +33,10 @@
-4.000000e-01
],
[
-2.441934e-14
-2.441148e-14
],
[
4.087442e-17
4.090153e-17
]
],
"type": "polynomial"

View File

@@ -16,7 +16,7 @@
7.000000e-04
],
[
6.853149e-17
6.853806e-17
],
[
-2.456396e-20
@@ -33,7 +33,7 @@
-4.000000e-01
],
[
-8.005915e-15
-8.003880e-15
],
[
-3.881444e-17

View File

@@ -33,7 +33,7 @@
-4.000000e-01
],
[
2.591269e-14
2.590801e-14
],
[
-5.114724e-17

View File

@@ -65,7 +65,7 @@
2.100000e+00
],
[
8.650944e-15
8.662076e-15
],
[
-1.546072e-16

View File

@@ -38,7 +38,7 @@
0.000000e+00
],
[
7.748626e-09,
7.748627e-09,
-1.399289e-07,
2.343560e-07,
0.000000e+00,

View File

@@ -36,7 +36,7 @@
-4.200618e-10
],
[
4.712469e-13
4.712470e-13
]
],
"type": "polynomial"

View File

@@ -33,7 +33,7 @@
-5.524646e-01
],
[
-3.713730e-11
-3.713729e-11
],
[
3.828340e-14

View File

@@ -33,10 +33,10 @@
-6.044286e-01
],
[
-2.253584e-12
-2.253580e-12
],
[
-3.942105e-15
-3.942159e-15
]
],
"type": "polynomial"