module s2dw_fileio_mod ! Uses use s2dw_types_mod use s2dw_error_mod use s2dw_core_mod ! Interfaces public interface s2dw_fileio_matlab_wav_write public interface s2dw_fileio_matlab_wav_read public interface s2dw_fileio_fits_wav_write public interface s2dw_fileio_fits_wav_read ! Subroutines and functions private subroutine s2dw_fileio_matlab_wav_write_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) private subroutine s2dw_fileio_matlab_wav_write_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) private subroutine s2dw_fileio_matlab_wav_read_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename) private subroutine s2dw_fileio_matlab_wav_read_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename) private subroutine s2dw_fileio_fits_wav_write_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) private subroutine s2dw_fileio_fits_wav_write_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) private subroutine s2dw_fileio_fits_wav_read_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename) private subroutine s2dw_fileio_fits_wav_read_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename) private subroutine s2dw_fileio_fits_error_check (status, halt) private subroutine s2dw_fileio_fits_exists (filename, status, exists) private subroutine s2dw_fileio_fits_del (filename, status) end module s2dw_fileio_modFunctionality to read and write S2DW formatted fits and matlab files containing wavelet and scaling coefficients. Both statically and dynamically allocated wavelet coefficients may be written and read from files (both data types have the same fits file format).

**Author:** J. D. McEwen (mcewen@mrao.cam.ac.uk)

**Version:** 0.1 November 2007

public interface s2dw_fileio_matlab_wav_write module procedure s2dw_fileio_matlab_wav_write_static module procedure s2dw_fileio_matlab_wav_write_dynamic end interface s2dw_fileio_matlab_wav_write

public interface s2dw_fileio_matlab_wav_read module procedure s2dw_fileio_matlab_wav_read_static module procedure s2dw_fileio_matlab_wav_read_dynamic end interface s2dw_fileio_matlab_wav_read

public interface s2dw_fileio_fits_wav_write module procedure s2dw_fileio_fits_wav_write_static module procedure s2dw_fileio_fits_wav_write_dynamic end interface s2dw_fileio_fits_wav_write

public interface s2dw_fileio_fits_wav_read module procedure s2dw_fileio_fits_wav_read_static module procedure s2dw_fileio_fits_wav_read_dynamic end interface s2dw_fileio_fits_wav_read

private subroutine s2dw_fileio_matlab_wav_write_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) real (kind=dp), intent(in), dimension (0:J, 0:2*B-2, 0:2*B-1, 0:N-1) :: wav complex (kind=dpc), intent(in), dimension (0:bl_scoeff-1, 0:bl_scoeff-1) :: scoeff integer, intent(in) :: J integer, intent(in) :: B integer, intent(in) :: N integer, intent(in) :: bl_scoeff real (kind=dp), intent(in) :: alpha character (len=*), intent(in) :: filename character (len=*), optional, intent(in) :: comment end subroutine s2dw_fileio_matlab_wav_write_staticWrites (statically allocated) wavelet and scaling coefficients to an output S2DW formatted .m matlab file and corresponding .dat data files.

Variables:

- wav(0:J, 0:2*B-2, 0:2*B-1, 0:N-1): Wavelet coefficients [input].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [input].
- J: Maximum analysis scale depth [input].
- B: Harmonic band limit [input].
- N: Azimuthal band limit [input].
- bl_scoeff: Upper band limit for scaling coefficients [input].
- alpha: Basis dilation factor [input].
- filename: Name of the output matlab file to write [input].
- [comment]: Optional comment string to be added to the output matlab file header if present [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_matlab_wav_write_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) type (s2dw_wav_abg), intent(in), allocatable, dimension (:) :: wavdyn complex (kind=dpc), intent(in), dimension (0:bl_scoeff-1, 0:bl_scoeff-1) :: scoeff integer, intent(in) :: J integer, intent(in) :: B integer, intent(in) :: N integer, intent(in) :: bl_scoeff real (kind=dp), intent(in) :: alpha character (len=*), intent(in) :: filename character (len=*), optional, intent(in) :: comment end subroutine s2dw_fileio_matlab_wav_write_dynamicWrites (dynamically allocated) wavelet and scaling coefficients to an output S2DW formatted .m matlab file ad corresponding data .dat file.

Variables:

- wavdyn(0:J)%coeff: Dynamically allocated wavelet coefficients for each scale [input].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [input].
- J: Maximum analysis scale depth [input].
- B: Harmonic band limit [input].
- N: Azimuthal band limit [input].
- bl_scoeff: Upper band limit for scaling coefficients [input].
- alpha: Basis dilation factor [input].
- filename: Name of the output matlab file to write [input].
- [comment]: Optional comment string to be added to the output matlab file header if present [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_matlab_wav_read_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename) real (kind=dp), allocatable, intent(out), dimension (:,:,:,:) :: wav complex (kind=dpc), allocatable, intent(out), dimension (:,:) :: scoeff integer, intent(out) :: J integer, intent(out) :: B integer, intent(out) :: N integer, intent(out) :: bl_scoeff real (kind=dp), intent(out) :: alpha character (len=*), intent(in) :: filename ! Calls: s2dw_error end subroutine s2dw_fileio_matlab_wav_read_staticReads (statically allocated) wavelet and scaling coefficients from a S2DW formatted .m matlab file and corresponding .dat data files.

