Debian Codegraph @

src/map_columns.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2023 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-----------------------------------------------------------------
24 ! module which handles prompt for mapping of exact solution
25 ! columns to the data columns in splash
26 !-----------------------------------------------------------------
27 module map_columns
28 use asciiutils, only:match_integer
29 implicit none
30 integer :: nlab
31 character(len=120), allocatable :: label_local(:)
32 character(len=120), allocatable :: exact_label_local(:)
33 integer, allocatable :: imap(:)
34
35 procedure(print_map), pointer, private :: printmap => null()
36 procedure(add_map), pointer, private :: addmap => null()
37 procedure(delete_map), pointer, private :: delmap => null()
38
39 public :: map_labels,map_columns_in_file,map_columns_interactive
40 public :: print_mapping
41
42 private
43
44 contains
45
46 !---------------------------------------------------
47 ! map labels in one list onto corresponding labels
48 ! in another list
49 !---------------------------------------------------
50 function map_labels(list1,list2) result(imap)
51 use asciiutils, only:match_lists
52 use labels, only:label_synonym
53 character(len=*), intent(in) :: list1(:),list2(:)
54 integer :: imap(size(list1)),i
55
56 imap = match_lists(label_synonym(list1),label_synonym(list2))
57
58 ! make mapping unique
59 do i=2,size(imap)
60 if (any(imap(1:i-1)==imap(i))) imap(i) = 0
61 enddo
62
63 end function map_labels
64
65 !---------------------------------------------------
66 ! map labels in the ascii file read as the exact
67 ! solution onto corresponding labels in the data
68 ! (i.e. find which columns correspond to which)
69 !---------------------------------------------------
70 subroutine map_columns_in_file(filename,ncols,nrows,imap,label,exact_labels,nlabels,ierr)
71 use asciiutils, only:get_ncolumns,get_nrows,read_column_labels
72 use labels, only:set_default_labels
73 character(len=*), intent(in) :: filename
74 integer, intent(out) :: ncols,nrows,nlabels
75 integer, intent(out) :: imap(:), ierr
76 character(len=*), intent(in) :: label(:)
77 character(len=*), intent(out) :: exact_labels(:)
78 integer :: nheaderlines,ncolumns,iu
79
80 ncolumns = size(label)
81 nlabels = 0; ncols = 0; nrows = 0
82 call set_default_labels(exact_labels(:))
83
84 open(newunit=iu,file=filename,status='old',iostat=ierr)
85 if (ierr == 0) then
86 call get_ncolumns(iu,ncols,nheaderlines)
87 call get_nrows(iu,nheaderlines,nrows)
88 call read_column_labels(iu,nheaderlines,ncols,nlabels,exact_labels) !,debug=.true.
89 close(iu)
90 !if (nlabels > 0) print "(/,1x,a,2(i0,a),/)",trim(filename)//': ',nrows,' lines, ',nlabels,' columns'
91 endif
92 if (nlabels > 0) then
93 nlabels = min(nlabels,size(imap),size(exact_labels),ncols)
94 imap(1:nlabels) = map_labels(exact_labels(1:nlabels),label(1:ncolumns))
95 else
96 nlabels = min(ncols,size(exact_labels))
97 imap = 0
98 endif
99
100 end subroutine map_columns_in_file
101
102 !-----------------------------------------------------------------
103 ! interactive prompting for mapping list
104 !-----------------------------------------------------------------
105 subroutine map_columns_interactive(imapexact,label,exact_label,nlab_exact)
106 use promptlist, only:prompt_list
107 integer, intent(inout) :: imapexact(:)
108 character(len=*), intent(in) :: label(:),exact_label(:)
109 integer, intent(in) :: nlab_exact
110 integer :: nmap
111
112 ! allocate memory and copy input arrays
113 imap = imapexact
114 label_local = label
115 exact_label_local = exact_label
116 nlab = min(nlab_exact,size(imap))
117 !maxlabels = size(label)
118 if (nlab < 2) return
119
120 nmap = min(nlab,size(imap))
121
122 ! prompt for edited map
123 printmap => print_map
124 addmap => add_map
125 delmap => delete_map
126 call prompt_list(nmap,nlab,'mapping',printmap,addmap,delmap)
127
128 ! copy back to actual array
129 imapexact = imap
130
131 ! clean up
132 if (allocated(label_local)) deallocate(label_local)
133 if (allocated(exact_label_local)) deallocate(exact_label_local)
134
135 end subroutine map_columns_interactive
136
137 !-------------------------------------------
138 ! print the current list of column mappings
139 !-------------------------------------------
140 subroutine print_mapping(ncols1,imap1,list1,list2)
141 integer, intent(in) :: ncols1
142 integer, intent(in) :: imap1(ncols1)
143 character(len=*), intent(in) :: list1(:),list2(:)
144 integer :: j
145
146 print "(/,a)", ' Current mapping:'
147
148 do j=1,ncols1
149 if (imap1(j) > 0 .and. imap1(j) <= size(list2)) then
150 print "(i3,': ',a12,a,i2,a)",j,list1(j),' -> ',imap1(j),') '//trim(list2(imap1(j)))
151 else
152 print "(i3,': ',a12,a)",j,list1(j),' -> None'
153 endif
154 enddo
155
156 end subroutine print_mapping
157
158 !-------------------------------------------
159 ! print the current list of column mappings
160 !-------------------------------------------
161 subroutine print_map(nmap)
162 integer, intent(in) :: nmap
163
164 if (nmap==0) imap = 0 ! reset if nmap = 0, happens after clear operation
165 call print_mapping(nlab,imap,exact_label_local,label_local)
166
167 end subroutine print_map
168
169 !------------------------------------------
170 ! add new mapping
171 !------------------------------------------
172 subroutine add_map(istart,iend,nmap)
173 use prompting, only:prompt
174 use asciiutils, only:match_tag_start
175 use labels, only:label_synonym
176 integer, intent(in) :: istart,iend
177 integer, intent(inout) :: nmap
178 integer :: icol
179
180 if (istart == nmap) then
181 ! default prompt is the first column not already mapped
182 icol = 1
183 do while (imap(icol) > 0)
184 icol = icol + 1
185 enddo
186 call prompt('which exact column to map?',icol,0,nlab)
187 else
188 icol = istart+1
189 endif
190 if (icol <= 0) return
191
192 if (imap(icol)==0) then
193 imap(icol) = match_tag_start(label_synonym(label_local),&
194 label_synonym(exact_label_local(icol)))
195 endif
196 call prompt('which splash column to map '//trim(exact_label_local(icol))// &
197 ' to?',imap(icol),0,size(label_local))
198
199 nmap = nlab
200
201 end subroutine add_map
202
203 !------------------------------------------
204 ! delete column mapping
205 !------------------------------------------
206 subroutine delete_map(icol,nmap)
207 integer, intent(in) :: icol
208 integer, intent(inout) :: nmap
209
210 if (icol > 0 .and. icol <= size(imap)) imap(icol) = 0
211 print*,' deleting ',icol
212
213 nmap = nlab
214
215 end subroutine delete_map
216
217 end module map_columns