Debian Codegraph @

src/no_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) 2020- Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22 !----------------------------------------------------------------------
23 !
24 ! Module handling read and write of FITS files
25 ! With thanks to Christophe Pinte
26 !
27 !----------------------------------------------------------------------
28 module readwrite_fits
29 use iso_fortran_env, only:real32,real64
30 implicit none
31 public :: read_fits_image,write_fits_image,fits_error
32 public :: read_fits_cube,write_fits_cube
33 public :: read_fits_header
34 public :: get_floats_from_fits_header,get_from_header,get_from_header_s
35 public :: append_to_fits_cube
36 public :: get_velocity_from_fits_header
37 public :: flatten_header
38
39 interface write_fits_image
40 module procedure write_fits_image,write_fits_image64
41 end interface write_fits_image
42
43 interface write_fits_cube
44 module procedure write_fits_cube,write_fits_cube64
45 end interface write_fits_cube
46
47 interface read_fits_image
48 module procedure read_fits_image,read_fits_image64
49 end interface read_fits_image
50
51 interface read_fits_cube
52 module procedure read_fits_cube,read_fits_cube64
53 end interface read_fits_cube
54
55 interface append_to_fits_cube
56 module procedure append_to_fits_cube
57 end interface append_to_fits_cube
58
59 private
60
61 contains
62
63 !---------------------------------------------------
64 ! subroutine to read image from FITS file
65 ! using cfitsio library
66 !---------------------------------------------------
67 subroutine read_fits_image(filename,image,naxes,ierr,hdr)
68 character(len=*), intent(in) :: filename
69 real(kind=real32), intent(out), allocatable :: image(:,:)
70 character(len=80), intent(inout), allocatable, optional :: hdr(:)
71 integer, intent(out) :: naxes(2),ierr
72
73 ierr = 1
74 naxes = 0
75 end subroutine read_fits_image
76
77 !---------------------------------------------------
78 ! read FITS header from file
79 !---------------------------------------------------
80 subroutine read_fits_header(filename,hdr,ierr)
81 character, intent(in) :: filename
82 character(len=80), allocatable, intent(out) :: hdr(:)
83 integer, intent(out) :: ierr
84
85 ierr = 1
86
87 end subroutine read_fits_header
88
89 !---------------------------------------------------
90 ! internal subroutine to read FITS header information
91 !---------------------------------------------------
92 subroutine read_fits_head(iunit,hdr,ierr)
93 integer, intent(in) :: iunit
94 integer, intent(out) :: ierr
95 character(len=80), allocatable, intent(inout) :: hdr(:)
96
97 ierr = 1
98
99 end subroutine read_fits_head
100
101 !---------------------------------------------------
102 ! internal subroutine to write FITS header information
103 ! excluding things we have changed
104 !---------------------------------------------------
105 subroutine write_fits_head(iunit,hdr,ierr)
106 integer, intent(in) :: iunit
107 character(len=80), intent(in) :: hdr(:)
108 integer, intent(out) :: ierr
109
110 ierr = 1
111
112 end subroutine write_fits_head
113
114 !---------------------------------------------------
115 ! subroutine to read spectral cube from FITS file
116 ! using cfitsio library
117 !---------------------------------------------------
118 subroutine read_fits_cube(filename,image,naxes,ierr,hdr,hdu,velocity)
119 character(len=*), intent(in) :: filename
120 real(kind=real32), intent(out), allocatable :: image(:,:,:)
121 character(len=80), intent(inout), allocatable, optional :: hdr(:)
122 real(kind=real32), intent(out), allocatable, optional :: velocity(:)
123 integer, intent(out) :: naxes(4),ierr
124 integer, intent(in), optional :: hdu ! specify which hdu to read
125
126 ierr = 1
127 naxes = 0
128
129 end subroutine read_fits_cube
130
131 !---------------------------------------------------
132 ! error code handling
133 !---------------------------------------------------
134 character(len=50) function fits_error(ierr)
135 integer, intent(in) :: ierr
136
137 select case(ierr)
138 case(1)
139 fits_error = 'not compiled with fits lib'
140 case default
141 fits_error = 'unknown error'
142 end select
143
144 end function fits_error
145
146 !------------------------------------------------
147 ! Writing new fits file
148 !------------------------------------------------
149 subroutine write_fits_image(filename,image,naxes,ierr,hdr)
150 character(len=*), intent(in) :: filename
151 integer, intent(in) :: naxes(2)
152 real(kind=real32), intent(in) :: image(naxes(1),naxes(2))
153 integer, intent(out) :: ierr
154 character(len=80), intent(in), optional :: hdr(:)
155
156 ierr = 1
157
158 end subroutine write_fits_image
159
160 !-------------------------------------------------------------
161 ! Writing new fits file (convert from double precision input)
162 !-------------------------------------------------------------
163 subroutine write_fits_image64(filename,image,naxes,ierr,hdr)
164 character(len=*), intent(in) :: filename
165 integer, intent(in) :: naxes(2)
166 real(kind=real64),intent(in) :: image(naxes(1),naxes(2))
167 real(kind=real32), allocatable :: img32(:,:)
168 integer, intent(out) :: ierr
169 character(len=80), intent(in), optional :: hdr(:)
170
171 ierr = 1
172
173 end subroutine write_fits_image64
174
175 !------------------------------------------------
176 ! Writing new fits file
177 !------------------------------------------------
178 subroutine write_fits_cube(filename,image,naxes,ierr,hdr)
179 character(len=*), intent(in) :: filename
180 integer, intent(in) :: naxes(3)
181 real(kind=real32), intent(in) :: image(naxes(1),naxes(2),naxes(3))
182 integer, intent(out) :: ierr
183 character(len=80), intent(in), optional :: hdr(:)
184
185 ierr = 1
186
187 end subroutine write_fits_cube
188
189
190 !------------------------------------------------
191 ! Writing new fits file
192 !------------------------------------------------
193 subroutine append_to_fits_cube(filename,image,naxes,ierr,hdr)
194 character(len=*), intent(in) :: filename
195 integer, intent(in) :: naxes(3)
196 real(kind=real32), intent(in) :: image(naxes(1),naxes(2),naxes(3))
197 integer, intent(out) :: ierr
198 character(len=80), intent(in), optional :: hdr(:)
199
200 ierr = 1
201
202 end subroutine append_to_fits_cube
203
204 !-------------------------------------------------------------
205 ! Writing new fits file (convert from double precision input)
206 !-------------------------------------------------------------
207 subroutine write_fits_cube64(filename,image,naxes,ierr,hdr)
208 character(len=*), intent(in) :: filename
209 integer, intent(in) :: naxes(3)
210 real(kind=real64),intent(in) :: image(naxes(1),naxes(2),naxes(3))
211 character(len=80), intent(in), optional :: hdr(:)
212 integer, intent(out) :: ierr
213
214 ierr = 1
215
216 end subroutine write_fits_cube64
217
218 !-------------------------------------------------------------
219 ! read fits file and convert to double precision
220 !-------------------------------------------------------------
221 subroutine read_fits_image64(filename,image,naxes,ierr,hdr)
222 character(len=*), intent(in) :: filename
223 real(kind=real64), intent(out), allocatable :: image(:,:)
224 character(len=80), intent(inout), allocatable, optional :: hdr(:)
225 integer, intent(out) :: naxes(2),ierr
226
227 ierr = 1
228 naxes = 0
229
230 end subroutine read_fits_image64
231
232 !-------------------------------------------------------------
233 ! read fits cube and convert to double precision
234 !-------------------------------------------------------------
235 subroutine read_fits_cube64(filename,image,naxes,ierr,hdr,velocity)
236 character(len=*), intent(in) :: filename
237 real(kind=real64), intent(out), allocatable :: image(:,:,:)
238 character(len=80), intent(inout), allocatable, optional :: hdr(:)
239 real(kind=real64), intent(inout), allocatable, optional :: velocity(:)
240 integer, intent(out) :: naxes(4),ierr
241
242 ierr = 1
243 naxes = 0
244
245 end subroutine read_fits_cube64
246
247 !--------------------------------------------------
248 ! read all floating point variables from fits header
249 !--------------------------------------------------
250 subroutine get_floats_from_fits_header(hdr,tags,vals)
251 character(len=80), intent(in) :: hdr(:)
252 character(len=*), intent(out) :: tags(:)
253 real, intent(out) :: vals(:)
254
255 tags = ''
256 vals = 0.
257
258 end subroutine get_floats_from_fits_header
259
260 !------------------------------------------------
261 ! get tag:val pairs from fits header record
262 ! will extract anything readable as a floating
263 ! point number
264 !------------------------------------------------
265 subroutine get_fits_header_entry(record,key,rval,ierr)
266 character(len=80), intent(in) :: record
267 character(len=*), intent(out) :: key
268 real, intent(out) :: rval
269 integer, intent(out) :: ierr
270
271 ierr = 1
272 rval = 0.
273
274 end subroutine get_fits_header_entry
275
276 !------------------------------------------------
277 ! get tag:val pairs from fits header record
278 ! returns the string value
279 !------------------------------------------------
280 subroutine get_fits_header_key_val(record,key,val,ierr)
281 character(len=80), intent(in) :: record
282 character(len=*), intent(out) :: key
283 character(len=*), intent(out) :: val
284 integer, intent(out) :: ierr
285
286 ierr = 1
287 key = ''
288 val = ''
289
290 end subroutine get_fits_header_key_val
291
292 !------------------------------------------------
293 ! search fits header to find a particular variable
294 ! e.g. bmaj = get_from_header('BMAJ',hdr,ierr)
295 !------------------------------------------------
296 function get_from_header(key,hdr,ierr) result(val)
297 character(len=*), intent(in) :: key
298 character(len=80), intent(in) :: hdr(:)
299 integer, intent(out) :: ierr
300 real :: val
301
302 ierr = 1
303 val = 0.
304
305 end function get_from_header
306
307 !------------------------------------------------
308 ! search fits header to find a particular string
309 ! e.g. bmaj = get_from_header('BMAJ',hdr,ierr)
310 !------------------------------------------------
311 function get_from_header_s(key,hdr,ierr) result(val)
312 character(len=*), intent(in) :: key
313 character(len=80), intent(in) :: hdr(:)
314 integer, intent(out) :: ierr
315 character(len=1) :: val
316
317 ierr = 1
318 val = ''
319
320 end function get_from_header_s
321
322 !------------------------------------------------
323 ! delete third dimension in the fits header
324 !------------------------------------------------
325 subroutine flatten_header(hdr)
326 character(len=80), intent(inout) :: hdr(:)
327
328 end subroutine flatten_header
329
330 !------------------------------------------------
331 ! get velocity grid from the fits file
332 !------------------------------------------------
333 subroutine get_velocity_from_fits_header(nv,vel,hdr,ierr)
334 integer, intent(in) :: nv
335 real(kind=real32), intent(out) :: vel(nv)
336 character(len=80), intent(in) :: hdr(:)
337 integer, intent(out) :: ierr
338
339 ierr = 1
340 vel = 0.
341
342 end subroutine get_velocity_from_fits_header
343
344 end module readwrite_fits