Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process grid data

Status
Not open for further replies.

raghu81

Technical User
Apr 9, 2008
28
DE
Dear all,

I have a grid based data which is a 3-D array

data(N1,N2,N3)

where

N1 = (x_max - x_min + step_x) / step_x
N2 = (y_max - y_min + step_y) / step_y
N3 = (z_max - z_min + step_z) / step_z

x_min is the minimum value for the x-coordinate
x_max is the maximum value for the x-coordinate
step_x is the step size along x

and so on (all these values are integers).

The data which I have is from an output file where the data is printed 'possibly' using the following code. I only have the binary and this is a guess.

N_data = 0
do x = x_min, x_max, step_x ! one can also use do 1 : N1

do y = y_min, y_max, step_y

do z = z_min, z_max, step_z

if ( data(x,y,z) .ne. 0) then
N_data = N_data + 1
print *, x, y, z, data(x,y,z)
endif

enddo
enddo
enddo

when data(x,y,z) = 0, the value is not printed. Currently I am simply reading the grid data as a 2-D array

grid_data(N_data, 4) ! 4 columns for x, y, z, data(x,y,z)

I am trying to implement an expression where I need the full 3-D array (data) including the zero values with proper array indices. So I have to back transform the grid_data to the original data(N1, N2, N3). This I have been doing as shown below

ii = 0
do ind_x = x_min, x_max, stepx
ii = ii + 1

jj = 0
do ind_y = y_min, y_max, stepy
jj = jj + 1

kk = 0
do ind_z = z_min, z_max, stepz
kk = kk + 1

do ind_g = 1, N_grids

if( (ind_x .eq. grid_data(ind_g, 1) ) .and. ( (ind_y .eq. grid_data(ind_g, 2) ) .and. ( (ind_z .eq. grid_data(ind_g, 3) ) ) then

data(ii,jj,kk) = grid_data(ind_g, 4)

endif

enddo

enddo

enddo

enddo


The code works, but for large number of grids it runs for ever. Is there a way to do it efficiently. Any help is greatly appreciated. Thank you in advance.

regards,
Raghu

 
Here is an example

Actual data for

x_min = y_min = z_min = 1
x_max = y_max = z_max = 3
step_x = step_y = step_z = 1

This is what I want

# X Y Z data(X,Y,Z)

1 1 1 0
1 1 2 0
1 1 3 0.1
1 2 1 0.2
1 2 2 0
1 2 3 0
1 3 1 0
1 3 2 0.1
1 3 3 0.4
2 1 1 0
2 1 2 0.5
2 1 3 0
2 2 1 0
2 2 2 0
2 2 3 0
2 3 1 0
2 3 2 0
2 3 3 0
3 1 1 0.3
3 1 2 0
3 1 3 0
3 2 1 0
3 2 2 0
3 2 3 0
3 3 1 0.4
3 3 2 0
3 3 3 0

But this is what I have: Only non-zero data(X,Y,Z) are available

# X Y Z data(X,Y,Z)

1 1 3 0.1
1 2 1 0.2
1 3 2 0.1
1 3 3 0.4
2 1 2 0.5
3 1 1 0.3
3 3 1 0.4
N_data = 7

The problem is to plug in the zero values for data(X,Y,Z) appropriately to get back the original 3^3 = 27 values as a 3-D array.
 
Ehm, you say:

"The data which I have is from an output file where the data is printed 'possibly' using the following code. I only have the binary and this is a guess."

But in "the following code" you write the data to the screen, not to any file.

Furthermore, if the file is binary you'll have to know how it has been written, with how many bits, in little or big endian format, as real or as integer, etc. in order to be able to read it. If it's binary indeed, and no index is kept up, I can't see quickly how one is able to save only non zero values, because when you start reading it you don't know anymore which values were zero and which weren't.

You're not talking just about what appears on the screen, are you?

 
@GerritGroot:

Let me define the problem through the example which I had posted.


I have the grid data

# X Y Z data(X,Y,Z)

1 1 3 0.1
1 2 1 0.2
1 3 2 0.1
1 3 3 0.4
2 1 2 0.5
3 1 1 0.3
3 3 1 0.4

N_data = 7

For 3 grids along x, y, z there should be 3 x 3 x 3=27 values, but in the table above only non-zero values (7 values) are available.

Now I have to read the table as a 2-D array grid_data(N_grid, 4) and get the 3-D array data(N1, N2, N3) which is needed to implement some other expressions.

At the end of my first post, I have the code which I have been using to do the task, but for large values of N1, N2 and N3 the code is very slow.

I am only looking for an efficient way to do the task.

 
raghu81 said:
The code works, but for large number of grids it runs for ever.
IMHO: What you posted could not work.
1)
In one loop you are using step_x:
do x = x_min, x_max, step_x
...
but in the other loop you are using stepx:
do ind_x = x_min, x_max, stepx
...
It's very messy. It seems that the value of stepx is undefined. This could be compiled only if you use implicite fortran types, but then ind_x would be integer and x_min floating point number.

2)
It could run forever, because you are using floating point control variable and step size in do-loop.
 
@mikrom:

IMHO: What you posted could not work.
1)
In one loop you are using step_x:
do x = x_min, x_max, step_x
...
but in the other loop you are using stepx:
do ind_x = x_min, x_max, stepx


The first code was my guess, I think this how the program I had used gave me only non-zero data. This piece of code is not related to the second code which I used to get the 3-D array.


It's very messy. It seems that the value of stepx is undefined. This could be compiled only if you use implicite fortran types, but then ind_x would be integer and x_min floating point number.


Step sizes, grid points are integers... I mentioned that in the first post
"x_min is the minimum value for the x-coordinate
x_max is the maximum value for the x-coordinate
step_x is the step size along x

and so on (all these values are integers)."


It could run forever, because you are using floating point control variable and step size in do-loop.


I am using only integer running indices, as in the example. Going from an integer grids problem to a floating point grid problem is another issue for which I have a recipe.


 
If what you are reading is:

# X Y Z data(X,Y,Z)

1 1 3 0.1
1 2 1 0.2
1 3 2 0.1
1 3 3 0.4
2 1 2 0.5
3 1 1 0.3
3 3 1 0.4

Just initialise with:
Code:
data(:,:,:)=0.
And read
Code:
READ(1,*)nx,ny,nz,data(nx,ny,nz)
And everything will turn out fine
 
@GerritGroot:

Yes. Assigning the whole 3-D arrays to zero and then reading the indices for non-zero values and then the values themselves solves the problem. Thanks to mikrom and GerritGroot.

Note: I did the same also for the case involving floating point values for x, y, z grid coordinates as

READ(1,*)nx,ny,nz,data(fn(nx),fn(ny),fn(nz))

the function 'fn' maps the floating point grid coordinate to grid counting index.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top