Debian Codegraph @

src/write_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 integer :: iunit,ireadwrite,npixels,blocksize
73 integer :: firstpix,nullval,group,nfound!,bitpix
74 logical :: anynull
75 !
76 !--open file and read header information
77 !
78 ierr = 0
79 call ftgiou(iunit,ierr)
80
81 ireadwrite = 0
82 call ftopen(iunit,filename,ireadwrite,blocksize,ierr)
83
84 if (ierr /= 0) then
85 ierr = -1
86 return
87 endif
88 !
89 !--read fits header (this is optional)
90 !
91 if (present(hdr)) call read_fits_head(iunit,hdr,ierr)
92 !
93 !--get the essential things from the header
94 !
95 call ftgknj(iunit,'NAXIS',1,2,naxes,nfound,ierr)
96 npixels = naxes(1)*naxes(2)
97 !if (present(hdr)) bitpix = abs(get_from_header('BITPIX',hdr,ierr))
98 !
99 !--sanity check the header read
100 !
101 if (npixels <= 0) then
102 !print*,' ERROR: No pixels found'
103 ierr = 1
104 return
105 endif
106 !
107 ! read image
108 !
109 firstpix = 1
110 nullval = -999
111 group = 1
112 allocate(image(naxes(1),naxes(2)),stat=ierr)
113 if (ierr /= 0) then
114 ierr = 2
115 return
116 endif
117 ierr = 0
118 call ftgpve(iunit,group,firstpix,npixels,nullval,image,anynull,ierr)
119 call ftclos(iunit,ierr)
120 call ftfiou(iunit,ierr)
121
122 end subroutine read_fits_image
123
124 !---------------------------------------------------
125 ! read FITS header from file
126 !---------------------------------------------------
127 subroutine read_fits_header(filename,hdr,ierr)
128 character, intent(in) :: filename
129 character(len=80), allocatable, intent(out) :: hdr(:)
130 integer, intent(out) :: ierr
131 integer :: ireadwrite,iunit,blocksize
132
133 ierr = 0
134 call ftgiou(iunit,ierr)
135
136 ireadwrite = 0
137 call ftopen(iunit,filename,ireadwrite,blocksize,ierr)
138 if (ierr /= 0) return
139 !
140 !--read fits header (this is optional)
141 !
142 call read_fits_head(iunit,hdr,ierr)
143 call ftclos(iunit,ierr)
144 call ftfiou(iunit,ierr)
145
146 end subroutine read_fits_header
147
148 !---------------------------------------------------
149 ! internal subroutine to read FITS header information
150 !---------------------------------------------------
151 subroutine read_fits_head(iunit,hdr,ierr)
152 integer, intent(in) :: iunit
153 integer, intent(out) :: ierr
154 character(len=80), allocatable, intent(inout) :: hdr(:)
155 character(len=80) :: record
156 integer :: i,nkeys,nspace
157
158 ! The FTGHSP subroutine returns the number of existing keywords in the
159 ! current header data unit (CHDU), not counting the required END keyword,
160 call ftghsp(iunit,nkeys,nspace,ierr)
161 !
162 ! allocate memory
163 !
164 if (allocated(hdr)) deallocate(hdr)
165 allocate(hdr(nkeys))
166
167 ! Read each 80-character keyword record, and print it out.
168 do i = 1, nkeys
169 call ftgrec(iunit,i,record,ierr)
170 hdr(i) = record
171 !print *,hdr(i)
172 end do
173
174 end subroutine read_fits_head
175
176 !---------------------------------------------------
177 ! internal subroutine to write FITS header information
178 ! excluding things we have changed
179 !---------------------------------------------------
180 subroutine write_fits_head(iunit,hdr,ierr)
181 integer, intent(in) :: iunit
182 character(len=80), intent(in) :: hdr(:)
183 integer, intent(out) :: ierr
184 integer :: i,morekeys
185
186 ierr = 0
187 morekeys = 0
188 ! count non-blank header lines
189 do i=1,size(hdr)
190 if (len_trim(hdr(i)) > 0) then
191 morekeys = morekeys + 1
192 endif
193 enddo
194 ! write header size to fits file
195 call fthdef(iunit,morekeys,ierr)
196 if (ierr /= 0) return
197
198 ! write header
199 do i=1,size(hdr)
200 if (len_trim(hdr(i)) > 0) then
201 select case(hdr(i)(1:6))
202 case('SIMPLE','BITPIX','NAXIS ','NAXIS1','NAXIS2','NAXIS3','NAXIS4','EXTEND')
203 ! skip the above keywords
204 case default
205 call ftprec(iunit,hdr(i),ierr)
206 end select
207 endif
208 enddo
209
210 end subroutine write_fits_head
211
212 !---------------------------------------------------
213 ! subroutine to read spectral cube from FITS file
214 ! using cfitsio library
215 !---------------------------------------------------
216 subroutine read_fits_cube(filename,image,naxes,ierr,hdr,hdu,velocity)
217 character(len=*), intent(in) :: filename
218 real(kind=real32), intent(out), allocatable :: image(:,:,:)
219 character(len=80), intent(inout), allocatable, optional :: hdr(:)
220 real(kind=real32), intent(out), allocatable, optional :: velocity(:)
221 integer, intent(out) :: naxes(4),ierr
222 integer, intent(in), optional :: hdu ! specify which hdu to read
223 integer :: iunit,ireadwrite,npixels,blocksize
224 integer :: firstpix,nullval,group,hdutype
225 logical :: anynull
226 integer :: ndim
227 !
228 !--open file and read header information
229 !
230 ierr = 0
231 call ftgiou(iunit,ierr)
232
233 ireadwrite = 0
234 call ftopen(iunit,filename,ireadwrite,blocksize,ierr)
235 if (ierr /= 0) then
236 ierr = -1
237 return
238 endif
239 !
240 ! switch to specified hdu, if argument is given
241 !
242 if (present(hdu)) then
243 print*,' reading hdu ',hdu
244 call ftmahd(iunit,hdu,hdutype,ierr) ! hdutype==0
245 if (ierr /= 0 .or. hdutype /= 0) then
246 ierr = -2
247 return
248 endif
249 endif
250
251 if (present(hdr)) call read_fits_head(iunit,hdr,ierr)
252
253 call ftgidm(iunit,ndim,ierr) ! get_img_dim
254 if (ndim>=3) ndim = 3
255 call ftgisz(iunit,3,naxes(1:ndim),ierr)
256 if (ndim==2) naxes(3) = 1
257
258 if (present(hdr) .and. present(velocity) .and. ndim >= 3) then
259 if (.not.allocated(velocity)) allocate(velocity(naxes(3)))
260 call get_velocity_from_fits_header(naxes(3),velocity,hdr,ierr)
261 endif
262
263 !if (present(hdr)) bitpix = abs(get_from_header('BITPIX',hdr,ierr))
264
265 ! call ftgknj(iunit,'NAXIS',1,2,naxes,nfound,ierr)
266 npixels = product(naxes(1:ndim))
267 !
268 ! sanity check the header read
269 !
270 if (npixels <= 0) then
271 ierr = 1
272 return
273 endif
274 !
275 ! read images
276 !
277 firstpix = 1
278 nullval = -999
279 group = 1
280 allocate(image(naxes(1),naxes(2),naxes(3)),stat=ierr)
281 if (ierr /= 0) then
282 ierr = 2
283 return
284 endif
285 ierr = 0
286 call ftgpve(iunit,group,firstpix,npixels,nullval,image,anynull,ierr)
287 call ftclos(iunit,ierr)
288 call ftfiou(iunit,ierr)
289
290 end subroutine read_fits_cube
291
292 !---------------------------------------------------
293 ! error code handling
294 !---------------------------------------------------
295 character(len=50) function fits_error(ierr)
296 integer, intent(in) :: ierr
297
298 select case(ierr)
299 case(3)
300 fits_error = 'could not match floating point type for fits image'
301 case(2)
302 fits_error = 'could not allocate memory'
303 case(1)
304 fits_error = 'no pixels found'
305 case(-2)
306 fits_error = 'could not open specified hdu in fits file'
307 case(-1)
308 fits_error = 'could not open fits file'
309 case default
310 fits_error = 'unknown error'
311 end select
312
313 end function fits_error
314
315 !------------------------------------------------
316 ! Writing new fits file
317 !------------------------------------------------
318 subroutine write_fits_image(filename,image,naxes,ierr,hdr)
319 character(len=*), intent(in) :: filename
320 integer, intent(in) :: naxes(2)
321 real(kind=real32), intent(in) :: image(naxes(1),naxes(2))
322 integer, intent(out) :: ierr
323 character(len=80), intent(in), optional :: hdr(:)
324 integer :: iunit,blocksize,group,firstpixel,bitpix,npixels
325 logical :: simple,extend
326
327 ! Get an unused Logical Unit Number to use to open the FITS file.
328 ierr = 0
329 call ftgiou(iunit,ierr)
330
331 ! Create the new empty FITS file.
332 blocksize=1
333 print "(a)",' writing '//trim(filename)
334 call ftinit(iunit,filename,blocksize,ierr)
335
336 ! Initialize parameters about the FITS image
337 simple=.true.
338 ! data size
339 bitpix=-32
340 extend=.true.
341
342 ! Write the required header keywords.
343 call ftphpr(iunit,simple,bitpix,2,naxes,0,1,extend,ierr)
344 ! Write additional header keywords, if present
345 if (present(hdr)) call write_fits_head(iunit,hdr,ierr)
346
347 group=1
348 firstpixel=1
349 npixels = naxes(1)*naxes(2)
350 ! write as real*4
351 call ftppre(iunit,group,firstpixel,npixels,image,ierr)
352
353 ! Close the file and free the unit number
354 call ftclos(iunit, ierr)
355 call ftfiou(iunit, ierr)
356
357 end subroutine write_fits_image
358
359 !-------------------------------------------------------------
360 ! Writing new fits file (convert from double precision input)
361 !-------------------------------------------------------------
362 subroutine write_fits_image64(filename,image,naxes,ierr,hdr)
363 character(len=*), intent(in) :: filename
364 integer, intent(in) :: naxes(2)
365 real(kind=real64),intent(in) :: image(naxes(1),naxes(2))
366 real(kind=real32), allocatable :: img32(:,:)
367 integer, intent(out) :: ierr
368 character(len=80), intent(in), optional :: hdr(:)
369
370 img32 = real(image,kind=real32) ! copy and allocate
371 if (present(hdr)) then
372 call write_fits_image(filename,img32,naxes,ierr,hdr)
373 else
374 call write_fits_image(filename,img32,naxes,ierr)
375 endif
376 deallocate(img32,stat=ierr)
377
378 end subroutine write_fits_image64
379
380 !------------------------------------------------
381 ! Writing new fits file
382 !------------------------------------------------
383 subroutine write_fits_cube(filename,image,naxes,ierr,hdr)
384 character(len=*), intent(in) :: filename
385 integer, intent(in) :: naxes(3)
386 real(kind=real32), intent(in) :: image(naxes(1),naxes(2),naxes(3))
387 integer, intent(out) :: ierr
388 character(len=80), intent(in), optional :: hdr(:)
389 integer :: iunit,blocksize,group,firstpixel,bitpix,npixels
390 logical :: simple,extend
391
392 ! Get an unused Logical Unit Number to use to open the FITS file.
393 ierr = 0
394 call ftgiou(iunit,ierr)
395
396 ! Create the new empty FITS file.
397 blocksize=1
398 call ftinit(iunit,filename,blocksize,ierr)
399 if (ierr /= 0) then
400 print "(a)",' ERROR: '//trim(filename)//' already exists or is not writeable'
401 return
402 else
403 print "(a)",' writing '//trim(filename)
404 endif
405
406 ! Initialize parameters about the FITS image
407 simple=.true.
408 ! data size
409 bitpix=-32
410 extend=.true.
411
412 ! Write the required header keywords.
413 call ftphpr(iunit,simple,bitpix,3,naxes,0,1,extend,ierr)
414 ! Write additional header keywords, if present
415 if (present(hdr)) call write_fits_head(iunit,hdr,ierr)
416
417 group=1
418 firstpixel=1
419 npixels = product(naxes)
420 ! write as real*4
421 call ftppre(iunit,group,firstpixel,npixels,image,ierr)
422
423 ! Close the file and free the unit number
424 call ftclos(iunit, ierr)
425 call ftfiou(iunit, ierr)
426
427 end subroutine write_fits_cube
428
429
430 !------------------------------------------------
431 ! Writing new fits file
432 !------------------------------------------------
433 subroutine append_to_fits_cube(filename,image,naxes,ierr,hdr)
434 character(len=*), intent(in) :: filename
435 integer, intent(in) :: naxes(3)
436 real(kind=real32), intent(in) :: image(naxes(1),naxes(2),naxes(3))
437 integer, intent(out) :: ierr
438 character(len=80), intent(in), optional :: hdr(:)
439 integer :: iunit,blocksize,group,firstpixel,bitpix,npixels,ireadwrite
440 logical :: simple,extend
441
442 ! Get an unused Logical Unit Number to use to open the FITS file.
443 ierr = 0
444 call ftgiou(iunit,ierr)
445
446 ! Open the FITS file for read/write
447 blocksize=1
448 ireadwrite=1
449 call ftopen(iunit,filename,ireadwrite,blocksize,ierr)
450 if (ierr /= 0) then
451 print "(a)",' ERROR: '//trim(filename)//' does not exist or is not read/writeable'
452 return
453 else
454 print "(a)",' appending to '//trim(filename)
455 endif
456
457 !--create new hdu in the fits file
458 call ftcrhd(iunit,ierr)
459 if (ierr /= 0) then
460 print "(a)",'ERROR creating new hdu in '//trim(filename)
461 return
462 endif
463
464 ! Initialize parameters about the FITS image
465 simple=.true.
466 ! data size
467 bitpix=-32
468 extend=.true.
469
470 ! Write the required header keywords.
471 call ftphpr(iunit,simple,bitpix,3,naxes,0,1,extend,ierr)
472 ! Write additional header keywords, if present
473 if (present(hdr)) call write_fits_head(iunit,hdr,ierr)
474
475 group=1
476 firstpixel=1
477 npixels = product(naxes)
478 ! write as real*4
479 call ftppre(iunit,group,firstpixel,npixels,image,ierr)
480
481 ! Close the file and free the unit number
482 call ftclos(iunit, ierr)
483 call ftfiou(iunit, ierr)
484
485 end subroutine append_to_fits_cube
486
487 !-------------------------------------------------------------
488 ! Writing new fits file (convert from double precision input)
489 !-------------------------------------------------------------
490 subroutine write_fits_cube64(filename,image,naxes,ierr,hdr)
491 character(len=*), intent(in) :: filename
492 integer, intent(in) :: naxes(3)
493 real(kind=real64),intent(in) :: image(naxes(1),naxes(2),naxes(3))
494 character(len=80), intent(in), optional :: hdr(:)
495 integer, intent(out) :: ierr
496 real(kind=real32), allocatable :: img32(:,:,:)
497
498 img32 = real(image,kind=real32) ! copy and allocate
499 if (present(hdr)) then
500 call write_fits_cube(filename,img32,naxes,ierr,hdr)
501 else
502 call write_fits_cube(filename,img32,naxes,ierr)
503 endif
504 deallocate(img32,stat=ierr)
505
506 end subroutine write_fits_cube64
507
508 !-------------------------------------------------------------
509 ! read fits file and convert to double precision
510 !-------------------------------------------------------------
511 subroutine read_fits_image64(filename,image,naxes,ierr,hdr)
512 character(len=*), intent(in) :: filename
513 real(kind=real64), intent(out), allocatable :: image(:,:)
514 character(len=80), intent(inout), allocatable, optional :: hdr(:)
515 integer, intent(out) :: naxes(2),ierr
516 real(kind=real32), allocatable :: img32(:,:)
517
518 if (present(hdr)) then
519 call read_fits_image(filename,img32,naxes,ierr,hdr)
520 else
521 call read_fits_image(filename,img32,naxes,ierr)
522 endif
523 image = img32 ! allocate and copy, converting real type
524 deallocate(img32,stat=ierr)
525
526 end subroutine read_fits_image64
527
528 !-------------------------------------------------------------
529 ! read fits cube and convert to double precision
530 !-------------------------------------------------------------
531 subroutine read_fits_cube64(filename,image,naxes,ierr,hdr,velocity)
532 character(len=*), intent(in) :: filename
533 real(kind=real64), intent(out), allocatable :: image(:,:,:)
534 character(len=80), intent(inout), allocatable, optional :: hdr(:)
535 real(kind=real64), intent(inout), allocatable, optional :: velocity(:)
536 integer, intent(out) :: naxes(4),ierr
537 real(kind=real32), allocatable :: img32(:,:,:),velocity32(:)
538
539 if (present(hdr)) then
540 if (present(velocity)) then
541 call read_fits_cube(filename,img32,naxes,ierr,hdr=hdr,velocity=velocity32)
542 velocity = velocity32 ! allocate and copy, converting real type
543 else
544 call read_fits_cube(filename,img32,naxes,ierr,hdr)
545 endif
546 else
547 call read_fits_cube(filename,img32,naxes,ierr)
548 endif
549 image = img32 ! allocate and copy, converting real type
550 deallocate(img32,stat=ierr)
551 if (allocated(velocity32)) deallocate(velocity32,stat=ierr)
552
553 end subroutine read_fits_cube64
554
555 !--------------------------------------------------
556 ! read all floating point variables from fits header
557 !--------------------------------------------------
558 subroutine get_floats_from_fits_header(hdr,tags,vals)
559 character(len=80), intent(in) :: hdr(:)
560 character(len=*), intent(out) :: tags(:)
561 real, intent(out) :: vals(:)
562 integer :: i, n, ierr
563
564 n = 0
565 do i=1,size(hdr)
566 n = n + 1
567 if (n <= size(tags) .and. n <= size(vals)) then
568 call get_fits_header_entry(hdr(i),tags(n),vals(n),ierr)
569 endif
570 if (ierr /= 0) n = n - 1
571 enddo
572
573 end subroutine get_floats_from_fits_header
574
575 !------------------------------------------------
576 ! get tag:val pairs from fits header record
577 ! will extract anything readable as a floating
578 ! point number
579 !------------------------------------------------
580 subroutine get_fits_header_entry(record,key,rval,ierr)
581 character(len=80), intent(in) :: record
582 character(len=*), intent(out) :: key
583 real, intent(out) :: rval
584 integer, intent(out) :: ierr
585 character(len=80) :: val
586
587 call get_fits_header_key_val(record,key,val,ierr)
588 if (ierr == 0) then
589 read(val,*,iostat=ierr) rval
590 endif
591
592 end subroutine get_fits_header_entry
593
594 !------------------------------------------------
595 ! get tag:val pairs from fits header record
596 ! returns the string value
597 !------------------------------------------------
598 subroutine get_fits_header_key_val(record,key,val,ierr)
599 character(len=80), intent(in) :: record
600 character(len=*), intent(out) :: key
601 character(len=*), intent(out) :: val
602 integer, intent(out) :: ierr
603 integer :: ieq
604
605 key = ''
606 val = ''
607 ierr = -1
608 ! split on equals sign
609 ieq = index(record,'=')
610 if (ieq > 0) then
611 key = record(1:ieq-1)
612 val = record(ieq+1:)
613 ierr = 0
614 endif
615
616 end subroutine get_fits_header_key_val
617
618 !------------------------------------------------
619 ! search fits header to find a particular variable
620 ! e.g. bmaj = get_from_header('BMAJ',hdr,ierr)
621 !------------------------------------------------
622 function get_from_header(key,hdr,ierr) result(val)
623 character(len=*), intent(in) :: key
624 character(len=80), intent(in) :: hdr(:)
625 integer, intent(out) :: ierr
626 character(len=len(key)) :: mykey
627 real :: val,myval
628 integer :: i
629
630 val = 0.
631 ierr = -1
632 do i=1,size(hdr)
633 call get_fits_header_entry(hdr(i),mykey,myval,ierr)
634 if (trim(adjustl(mykey))==trim(key) .and. ierr==0) then
635 val = myval
636 ierr = 0
637 return
638 endif
639 enddo
640
641 end function get_from_header
642
643 !------------------------------------------------
644 ! search fits header to find a particular string
645 ! e.g. bmaj = get_from_header('BMAJ',hdr,ierr)
646 !------------------------------------------------
647 function get_from_header_s(key,hdr,ierr) result(val)
648 character(len=*), intent(in) :: key
649 character(len=80), intent(in) :: hdr(:)
650 integer, intent(out) :: ierr
651 character(len=len(key)) :: mykey
652 character(len=80) :: val,myval
653 integer :: i,i1,i2,islash
654
655 val = ''
656 ierr = -1
657 do i=1,size(hdr)
658 call get_fits_header_key_val(hdr(i),mykey,myval,ierr)
659 if (trim(adjustl(mykey))==trim(key) .and. ierr==0) then
660 i1 = index(myval,"'")
661 islash = index(myval,"/")
662 if (islash == 0) islash = len_trim(myval)
663 i2 = index(myval(1:islash),"'",back=.true.)
664 if (i1 > 0) then
665 val = myval(i1+1:i2-1) ! trim quotation marks
666 else
667 val = myval ! not a string variable, return whole string
668 endif
669 ierr = 0
670 return
671 endif
672 enddo
673
674 end function get_from_header_s
675
676 !------------------------------------------------
677 ! delete third dimension in the fits header
678 !------------------------------------------------
679 subroutine flatten_header(hdr)
680 character(len=80), intent(inout) :: hdr(:)
681 character(len=80) :: mykey,myval
682 integer :: i,ierr
683
684 do i=1,size(hdr)
685 call get_fits_header_key_val(hdr(i),mykey,myval,ierr)
686 ! delete anything in the header that ends in '3'
687 if (len_trim(mykey) > 0) then
688 if (mykey(len_trim(mykey):len_trim(mykey))=='3') then
689 !print*,' deleting ',hdr(i)
690 hdr(i) = ''
691 endif
692 endif
693 enddo
694
695 end subroutine flatten_header
696
697 !------------------------------------------------
698 ! get velocity grid from the fits file
699 !------------------------------------------------
700 subroutine get_velocity_from_fits_header(nv,vel,hdr,ierr)
701 integer, intent(in) :: nv
702 real(kind=real32), intent(out) :: vel(nv)
703 character(len=80), intent(in) :: hdr(:)
704 integer, intent(out) :: ierr
705 integer :: k
706 real :: dv,restfreq,crpix,crval,nu
707 character(len=80) :: velocity_type
708 real, parameter :: c = 2.997924e5
709
710 dv = get_from_header('CDELT3',hdr,ierr)
711 restfreq = get_from_header('RESTFRQ',hdr,ierr)
712 velocity_type = get_from_header_s('CTYPE3',hdr,ierr)
713 select case (trim(velocity_type))
714 case("VELO-LSR","VRAD")
715 if (dv > 10.) then
716 dv = dv*1e-3 ! assume m/s
717 endif
718 crpix = get_from_header('CRPIX3',hdr,ierr)
719 crval = get_from_header('CRVAL3',hdr,ierr)
720 do k=1,nv
721 vel(k) = real(crval + dv * (k - crpix),kind=real32)
722 enddo
723 case("FREQ")
724 crpix = get_from_header('CRPIX3',hdr,ierr)
725 crval = get_from_header('CRVAL3',hdr,ierr)
726 do k=1,nv
727 nu = crval + dv*(k - crpix)
728 vel(k) = real(-(nu - restfreq)/restfreq * c,kind=real32) ! c is in km/s
729 enddo
730 case default
731 write(*,"(1x,a)") 'warning: velocity type '//trim(velocity_type)//' not recognised in fits header'
732 dv = 1.
733 do k=1,nv
734 vel(k) = real(dv * (k - 0.5),kind=real32)
735 enddo
736 end select
737
738 end subroutine get_velocity_from_fits_header
739
740 end module readwrite_fits