!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module image_def
  use gildas_def
  !
  integer, parameter :: code_read_data     =-1
  integer, parameter :: code_read_header   =-2
  integer, parameter :: code_update_header =-3
  integer, parameter :: code_write_header  =-4
  integer, parameter :: code_write_data    =-5
  integer, parameter :: code_read_image    =-6
  integer, parameter :: code_free_image    =-7
  integer, parameter :: code_create_image  =-8
  !
  ! /XIPAR/
  type :: location
    sequence
    integer*4 :: al64                     ! Padding to align ADDR
    integer*4 :: size                     ! Size of image
    integer(kind=address_length) :: addr  ! Address of image
    integer :: islo                       ! Image Slot number
    integer :: mslo                       ! Memory Slot number
    logical :: read                       ! ReadOnly status
    logical :: getvm                      ! Memory / File indicator
  end type location
  ! /XCPAR/
  type :: strings
    sequence
    character(len=12) :: type             !     Image Type (see ITYP)
    character(len=12) :: unit             !     Data Units (see IUNI)
    character(len=12) :: code(4)          ! 59  Axis codes (see ICOD)
    character(len=12) :: syst             ! 71  Coordinate system (see ISYS)
    character(len=12) :: name             ! 75  Source name (see ISOU)
    character(len=12) :: line             !     Line name (see ILIN)
  end type strings
  ! /XPAR/
  type :: gildas_header
    sequence
    integer*4 :: ityp(3)                  !  1
    integer*4 :: form                     !  4  Data format (FMT_xx)
    integer*4 :: nvb                      !  5  Number of blocks
    integer*4 :: fill(5)                  !  6  Reserved space
    integer*4 :: gene                     ! 11  General section length
    integer*4 :: ndim                     ! 12  Number of dimensions
    integer*4 :: dim(4)                   ! 13  Dimensions
    real*8 :: ref1                        ! 17  Reference pixel
    real*8 :: val1                        ! 19  Value at reference pixel
    real*8 :: inc1                        ! 21  Increment per pixel
    real*8 :: ref2                        ! 23  idem for axis 2
    real*8 :: val2                        ! 25
    real*8 :: inc2                        ! 27
    real*8 :: ref3                        ! 29  idem for axis 3
    real*8 :: val3                        ! 31
    real*8 :: inc3                        ! 33
    real*8 :: ref4                        ! 35  idem for axis 4
    real*8 :: val4                        ! 37
    real*8 :: inc4                        ! 39
    !
    integer*4 :: blan                     ! 41  Blanking section length
    real*4 :: bval                        ! 42  Blanking value
    real*4 :: eval                        ! 43  Tolerance
    !
    integer*4 :: extr                     ! 44  Extrema section length
    real*4 :: rmin                        ! 45  Minimum
    real*4 :: rmax                        ! 46  Maximum
    integer*4 :: min1                     ! 47  Pixel of min on axis 1
    integer*4 :: max1                     ! 48  Pixel of max ......
    integer*4 :: min2                     ! 49  idem for axis 2
    integer*4 :: max2                     ! 50
    integer*4 :: min3                     ! 51           axis 3
    integer*4 :: max3                     ! 52
    integer*4 :: min4                     ! 53           axis 4
    integer*4 :: max4                     ! 54
    !
    integer*4 :: desc                     ! 55  Description section length
    integer*4 :: iuni(3)                  ! 56  Data Unit
    integer*4 :: icod(3,4)                ! 59  Axis names
    integer*4 :: isys(3)                  ! 71  Coordinate System
    integer*4 :: dum1                     ! Void for padding
    !
    integer*4 :: posi                     ! 74  Position section length
    integer*4 :: isou(3)                  ! 75  Source name
    real*8 :: ra                          ! 78  Right Ascension
    real*8 :: dec                         ! 80  Declination
    real*8 :: lii                         ! 82  Galactic longitude
    real*8 :: bii                         ! 84           latitude
    real*4 :: epoc                        ! 86  Epoch of coordinates
    integer*4 :: dum2                     ! Void for padding
    !
    integer*4 :: proj                     ! 87  Projection section length
    integer*4 :: ptyp                     ! 88  Projection type
    real*8 :: a0                          ! 89  X of projection center
    real*8 :: d0                          ! 91  Y of projection center
    real*8 :: pang                        ! 93  Projection angle
    integer*4 :: xaxi                     ! 95  X axis
    integer*4 :: yaxi                     ! 96  Y axis
    !
    integer*4 :: spec                     ! 97  Spectroscopy section length
    integer*4 :: ilin(3)                  ! 98  Line name
    real*8 :: fres                        !101  Frequency resolution
    real*8 :: fima                        !103  Image frequency
    real*8 :: freq                        !105  Rest Frequency
    real*4 :: vres                        !107  Velocity resolution
    real*4 :: voff                        !108  Velocity offset
    integer*4 :: faxi                     !109  Frequency axis
    !
    integer*4 :: reso                     !110  Resolution section length
    real*4 :: majo                        !111  Major axis
    real*4 :: mino                        !112  Minor axis
    real*4 :: posa                        !113  Position angle
    !
    integer*4 :: sigm                     ! 114 Noise section length
    real*4 :: noise                       ! 115 Theoretical noise
    real*4 :: rms                         ! 116 Actual noise
    !
    integer*4 :: prop                     ! 117 Proper motion section length
    real*4 :: mura                        ! 118 along RA, in mas/yr
    real*4 :: mudec                       ! 119 along Dec, in mas/yr
    real*4 :: parallax                    ! 120 in mas
