Debian Codegraph @

src/sph_moments.f90

raw
1 !---------------------------------------------------------------------------------
2 !
3 ! sphmoments - a utility for computing kernel-smoothed moment maps
4 ! of observational data cubes
5 !
6 ! Copyright (C) 2023 Daniel Price
7 ! [email protected]
8 !
9 ! --------------------------------------------------------------------------
10 !
11 ! This program is free software; you can redistribute it and/or modify
12 ! it under the terms of the GNU General Public License as published by
13 ! the Free Software Foundation; either version 2 of the License, or
14 ! (at your option) any later version.
15 !
16 ! This program is distributed in the hope that it will be useful,
17 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ! GNU General Public License for more details.
20 !
21 ! You should have received a copy of the GNU General Public License
22 ! along with this program; if not, write to the Free Software
23 ! Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 !
25 ! -------------------------------------------------------------------------
26 ! Version history/ Changelog:
27 ! 0.9.7 : (06/11/23)
28 ! fix velocity array read if comments in the fits header;
29 ! bug fix with moment calculation if velocity array in reverse order
30 ! 0.9.1 : (09/10/23)
31 ! fix homebrew install
32 ! 0.9.0 : (09/10/23)
33 ! velocity array read correctly from fits header
34 ! 0.8.0 : (09/10/23)
35 ! write correct x and y units to output files;
36 ! copy and flatten fits header from original cube
37 ! 0.7.0 : (06/10/23)
38 ! basic working version implemented
39 !
40 ! -------------------------------------------------------------------------
41 !
42 ! Written by DJP initially as part of the exoALMA project
43 !
44 !---------------------------------------------------------------------------------
45 program sph_moments
46 use readwrite_fits, only:read_fits_cube,write_fits_image,get_from_header,flatten_header
47 use iso_fortran_env, only:stderr=>error_unit, stdout=>output_unit
48 use system_utils, only:get_command_option,count_matching_args,ienvlist
49 use moments, only:get_moments
50 use kernels, only:select_kernel,kernelname
51 !$ use omp_lib, only:omp_get_num_threads
52 implicit none
53 character(len=256) :: file1,fileout,fileout_m(5),tagline
54 integer :: ierr,jext,nfiles,naxes(4),k,iarglist(2),nm,ns,ikernel
55 real, allocatable :: cube(:,:,:),moment(:,:,:),zvals(:)
56 character(len=80), allocatable :: fitsheader(:)
57 character(len=12) :: string
58 real :: dv,hfac
59 logical :: iexist
60 integer :: moment_type(5)
61
62 nfiles = count_matching_args('.fits',iarglist)
63
64 tagline = 'sph_moments v0.9.7: a SPLASH imaging utility (c) 2023 Daniel Price'
65 if (nfiles < 1) then
66 print "(a)",trim(tagline)
67 print "(/,a)",'Usage: sph_moments [options] infile.fits [outfile.fits]'
68 print "(/,a)",'Options: --moments=0,1,9 [which moments to take, default=0,1,2,8,9]'
69 print "(a)", ' --kernel=0 [which smoothing kernel to use: 0=cubic spline 2=quartic 3=quintic]'
70 print "(a)", ' --hfac=3. [ratio of smoothing length to channel spacing in spectral dimension]'
71 print "(a)", ' --sample=10 [factor by which to oversample line profiles, not very important]'
72 stop
73 endif
74
75 !
76 ! read filenames from the command line
77 !
78 call get_command_argument(iarglist(1),file1)
79 if (nfiles >= 2) then
80 call get_command_argument(iarglist(2),fileout)
81 else
82 fileout = 'moment.fits' ! if no .fits extension found in original file
83 jext = index(file1,'.fits')
84 fileout = file1(1:jext-1)//'_moment.fits' ! default if no output filename given
85 endif
86 !
87 ! get options from the command line
88 !
89 ns = nint(get_command_option('sample'))
90 hfac = get_command_option('hfac',default=3.)
91 ikernel = nint(get_command_option('kernel',default=0.))
92 call select_kernel(ikernel)
93
94 !
95 ! list of moments to compute
96 !
97 moment_type = ienvlist('moments',size(moment_type),errval=-1)
98 nm = 0
99 do k=1,size(moment_type)
100 if (k > 1) then
101 if (moment_type(k)==moment_type(k-1)) exit
102 endif
103 if (moment_type(k) >= 0 .and. moment_type(k) <= 9) then
104 nm = nm + 1
105 endif
106 enddo
107 ! default behaviour if no moments specified is to compute all
108 if (nm==0) then
109 nm = 5
110 moment_type = (/0,1,2,8,9/)
111 endif
112
113 ! check output files do not already exist
114 iexist = .false.
115 do k=1,nm
116 fileout_m(k) = fileout
117 jext = index(fileout_m(k),'.fits')
118 write(string,"(i0)") moment_type(k)
119 fileout_m(k) = fileout_m(k)(1:jext-1)//trim(string)//'.fits'
120 inquire(file=fileout_m(k),exist=iexist)
121 if (iexist) then
122 write(stderr,*) 'ERROR: '//trim(fileout_m(k))//' already exists: please move or rename and try again'
123 endif
124 enddo
125 if (iexist) stop
126
127 ! read original file
128 write(stdout,*) '>> reading '//trim(file1)
129 call read_fits_cube(file1,cube,naxes,ierr,hdr=fitsheader,velocity=zvals)
130 if (ierr /= 0) stop 'error reading file'
131
132 ! eliminate NaNs
133 where (isnan(cube))
134 cube = 0.
135 end where
136
137 if (naxes(3) <= 1) stop 'this is a 2D image but require 3D fits cube for moment generation'
138
139 !
140 ! find moments using either all channels or range of channels
141 !
142 write(stdout,"(1x,a,f5.2)") '>> computing moments using '//trim(kernelname(ikernel))//' kernel with h/dz = ',hfac
143 ! warn about running in serial
144 !$omp parallel
145 !$ if (omp_get_num_threads()<=1) write(stderr,"(/,1x,a)") &
146 !$ 'WARNING: running in serial: type "export OMP_NUM_THREADS=8" to use 8 threads'
147 !$omp end parallel
148 call get_moments(cube,zvals,hfac,moment_type(1:nm),moment)
149 !
150 ! write results to a fits file
151 !
152 call flatten_header(fitsheader)
153 do k=1,nm
154 call write_fits_image(fileout_m(k),moment(:,:,k),naxes(1:2),ierr,hdr=fitsheader)
155 enddo
156
157 deallocate(cube,moment,zvals)
158
159 end program sph_moments