Debian Codegraph @

src/read_data_fits.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) 2019- Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !-------------------------------------------------------------------------
24 ! this subroutine reads from the data file(s)
25 ! change this to change the format of data input
26 !
27 ! THIS VERSION IS FOR READING FITS FILES
28 !
29 ! the data is stored in the global array dat
30 !
31 ! >> this subroutine must return values for the following: <<
32 !
33 ! ncolumns : number of data columns
34 ! ndim, ndimV : number of spatial, velocity dimensions
35 ! nstepsread : number of steps read from this file
36 !
37 ! dat(maxpart,maxplot,maxstep) : main data array
38 !
39 ! npartoftype(maxstep): number of particles of each type in each timestep
40 !
41 ! time(maxstep) : time at each step
42 ! gamma(maxstep) : gamma at each step
43 ! (used in calc_quantities for calculating the pressure)
44 !
45 ! most of these values are stored in global arrays
46 ! in the module 'particle_data'
47 !
48 ! Columns with the 'required' flag set to false are not read
49 !-------------------------------------------------------------------------
50
51 !-------------------------------------------------------------------------
52 !
53 ! The routine that reads the data into splash's internal arrays
54 !
55 !-------------------------------------------------------------------------
56
57 module readdata_fits
58 implicit none
59
60 public :: read_data_fits, set_labels_fits
61
62 private
63 contains
64
65 subroutine read_data_fits(rootname,istepstart,ipos,nstepsread)
66 use particle_data, only:dat,npartoftype,masstype,maxcol,maxpart,headervals
67 use settings_data, only:ndim,ndimV,ncolumns,ncalc,ipartialread,iverbose,debugmode
68 use mem_allocation, only:alloc
69 use readwrite_fits, only:read_fits_cube,fits_error,write_fits_image,&
70 get_floats_from_fits_header,get_velocity_from_fits_header
71 use imageutils, only:image_denoise
72 use labels, only:headertags
73 use system_utils, only:get_command_option
74 use asciiutils, only:get_value
75 integer, intent(in) :: istepstart,ipos
76 integer, intent(out) :: nstepsread
77 character(len=*), intent(in) :: rootname
78 character(len=len(rootname)+10) :: datfile
79 integer :: i,j,k,l,n,ierr,nextra,naxes(4)
80 integer :: ncolstep,npixels,nsteps_to_read,ihdu
81 logical :: iexist,reallocate
82 real(kind=4), dimension(:,:,:), allocatable :: image
83 real(kind=4), dimension(:), allocatable :: vels
84 character(len=80), allocatable :: fitsheader(:)
85 real :: dx,dy,dz,j0,k0,pixelscale
86 logical :: centre_image
87
88 nstepsread = 0
89 nsteps_to_read = 1
90
91 if (len_trim(rootname) > 0) then
92 datfile = trim(rootname)
93 else
94 print*,' **** no data read **** '
95 return
96 endif
97 !
98 !--check if first data file exists
99 !
100 if (iverbose==1 .and. ipos==1) print "(1x,a)",'reading FITS format'
101 inquire(file=datfile,exist=iexist)
102 if (.not.iexist) then
103 !
104 !--append .fits on the endif not already present
105 !
106 datfile=trim(rootname)//'.fits'
107 inquire(file=datfile,exist=iexist)
108 endif
109 if (.not.iexist) then
110 print "(a)",' *** error: '//trim(rootname)//': file not found ***'
111 return
112 endif
113 !
114 !--set parameters which do not vary between timesteps
115 !
116 ndim = 2
117 ndimV = 2
118 nextra = 0
119 ihdu = nint(get_command_option('hdu'))
120 !
121 !--read data from snapshots
122 !
123 i = istepstart
124 write(*,"(23('-'),1x,a,1x,23('-'))") trim(datfile)
125 !
126 !--open file and read header information
127 !
128 if (ihdu > 0) then
129 call read_fits_cube(datfile,image,naxes,ierr,hdr=fitsheader,hdu=ihdu)
130 else
131 call read_fits_cube(datfile,image,naxes,ierr,hdr=fitsheader)
132 endif
133 if (ierr /= 0) then
134 print*,'ERROR: '//trim(fits_error(ierr))
135 if (allocated(image)) deallocate(image)
136 return
137 endif
138 if (naxes(3) > 1) then
139 ndim = 3
140 ndimV = 3
141 endif
142 npixels = product(naxes(1:ndim))
143 if (npixels <= 0) then
144 print "(a)",' ERROR: got npixels = 0 from fits header (extension not supported?)'
145 return
146 endif
147 if (ndim==3) then
148 allocate(vels(naxes(3)))
149 call get_velocity_from_fits_header(naxes(3),vels,fitsheader,ierr)
150 endif
151 ncolstep = ndim + 3 ! x, y, h, I, m
152 if (iverbose >= 1) then
153 if (ndim==3) then
154 print "(' image size: ',i5,' x ',i5,' x ',i5)",naxes(1:ndim)
155 else
156 print "(' image size: ',i5,' x ',i5)",naxes(1:2)
157 endif
158 endif
159 !
160 !--allocate or reallocate memory for main data array
161 !
162 ncolumns = ncolstep + nextra
163 nstepsread = 1
164 reallocate = (npixels > maxpart)
165 if (reallocate .or. .not.(allocated(dat))) then
166 call alloc(npixels,nsteps_to_read,max(ncolumns+ncalc,maxcol),mixedtypes=.false.)
167 endif
168 !
169 !--now memory has been allocated, set information that splash needs
170 !
171 call get_floats_from_fits_header(fitsheader,headertags,headervals(:,i))
172
173 ipartialread = .false.
174 masstype(1,i) = 0.0
175 npartoftype(1,i) = npixels
176
177 pixelscale = get_value('CDELT2',headertags,headervals(:,i),default=1.)
178 if (abs(pixelscale - 1.) > tiny(1.)) pixelscale = pixelscale * 3600. ! arcsec
179
180 !
181 ! set x,y and other things needed for splash
182 !
183 centre_image = .true.
184 j0 = 0.5*naxes(1)
185 k0 = 0.5*naxes(2)
186 dx = 1.*pixelscale
187 dy = 1.*pixelscale
188 dz = 1.*pixelscale
189 if (debugmode) print*,'DEBUG: mapping...'
190 !$omp parallel do private(j,k,l,n) &
191 !$omp shared(dat,naxes,centre_image,dx,dy,dz,j0,k0,image,i,pixelscale,ndim)
192 do l=1,naxes(3)
193 do k=1,naxes(2)
194 do j=1,naxes(1)
195 n = (l-1)*naxes(1)*naxes(2) + (k-1)*naxes(1) + j
196 if (centre_image) then
197 dat(n,1,i) = (j - j0)*dx
198 dat(n,2,i) = (k - k0)*dy
199 else
200 dat(n,1,i) = j*dx
201 dat(n,2,i) = k*dy
202 endif
203 if (ndim >= 3) dat(n,3,i) = l*dz
204 dat(n,ndim+1,i) = 1.*pixelscale ! smoothing length == pixel scale
205 dat(n,ndim+2,i) = image(j,k,l)
206 dat(n,ndim+3,i) = image(j,k,l)*dx*dy*dz ! flux==equivalent of "mass"
207 enddo
208 enddo
209 enddo
210 !$omp end parallel do
211 if (debugmode) print*,'DEBUG: done'
212 !
213 ! clean up
214 !
215 deallocate(image) !,old_image)
216
217 end subroutine read_data_fits
218
219 !------------------------------------------------------------
220 ! set labels for each column of data
221 !------------------------------------------------------------
222 subroutine set_labels_fits
223 use labels, only:label,labeltype,ix,ipmass,ih,irho
224 use settings_data, only:ndim,ndimV,ntypes,UseTypeInRenderings,iautorender
225 use geometry, only:labelcoord
226
227 if (ndim <= 0 .or. ndim > 3) then
228 print*,'*** ERROR: ndim = ',ndim,' in set_labels_fits ***'
229 return
230 endif
231 if (ndimV <= 0 .or. ndimV > 3) then
232 print*,'*** ERROR: ndimV = ',ndimV,' in set_labels_fits ***'
233 return
234 endif
235
236 ix(1) = 1
237 ix(2) = 2
238 if (ndim >= 3) ix(3) = 3
239 ih = ndim+1
240 irho = ndim+2
241 ipmass = ndim+3
242
243 ! set labels of the quantities read in
244 if (ix(1) > 0) label(ix(1:ndim)) = labelcoord(1:ndim,1)
245 if (irho > 0) label(irho) = 'intensity'
246 if (ipmass > 0) label(ipmass) = 'flux'
247 if (ih > 0) label(ih) = 'sigma'
248
249 if (ndim >= 2) iautorender = irho ! automatically select this column for first plot
250
251 ! set labels for each particle type
252 ntypes = 1
253 labeltype(1) = 'pixel'
254 UseTypeInRenderings(:) = .true.
255
256 end subroutine set_labels_fits
257 end module readdata_fits