!!  real*4 :: pepoch                      ! 121 in yrs ?
  end type gildas_header
  !
  type :: sic_header
    sequence
    integer*4 :: ityp(3)                  !  1
    integer*4 :: form                     !  4
    integer*4 :: nvb                      !  5
    integer*4 :: fill(5)                  !  6
    integer*4 :: gene                     ! 11
    integer*4 :: ndim                     ! 12
    integer*4 :: dim(4)                   ! 13
    real*8 :: convert(3,4)                ! 17-40
    !
    integer*4 :: blan                     ! 41
    real*4 :: blank(2)                    ! 42-43
    !
    integer*4 :: extrema                  ! 44
    real*4 :: min                         ! 45
    real*4 :: max                         ! 46
    integer*4 :: where(2,4)               ! 47-54
    !
    integer*4 :: desc                     ! 55
    integer*4 :: iuni(3)                  ! 56 Unit
    integer*4 :: icod(3,4)                ! 59 Unit1,2,3,4
    integer*4 :: isys(3)                  ! 71 System
    integer*4 :: dum1                     ! Void
    !
    integer*4 :: posi                     ! 74
    integer*4 :: isou(3)                  ! 75  Source
    real*8 :: ra                          ! 78
    real*8 :: dec                         ! 80
    real*8 :: lii                         ! 82
    real*8 :: bii                         ! 84
    real*4 :: epoc                        ! 86
    integer*4 :: dum2                     ! Void
    !
    integer*4 :: proj                     ! 87
    integer*4 :: ptyp                     ! 88
    real*8 :: a0                          ! 89
    real*8 :: d0                          ! 91
    real*8 :: angle                       ! 93
    integer*4 :: x_axis                   ! 95
    integer*4 :: y_axis                   ! 96
    !
    integer*4 :: spec                     ! 97
    integer*4 :: ilin(3)                  ! 98 Line
    real*8 :: freqres                     ! 101
    real*8 :: freqoff                     ! 103
    real*8 :: restfre                     ! 105
    real*4 :: velres                      ! 107
    real*4 :: veloff                      ! 108
    integer*4 :: f_axis                   ! 109
    !
    integer*4 :: beam                     ! 110
    real*4 :: major                       ! 111
    real*4 :: minor                       ! 112
    real*4 :: pa                          ! 113
    !
    integer*4 :: sigma                    ! 114
    real*4 :: noise                       ! 115
    real*4 :: rms                         ! 116
    !
    integer*4 :: proper                   ! 117 Proper motion section length
    real*4 :: mura                        ! 118 along RA, in mas/yr
    real*4 :: mudec                       ! 119 along Dec, in mas/yr
    real*4 :: parallax                    ! 120 in mas
!!  real*4 :: pepoch                      ! 121 in yrs ?
  end type sic_header
  !
  ! Gildas X,Y,Z version
  type :: gildas
    sequence
    character(len=256) :: file            ! File name
    type (strings) :: char
    type (location) :: loca
    type (gildas_header) :: gil
    integer*4 :: blc(4)                   ! Bottom left corner
    integer*4 :: trc(4)                   ! Top right corner
    integer*4 :: header                   ! Defined / Undefined
    integer*4 :: status                   ! Last error code
    real,         pointer :: r1d(:)       ! Pointer to 1D data
    real(kind=8), pointer :: d1d(:)
    integer,      pointer :: i1d(:)
    real,         pointer :: r2d(:,:)     ! Pointer to 2D data
    real(kind=8), pointer :: d2d(:,:)
    integer,      pointer :: i2d(:,:)
    real,         pointer :: r3d(:,:,:)
    real(kind=8), pointer :: d3d(:,:,:)   ! to 3D data
    integer,      pointer :: i3d(:,:,:)
    real,         pointer :: r4d(:,:,:,:)
    real(kind=8), pointer :: d4d(:,:,:,:) ! to 4D data
    integer,      pointer :: i4d(:,:,:,:)
  end type gildas
  !
  ! SIC Variable version
  type :: sic
    sequence
    character(len=256) :: file            ! File name
    type (strings) :: char
    type (location) :: loca
    type (sic_header) :: sic
    integer*4 :: blc(4)
    integer*4 :: trc(4)
    integer*4 :: header
    integer*4 :: status
    real,         pointer :: r1d(:)
    real(kind=8), pointer :: d1d(:)
    integer,      pointer :: i1d(:)
    real,         pointer :: r2d(:,:)
    real(kind=8), pointer :: d2d(:,:)
    integer,      pointer :: i2d(:,:)
    real,         pointer :: r3d(:,:,:)
    real(kind=8), pointer :: d3d(:,:,:)
    integer,      pointer :: i3d(:,:,:)
    real,         pointer :: r4d(:,:,:,:)
    real(kind=8), pointer :: d4d(:,:,:,:)
    integer,      pointer :: i4d(:,:,:,:)
  end type sic
  !
end module image_def
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
