| 1 |
!--------------------------------------------------------------------------------- |
| 2 |
! |
| 3 |
! denoise - Command-line tool to remove shot noise from astronomical |
| 4 |
! images and cubes using adaptive kernel smoothing |
| 5 |
! |
| 6 |
! Copyright (C) 2020-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 |
! 1.0.0 : (10/10/2023) |
| 28 |
! now in mature usage; allow up to 1024 characters in file names |
| 29 |
! |
| 30 |
! 0.2.1 : (10/08/2021) |
| 31 |
! updated splash |
| 32 |
! |
| 33 |
! 0.2.0 : (27/08/2020) |
| 34 |
! bug fixes with homebrew install; |
| 35 |
! bug fix with reading beam size for ALMA images/cubes; |
| 36 |
! brew install uses denoise repo, not splash repo (requires fewer dependencies); |
| 37 |
! automated update of homebrew package |
| 38 |
! |
| 39 |
! 0.1.0 : (20/08/2020) |
| 40 |
! first working version of Denoise utility, as used in the Appendix to Ménard et al 2020; |
| 41 |
! support for both single fits images and spectral cubes; |
| 42 |
! can process either single channel or whole cube; |
| 43 |
! reads the beam size from the fits header if available (e.g. in radio images); |
| 44 |
! transfers header information between input and output fits files; |
| 45 |
! option for 2D (channel-by-channel) or 3D denoising of spectral cubes; |
| 46 |
! support for polarisation images (assumed if two fits files given as input), |
| 47 |
! can either denoise each polarisation separately or denoise the polarised intensity image; |
| 48 |
! standalone denoise GitHub repository |
| 49 |
! |
| 50 |
! ------------------------------------------------------------------------- |
| 51 |
! |
| 52 |
! Written by DJP, 2020-2023 |
| 53 |
! |
| 54 |
!--------------------------------------------------------------------------------- |
| 55 |
program denoise |
| 56 |
use readwrite_fits, only:read_fits_cube,write_fits_cube,write_fits_image,get_from_header |
| 57 |
use imageutils, only:image_denoise,image_denoise3D,image_rotate |
| 58 |
use iso_fortran_env, only:stderr=>error_unit, stdout=>output_unit |
| 59 |
use system_utils, only:get_command_option,count_matching_args,get_command_flag |
| 60 |
implicit none |
| 61 |
character(len=1024) :: file1,file2,fileout,tagline,filek |
| 62 |
integer :: ierr,jext,nfiles,its,naxes(4),npixels,k,i,iarglist(3),kstart,kend,k1,k2 |
| 63 |
real, allocatable :: hh(:) |
| 64 |
real, allocatable :: image(:,:,:),image1(:,:,:),image2(:,:,:) |
| 65 |
real, allocatable :: image_old(:,:,:),image_residuals(:,:,:) |
| 66 |
real, allocatable :: snrmap(:,:) |
| 67 |
character(len=80), allocatable :: fitsheader(:) |
| 68 |
real :: beam,fluxold,fluxnew,imax,imagemax,image_max,use3D,bmaj,bmin,cdelt1,rotate |
| 69 |
real :: signal,noise |
| 70 |
logical :: iexist,use_3D,skip,get_residuals |
| 71 |
|
| 72 |
nfiles = count_matching_args('.fits',iarglist) |
| 73 |
|
| 74 |
tagline = 'denoise: a SPLASH imaging utility (c) 2020-2023 Daniel Price' |
| 75 |
if (nfiles < 1) then |
| 76 |
print "(a)",trim(tagline) |
| 77 |
print "(/,a)",'Usage: denoise [options] infile.fits [outfile.fits]' |
| 78 |
print "(/,a)",'Options: --beam=1.0 [beam size in pixels at max intensity]' |
| 79 |
print "(a)", ' --imax=3.4e-2 [intensity value above which no smoothing is applied]' |
| 80 |
print "(a)", ' --its=4 [maximum number of smoothing length iterations]' |
| 81 |
print "(a)", ' --use3D=1 [denoise in 3D for spectral cubes]' |
| 82 |
print "(a)", ' --start=1 [denoise from channel 1 onwards]' |
| 83 |
print "(a)", ' --end=10 [denoise only up to channel 10]' |
| 84 |
print "(a)", ' --nosubsample [do not subsample if beam size >> pixel scale]' |
| 85 |
print "(a)", ' --residuals [output residual image as residuals.fits]' |
| 86 |
print "(a)", ' --rotate=30 [rotate image by 30 degrees]' |
| 87 |
stop |
| 88 |
endif |
| 89 |
|
| 90 |
! |
| 91 |
! read filenames from the command line |
| 92 |
! |
| 93 |
call get_command_argument(iarglist(1),file1) |
| 94 |
if (nfiles >= 3) then |
| 95 |
call get_command_argument(iarglist(2),file2) |
| 96 |
call get_command_argument(iarglist(3),fileout) |
| 97 |
elseif (nfiles >= 2) then |
| 98 |
call get_command_argument(iarglist(2),fileout) |
| 99 |
else |
| 100 |
fileout = 'denoise.fits' ! if no .fits extension found in original file |
| 101 |
jext = index(file1,'.fits') |
| 102 |
fileout = file1(1:jext-1)//'_denoise.fits' ! default if no output filename given |
| 103 |
endif |
| 104 |
! |
| 105 |
! get options from the command line |
| 106 |
! |
| 107 |
imax = get_command_option('imax',default=0.) |
| 108 |
beam = get_command_option('beam',default=0.) |
| 109 |
use3D = get_command_option('use3D',default=0.) |
| 110 |
kstart = nint(get_command_option('start',default=0.)) |
| 111 |
kend = nint(get_command_option('end',default=0.)) |
| 112 |
its = nint(get_command_option('its',default=4.)) |
| 113 |
skip = .not.get_command_flag('nosubsample') |
| 114 |
rotate = get_command_option('rotate',default=0.) |
| 115 |
get_residuals = get_command_flag('residuals') |
| 116 |
|
| 117 |
!print*,' GOT imax=',imax,' fac = ',fac,trim(file1),trim(file2),trim(fileout) |
| 118 |
|
| 119 |
! check output file does not already exist |
| 120 |
inquire(file=fileout,exist=iexist) |
| 121 |
if (iexist) then |
| 122 |
write(stderr,*) 'ERROR: '//trim(fileout)//' already exists: please move or rename and try again' |
| 123 |
stop |
| 124 |
endif |
| 125 |
|
| 126 |
! read original file |
| 127 |
call read_fits_cube(file1,image1,naxes,ierr,hdr=fitsheader) |
| 128 |
if (ierr /= 0) stop 'error reading file' |
| 129 |
|
| 130 |
if (nfiles >= 3) then |
| 131 |
call read_fits_cube(file2,image2,naxes,ierr) |
| 132 |
if (ierr /= 0) stop 'error reading file' |
| 133 |
! polarisation images |
| 134 |
write(stdout,"(a)") '>> Polarisation mode: sqrt(p**2 + q**2) using p='//trim(file1)//' q='//trim(file2) |
| 135 |
image = sqrt(image1**2 + image2**2) |
| 136 |
else |
| 137 |
image = image1 |
| 138 |
endif |
| 139 |
|
| 140 |
! eliminate NaNs |
| 141 |
where (isnan(image)) |
| 142 |
image = 0. |
| 143 |
end where |
| 144 |
|
| 145 |
image_old = image |
| 146 |
|
| 147 |
use_3D = (naxes(3) > 1) .and. (use3D > 0.) |
| 148 |
if (use_3D) then |
| 149 |
npixels = naxes(1)*naxes(2)*naxes(3) |
| 150 |
else |
| 151 |
npixels = naxes(1)*naxes(2) |
| 152 |
endif |
| 153 |
allocate(hh(npixels)) |
| 154 |
!allocate(hh(naxes(1),naxes(2))) |
| 155 |
|
| 156 |
image_max = maxval(image) |
| 157 |
if (imax > 0.) then |
| 158 |
imagemax = imax |
| 159 |
else |
| 160 |
imagemax = image_max |
| 161 |
endif |
| 162 |
! |
| 163 |
! get beamsize from fits header, in units of pixels |
| 164 |
! |
| 165 |
if (beam <= tiny(beam)) then |
| 166 |
bmaj = get_from_header('BMAJ',fitsheader,ierr) |
| 167 |
bmin = get_from_header('BMIN',fitsheader,ierr) |
| 168 |
cdelt1 = get_from_header('CDELT1',fitsheader,ierr) |
| 169 |
if (abs(cdelt1) > 0. .and. ierr == 0) then |
| 170 |
beam = bmaj/abs(cdelt1) !sqrt(bmin*bmaj)/abs(cdelt1) |
| 171 |
print*,' bmin = ',bmin/abs(cdelt1),' bmaj = ',bmaj/abs(cdelt1), ' mean = ',sqrt(bmin*bmaj)/abs(cdelt1) |
| 172 |
endif |
| 173 |
if (beam < 1. .or. beam > 20.) beam = 1. |
| 174 |
endif |
| 175 |
print "(3(a,1pg16.4))",'>> max intensity = ',imagemax,' of ',image_max,', min pix per beam = ',beam |
| 176 |
|
| 177 |
if (use_3D) then |
| 178 |
! |
| 179 |
! denoise all channels simultaneously in 3D |
| 180 |
! |
| 181 |
call image_denoise3D(naxes,image,hh,iterations=its,imax=imagemax,beam=beam) |
| 182 |
else |
| 183 |
! |
| 184 |
! denoise channel by channel in 2D |
| 185 |
! |
| 186 |
k1 = 1 |
| 187 |
k2 = naxes(3) |
| 188 |
if (kstart > 0) k1 = kstart |
| 189 |
if (kend > 0 .and. kend <= naxes(3)) k2 = kend |
| 190 |
do k=k1,k2 |
| 191 |
if (naxes(3) > 1) print "(a,i5,a,i5)",'>> channel ',k, ' of ',naxes(3) |
| 192 |
!print*,'min, max,mean h = ',minval(hh),maxval(hh),sum(hh)/real(npixels) |
| 193 |
|
| 194 |
! for polarised images, denoise individual polarisations |
| 195 |
! then stitch the denoised images back together |
| 196 |
if (nfiles >= 3) then |
| 197 |
write(stdout,'(/,a)') '>> de-noising p...' |
| 198 |
call image_denoise(naxes,image1,hh,iterations=its,imax=imagemax,beam=beam,skip=skip) |
| 199 |
write(stdout,'(/,a)') '>> de-noising q...' |
| 200 |
call image_denoise(naxes,image2,hh,iterations=its,imax=imagemax,beam=beam,skip=skip) |
| 201 |
write(stdout,'(/,a)') '>> recombining to get total intensity...' |
| 202 |
image = sqrt(image1**2 + image2**2) |
| 203 |
elseif (abs(rotate) > tiny(0.)) then |
| 204 |
write(stdout,'(/,a,es10.3,a)') 'rotating by ',rotate,' deg' |
| 205 |
call image_rotate(naxes(1:2),image(:,:,k),rotate,ierr) |
| 206 |
else |
| 207 |
call image_denoise(naxes(1:2),image(:,:,k),hh,iterations=its,imax=imagemax,beam=beam,skip=skip) |
| 208 |
endif |
| 209 |
|
| 210 |
fluxold = sum(image_old(:,:,k)) |
| 211 |
fluxnew = sum(image(:,:,k)) |
| 212 |
print "(a,g16.8,a,g16.8,a,1pg10.2,a)",'>> total intensity =',fluxnew,' was ',fluxold,& |
| 213 |
' err=',100.*(fluxnew-fluxold)/abs(fluxold),' %' |
| 214 |
|
| 215 |
!signal = fluxold/(naxes(1)*naxes(2)) |
| 216 |
!noise = sqrt(sum((image_old(:,:,k)-signal)**2)/(naxes(1)*naxes(2))) |
| 217 |
!print*,' Orig SNR = ',signal/noise, ' signal = ',signal,' noise = ',noise |
| 218 |
!signal = fluxnew/(naxes(1)*naxes(2)) |
| 219 |
!noise = sqrt(sum((image(:,:,k)-signal)**2)/(naxes(1)*naxes(2))) |
| 220 |
!print*,' New SNR = ',signal/noise, ' signal = ',signal,' noise = ',noise |
| 221 |
|
| 222 |
!call write_fits_image('hh.fits',hh,naxes(1:2),ierr) |
| 223 |
|
| 224 |
! if only one channel is selected, also print it out as a separate .fits file |
| 225 |
if (naxes(3) > 1 .and. k2==k1) then |
| 226 |
i = index(fileout,'.fits') |
| 227 |
write(filek,*) k |
| 228 |
filek = fileout(1:i-1)//'-c'//trim(adjustl(filek))//'.fits' |
| 229 |
call write_fits_image(filek,image(:,:,k),naxes(1:2),ierr,hdr=fitsheader) |
| 230 |
if (get_residuals) then |
| 231 |
image_residuals = image - image_old ! to allocate memory |
| 232 |
!image_residuals(:,:,k) = image(:,:,k) - image_old(:,:,k) |
| 233 |
write(filek,*) k |
| 234 |
filek = fileout(1:i-1)//'-c'//trim(adjustl(filek))//'_original.fits' |
| 235 |
call write_fits_image(filek,image_old(:,:,k),naxes(1:2),ierr,hdr=fitsheader) |
| 236 |
write(filek,*) k |
| 237 |
filek = fileout(1:i-1)//'-c'//trim(adjustl(filek))//'_residuals.fits' |
| 238 |
call write_fits_image(filek,image_residuals(:,:,k),naxes(1:2),ierr) |
| 239 |
endif |
| 240 |
endif |
| 241 |
enddo |
| 242 |
endif |
| 243 |
|
| 244 |
call write_fits_cube(fileout,image,naxes,ierr,hdr=fitsheader) |
| 245 |
if (get_residuals) then |
| 246 |
image_residuals = image - image_old |
| 247 |
jext = index(fileout,'.fits') |
| 248 |
fileout = fileout(1:jext-1)//'_residuals.fits' |
| 249 |
call write_fits_cube(fileout,image_residuals,naxes,ierr) |
| 250 |
endif |
| 251 |
if (ierr /= 0) write(stderr,*) 'error writing output file(s)' |
| 252 |
|
| 253 |
deallocate(image,hh) |
| 254 |
if (allocated(image_residuals)) deallocate(image_residuals) |
| 255 |
if (allocated(image1)) deallocate(image1) |
| 256 |
if (allocated(image2)) deallocate(image2) |
| 257 |
|
| 258 |
end program denoise |