Notes:

- Memory for the wavelet and scaling coefficients is allocated herein and should be freed by the calling routine.
- Input file must be precisely formatted as written by appropriate
S2DW Fortran or Matlab routine (file error checking is
**not**performed).

- wav(0:J, 0:2*B-2, 0:2*B-1, 0:N-1): Wavelet coefficients [output].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [output].
- J: Maximum analysis scale depth [output].
- B: Harmonic band limit [output].
- N: Azimuthal band limit [output].
- bl_scoeff: Upper band limit for scaling coefficients [output].
- alpha: Basis dilation factor [output].
- filename: Name of the matlab input file containing the wavelet and scaling coefficients to be read [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_matlab_wav_read_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename) type (s2dw_wav_abg), intent(out), allocatable, dimension (:) :: wavdyn complex (kind=dpc), allocatable, intent(out), dimension (:,:) :: scoeff integer, intent(out) :: J integer, intent(out) :: B integer, intent(out) :: N integer, intent(out) :: bl_scoeff real (kind=dp), intent(out) :: alpha character (len=*), intent(in) :: filename ! Calls: s2dw_error end subroutine s2dw_fileio_matlab_wav_read_dynamicReads (dynamically allocated) wavelet and scaling coefficients from a S2DW formatted .m matlab file and corresponding .dat data files.

Notes:

- Memory for the wavelet and scaling coefficients is allocated herein and should be freed by the calling routine.
- Input file must be precisely formatted as written by appropriate
S2DW Fortran or Matlab routine (file error checking is
**not**performed).

- wavdyn(0:J)%coeff: Dynamically allocated wavelet coefficients for each scale (memory allocated herein) [output].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [output].
- J: Maximum analysis scale depth [output].
- B: Harmonic band limit [output].
- N: Azimuthal band limit [output].
- bl_scoeff: Upper band limit for scaling coefficients [output].
- alpha: Basis dilation factor [output].
- filename: Name of the matlab input file containing the wavelet and scaling coefficients to be read [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_wav_write_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) real (kind=dp), intent(in), dimension (0:J, 0:2*B-2, 0:2*B-1, 0:N-1) :: wav complex (kind=dpc), intent(in), dimension (0:bl_scoeff-1, 0:bl_scoeff-1) :: scoeff integer, intent(in) :: J integer, intent(in) :: B integer, intent(in) :: N integer, intent(in) :: bl_scoeff real (kind=dp), intent(in) :: alpha character (len=*), intent(in) :: filename character (len=*), optional, intent(in) :: comment ! Calls: ftclos, ftfiou, ftgiou, ftiimg, ftinit, ftp2dd, ftp3dd, ftpcom, ftpdat, ftphpr, ftpkyd, ftpkyj, ftpkys, s2dw_error, s2dw_fileio_fits_error_check, s2dw_fileio_fits_exists end subroutine s2dw_fileio_fits_wav_write_staticWrites (statically allocated) wavelet and scaling coefficients to an output S2DW formatted fits file.

Variables:

- wav(0:J, 0:2*B-2, 0:2*B-1, 0:N-1): Wavelet coefficients [input].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [input].
- J: Maximum analysis scale depth [input].
- B: Harmonic band limit [input].
- N: Azimuthal band limit [input].
- bl_scoeff: Upper band limit for scaling coefficients [input].
- alpha: Basis dilation factor [input].
- filename: Name of the output fits file to write [input].
- [comment]: Optional comment string to be added to the output fits file header if present [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_wav_write_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename, comment) type (s2dw_wav_abg), intent(in), allocatable, dimension (:) :: wavdyn complex (kind=dpc), intent(in), dimension (0:bl_scoeff-1, 0:bl_scoeff-1) :: scoeff integer, intent(in) :: J integer, intent(in) :: B integer, intent(in) :: N integer, intent(in) :: bl_scoeff real (kind=dp), intent(in) :: alpha character (len=*), intent(in) :: filename character (len=*), optional, intent(in) :: comment ! Calls: ftclos, ftfiou, ftgiou, ftiimg, ftinit, ftp2dd, ftp3dd, ftpcom, ftpdat, ftphpr, ftpkyd, ftpkyj, ftpkys, s2dw_error, s2dw_fileio_fits_error_check, s2dw_fileio_fits_exists end subroutine s2dw_fileio_fits_wav_write_dynamicWrites (dynamically allocated) wavelet and scaling coefficients to an output S2DW formatted fits file.

