0
Iteration, $X, $Y
1,0.581261625,0.010561718
2,0.470764982,0.01282931
3,0.44202262,0.011346878
4,0.447475587,0.011324991

I have this file in this form, out of 500 values I put the first five. For every $X and $Y at the same iteration there is Z that will be calculated from the following code from an old X and Y.

My following code is to call the file and to find the equivalent Z for each $X and $Y that is already provided in the code.

import sys
from math import log
import datetime
import re
import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
import itertools
from scipy.optimize import curve_fit


##interpolation data for Z 
xx = 0.15, 0.33, 0.35, 0.5, 0.67, 0.8
yy = 0.01, 0.05, 0.08, 0.1, 0.15, 0.2
zz = 0.75, 0.81, 0.83, 1.00, 0.92, 0.91, 

# Read parameter file
parametersAndValues = {}
parameterFile = open('ParameterValues') #open(os.environ['RAVEN_PARAMETERFILE'])

try:
    for line in parameterFile:
        parametersAndValues[line.split()[0]] = ((float(line.split()[1])))
finally:
    parameterFile.close()

X = parametersAndValues.get('$X')
Y = parametersAndValues.get('$Y')


for parameter in parametersAndValues:
    ( parameter + ' = ' + str(parametersAndValues.get(parameter))) 

This part of the code is to interpolate the values of X and Y to find Z

##interpolation data for TLMIXPAR
tck = interpolate.bisplrep(xx, yy, zz, s=0)    
def givemeomega(X,Y):
        result = interpolate.bisplev(X,Y,tck)
        return '{:.5f}'.format(min(1.0, result))

The error code I keep getting is ValueError: could not convert string to float: '$X,', so I am not able to call the value properly neither find the proper Z for $X and $Y

I appreciate all the help you can offer

OriolAbril
  • 7,315
  • 4
  • 29
  • 40

1 Answers1

1

You forgot about the header when you read your file

This is how I would read the file

X, Y = [], []
with open('ParameterValues', 'r') as parameterFile:
    header = parameterFile.readline()
    for line in parameterFile:
        items = [float(x) for x in line.split(',')]
        X.append(items[1])
        Y.append(items[2])

_

print X
# [0.581261625, 0.470764982, 0.44202262, 0.447475587]

print Y
# [0.010561718, 0.01282931, 0.011346878, 0.011324991]

And then do your interpolation which I don't really understand how is supposed to work
(This is an entirely separate issue)

Z = givemeomege(X,Y)
Josh Wilkins
  • 193
  • 1
  • 8
  • Thank you very much for your help, but I got this error 'header = parameterFile.next()' 'AttributeError: '_io.TextIOWrapper' object has no attribute 'next'', –  Apr 02 '18 at 22:33
  • Are you in python 2 or 3? [This](https://stackoverflow.com/questions/26967509/attributeerror-io-textiowrapper-object-has-no-attribute-next-python) answer to your error seems to indicate .next() doesn't work in this instance for python 3 (but does for python 2). Try .readline() instead of .next(), or add a 'r' to your open command to indicate you are reading the file – Josh Wilkins Apr 03 '18 at 14:17
  • Exactly that what I am trying to do 'df['Z'] = givemeomega(df['$X'], df['$Y'])' for each line but I have three columns and multiple lines, but Pandas is not installed and i got error every time that I install pandas through pip, is there another way to do it without Pandas –  Apr 04 '18 at 10:54
  • I am on python 3 –  Apr 04 '18 at 10:55
  • Updated answer to not use pandas (just return an array or something with your function now). Your issue now is not with pandas, but with the interpolation. I can't help you with this, I have no experience with that tool. – Josh Wilkins Apr 04 '18 at 14:46
  • Thank you very much, I really appreciate your help –  Apr 04 '18 at 14:58