This is a follow-up question from Fortran: Open, form='(un)formatted', read. I have to create an array that saves the number of elements under certain criteria.
program location
implicit none
interface
function distkm(deglat1,deglon1,deglat2,deglon2)
real :: distkm
real, intent(in) :: deglat1,deglon1,deglat2,deglon2
end function distkm
end interface
integer, parameter :: maxnr = 200000
integer :: nr, i, j, ios
character(len=1) :: junkfornr
real :: start, finish
! My variable declaration
character(len=15), dimension(:), allocatable :: key
real, dimension(:), allocatable :: lat, lon
integer, dimension(:), allocatable :: jobs
! Secondary arrays
integer, dimension(:), allocatable :: jobs_within
integer, dimension(:), allocatable :: sum_jobs
! Determine total number of lines in file
nr=0
open(10, file='location_data2.txt', status='old')
do i=1,maxnr
read(10,*,iostat=ios) junkfornr
if (ios/=0) exit
if (i == maxnr) then
stop
endif
nr = nr + 1
end do
! Create variables: key, lat, lon, jobs
allocate(key(nr))
allocate(lat(nr))
allocate(lon(nr))
allocate(jobs(nr))
allocate(jobs_within(nr))
allocate(sum_jobs(nr))
rewind(10)
do i=1,nr
read(10,*) key(i), lat(i), lon(i), jobs(i)
end do
do i=1,nr
do j=1,nr
if (distkm(lat(i),lon(i),lat(j),lon(j)) <= 0.3) then
jobs_within(j) = jobs(j)
else
jobs_within(j) = 0
end if
end do
sum_jobs(i) = sum(jobs_within)
end do
close(10)
open(20,file='key_sum_jobs_0.3.txt')
do i=1,nr
write(20,100) key(i), sum_jobs(i)
end do
100 format(A15,I6)
end program location
The do loops save the sum of workers within 0.3 kilometers for each firm. Here, for each firm, the number of firms within 0.3 kilometers varies. How can I create an array (say, n_neighbor
) that records the number of adjacent firms for each firm?
EDIT: I believe the following solves the problem:
do i=1,nr
n_aux(1:nr) = 0
do j=1,nr
if (distkm(lat(i),lon(i),lat(j),lon(j)) <= 1) then
jobs_within(j) = jobs(j)
n_aux(j) = n_aux(j) + 1
else
jobs_within(j) = 0
end if
end do
write(20,100) key(i), sum(jobs_within), sum(n_aux)
100 format(A15,I8,1X,I3)
end do
I create an integer array (n_aux
). Then, the output text file of unit=20
saves three columns: (1) firm key, (2) sum of workers within 0.3km, and (3) the number of firms within 0.3km.
Things to ponder:
I set all the elements of n_aux
be 0
ahead of the j
loop. Then, I throw 1's
where the if
condition holds. Should there be other way to express this operation from the perspective of something like...dynamically assigning values onto an array?