in Education by
I am looking for a way to return the interpolated coordinate value of a variable at a query variable value. I think it is the opposite use to a normal interpolation/table-look up. For example, here is a simple, normal interpolation, From this very good tutorial: x_data = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) f = xr.DataArray(x_data**2, dims=['x'], coords={'x': x_data}) f.plot(marker='o') Which produces this plot: So a standard lookup (e.g. f.interp(x=4.5)) would look for the value of the variable at some coordinate value of x. But I want to look for the value of the x where the variable is equal to some value. For example in the above plot, what is the value of x where the variable equals 20. I am ultimately trying to do this for a 3D field of temperature (longitude x latitude x depth). So, how do I find the value of depth, where the temperature field is closest to a value that I specify. Many thanks in advance. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I have found a simple method for solving this using scipy.interpolate which works, though is relatively slow to compute. from scipy import interpolate ii = interpolate.interp1d(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])**2,np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) ii(20) Which returns array(4.44444444). I think this is an answer, which might help someone, but not the ideal answer. Ideally this could be done in a pythonic manner, perhaps using the xarray module. To extend this to 3D, I had to loop it within 2 for-loops, like this: def calc_depthToTempROMS(tempfield,depthfield,queryfield,xxlims,eelims): ## the two input fields are the tempfield (equivalent to the coordinates) and the depthfield (equivalent to the function to be interpolated). ## queryfield is the values of temperature to interpolate for depth. ## xxlims and eelims are the for-loop bounds, which are model specific for xx in xxlims: for ee in eelims: ii = interpolate.interp1d(tempfield.isel(xi_rho=xx,eta_rho=ee),depthfield.isel(xi_rho=xx,eta_rho=ee)) # make the interpolator depthTC[dict(eta_rho=ee,xi_rho=xx)] = ii(queryfield.isel(xi_rho=xx,eta_rho=ee)) # interpolate for each location if xx.values in np.round(np.linspace(0,len(xxlims.values),10)): print(xx.values) # print every 10th index to monitor progress return depthTC depthTC = (truth.tempTC.mean(dim='ocean_time')*np.nan).load() # initialise an empty matrix tempfield = truth.temp.mean(dim='ocean_time').load() # coordinates depthfield = truth.z_rho0.load() # function to interpolate queryfield = truth.tempTC.mean(dim='ocean_time').load() # target coordinate xxlims = truth.xi_rho.load() eelims = truth.eta_rho.load() depthTC = calc_depthToTempROMS(tempfield,depthfield,queryfield,xxlims,eelims) Someone else using xarray for processing earth science model data may find the above code useful. I will leave this unaccepted for now - hopefully someone can come up with a better, xarray-based method for achieving this.

Related questions

0 votes
    I have two data frames with the closest matching DateTime index, sometimes matching. An object is to merge two of ... I achieve this? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Is this possible? From what I'm seeing, the only way to get options into the the python class is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    What is the meaning of _ after for in this code? if tbh.bag: n = 0 for _ in tbh.bag.atom_set(): n += 1 Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Alright, I know how to print variables and strings. But how can I print something like "My string" card.price ... variable card.price). Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    webbrowser.open('https://api.WhatsApp.com/send?phone=number') I want to send WhatsApp messages to numbers without ... in this link. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    In python, I want to sum all of the values in a variable. My code is x=100 y=200 for i in range(a,b) ... of all numbers which is 7500. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    In the python documentation, an instance of a named_tuple is created by using the below code: Point = named_tuple(' ... of doing it? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I want to assign a value to a TensorFlow variable in Python and I am using this: import tensorflow as tf import ... do to correct this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How does one write a unittest that fails only if a function doesn't throw an expected exception? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
...