Variables:

- wavdyn(0:J)%coeff: Dynamically allocated wavelet coefficients for each scale [input].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [input].
- J: Maximum analysis scale depth [input].
- B: Harmonic band limit [input].
- N: Azimuthal band limit [input].
- bl_scoeff: Upper band limit for scaling coefficients [input].
- alpha: Basis dilation factor [input].
- filename: Name of the output fits file to write [input].
- [comment]: Optional comment string to be added to the output fits file header if present [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_wav_read_static (wav, scoeff, J, B, N, bl_scoeff, alpha, filename) real (kind=dp), allocatable, intent(out), dimension (:,:,:,:) :: wav complex (kind=dpc), allocatable, intent(out), dimension (:,:) :: scoeff integer, intent(out) :: J integer, intent(out) :: B integer, intent(out) :: N integer, intent(out) :: bl_scoeff real (kind=dp), intent(out) :: alpha character (len=*), intent(in) :: filename ! Calls: ftclos, ftfiou, ftg2dd, ftg3dd, ftgiou, ftgkyd, ftgkyj, ftmahd, ftopen, ftthdu, s2dw_error, s2dw_fileio_fits_error_check, s2dw_fileio_fits_exists end subroutine s2dw_fileio_fits_wav_read_staticReads (statically allocated) wavelet and scaling coefficients from a S2DW formatted fits file.

Notes:

- Memory for the wavelet and scaling coefficients is allocated herein
and should be freed by the calling routine.

- wav(0:J, 0:2*B-2, 0:2*B-1, 0:N-1): Wavelet coefficients [output].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [output].
- J: Maximum analysis scale depth [output].
- B: Harmonic band limit [output].
- N: Azimuthal band limit [output].
- bl_scoeff: Upper band limit for scaling coefficients [output].
- alpha: Basis dilation factor [output].
- filename: Name of the fits input file containing the wavelet and scaling coefficients to be read [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_wav_read_dynamic (wavdyn, scoeff, J, B, N, bl_scoeff, alpha, filename) type (s2dw_wav_abg), intent(out), allocatable, dimension (:) :: wavdyn complex (kind=dpc), allocatable, intent(out), dimension (:,:) :: scoeff integer, intent(out) :: J integer, intent(out) :: B integer, intent(out) :: N integer, intent(out) :: bl_scoeff real (kind=dp), intent(out) :: alpha character (len=*), intent(in) :: filename ! Calls: ftclos, ftfiou, ftg2dd, ftg3dd, ftgiou, ftgkyd, ftgkyj, ftmahd, ftopen, ftthdu, s2dw_error, s2dw_fileio_fits_error_check, s2dw_fileio_fits_exists end subroutine s2dw_fileio_fits_wav_read_dynamicReads (dynamically allocated) wavelet and scaling coefficients from a S2DW formatted fits file.

Notes:

- Memory for the wavelet and scaling coefficients is allocated herein
and should be freed by the calling routine.

- wavdyn(0:J)%coeff: Dynamically allocated wavelet coefficients for each scale (memory allocated herein) [output].
- scoeff(0:bl_scoeff-1, 0:bl_scoeff-1): Scaling coefficients [output].
- J: Maximum analysis scale depth [output].
- B: Harmonic band limit [output].
- N: Azimuthal band limit [output].
- bl_scoeff: Upper band limit for scaling coefficients [output].
- alpha: Basis dilation factor [output].
- filename: Name of the fits input file containing the wavelet and scaling coefficients to be read [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_error_check (status, halt) integer, intent(inout) :: status logical, intent(in) :: halt ! Calls: ftgerr, ftgmsg end subroutine s2dw_fileio_fits_error_checkChecks if a fits error has occured and print error message. Halt program execution if halt flag is set.

Variables:

- status: Fits integer status code [input/output].
- halt: Logical to indicate whether to halt program execution if an error is detected [input].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_exists (filename, status, exists) character (len=*), intent(in) :: filename integer, intent(inout) :: status logical, intent(out) :: exists ! Calls: ftclos, ftfiou, ftgiou, ftopen, s2dw_fileio_fits_error_check end subroutine s2dw_fileio_fits_existsChecks if a fits file exists.

Variables:

- filename: Name of fits file to check existence of [input].
- status: Fits integer status code [input].
- exists: Logical indicating whether the fits file already exists [output].

**Author:** J. D. McEwen

private subroutine s2dw_fileio_fits_del (filename, status) character (len=*), intent(in) :: filename integer, intent(inout) :: status ! Calls: ftcmsg, ftdelt, ftfiou, ftgiou, ftopen end subroutine s2dw_fileio_fits_delDeletes a fits file.

Variables:

- filename: Name of fits file to detele [input].
- status: Fits integer status code [input].

**Author:** J. D. McEwen

**Version:** 0.1 - November 2007