; docformat = 'rst' ;+ ; :Description: ; Unit tests for the cube_generator fits reader ; ; :Requires: ; Written with IDL 8.6.1, but should run with anything > 8.3 ; mgunit : https://github.com/mgalloy/mgunit ; mglib : https://github.com/mgalloy/mglib ; IDLAstro : https://github.com/wlandsman/IDLAstro ; ; :Author: ; Josh Elliott ; joshua.elliott@lasp.colorado.edu ; ; :History: ; Created 21 November 2017 ;- ;+ ; :Description: ; Setup for each test ;- pro uvis_read_fits_cube_ut::setup compile_opt idl2 self.test_struct = ptr_new({UVIS:findgen(10,42), X:42, Y:'test', Z:indgen(4,2,3)}) self.fits_file_name = file_dirname(routine_filepath()) + path_sep() + 'test_file.fits' if file_test(self.fits_file_name) then begin file_delete, self.fits_file_name endif s = *(self.test_struct) uvis_fits_writer, self.fits_file_name, s, replicate(s, 4), replicate(s, 2) end ;+ ; :Description: ; Teardown for each test ; ;- pro uvis_read_fits_cube_ut::teardown compile_opt idl2 if file_test(self.fits_file_name) then begin file_delete, self.fits_file_name endif end ;+ ; :Description: ; Helper method that is used below to verify that both structs are identical. ; ;- pro uvis_read_fits_cube_ut::_verify_structs_match, struct1, struct2 compile_opt idl2 assert, n_tags(struct1) eq n_tags(struct2), 'Struct has the wrong number of tags' assert, array_equal(tag_names(struct1), tag_names(struct1)), "Tag names don't match" tn = tag_names(struct1) for i = 0L, n_tags(struct1)-1 do begin if tn[i] eq 'KERNELS' then continue ; TODO: Find out why a null string becomes a space, ' ', in the fits file. if (isa(struct1.(i), /ARRAY)) then begin assert, array_equal(struct1.(i), struct2.(i)), "Struct field " + tn[i] + " doesn't match" endif else begin assert, struct1.(i) eq struct2.(i), "Struct field " + tn[i] + " doesn't match" endelse endfor end ;+ ; :Description: ; Test that we can read a simple file. ;- function uvis_read_fits_cube_ut::test_read compile_opt idl2 !null = uvis_read_fits_cube(self.fits_file_name, NAME='test') return, 1 end ;+ ; :Description: ; Test that the header is present. ; ;- function uvis_read_fits_cube_ut::test_header compile_opt idl2 !null = uvis_read_fits_cube(self.fits_file_name, HEADER=header, NAME='test') found = !FALSE foreach line, header do begin found = line.contains('The UVIS instrument is part of the remote sensing payload') if found then break endforeach assert, found, 'Header information is missing.' return, 1 end ;+ ; :Description: ; Test that the data are present. ; ;- function uvis_read_fits_cube_ut::test_verify_data compile_opt idl2 s = *(self.test_struct) s1 = replicate(s, 4) s2 = replicate(s, 2) hdus = uvis_read_fits_cube(self.fits_file_name, HEADER=header, NAME='test') data = hdus.datastruct assert, s.x eq data.x, 'File data does not match test data.' assert, s.y eq data.y, 'File data does not match test data.' assert, array_equal(s.z, data.z), 'File data does not match test data.' return, 1 end ;+ ; :Description: ; Verify multiple readouts. ; ;- function uvis_read_fits_cube_ut::test_verify_multiple_readouts compile_opt idl2 s = *(self.test_struct) s1 = replicate(s, 4) s2 = replicate(s, 2) hdus = uvis_read_fits_cube(self.fits_file_name, HEADER=header, NAME='test') data = hdus.datastruct_initial assert, array_equal(s1.x, data.x), 'File data does not match test data.' assert, array_equal(s1.y, data.y), 'File data does not match test data.' assert, array_equal(s1.z, data.z), 'File data does not match test data.' return, 1 end ;+ ; :Description: ; This tests an actual Cube file. A fits file is created from the data ; and we read it back in and verify that all data are identical to the original ; IDL save file. ; ;- function uvis_read_fits_cube_ut::test_read_cube_file compile_opt idl2 ; Get the data from the save file file_base = file_basename('FUV2005_288_21_13_59_UVIS_016DI_ICYLON014_ISS.sav', '.sav') file_dir = file_dirname(routine_filepath()) + path_sep() + 'testdata' sav_file = file_dir + path_sep() + file_base + '.sav' restore, sav_file ; Write the fits file fits_file = file_dir + path_sep() + file_base + '.fits' if file_test(fits_file) then begin file_delete, fits_file endif uvis_fits_writer, fits_file, datastruct2, datastruct2_initial, datastruct2_final ; Read the fits file data = uvis_read_fits_cube(fits_file, HEADER=hdr) ; Verify that all tags are the same and that they contain the same data. self._verify_structs_match, data.datastruct, datastruct2 self._verify_structs_match, data.datastruct_initial, datastruct2_initial self._verify_structs_match, data.datastruct_final, datastruct2_final file_delete, fits_file return, 1 end ;+ ; :Description: ; Class data definition procedure ;- pro uvis_read_fits_cube_ut__define compile_opt idl2 !NULL = {uvis_read_fits_cube_ut, $ inherits MGutTestCase, $ test_struct: ptr_new(), $ fits_file_name: '' $ } end