Debian Codegraph @

src/transform.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-2009 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !------------------------------------------------------------------------
24 !
25 ! module containing subroutines for applying transformations to data
26 ! prior to plotting.
27 !
28 ! the transformations are:
29 !
30 ! 1) log10(x)
31 ! 2) |x|
32 ! 3) 1/x
33 ! 4) sqrt(x)
34 ! 5) x^2
35 ! 6) ln(x)
36 !
37 ! * combinations of transformations are done when the
38 ! input number is > 10 (e.g. 321 means 1/x, then abs, then log10)
39 !
40 ! subroutines contained within this module are the following:
41 !
42 ! transform : applies transformation to a one dimensional array
43 ! transform2 : applies the transformation to a two dimensional array
44 ! transform_limits : transforms the plot limits appropriately
45 ! transform_label : changes the plot label appropriately
46 !
47 ! Written for use in supersphplot/splash by
48 ! Daniel Price, Institute of Astronomy, Cambridge, UK 2002-2004
49 ! University of Exeter, UK 2004-
50 ! [email protected]
51 !
52 !------------------------------------------------------------------------
53 module transforms
54 implicit none
55 integer, parameter, public :: ntrans = 6 ! this is the number of different transformations
56 real, parameter, private :: zerolog = 1.e-12 ! this is minimum set if xmin = 0 and log
57 public :: transform,transform_inverse,trans
58 public :: transform_limits,transform_limits_inverse,transform_label
59 public :: convert_to_ln_fac
60 public :: islogged
61
62 private
63
64 interface transform
65 module procedure transform,transform_limits,transform2,transforma
66 end interface transform
67
68 !--function interface
69 interface trans
70 module procedure transformarray
71 end interface trans
72
73 interface transform_inverse
74 module procedure transform_inverse,transform_limits_inverse,transforma_inverse
75 end interface transform_inverse
76
77 contains
78 !------------------------------------------------------------------------
79 !
80 ! subroutine returns log, 1/x of a given array
81 !
82 ! * can specify up to 9 individual operations to perform
83 ! * combinations of transformations are done when the
84 ! input number is > 10 (e.g. 321 means 1/x, then abs, then log10)
85 !
86 !------------------------------------------------------------------------
87 subroutine transform(array,itrans,errval)
88 integer, intent(in) :: itrans
89 real, dimension(:), intent(inout) :: array
90 real, intent(in), optional :: errval
91 real, dimension(size(array)) :: arraytemp
92 real :: errvali
93 character(len=20) :: string
94 integer :: i
95 !
96 !--errval is the value to be set for errors
97 ! (default is zero if not present)
98 !
99 if (present(errval)) then
100 errvali = errval
101 else
102 errvali = 0.
103 endif
104 !
105 !--extract the digits from the input number
106 !
107 if (itrans > 0) then
108
109 write(string,*) itrans
110 !
111 !--do a transformation for each digit
112 !
113 arraytemp = array
114
115 do i=1,len_trim(string)
116 !
117 !--perform transformation appropriate to this digit
118 !
119 select case(string(i:i))
120 case('1')
121 where (arraytemp > 0. .and. arraytemp /= errvali)
122 arraytemp = log10(arraytemp)
123 elsewhere
124 arraytemp = errvali
125 end where
126 case('2')
127 where (arraytemp /= errvali)
128 arraytemp = abs(arraytemp)
129 end where
130 case('3')
131 where (arraytemp /= 0. .and. arraytemp /= errvali)
132 arraytemp = 1./arraytemp
133 elsewhere
134 arraytemp = errvali
135 end where
136 case('4')
137 where (arraytemp > 0. .and. arraytemp /= errvali)
138 arraytemp = sqrt(arraytemp)
139 elsewhere
140 arraytemp = errvali
141 end where
142 case('5')
143 where (arraytemp /= errvali)
144 arraytemp = arraytemp**2
145 end where
146 case('6')
147 where (arraytemp > 0. .and. arraytemp /= errvali)
148 arraytemp = log(arraytemp)
149 elsewhere
150 arraytemp = errvali
151 end where
152 end select
153 enddo
154
155 array = arraytemp
156
157 elseif (itrans < 0) then
158 !
159 ! perform the inverse transform if itrans is negative
160 !
161 call transform_inverse(array,abs(itrans),errvali)
162 endif
163
164 end subroutine transform
165
166 !------------------------------------------------------------------------
167 !
168 ! interface to above for a single real number
169 !
170 !------------------------------------------------------------------------
171 subroutine transforma(aa,itrans,errval)
172 integer, intent(in) :: itrans
173 real, intent(inout) :: aa
174 real, intent(in), optional :: errval
175 real, dimension(1) :: array
176
177 array(1) = aa
178 if (present(errval)) then
179 call transform(array,itrans,errval=errval)
180 else
181 call transform(array,itrans)
182 endif
183 aa = array(1)
184
185 end subroutine transforma
186
187 !------------------------------------------------------------------------
188 !
189 ! function interface: returns array valued function
190 !
191 !------------------------------------------------------------------------
192 function transformarray(array,itrans,errval)
193 integer, intent(in) :: itrans
194 real, intent(in), dimension(:) :: array
195 real, dimension(size(array)) :: transformarray
196 real, intent(in), optional :: errval
197
198 transformarray = array
199 if (present(errval)) then
200 call transform(transformarray,itrans,errval=errval)
201 else
202 call transform(transformarray,itrans)
203 endif
204
205 end function transformarray
206
207 !------------------------------------------------------------------------
208 !
209 ! inverse transform
210 !
211 !------------------------------------------------------------------------
212 subroutine transform_inverse(array,itrans,errval)
213 integer, intent(in) :: itrans
214 real, dimension(:), intent(inout) :: array
215 real, intent(in), optional :: errval
216 real, dimension(size(array)) :: arraytemp
217 real :: errvali
218 character(len=20) :: string
219 integer :: i
220 !
221 !--errval is the value to be set for errors
222 ! (default is zero if not present)
223 !
224 if (present(errval)) then
225 errvali = errval
226 else
227 errvali = 0.
228 endif
229 !
230 !--extract the digits from the input number
231 !
232 if (itrans > 0) then
233
234 write(string,*) itrans
235 !
236 !--do a transformation for each digit
237 !
238 arraytemp = array
239
240 do i=len_trim(string),1,-1
241 !
242 !--perform transformation appropriate to this digit
243 !
244 select case(string(i:i))
245 case('1')
246 where (arraytemp /= errvali)
247 arraytemp = 10**arraytemp
248 end where
249 case('3')
250 where (arraytemp /= 0. .and. arraytemp /= errvali)
251 arraytemp = 1./arraytemp
252 elsewhere
253 arraytemp = errvali
254 end where
255 case('4')
256 where (arraytemp /= errvali)
257 arraytemp = arraytemp**2
258 end where
259 case('5')
260 where (arraytemp > 0. .and. arraytemp /= errvali)
261 arraytemp = sqrt(arraytemp)
262 elsewhere
263 arraytemp = errvali
264 end where
265 case('6')
266 where (arraytemp /= errvali)
267 arraytemp = exp(arraytemp)
268 end where
269 end select
270 enddo
271
272 array = arraytemp
273
274 elseif (itrans < 0) then
275 !
276 ! perform the forward transform if itrans is negative
277 !
278 call transform(array,abs(itrans),errvali)
279
280 endif
281
282 end subroutine transform_inverse
283
284
285 !------------------------------------------------------------------------
286 !
287 ! interface to above for a single real number
288 !
289 !------------------------------------------------------------------------
290 subroutine transforma_inverse(aa,itrans,errval)
291 integer, intent(in) :: itrans
292 real, intent(inout) :: aa
293 real, intent(in), optional :: errval
294 real, dimension(1) :: array
295
296 array(1) = aa
297 if (present(errval)) then
298 call transform_inverse(array,itrans,errval=errval)
299 else
300 call transform_inverse(array,itrans)
301 endif
302 aa = array(1)
303
304 end subroutine transforma_inverse
305
306 !------------------------------------------------------------------------
307 !
308 ! same as transform but for a two dimensional array
309 ! applies the transformation to the same array as was input
310 !
311 !------------------------------------------------------------------------
312 subroutine transform2(array,itrans,errval)
313 integer, intent(in) :: itrans
314 real, dimension(:,:), intent(inout) :: array
315 real, intent(in), optional :: errval
316 real, dimension(size(array,1),size(array,2)) :: arraytemp
317 real :: errvali
318 character(len=20) :: string
319 integer :: i
320 !
321 !--errval is the value to be set for errors
322 ! (default is zero if not present)
323 !
324 if (present(errval)) then
325 errvali = errval
326 else
327 errvali = 0.
328 endif
329 !
330 !--extract the digits from the input number
331 !
332 if (itrans > 0) then
333
334 write(string,*) itrans
335 !
336 !--do a transformation for each digit
337 !
338 arraytemp = array
339
340 do i=1,len_trim(string)
341 !
342 !--perform transformation appropriate to this digit
343 !
344 select case(string(i:i))
345 case('1')
346 where (arraytemp > 0. .and. arraytemp /= errvali)
347 arraytemp = log10(arraytemp)
348 elsewhere
349 arraytemp = errvali
350 end where
351 case('2')
352 where (arraytemp /= errvali)
353 arraytemp = abs(arraytemp)
354 end where
355 case('3')
356 where (arraytemp /= 0. .and. arraytemp /= errvali)
357 arraytemp = 1./arraytemp
358 elsewhere
359 arraytemp = errvali
360 end where
361 case('4')
362 where (arraytemp > 0. .and. arraytemp /= errvali)
363 arraytemp = sqrt(arraytemp)
364 elsewhere
365 arraytemp = errvali
366 end where
367 case('5')
368 where (arraytemp /= errvali)
369 arraytemp = arraytemp**2
370 end where
371 case('6')
372 where (arraytemp > 0. .and. arraytemp /= errvali)
373 arraytemp = log(arraytemp)
374 elsewhere
375 arraytemp = errvali
376 end where
377 end select
378 enddo
379
380 array = arraytemp
381
382 !elseif (itrans < 0) then
383 !
384 ! perform the inverse transform if itrans is negative
385 !
386 !call transform2_inverse(array,abs(itrans),errval)
387
388 endif
389
390 end subroutine transform2
391
392 !------------------------------------------------------------------------
393 !
394 ! same as transform but for the plot limits
395 ! (min can become max and vice versa)
396 !
397 !------------------------------------------------------------------------
398 subroutine transform_limits(xmin,xmax,itrans)
399 integer, intent(in) :: itrans
400 real, intent(inout) :: xmin,xmax
401 real :: xmintemp,xmaxtemp
402 character(len=20) :: string
403 integer :: i
404 !
405 !--extract the digits from the input number
406 !
407 if (itrans > 0) then
408
409 write(string,*) itrans
410 !
411 !--do a transformation for each digit
412 !
413 xmintemp = xmin
414 xmaxtemp = xmax
415
416 do i=1,len_trim(string)
417 !
418 !--perform transformation appropriate to this digit
419 !
420 select case(string(i:i))
421 case('1')
422 if (xmintemp > 0) then
423 xmintemp = log10(xmintemp)
424 elseif (xmintemp==0) then
425 print*,' log10(xmin = 0): min set to ',zerolog
426 xmintemp = log10(zerolog)
427 endif
428 if (xmaxtemp > 0) then
429 xmaxtemp = log10(xmaxtemp)
430 elseif (xmaxtemp==0) then
431 print*,' log10(xmax = 0): max set to ',zerolog
432 xmaxtemp = log10(zerolog)
433 endif
434 case('2')
435 if ((xmintemp < 0. .and. xmaxtemp > 0.) &
436 .or.(xmaxtemp < 0. .and. xmintemp > 0.)) then
437 !
438 !--minimum is zero if limits have opposite signs
439 !
440 xmaxtemp = max(abs(xmintemp),abs(xmaxtemp))
441 xmintemp = 0.
442 else
443 !
444 !--or just take magnitude
445 !
446 xmintemp = abs(xmintemp)
447 xmaxtemp = abs(xmaxtemp)
448 endif
449 case('3')
450 if (xmintemp /= 0) then
451 xmintemp = 1./xmintemp
452 else
453 xmintemp = 0.
454 endif
455 if (xmaxtemp /= 0) then
456 xmaxtemp = 1./xmaxtemp
457 else
458 xmaxtemp = 0.
459 endif
460 case('4')
461 if (xmintemp >= 0) then
462 xmintemp = sqrt(xmintemp)
463 else
464 xmintemp = 0.
465 endif
466 if (xmaxtemp >= 0) then
467 xmaxtemp = sqrt(xmaxtemp)
468 else
469 xmaxtemp = 0.
470 endif
471 case('5')
472 xmintemp = xmintemp**2
473 xmaxtemp = xmaxtemp**2
474 case('6')
475 if (xmintemp > 0) then
476 xmintemp = log(xmintemp)
477 elseif (xmintemp==0) then
478 print*,' ln(xmin = 0): min set to ',zerolog
479 xmintemp = log(zerolog)
480 endif
481 if (xmaxtemp > 0) then
482 xmaxtemp = log(xmaxtemp)
483 elseif (xmaxtemp==0) then
484 print*,' ln(xmax = 0): max set to ',zerolog
485 xmaxtemp = log(zerolog)
486 endif
487 end select
488 enddo
489
490 xmin = min(xmintemp,xmaxtemp)
491 xmax = max(xmintemp,xmaxtemp)
492
493 elseif (itrans < 0) then
494 !
495 ! perform the inverse transform if itrans is negative
496 !
497 call transform_limits_inverse(xmin,xmax,itrans)
498
499 endif
500
501 end subroutine transform_limits
502
503 !------------------------------------------------------------------------
504 !
505 ! inverse transform for the plot limits
506 ! (so that we can change the transformed limits and set the
507 ! untransformed limits accordingly)
508 !
509 !------------------------------------------------------------------------
510 subroutine transform_limits_inverse(xmin,xmax,itrans)
511 integer, intent(in) :: itrans
512 real, intent(inout) :: xmin,xmax
513 real :: xmintemp,xmaxtemp,xtemp
514 character(len=20) :: string
515 integer :: i
516 !
517 !--extract the digits from the input number
518 !
519 if (itrans > 0) then
520
521 write(string,*) itrans
522 !
523 !--do a transformation for each digit
524 !
525 xmintemp = xmin
526 xmaxtemp = xmax
527
528 do i=len_trim(string),1,-1 ! do digits in reverse
529 !
530 !--perform transformation appropriate to this digit
531 !
532 select case(string(i:i))
533 case('1')
534 xmintemp = 10**xmintemp
535 xmaxtemp = 10**xmaxtemp
536 case('2')
537 !
538 !--if minimum is zero give limits opposite signs
539 ! (but same magnitude), otherwise do nothing
540 !
541 if (xmintemp==0.) then
542 xtemp = max(abs(xmintemp),abs(xmaxtemp))
543 xmintemp = -xtemp
544 xmaxtemp = xtemp
545 endif
546 case('3')
547 if (xmintemp /= 0) then
548 xmintemp = 1./xmintemp
549 else
550 xmintemp = 0.
551 endif
552 if (xmaxtemp /= 0) then
553 xmaxtemp = 1./xmaxtemp
554 else
555 xmaxtemp = 0.
556 endif
557 case('4')
558 xmintemp = xmintemp**2
559 xmaxtemp = xmaxtemp**2
560 case('5')
561 if (xmintemp > 0) then
562 xmintemp = sqrt(xmintemp)
563 else
564 xmintemp = 0.
565 endif
566 if (xmaxtemp > 0) then
567 xmaxtemp = sqrt(xmaxtemp)
568 else
569 xmaxtemp = 0.
570 endif
571 case('6')
572 xmintemp = exp(xmintemp)
573 xmaxtemp = exp(xmaxtemp)
574 end select
575 enddo
576
577 xmin = min(xmintemp,xmaxtemp)
578 xmax = max(xmintemp,xmaxtemp)
579
580 elseif (itrans < 0) then
581 !
582 ! perform the forward transform if itrans is negative
583 !
584 call transform_limits(xmin,xmax,itrans)
585
586 endif
587
588 end subroutine transform_limits_inverse
589
590 !------------------------------------------------------------------------
591 !
592 ! function to adjust the label of a plot if log, 1/x etc
593 !
594 ! Note: *cannot* put print or write statements into this function
595 ! as it is used in the middle of write or print statements
596 ! this means that finding the digits is a bit trickier
597 !
598 !------------------------------------------------------------------------
599 function transform_label(label,itrans)
600 integer, intent(in) :: itrans
601 character(len=*), intent(in) :: label
602 integer :: itransmulti,i,ndigits
603 integer, dimension(5) :: digit
604 character(len=len(label)+20) :: transform_label
605 character(len=len(label)+20) :: temp_label
606 !
607 !--extract the digits from the input number
608 !
609 if (itrans > 0) then
610 call get_digits(itrans,digit,ndigits)
611 temp_label = label
612 !
613 !--do a transformation for each digit
614 !
615 do i=1,ndigits
616 itransmulti = digit(i)
617 !
618 !--perform transformation appropriate to this digit
619 !
620 select case(itransmulti)
621 case(1)
622 temp_label = 'log '//trim(temp_label)
623 case(2)
624 temp_label = '|'//trim(temp_label)//'|'
625 case(3)
626 temp_label = '1/'//trim(temp_label)
627 case(4)
628 temp_label = 'sqrt('//trim(temp_label)//')'
629 case(5)
630 temp_label = trim(temp_label)//'^2'
631 case(6)
632 temp_label = 'ln '//trim(temp_label)
633 case default
634 temp_label = trim(temp_label)
635 end select
636 enddo
637
638 transform_label = temp_label
639 else
640 transform_label = label
641 endif
642
643 end function transform_label
644
645 !------------------------------------------------------------------------
646 ! get_digits: for an integer i returns number of digits it contains
647 ! and a list of these *without* using write statements
648 !
649 ! i : integer to split into digits
650 ! nmax : dimensions of digits array
651 ! digits(nmax) : array of digits
652 ! ndigits : number of digits in i
653 !------------------------------------------------------------------------
654
655 subroutine get_digits(i,digits,ndigits)
656 integer, intent(in) :: i
657 integer, intent(out) :: ndigits
658 integer, intent(out), dimension(:) :: digits
659 integer :: j,isubtract,idigit
660
661 ndigits = 0
662
663 isubtract = 0
664
665 do j=size(digits),0,-1
666 if (i >= 10**j) then
667 ndigits = ndigits + 1
668 idigit = (i - isubtract)/10**j
669 digits(ndigits) = idigit
670 isubtract = isubtract + digits(ndigits)*10**j
671 endif
672 enddo
673
674 end subroutine get_digits
675
676 !------------------------------------------------------------------------
677 ! function returning the correction factor by which to multiply
678 ! to convert the log in the transform to a natural log
679 !------------------------------------------------------------------------
680 real function convert_to_ln_fac(itrans)
681 integer, intent(in) :: itrans
682 character(len=20) :: string
683 integer :: i
684 real :: xtemp
685
686 !
687 !--default conversion factor is unity
688 !
689 xtemp = 1.0
690 !
691 !--extract the digits from the input number
692 !
693 if (itrans > 0) then
694 write(string,*) itrans
695 do i=len_trim(string),1,-1 ! do digits in reverse
696 !
697 !--perform transformation appropriate to this digit
698 !
699 select case(string(i:i))
700 !
701 !--correction factor is ln(10.) but can be
702 ! 1./ln(10.), sqrt(1./ln(10.), etc. depending
703 ! on other transformations
704 !
705 case('1')
706 xtemp = log(10.)*xtemp
707 case('2')
708 xtemp = abs(xtemp)
709 case('3')
710 xtemp = 1./xtemp
711 case('4')
712 xtemp = sqrt(xtemp)
713 case('5')
714 xtemp = xtemp**2
715 !case('6')
716 ! xtemp = xtemp
717 end select
718 enddo
719 endif
720
721 convert_to_ln_fac = xtemp
722
723 end function convert_to_ln_fac
724
725 !---------------------------------------
726 ! query function to return whether the
727 ! transformation involves a log or not
728 !---------------------------------------
729 logical function islogged(itrans)
730 integer, intent(in) :: itrans
731 character(len=20) :: string
732
733 write(string,"(i8)") itrans
734 islogged = (index(string,'1') /= 0 .or. index(string,'6') /= 0)
735
736 end function islogged
737
738 end module transforms