0

I have two files, one with spatial data on every line (x,y,z); and one with the field value at every line. Each line in the spatial data file corresponds to the line in the other field value file.

spatial data:

(-2 2 0.1)
(-2 1.92708 0.1)
(-2 1.85417 0.1)
...
(4.92784 1.92708 0.1)
(5 1.85417 0.1)
... 
etc

field value data:

4.35
8.90
5.6
44.4
3.4 
.. etc

I would like to create a 2D array out of this. My x data: I have 98 points spaced evenly from -2 -> 5 My y data: I have 49 points spaced evenly from -1.5 -> 2

I want a 2D matrix of field value data of size 98x49, corresponding to the spatial position.

The problem is, my data is not in order, so I need to map the spatial (x,y) to its value. Then, I need to give it the proper 2D array index.

Thank you for your help!

1
  • I'm not sure we could help with mapping the spatial data to the field value. My recommendation is to tack on the field data to the spatial data as another column. Then you can reshape as you like. Alternatively, as you don't seem to be using the z column, you could replace it with the field data, then reshape. Commented Aug 31, 2020 at 20:21

1 Answer 1

1

Assuming you have an array of spatial data coords, where every row gives the (x,y,z) coordinate of a data point, and another array of the field values at the point data_vals, I would do something like:

# create empty zeros array to be filled with the data vals 
output = np.zeros((98, 49))

# evenly spaced in x and y, so determine the size of the step along each dimension
# there are 97 steps between -2 and 5 according to your question  
XstepSize = (5 - -2) / 97 
YstepSize = (2 - -1.5) / 48 

# loop for every ROW in your data
for i in range(98): 
      # the loop variable is the row    
      # index of the coordinates 
      # array (every row 
      # corresponds to a data 
      # value) 
      
      # determine the x index
      Xval = coords[i,0] 

      # since your data is evenly spaced in x and y direction
      Xidx = int((Xval - xmin) / XstepSize)
        
      # determine the y index 
      Yval = coords[i,1] 
      Yidx = int((Yval - Ymin) / YstepSize)

      # fill your output with the
      # data at the desired index 
      output[xidx, yidx] = data_vals[i]

This method takes the continuous (x,y) coordiante of a data point and maps it to the correct index in a 2D array of data_vals.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.