roninkoi

Fortran plot

Aug 29th, 2019
1,384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module plotutil
  2.   implicit none
  3.  
  4.   integer, parameter :: rk = 8
  5.  
  6.   character(3000) :: line
  7.  
  8. contains
  9.   subroutine mka(a, min, max, diff) ! make array
  10.     real(rk), allocatable :: a(:)
  11.     real(rk) :: max, min, diff
  12.  
  13.     integer :: n, i
  14.  
  15.     n = (max - min)/diff
  16.  
  17.     allocate(a(n))
  18.  
  19.     a(1) = min
  20.     do i = 2, n, 1
  21.        a(i) = a(i - 1) + diff
  22.     end do
  23.   end subroutine mka
  24.  
  25.   subroutine column(r, c)
  26.     integer :: c
  27.     real(rk) :: r
  28.     character :: ch
  29.     character(3000) :: cch
  30.     logical :: ws
  31.  
  32.     integer :: i, n, cc
  33.  
  34.     i = 1
  35.     n = 3000
  36.     cc = 0
  37.     ws = .false.
  38.     cch = ''
  39.  
  40.     do while (i < n)
  41.        ch = line(i:i)
  42.        !print *, trim(ch), trim(cch)
  43.  
  44.        if (ch == ' ' .or. ch == char(9) .or. ch == char(10)) then
  45.           ws = .true.
  46.        else
  47.           if (ws .eqv. .true.) then
  48.              ws = .false.
  49.              cc = cc + 1
  50.  
  51.              if (cc == c) then
  52.                 exit;
  53.              else
  54.                 cch = ''
  55.              end if
  56.           end if
  57.  
  58.           cch = trim(cch) // ch
  59.        end if
  60.  
  61.        i = i + 1
  62.     end do
  63.  
  64.     read(cch, *) r
  65.   end subroutine column
  66.  
  67.   function parser(p, c) ! create array from file
  68.     character(32) :: p
  69.     integer :: c
  70.     real(rk), allocatable :: parser(:)
  71.  
  72.     integer :: i, n, io
  73.     real(rk), allocatable :: a(:)
  74.     real(rk) :: s
  75.  
  76.     open(8, file = p, action = 'read')
  77.  
  78.     allocate(a(0))
  79.  
  80.     n = 2
  81.     i = 1
  82.     do while (i < n)
  83.        read(8, '(A)', iostat = io) line
  84.        call column(s, c)
  85.  
  86.        if (io == 0) then
  87.           n = n + 1
  88.           call append(a, s)
  89.        end if
  90.        i = i + 1
  91.     end do
  92.  
  93.     allocate(parser(n))
  94.  
  95.     close(8)
  96.  
  97.     print *, a
  98.  
  99.     parser = a
  100.  
  101.     deallocate(a)
  102.   end function parser
  103.  
  104.   subroutine append(a, v) ! append real to array
  105.     real(rk), allocatable :: a(:)
  106.     real(rk) :: v
  107.  
  108.     real(rk), allocatable :: b(:)
  109.     integer :: n
  110.  
  111.     n = size(a)
  112.  
  113.     allocate(b(n))
  114.  
  115.     b = a
  116.     deallocate(a)
  117.     allocate(a(n + 1))
  118.  
  119.     a(1:n) = b
  120.     a(n + 1) = v
  121.   end subroutine append
  122. end module plotutil
  123.  
  124. program main
  125.   use plotutil
  126.   use ogpf ! gnuplot
  127.  
  128.   implicit none
  129.  
  130.   character(32) :: s
  131.   integer :: n
  132.  
  133.   real(rk), allocatable :: lum(:)
  134.   real(rk), allocatable :: efftemp(:)
  135.   real(rk), allocatable :: temp(:)
  136.   real(rk), allocatable :: a(:)
  137.   real(rk), allocatable :: b(:)
  138.   real(rk), allocatable :: time(:)
  139.  
  140.   real(rk) :: s1 = 1.0140  
  141.   real(rk) :: a1 = 8.1774e-5
  142.   real(rk) :: b1 = 1.7063e-9
  143.   real(rk) :: c1 = -4.3241e-12
  144.   real(rk) :: d1 = -6.6462e-16
  145.  
  146.   real(rk) :: s2 = 0.3438
  147.   real(rk) :: a2 = 5.8942e-5
  148.   real(rk) :: b2 = 1.6558e-9
  149.   real(rk) :: c2 = -3.0045e-12
  150.   real(rk) :: d2 = -5.2983e-16
  151.  
  152.   real(rk) :: solum = 3.828e26 ! W
  153.  
  154.   type(gpf):: gp
  155.  
  156.   call get_command_argument(1, s)
  157.  
  158.   print *, "Reading data..."
  159.  
  160.   time = parser(s, 4)
  161.  
  162.   efftemp = parser(s, 39) ! read data from file in command line arg
  163.  
  164.   lum = parser(s, 40) ! read data from file in command line arg
  165.  
  166. !  n = size()
  167.  
  168.   efftemp = 10**(efftemp) ! log -> lin
  169.   lum = 10**(lum)
  170.  
  171.   temp = efftemp - 5780.0 ! t
  172.  
  173.   a = s1 + a1 * temp + b1 * temp**2 + c1 * temp**3 + d1 * temp**4 ! seff
  174.   b = s2 + a2 * temp + b2 * temp**2 + c2 * temp**3 + d2 * temp**4
  175.  
  176.   a = (lum / a)**0.5 ! d
  177.   b = (lum / b)**0.5
  178.  
  179.   !call mka(t, 0.0_8, real(n, kind = 8), 1.0_8)
  180.  
  181.   ! Annotation: set title, xlabel, ylabel
  182.   call gp%title('Elinkelpoinen vyöhyke')
  183.   call gp%xlabel('t')
  184.   call gp%ylabel('d')
  185.   call gp%options('set key top left; set style data linespoints')
  186.   !Call Plot to draw a vector against a vector of data
  187.   call gp%plot(x1=time,y1=a,ls1='title "moist"',x2=time,y2=b,ls2='title "max"',&
  188.        x3=time,y3=a*0.95,ls3='title "moist -5%"',x4=time,y4=b*1.05,ls4='title "max +5%"')
  189.  
  190. !  call plot(t, v)
  191. end program main
Advertisement
Add Comment
Please, Sign In to add comment