Steve
Steve

Reputation: 23

Implementing Fortran's WHERE statement in Python

Is there a way of doing this in Python with NumPy arrays? preferably without using too many for loops.

I know numpy.where() exists but I am not sure how to implement it if it can be used as in the following Fortran snippet

z_ocean=0
do i=1,nstep_yr
   where(mldclim(:,:,i).gt.z_ocean) z_ocean = mldclim(:,:,i)
end do

Upvotes: 1

Views: 477

Answers (1)

jbdv
jbdv

Reputation: 1263

Consider what the where statement in your fortran code implies:

"assign to each element of z_ocean, where the corresponding element value from mldclim[i, :, :] is greater than the element value in z_ocean, the element value from mldclim[i, :, :]"

Edit: I modified the definition above to better reflect the fact that this is an elementwise operation, as stated in a comment by @francescalus

Looking at the documentation for numpy.where(), it can be used as where(*condition*, *value if true*, *value if false*). So in your case, something like this:
z_ocean = np.where(mldclim[i, :, :] > z_ocean, mldclim[i, :, :], z_ocean)

Here are examples in python (note that I've swapped around index order in python for direct comparison)

import numpy as np    

nstep_yr = 2    

# initialize randomly
# mldclim = np.random.rand(nstep_yr, 3, 3) - 0.5    
# use these values for fortran comparison
mldclim = np.reshape([ 0.09911714,  0.18911965,  0.30409478, -0.08548523, \
                      -0.03652424, -0.18361127,  0.49970408, -0.04139379, \
                      -0.03451338, -0.24631131,  0.35726568, -0.30630386, \
                       0.26184705,  0.01286879, -0.21745516,  0.46137956, \
                       0.40410629,  0.29996342], (nstep_yr, 3, 3) )    

# initialize and make a copies for testing...
z_ocean = np.zeros((3,3))
z_ocean_1 = np.copy(z_ocean)
z_ocean_2 = np.copy(z_ocean)    

for i in range(nstep_yr):
    # "direct translation" of your fortran code
    z_ocean = np.where(mldclim[i, :, :] > z_ocean, mldclim[i, :, :], z_ocean)
    # loop based version of @francescalus' comment
    z_ocean_1 = np.maximum(z_ocean_1, mldclim[i, :, :])    
# loop free version of @francescalus' comment
z_ocean_2 = np.maximum(z_ocean_2, np.max(mldclim, axis=0)) 

# check that all solutions are the same
assert np.allclose(z_ocean, z_ocean_1) and np.allclose(z_ocean, z_ocean_2)    

print(z_ocean.ravel())

and fortran (replicating your original code sample)

program test
    implicit none

    integer, parameter :: nstep_yr = 2
    real :: mldclim(3,3,nstep_yr)
    real :: z_ocean(3,3)
    integer :: i

    mldclim = reshape([ 0.09911714,  0.18911965,  0.30409478, -0.08548523, &
                       -0.03652424, -0.18361127,  0.49970408, -0.04139379, &
                       -0.03451338, -0.24631131,  0.35726568, -0.30630386, &
                        0.26184705,  0.01286879, -0.21745516,  0.46137956, &
                        0.40410629,  0.29996342], [3, 3, 2] )

    z_ocean=0
    do i=1,nstep_yr
        where(mldclim(:,:,i).gt.z_ocean) z_ocean = mldclim(:,:,i)
    end do
    print *, z_ocean

end program test

Which gives the following output:

  • python [0.09911714 0.35726568 0.30409478 0.26184705 0.01286879 0. 0.49970408 0.40410629 0.29996342]
  • fortran 9.91171375E-02 0.357265681 0.304094791 0.261847049 1.28687900E-02 0.00000000 0.499704093 0.404106289 0.299963415

Upvotes: 1

Related Questions