-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistToGraph_v2.f90
More file actions
148 lines (123 loc) · 4.54 KB
/
histToGraph_v2.f90
File metadata and controls
148 lines (123 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
! PROGRAM TO PLOT FROM RAW HISTOGRAM FILE
! AUTHOR, ABHIJIT BAISHYA
! FIRST EDIT: 04/10/2021
! LAST EDIT: 01/02/2022
program histToGraph
use hist_mod
implicit none
real,allocatable :: hist(:),gr_hist(:,:)
character(50) :: inname,outname,LowerLimit,UpperLimit,NumberOfBins,gpname,response
real :: lowlim,uplim,binsize
integer :: i,N,nbins,iostat
! CHECK FOR PROPER INPUT FORMAT AT THE TERMINAL, I.E. EITHER THE INPUT FILE HAS TO BE GIVEN,
! IN WHICH CASE THE OUTPUT FILE WILL BE GENERATED BY THE PROGRAM OAND PARAMETERS FOR THE HISTOGRAM
! WILL BE CALCULATED AUTOMATICALLY OR PARAMTERES CAN BE EXPLICITELY GIVEN
if (iargc() < 1 .or. iargc() > 4) then
print*,'Input format is "./program inputfile" or "./program inputfile LowerLimit UpperLimit NumberOfBins"'
stop
endif
! ASSIGNING INPUT FILE AND OUTPUT FILE NAMES TO VARIABLES
if (iargc() == 1) then
call getarg(1,inname)
outname = 'graph_'//TRIM(inname)
elseif (iargc() == 2) then
call getarg(1,inname)
outname = 'graph_'//TRIM(inname)
call getarg(2,LowerLimit)
read(LowerLimit,*)lowlim
elseif (iargc() == 3) then
call getarg(1,inname)
outname = 'graph_'//TRIM(inname)
call getarg(2,LowerLimit)
call getarg(3,UpperLimit)
read(LowerLimit,*)lowlim
read(UpperLimit,*)uplim
elseif (iargc() == 4) then
call getarg(1,inname)
outname = 'graph_'//TRIM(inname)
call getarg(2,LowerLimit)
call getarg(3,UpperLimit)
call getarg(4,NumberOfBins)
read(LowerLimit,*)lowlim
read(UpperLimit,*)uplim
read(NumberOfBins,*)nbins
endif
! GNUPLOT FILE FOR PLOTTING
gpname='gp_hist.gp'
! OPENING FILES FOR READING AND WRITING
open(10,file=inname,action='read') ! OPENING INPUT HISTOGRAM FILE
open(11,file=outname,action='write') ! OPENING OUPUT GRAPH FILE
open(12,file=gpname,action='write')
N = 0 ! NUMBER OF LINES
do
read(10,*,iostat=iostat)
if (iostat < 0) exit
N=N+1
enddo
! ALLOCATING THE HISTOGRAM ARRAY
allocate(hist(N))
rewind(10) ! REWINDING FILE FROM EOF TO THE BEGINNING
! READING DATA POINTS FROM HISTOGRAM FILE
do i=1,N
read(10,*)hist(i)
enddo
! ASSIGNING INPUT FILE AND OUTPUT FILE NAMES TO VARIABLES
if (iargc() == 1) then
call opt_binw(hist,binsize)
lowlim = minval(hist)
uplim = maxval(hist)
nbins = nint((uplim-lowlim)/binsize)
!print*,binsize,lowlim,uplim,nbins
! CALLING HIST_TO_GRAPH SUBROUTINE TO CONVERT HISTOGRAM TO GRAPH
call hist_to_graph(hist,nbins,lowlim,uplim,gr_hist)
elseif (iargc() == 2) then
call opt_binw(hist,binsize)
uplim = maxval(hist)
nbins = nint((uplim-lowlim)/binsize)
!print*,lowlim,uplim,nbins
! CALLING HIST_TO_GRAPH SUBROUTINE TO CONVERT HISTOGRAM TO GRAPH
call hist_to_graph(hist,nbins,lowlim,uplim,gr_hist)
elseif (iargc() == 3) then
call opt_binw(hist,binsize)
nbins = nint((uplim-lowlim)/binsize)
!print*,lowlim,uplim,nbins
! CALLING HIST_TO_GRAPH SUBROUTINE TO CONVERT HISTOGRAM TO GRAPH
call hist_to_graph(hist,nbins,lowlim,uplim,gr_hist)
elseif (iargc() == 4) then
!print*,lowlim,uplim,nbins
! CALLING HIST_TO_GRAPH SUBROUTINE TO CONVERT HISTOGRAM TO GRAPH
call hist_to_graph(hist,nbins,lowlim,uplim,gr_hist)
endif
! WRITING GRAPH X,Y DATA INTO FILE
do i=1,nbins+1
write(11,*)gr_hist(i,:)
enddo
! GENERATING GNUPLOT FILE FOR PLOTTING
!write(12,*)'set term qt'
write(12,*)'set xrange[',lowlim,':',uplim,']'
write(12,*)'plot "',trim(outname),'" u 1:2 sm csplines' ! SPLINE CURVE WILL BE PLOTTED, IF YOU WANT USUAL LINE PLOT OR SCATTER PLOT, EDIT THIS LINE
! CLOSING OPEN FILES
close(10)
close(11)
close(12)
! DEALLOCATING THE HIST ARRAY TO FREE MEMORY
deallocate(hist,gr_hist)
! PLOTTING COMMAND
!print*,'Whether you want to plot the histogram? Y/N?'
call execute_command_line('gnuplot -p gp_hist.gp')
! REMOVING OUTPUT GRAPH FILE, COMMENT OUT IF YOU NEEDED
print*,'Do you want to delete the ouput graph file? Y/N?'
do
read*,response
if (response == 'Y' .or. response == 'y') then
call execute_command_line('rm '//outname)
exit
elseif (response == 'N' .or. response == 'n') then
exit
else
print*,'Enter a valid command, e.g. Y/N'
endif
enddo
! REMOVING GNUPLOT FILE, COMMENT OUT IF NEEDED
call execute_command_line('rm gp_hist.gp')
end program