Fiqih Rahmanto
Fiqih Rahmanto

Reputation: 1

Calculating the cross-sectional area of a river with a large amount of coordinate data inputted using a txt file

So I want to calculate the cross-sectional area of a river with an irregular channel, and also I want to calculate the cross-sectional area for every 1 metre rise in water level to the highest elevation of the cross-section. I encountered a problem that the program I created could not loop to the next cross section, and hopefully I can interpolate correctly to calculate the cross-sectional area for every 1 metre rise in water level in the channel. The next problem is that I want to know how my code can detect when interpolating the depth (y) value between two variables of coordinate data and can be re-entered sequentially into one cross-section coordinate matrix. I hope the problem I expressed is clear enough and anyone can help me. The programming language I am using is Fortran 90. Thank you.

program Read_Cross

implicit none
integer, parameter :: dp = selected_real_kind(15,307)
real(kind=dp), allocatable :: x(:), y(:), z(:), Y_New(:), X_New(:) 
real(kind=dp) :: area1, area0, area, min_Y, max_Y, y_diff, X_int
integer :: i, n_Titik, iostat, n_Penampang, Y_Step, Y_Start
character(len=20) :: Header_Penampang

open(unit = 10, file = 'penampang_multi.txt', status = 'old', action = 'read')
n_Penampang = 0

do 
    ! Read Header Setiap Data Penampang
    read(10, '(A)', iostat=iostat) Header_Penampang
    if (iostat /= 0) exit  ! Akhiri jika sudah sampai akhir file

    n_Penampang = n_Penampang + 1 ! Menghitung jumlah penampang
    
    ! Read jumlah titik koordinat setiap penampang
    read(10, *, iostat=iostat) n_Titik
    if (iostat /= 0) exit ! Akhiri jika sudah mencapai akhir

    ! Alokasikan array sesuai jumlah titik
    allocate(x(n_Titik), y(n_Titik), z(n_Titik))

    ! Baca titik koordinat dari file
    do i = 1, n_Titik
        read(10, *) x(i), y(i), z(i)
    end do

    ! Mulai hitung luas penampang sebagai poligon
    area1 = 0.0_dp
    do i = 1, n_Titik - 1
        area1 = area1 + (x(i) * y(i + 1) - y(i) * x(i + 1))
    end do
    ! Menghubungkan titik terakhir dengan titik pertama
    area1 = area1 + (x(n_Titik) * y(1) - y(n_Titik) * x(1))
    area = abs(area1) / 2.0_dp

    ! Cari nilai minimum dan maksimum dari Y
    min_Y = minval(y)
    max_Y = maxval(y)

    ! Alokasikan array Y_New untuk interpolasi
    allocate(Y_New(n_Titik))
    allocate(X_New(n_Titik))
    
    Y_Step= int(max_Y-min_Y)
    
    ! Melakukan interpolasi nilai Y baru
    Y_Start = min_Y
    do i=2, Y_Step
        Y_New(1) = Y_Start+1
        Y_New(i) = Y_New(i-1) + 1.0_dp  ! Kenaikan Y sebesar 1 meter
            if (Y_New(i) > max_Y) then      ! Keluar dari loop jika Y_New melebihi Y maksimum
            end if
    end do

    ! Interpolasi nilai X baru berdasarkan Y baru
    do i = 1, Y_Step
        if ((Y_New(i) > y(i)).and.(Y_New(i) < y(i+1))) then
            X_New(i) = x(i) + (x(i+1) - x(i)) * (Y_New(i) - y(i)) / (y(i+1) - y(i))
        end if
    end do

    ! Print hasil koordinat dan luas penampang
    print *, "Penampang", n_Penampang
    do i = 1, n_Titik
        print *, "Koordinat (x, y, z): ", x(i), y(i), z(i)
    end do
    do i=1, Y_Step
    print *, "Hasil Interpolasi Kedalaman", Y_New(i), "m :", X_New(i), Y_New(i)
    end do
    print *, "Luas Penampang: ", area

    ! Deallocate array setelah selesai
    deallocate(x, y, z, Y_New, X_New)

end do

close(10)

end program Read_Cross

This is the format file of channel coordinates that have to input into the program using txt file

Penampang1

7 (Jumlah Titik)

0 10 0

0 1 0

2 1 0

3 1 0

4 1 0

4 10 0

0 10 0

Penampang2

4 (Jumlah Titik)

0 8 5

2 3 5

4 8 5

0 8 5

Penampang3

7 (Jumlah Titik)

0 12 6

2 8 6

4 8 6

5 8 6

6 8 6

8 12 6

0 12 6

Penampang4

6 (Jumlah Titik)

0 12 7

2 10 7

4 8 7

5 6 7

7 9 7

8 10 7

Penampang5

6 (Jumlah Titik)

0 12 8

2 10 8

4 8 8

5 6 8

7 9 8

8 10 8

Penampang6

6 (Jumlah Titik)

0 12 9

2 10 9

4 8 9

5 6 9

7 9 9

8 10 9

Penampang7

6 (Jumlah Titik)

0 12 10

2 10 10

4 8 10

5 6 10

7 9 10

8 10 10

My expectation is that the program can interpolate the coordinates needed to calculate the cross-sectional area of the channel at every 1 metre rise in water level and can be done with a large number and variety of cross-sections such as the case of rivers.

Upvotes: 0

Views: 131

Answers (0)

Related Questions