github.com/sbinet/go-cfitsio@v0.0.0-20140625105338-0307f985659e/hdu_test.go (about)

     1  package cfitsio
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type any interface{}
     9  
    10  var g_tables = []struct {
    11  	fname string
    12  	hdus  []HDU
    13  	tuple [][][]any
    14  	maps  []map[string]any
    15  	types []interface{}
    16  }{
    17  	{
    18  		fname: "testdata/swp06542llg.fits",
    19  		hdus: []HDU{
    20  			&PrimaryHDU{ImageHDU{
    21  				header: NewHeader(
    22  					[]Card{
    23  						{
    24  							Name:    "SIMPLE",
    25  							Value:   true,
    26  							Comment: "Standard FITS format",
    27  						},
    28  						{
    29  							Name:    "BITPIX",
    30  							Value:   int64(8),
    31  							Comment: "",
    32  						},
    33  						{
    34  							Name:    "NAXIS",
    35  							Value:   int64(0),
    36  							Comment: "no data in main file",
    37  						},
    38  						{
    39  							Name:    "EXTEND",
    40  							Value:   true,
    41  							Comment: "Extensions may exist",
    42  						},
    43  						{
    44  							Name:    "FILENAME",
    45  							Value:   "swp06542llg",
    46  							Comment: "original name of input file",
    47  						},
    48  						{
    49  							Name:    "TELESCOP",
    50  							Value:   "IUE",
    51  							Comment: "International Ultraviolet Explorer",
    52  						},
    53  						{
    54  							Name:    "ORIGIN",
    55  							Value:   "GODDARD",
    56  							Comment: "Tape writing location",
    57  						},
    58  						{
    59  							Name:    "CAMERA",
    60  							Value:   int64(3),
    61  							Comment: "IUE camera number",
    62  						},
    63  						{
    64  							Name:    "IMAGE",
    65  							Value:   int64(6542),
    66  							Comment: "IUE image sequence number",
    67  						},
    68  						{
    69  							Name:    "APERTURE",
    70  							Value:   "",
    71  							Comment: "Aperture",
    72  						},
    73  						{
    74  							Name:    "DISPERSN",
    75  							Value:   "LOW",
    76  							Comment: "IUE spectrograph dispersion",
    77  						},
    78  						{
    79  							Name:    "DATE-OBS",
    80  							Value:   "nn/nn/nn",
    81  							Comment: "Observation date (dd/mm/yy)",
    82  						},
    83  						{
    84  							Name:    "DATE-PRO",
    85  							Value:   "nn/nn/nn",
    86  							Comment: "Processing date (dd/mm/yy)",
    87  						},
    88  						{
    89  							Name:    "DATE",
    90  							Value:   "18-Feb-1993",
    91  							Comment: "Date file was written (dd/mm/yy)",
    92  						},
    93  						{
    94  							Name:    "RA",
    95  							Value:   0.0,
    96  							Comment: "Right Ascension in degrees",
    97  						},
    98  						{
    99  							Name:    "DEC",
   100  							Value:   0.0,
   101  							Comment: "Declination in degrees",
   102  						},
   103  						{
   104  							Name:    "EQUINOX",
   105  							Value:   1950.0,
   106  							Comment: "Epoch for coordinates (years)",
   107  						},
   108  						{
   109  							Name:    "THDA-RES",
   110  							Value:   0.0,
   111  							Comment: "THDA at time of read",
   112  						},
   113  						{
   114  							Name:    "THDA-SPE",
   115  							Value:   0.0,
   116  							Comment: "THDA at end of exposure",
   117  						},
   118  					},
   119  					IMAGE_HDU,
   120  					8,
   121  					[]int64{},
   122  				),
   123  			}},
   124  			&Table{
   125  				header: NewHeader(
   126  					[]Card{
   127  						{
   128  							Name:    "XTENSION",
   129  							Value:   "BINTABLE",
   130  							Comment: "Extension type",
   131  						},
   132  						{
   133  							Name:    "BITPIX",
   134  							Value:   int64(8),
   135  							Comment: "binary data",
   136  						},
   137  						{
   138  							Name:    "NAXIS",
   139  							Value:   int64(2),
   140  							Comment: "Number of Axes",
   141  						},
   142  						{
   143  							Name:    "NAXIS1",
   144  							Value:   int64(7532),
   145  							Comment: "width of table in bytes",
   146  						},
   147  						{
   148  							Name:    "NAXIS2",
   149  							Value:   int64(1),
   150  							Comment: "Number of entries in table",
   151  						},
   152  						{
   153  							Name:    "PCOUNT",
   154  							Value:   int64(0),
   155  							Comment: "Number of parameters/group",
   156  						},
   157  						{
   158  							Name:    "GCOUNT",
   159  							Value:   int64(1),
   160  							Comment: "Number of groups",
   161  						},
   162  						{
   163  							Name:    "TFIELDS",
   164  							Value:   int64(9),
   165  							Comment: "Number of fields in each row",
   166  						},
   167  						{
   168  							Name:    "EXTNAME",
   169  							Value:   "IUE MELO",
   170  							Comment: "name of table (?)",
   171  						},
   172  						{
   173  							Name:    "TFORM1",
   174  							Value:   "1I",
   175  							Comment: "Count and data type of field 1",
   176  						},
   177  						{
   178  							Name:    "TTYPE1",
   179  							Value:   "ORDER",
   180  							Comment: "spectral order (low dispersion = 1)",
   181  						},
   182  						{
   183  							Name:    "TUNIT1",
   184  							Value:   "",
   185  							Comment: "unitless",
   186  						},
   187  						{
   188  							Name:    "TFORM2",
   189  							Value:   "1I",
   190  							Comment: "field 2 has one 2-byte integer",
   191  						},
   192  						{
   193  							Name:    "TTYPE2",
   194  							Value:   "NPTS",
   195  							Comment: "number of non-zero points in each vector",
   196  						},
   197  						{
   198  							Name:    "TUNIT2",
   199  							Value:   "",
   200  							Comment: "unitless",
   201  						},
   202  						{
   203  							Name:    "TFORM3",
   204  							Value:   "1E",
   205  							Comment: "Count and data type of field 3",
   206  						},
   207  						{
   208  							Name:    "TTYPE3",
   209  							Value:   "LAMBDA",
   210  							Comment: "3rd field is starting wavelength",
   211  						},
   212  						{
   213  							Name:    "TUNIT3",
   214  							Value:   "ANGSTROM",
   215  							Comment: "unit is angstrom",
   216  						},
   217  						{
   218  							Name:    "TFORM4",
   219  							Value:   "1E",
   220  							Comment: "Count and Type of 4th field",
   221  						},
   222  						{
   223  							Name:    "TTYPE4",
   224  							Value:   "DELTAW",
   225  							Comment: "4th field is wavelength increment",
   226  						},
   227  						{
   228  							Name:    "TUNIT4",
   229  							Value:   "ANGSTROM",
   230  							Comment: "unit is angstrom",
   231  						},
   232  						{
   233  							Name:    "TFORM5",
   234  							Value:   "376E",
   235  							Comment: "Count and Type of 5th field",
   236  						},
   237  						{
   238  							Name:    "TTYPE5",
   239  							Value:   "GROSS",
   240  							Comment: "5th field is gross flux array",
   241  						},
   242  						{
   243  							Name:    "TUNIT5",
   244  							Value:   "FN",
   245  							Comment: "unit is IUE FN",
   246  						},
   247  						{
   248  							Name:    "TFORM6",
   249  							Value:   "376E",
   250  							Comment: "Count and Type of 6th field",
   251  						},
   252  						{
   253  							Name:    "TTYPE6",
   254  							Value:   "BACK",
   255  							Comment: "6th field is background flux array",
   256  						},
   257  						{
   258  							Name:    "TUNIT6",
   259  							Value:   "FN",
   260  							Comment: "unit is IUE FN",
   261  						},
   262  						{
   263  							Name:    "TFORM7",
   264  							Value:   "376E",
   265  							Comment: "Count and Type of 7th field",
   266  						},
   267  						{
   268  							Name:    "TTYPE7",
   269  							Value:   "NET",
   270  							Comment: "7th field is net flux array",
   271  						},
   272  						{
   273  							Name:    "TUNIT7",
   274  							Value:   "ERGS",
   275  							Comment: "unit is IUE FN",
   276  						},
   277  						{
   278  							Name:    "TFORM8",
   279  							Value:   "376E",
   280  							Comment: "Count and Type of 8th field",
   281  						},
   282  						{
   283  							Name:    "TTYPE8",
   284  							Value:   "ABNET",
   285  							Comment: "absolutely calibrated net flux array",
   286  						},
   287  						{
   288  							Name:    "TUNIT8",
   289  							Value:   "ERGS",
   290  							Comment: "unit is ergs/cm2/sec/angstrom",
   291  						},
   292  						{
   293  							Name:    "TFORM9",
   294  							Value:   "376E",
   295  							Comment: "Count and Type of 9th field",
   296  						},
   297  						{
   298  							Name:    "TTYPE9",
   299  							Value:   "EPSILONS",
   300  							Comment: "9th field is epsilons",
   301  						},
   302  						{
   303  							Name:    "TUNIT9",
   304  							Value:   "",
   305  							Comment: "unitless",
   306  						},
   307  					},
   308  					BINARY_TBL,
   309  					8,
   310  					[]int64{},
   311  				),
   312  			},
   313  		},
   314  		tuple: [][][]any{
   315  			nil,
   316  			{
   317  				// row-0
   318  				{
   319  					int16(1),
   320  					int16(376), float32(1000.8), float32(2.6515958), float32(19286.426),
   321  					float32(22999.92), float32(1001.04297), float32(1001.04297),
   322  					float32(88),
   323  				},
   324  			},
   325  		},
   326  		maps: []map[string]any{
   327  			{},
   328  			{
   329  				"ORDER":    int16(1),
   330  				"NPTS":     int16(376),
   331  				"LAMBDA":   float32(1000.8),
   332  				"DELTAW":   float32(2.6515958),
   333  				"GROSS":    float32(19286.426),
   334  				"BACK":     float32(22999.92),
   335  				"NET":      float32(1001.04297),
   336  				"ABNET":    float32(1001.04297),
   337  				"EPSILONS": float32(88),
   338  			},
   339  		},
   340  		types: []interface{}{
   341  			nil,
   342  			struct {
   343  				Order    int16   `fits:"ORDER"`
   344  				Npts     int16   `fits:"NPTS"`
   345  				DeltaW   float32 `fits:"DELTAW"` // switch order of deltaw w/ lambda
   346  				Lambda   float32 `fits:"LAMBDA"`
   347  				Gross    float32 `fits:"GROSS"`
   348  				Back     float32 `fits:"BACK"`
   349  				Net      float32 `fits:"NET"`
   350  				ABNET    float32 // test w/o struct-tag
   351  				EPSILONS float32 // ditto
   352  			}{},
   353  		},
   354  	},
   355  	{
   356  		fname: "testdata/file001.fits",
   357  		hdus: []HDU{
   358  			&PrimaryHDU{ImageHDU{
   359  				header: NewHeader(
   360  					[]Card{
   361  						{
   362  							Name:    "SIMPLE",
   363  							Value:   true,
   364  							Comment: "STANDARD FITS FORMAT (REV OCT 1981)",
   365  						},
   366  						{
   367  							Name:    "BITPIX",
   368  							Value:   int64(8),
   369  							Comment: "CHARACTER INFORMATION",
   370  						},
   371  						{
   372  							Name:    "NAXIS",
   373  							Value:   int64(0),
   374  							Comment: "NO IMAGE DATA ARRAY PRESENT",
   375  						},
   376  						{
   377  							Name:    "EXTEND",
   378  							Value:   true,
   379  							Comment: "THERE IS AN EXTENSION",
   380  						},
   381  						{
   382  							Name:    "ORIGIN",
   383  							Value:   "ESO",
   384  							Comment: "EUROPEAN SOUTHERN OBSERVATORY",
   385  						},
   386  						{
   387  							Name:    "OBJECT",
   388  							Value:   "SNG - CAT.",
   389  							Comment: "THE IDENTIFIER",
   390  						},
   391  						{
   392  							Name:    "DATE",
   393  							Value:   "27/ 5/84",
   394  							Comment: "DATE THIS TAPE WRITTEN DD/MM/YY",
   395  						},
   396  					},
   397  					IMAGE_HDU,
   398  					8,
   399  					[]int64{},
   400  				),
   401  			}},
   402  			&Table{
   403  				header: NewHeader(
   404  					[]Card{
   405  						{
   406  							Name:    "XTENSION",
   407  							Value:   "TABLE",
   408  							Comment: "TABLE EXTENSION",
   409  						},
   410  						{
   411  							Name:    "BITPIX",
   412  							Value:   int64(8),
   413  							Comment: "CHARACTER INFORMATION",
   414  						},
   415  						{
   416  							Name:    "NAXIS",
   417  							Value:   int64(2),
   418  							Comment: "SIMPLE 2-D MATRIX",
   419  						},
   420  						{
   421  							Name:    "NAXIS1",
   422  							Value:   int64(98),
   423  							Comment: "NO. OF CHARACTERS PER ROW",
   424  						},
   425  						{
   426  							Name:    "NAXIS2",
   427  							Value:   int64(10),
   428  							Comment: "NO. OF ROWS",
   429  						},
   430  						{
   431  							Name:    "PCOUNT",
   432  							Value:   int64(0),
   433  							Comment: "RANDOM PARAMETER COUNT",
   434  						},
   435  						{
   436  							Name:    "GCOUNT",
   437  							Value:   int64(1),
   438  							Comment: "GROUP COUNT",
   439  						},
   440  						{
   441  							Name:    "TFIELDS",
   442  							Value:   int64(7),
   443  							Comment: "NO. OF FIELDS PER ROW",
   444  						},
   445  						{
   446  							Name:    "TTYPE1",
   447  							Value:   "IDEN.",
   448  							Comment: "NAME OF ROW",
   449  						},
   450  						{
   451  							Name:    "TBCOL1",
   452  							Value:   int64(1),
   453  							Comment: "BEGINNING COLUMN OF THE FIELD",
   454  						},
   455  						{
   456  							Name:    "TFORM1",
   457  							Value:   "E14.7",
   458  							Comment: "FORMAT",
   459  						},
   460  						{
   461  							Name:    "TNULL1",
   462  							Value:   "",
   463  							Comment: "NULL VALUE",
   464  						},
   465  						{
   466  							Name:    "TTYPE2",
   467  							Value:   "RA",
   468  							Comment: "NAME OF ROW",
   469  						},
   470  						{
   471  							Name:    "TBCOL2",
   472  							Value:   int64(15),
   473  							Comment: "BEGINNING COLUMN OF THE FIELD",
   474  						},
   475  						{
   476  							Name:    "TFORM2",
   477  							Value:   "E14.7",
   478  							Comment: "FORMAT",
   479  						},
   480  						{
   481  							Name:    "TNULL2",
   482  							Value:   "",
   483  							Comment: "NULL VALUE",
   484  						},
   485  						{
   486  							Name:    "TTYPE3",
   487  							Value:   "DEC",
   488  							Comment: "NAME OF ROW",
   489  						},
   490  						{
   491  							Name:    "TBCOL3",
   492  							Value:   int64(29),
   493  							Comment: "BEGINNING COLUMN OF THE FIELD",
   494  						},
   495  						{
   496  							Name:    "TFORM3",
   497  							Value:   "E14.7",
   498  							Comment: "FORMAT",
   499  						},
   500  						{
   501  							Name:    "TNULL3",
   502  							Value:   "",
   503  							Comment: "NULL VALUE",
   504  						},
   505  						{
   506  							Name:    "TTYPE4",
   507  							Value:   "TYPE",
   508  							Comment: "NAME OF ROW",
   509  						},
   510  						{
   511  							Name:    "TBCOL4",
   512  							Value:   int64(43),
   513  							Comment: "BEGINNING COLUMN OF THE FIELD",
   514  						},
   515  						{
   516  							Name:    "TFORM4",
   517  							Value:   "E14.7",
   518  							Comment: "FORMAT",
   519  						},
   520  						{
   521  							Name:    "TNULL4",
   522  							Value:   "",
   523  							Comment: "NULL VALUE",
   524  						},
   525  						{
   526  							Name:    "TTYPE5",
   527  							Value:   "D25",
   528  							Comment: "NAME OF ROW",
   529  						},
   530  						{
   531  							Name:    "TBCOL5",
   532  							Value:   int64(57),
   533  							Comment: "BEGINNING COLUMN OF THE FIELD",
   534  						},
   535  						{
   536  							Name:    "TFORM5",
   537  							Value:   "E14.7",
   538  							Comment: "FORMAT",
   539  						},
   540  						{
   541  							Name:    "TNULL5",
   542  							Value:   "",
   543  							Comment: "NULL VALUE",
   544  						},
   545  						{
   546  							Name:    "TTYPE6",
   547  							Value:   "INCL.",
   548  							Comment: "NAME OF ROW",
   549  						},
   550  						{
   551  							Name:    "TBCOL6",
   552  							Value:   int64(71),
   553  							Comment: "BEGINNING COLUMN OF THE FIELD",
   554  						},
   555  						{
   556  							Name:    "TFORM6",
   557  							Value:   "E14.7",
   558  							Comment: "FORMAT",
   559  						},
   560  						{
   561  							Name:    "TNULL6",
   562  							Value:   "",
   563  							Comment: "NULL VALUE",
   564  						},
   565  						{
   566  							Name:    "TTYPE7",
   567  							Value:   "RV",
   568  							Comment: "NAME OF ROW",
   569  						},
   570  						{
   571  							Name:    "TBCOL7",
   572  							Value:   int64(85),
   573  							Comment: "BEGINNING COLUMN OF THE FIELD",
   574  						},
   575  						{
   576  							Name:    "TFORM7",
   577  							Value:   "E14.7",
   578  							Comment: "FORMAT",
   579  						},
   580  						{
   581  							Name:    "TNULL7",
   582  							Value:   "",
   583  							Comment: "NULL VALUE",
   584  						},
   585  					},
   586  					ASCII_TBL,
   587  					8,
   588  					[]int64{98, 10},
   589  				),
   590  			},
   591  		},
   592  		tuple: [][][]any{
   593  			nil,
   594  			{
   595  				{
   596  					-1116.59, 11.28, 59.5667, float64(3),
   597  					77.89999, float64(12), float64(0),
   598  				},
   599  				{
   600  					-1109.54, 11.15667, 54.300000000000004, float64(3),
   601  					float64(40), float64(12), float64(0),
   602  				},
   603  				{
   604  					-340.285, 3.6683, -28.0167, float64(3),
   605  					float64(70), float64(30), 4060.0000000000005,
   606  				},
   607  				{
   608  					536.0, 1.3933300000000002, 34.449999999999996,
   609  					3.5, 222.89999999999998, 59.9, float64(5160),
   610  				},
   611  				{
   612  					float64(3177), 10.23, 21.366699999999998, float64(3),
   613  					99.60001000000001, float64(36), float64(1220),
   614  				},
   615  				{
   616  					float64(3627), 11.29333, 13.2667, 3.2, 522.5999999999999, 59.9, float64(697),
   617  				},
   618  				{
   619  					float64(3756), 11.56667, 54.566700000000004, 4.2,
   620  					261.90000000000003, 56.699999999999996, float64(1071),
   621  				},
   622  				{
   623  					float64(5457), 14.025000000000002, 54.583000000000006, 6.2, 1614.8999999999999,
   624  					12.2, 266.0,
   625  				},
   626  				{
   627  					7292.0, 22.435, 30.049999999999997, 9.5, 128.3, 35.6, 934.0,
   628  				},
   629  				{
   630  					14237.0, 13.3633, -20.867, 3.5, 117.0, 42.199999999999996, 0.0,
   631  				},
   632  			},
   633  		},
   634  		maps: []map[string]any{
   635  			{},
   636  			{
   637  				"IDEN.": -1116.59,
   638  				"RA":    11.28,
   639  				"DEC":   59.5667,
   640  				"TYPE":  float64(3),
   641  				"D25":   77.89999,
   642  				"INCL.": float64(12),
   643  				"RV":    float64(0),
   644  				//"NOT-THERE": 0.0,
   645  			},
   646  		},
   647  		types: []interface{}{
   648  			nil,
   649  			struct {
   650  				Iden float64 `fits:"IDEN."`
   651  				Ra   float64 `fits:"RA"`
   652  				Dec  float64 `fits:"DEC"`
   653  				Type float64 `fits:"TYPE"`
   654  				D25  float64 `fits:"D25"`
   655  				Incl float64 `fits:"INCL."`
   656  				RV   float64 // test w/o struct-tag
   657  				//X_NotThere float64 `fits:"NOT_THERE"`
   658  			}{},
   659  		},
   660  	},
   661  }
   662  
   663  func TestHDU(t *testing.T) {
   664  	for _, table := range g_tables {
   665  		f, err := Open(table.fname, ReadOnly)
   666  		if err != nil {
   667  			t.Fatalf("could not open FITS file [%s]: %v", table.fname, err)
   668  		}
   669  		defer f.Close()
   670  
   671  		nhdus, err := f.NumHDUs()
   672  		if err != nil {
   673  			t.Fatalf("error num-hdus [%v]: %v", table.fname, err)
   674  		}
   675  		if nhdus != len(table.hdus) {
   676  			t.Fatalf("file [%v]: expected %v hdus. got %v", table.fname, nhdus, len(table.hdus))
   677  		}
   678  
   679  		for i := 0; i < len(table.hdus); i++ {
   680  			ref := table.hdus[i]
   681  			hdu := f.HDUs()[i]
   682  
   683  			if hdu.Type() != ref.Type() {
   684  				t.Fatalf("expected HDU-type [%v]. got [%v] (fname=%v)", ref.Type(), hdu.Type(), table.fname)
   685  			}
   686  
   687  			if hdu.Name() != ref.Name() {
   688  				t.Fatalf("expected HDU-name [%v]. got [%v] (fname=%v)", ref.Name(), hdu.Name(), table.fname)
   689  			}
   690  
   691  			if hdu.Version() != ref.Version() {
   692  				t.Fatalf("expected HDU-version [%v]. got [%v] (fname=%v)", ref.Version(), hdu.Version(), table.fname)
   693  			}
   694  
   695  			xhdr := hdu.Header()
   696  			rhdr := ref.Header()
   697  			if len(xhdr.slice) != len(rhdr.slice) {
   698  				t.Fatalf("#cards differ: ref=%v chk=%v (fname=%v)", len(rhdr.slice), len(xhdr.slice), table.fname)
   699  			}
   700  			for ii := 0; ii < len(rhdr.slice); ii++ {
   701  				if !reflect.DeepEqual(xhdr.slice[ii], rhdr.slice[ii]) {
   702  					t.Fatalf("cards differ (fname=%v).\nexpected:\n%v\ngot:\n%v", table.fname, rhdr.slice[ii], xhdr.slice[ii])
   703  				}
   704  
   705  			}
   706  		}
   707  
   708  	}
   709  }
   710  
   711  // EOF