{
"cells": [
{
"cell_type": "markdown",
"id": "74e354b4",
"metadata": {},
"source": [
"# FITS_tutorial2\n",
"Open a FITS file whose primary array represents\n",
"a spectrum (flux vs wavelength).\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Claudi Martinez \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:13 PM."
]
},
{
"cell_type": "markdown",
"id": "8e9f0a29",
"metadata": {},
"source": [
"We're gonna open a FITS file that contains the primary HDU and a little data table.\n",
"The primary HDU is an array of 2 rows by 2040 columns, and they represent a radiation\n",
"spectrum. The first row contains the flux data, whereas the second row the wavelengths.\n",
"Data copyright: NASA"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1aac8bdc",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:52.543628Z",
"iopub.status.busy": "2026-05-19T20:13:52.543496Z",
"iopub.status.idle": "2026-05-19T20:13:52.863421Z",
"shell.execute_reply": "2026-05-19T20:13:52.862749Z"
}
},
"outputs": [],
"source": [
"TString dir = gROOT->GetTutorialDir();"
]
},
{
"cell_type": "markdown",
"id": "d12b4749",
"metadata": {},
"source": [
"Open primary HDU from file"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9df0e6d6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:52.865560Z",
"iopub.status.busy": "2026-05-19T20:13:52.865428Z",
"iopub.status.idle": "2026-05-19T20:13:53.068206Z",
"shell.execute_reply": "2026-05-19T20:13:53.067546Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : The selected HDU contains an Image Extension\n"
]
}
],
"source": [
"TFITSHDU hdu(dir + \"/io/fitsio/sample2.fits\");"
]
},
{
"cell_type": "markdown",
"id": "3937d3fc",
"metadata": {},
"source": [
"Dump the HDUs within the FITS file\n",
"and also their metadata"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0e3f02ce",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:53.069759Z",
"iopub.status.busy": "2026-05-19T20:13:53.069626Z",
"iopub.status.idle": "2026-05-19T20:13:53.281215Z",
"shell.execute_reply": "2026-05-19T20:13:53.280555Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total: 2 HDUs\n",
" [0] IMAGE (PRIMARY)\n",
" SIMPLE = T / Standard FITS format\n",
" BITPIX = -32 / 32 bit IEEE floating point numbers\n",
" NAXIS = 2 / Number of axes\n",
" NAXIS1 = 2064 / \n",
" NAXIS2 = 2 / \n",
" EXTEND = T / There may be standard extensions\n",
" OPSIZE = 832 / PSIZE of original image\n",
" ORIGIN = 'ST-DADS ' / Institution that originated the FITS file\n",
" FITSDATE = '12/07/94' / Date FITS file was created\n",
" FILENAME = 'y19g0309t_cvt.c2h' / Original GEIS header file name with _cvt\n",
" ODATTYPE = 'FLOATING' / Original datatype\n",
" SDASMGNU = 2 / GCOUNT of original image\n",
" DADSFILE = 'Y19G0309T.C2F' / \n",
" DADSCLAS = 'CAL ' / \n",
" DADSDATE = '12-JUL-1994 02:44:39' / \n",
" CRVAL1 = 1.0000000000000 / \n",
" CRPIX1 = 1.0000000000000 / \n",
" CD1_1 = 1.0000000000000 / \n",
" DATAMIN = 0.00000000000000 / \n",
" DATAMAX = 2.7387550387959E-15 / \n",
" RA_APER = 182.63573015260 / \n",
" DEC_APER = 39.405888372580 / \n",
" FILLCNT = 0 / \n",
" ERRCNT = 0 / \n",
" FPKTTIME = 49099.133531036 / \n",
" LPKTTIME = 49099.133541164 / \n",
" CTYPE1 = 'PIXEL ' / \n",
" APER_POS = 'SINGLE ' / \n",
" PASS_DIR = 0 / \n",
" YPOS = -1516.0000000000 / \n",
" YTYPE = 'OBJ ' / \n",
" EXPOSURE = 31.249689102173 / \n",
" X_OFFSET = 0.00000000000000 / \n",
" Y_OFFSET = 0.00000000000000 / \n",
" = / \n",
" = / / GROUP PARAMETERS: OSS\n",
" = / \n",
" = / / GROUP PARAMETERS: PODPS\n",
" = / \n",
" = / / FOS DATA DESCRIPTOR KEYWORDS\n",
" INSTRUME = 'FOS ' / instrument in use\n",
" ROOTNAME = 'Y19G0309T ' / rootname of the observation set\n",
" FILETYPE = 'ERR ' / file type\n",
" BUNIT = 'ERGS/CM**2/S/A' / brightness units\n",
" = / \n",
" = / / GENERIC CONVERSION KEYWORDS\n",
" HEADER = T / science header line exists\n",
" TRAILER = F / reject array exists\n",
" YWRDSLIN = 516 / science words per packet\n",
" YLINSFRM = 5 / packets per frame\n",
" = / \n",
" = / / CALIBRATION FLAGS AND INDICATORS\n",
" GRNDMODE = 'SPECTROSCOPY ' / ground software mode\n",
" DETECTOR = 'AMBER ' / detector in use: amber, blue\n",
" APER_ID = 'B-2 ' / aperture id\n",
" POLAR_ID = 'C ' / polarizer id\n",
" POLANG = 0.0000000E+00 / initial angular position of polarizer\n",
" FGWA_ID = 'H57 ' / FGWA id\n",
" FCHNL = 0 / first channel\n",
" NCHNLS = 512 / number of channels\n",
" OVERSCAN = 5 / overscan number\n",
" NXSTEPS = 4 / number of x steps\n",
" YFGIMPEN = T / onboard GIMP correction enabled (T/F)\n",
" YFGIMPER = 'NO ' / error in onboard GIMP correction (YES/NO)\n",
" = / \n",
" = / / CALIBRATION REFERENCE FILES AND TABLES\n",
" DEFDDTBL = F / UDL disabled diode table used\n",
" BACHFILE = 'yref$b3m1128fy.r0h' / background header file\n",
" FL1HFILE = 'yref$baf13103y.r1h' / first flat-field header file\n",
" FL2HFILE = 'yref$n/a ' / second flat-field header file\n",
" IV1HFILE = 'yref$c3u13412y.r2h' / first inverse sensitivity header file\n",
" IV2HFILE = 'yref$n/a ' / second inverse sensitivity header file\n",
" RETHFILE = 'yref$n/a ' / waveplate retardation header file\n",
" DDTHFILE = 'yref$c861559ay.r4h' / disabled diode table header file\n",
" DQ1HFILE = 'yref$b2f1301qy.r5h' / first data quality initialization header file\n",
" DQ2HFILE = 'yref$n/a ' / second data quality initialization header file\n",
" CCG2 = 'mtab$a3d1145ly.cmg' / paired pulse correction parameters\n",
" CCS0 = 'ytab$a3d1145dy.cy0' / aperture parameters\n",
" CCS1 = 'ytab$aaj0732ay.cy1' / aperture position parameters\n",
" CCS2 = 'ytab$a3d1145fy.cy2' / sky emission line regions\n",
" CCS3 = 'ytab$a3d1145gy.cy3' / big and sky filter widths and prism X0\n",
" CCS4 = 'ytab$b9d1019my.cy4' / polarimetry parameters\n",
" CCS5 = 'ytab$a3d1145jy.cy5' / sky shifts\n",
" CCS6 = 'ytab$bck10546y.cy6' / wavelength coefficients\n",
" CCS7 = 'ytab$ba910502y.cy7' / GIMP correction scale factores\n",
" CCS8 = 'ytab$ba31407ly.cy8' / predicted background count rates\n",
" = / \n",
" = / / CALIBRATION SWITCHES\n",
" CNT_CORR = 'COMPLETE' / count to count rate conversion\n",
" OFF_CORR = 'OMIT ' / GIMP correction\n",
" PPC_CORR = 'COMPLETE' / paired pulse correction\n",
" BAC_CORR = 'COMPLETE' / background subtraction\n",
" GMF_CORR = 'COMPLETE' / scale reference background\n",
" FLT_CORR = 'COMPLETE' / flat-fielding\n",
" SKY_CORR = 'COMPLETE' / sky subtraction\n",
" WAV_CORR = 'COMPLETE' / wavelength scale generation\n",
" FLX_CORR = 'COMPLETE' / flux scale generation\n",
" ERR_CORR = 'COMPLETE' / propagated error computation\n",
" MOD_CORR = 'OMIT ' / ground software mode dependent reductions\n",
" = / \n",
" = / / PATTERN KEYWORDS\n",
" INTS = 2 / number of integrations\n",
" YBASE = -1516 / y base\n",
" YRANGE = 0 / y range\n",
" YSTEPS = 1 / number of y steps\n",
" YSPACE = 0.0000000E+00 / yrange * 32 / ysteps\n",
" SLICES = 1 / number of time slices\n",
" NPAT = 12 / number of patterns per readout\n",
" NREAD = 2 / number of readouts per memory clear\n",
" NMCLEARS = 1 / number of memory clears per acquisition\n",
" YSTEP1 = 'OBJ ' / first ystep data type: OBJ, SKY, BCK, NUL\n",
" YSTEP2 = 'NUL ' / second ystep data type: OBJ, SKY, BCK, NUL\n",
" YSTEP3 = 'NUL ' / third ystep data type: OBJ, SKY, BCK, NUL\n",
" XBASE = 0 / X-deflection base\n",
" XPITCH = 1521 / X-deflection pitch between diode\n",
" YPITCH = 1834 / Y-deflection pitch\n",
" = / \n",
" = / / CALIBRATION KEYWORDS\n",
" LIVETIME = 33333 / accumulator open time (unit=7.8125 microsec)\n",
" DEADTIME = 1280 / accumulator close time (unit=7.8125 microsec)\n",
" MAXCLK = 0 / maximum clock count\n",
" PA_APER = 0.2462417E+03 / position ang of aperture used with target (deg)\n",
" NOISELM = 65535 / burst noise rejection limit\n",
" OFFS_TAB = 'n/a ' / GIMP offsets (post-pipeline processing only)\n",
" MINWAVE = 4569.102 / minimum wavelength (angstroms)\n",
" MAXWAVE = 6817.517 / maximum wavelength (angstroms)\n",
" = / \n",
" = / / STATISTICAL KEYWORDS\n",
" DATE = '22/04/93 ' / date this file was written (dd/mm/yy)\n",
" PKTFMT = 96 / packet format code\n",
" PODPSFF = '0 ' / 0=(no podps fill), 1=(podps fill present)\n",
" STDCFFF = '0 ' / 0=(no st dcf fill), 1=(st dcf fill present)\n",
" STDCFFP = '0000 ' / st dcf fill pattern (hex)\n",
" = / \n",
" = / / APERTURE POSITION\n",
" RA_APER1 = 0.1826357301526E+03 / right ascension of the aperture (deg)\n",
" DECAPER1 = 0.3940588837258E+02 / declination of the aperture (deg)\n",
" = / \n",
" = / / EXPOSURE INFORMATION\n",
" EQUINOX = 'J2000 ' / equinox of the celestial coordinate system\n",
" SUNANGLE = 0.1225114E+03 / angle between sun and V1 axis (deg)\n",
" MOONANGL = 0.1191039E+03 / angle between moon and V1 axis (deg)\n",
" SUN_ALT = 0.4515910E+02 / altitude of the sun above Earth's limb (deg)\n",
" FGSLOCK = 'COARSE ' / commanded FGS lock (FINE,COARSE,GYROS,UNKNOWN)\n",
" = / \n",
" DATE-OBS = '22/04/93 ' / UT date of start of observation (dd/mm/yy)\n",
" TIME-OBS = '03:12:17 ' / UT time of start of observation (hh:mm:ss)\n",
" EXPSTART = 0.4909913202874E+05 / exposure start time (Modified Julian Date)\n",
" EXPEND = 0.4909913505303E+05 / exposure end time (Modified Julian Date)\n",
" EXPTIME = 0.2499975E+03 / exposure duration (seconds)--calculated\n",
" EXPFLAG = 'NORMAL ' / Exposure interruption indicator\n",
" = / \n",
" = / / TARGET & PROPOSAL ID\n",
" TARGNAME = 'NGC4151-CLOUD2 ' / proposer's target name\n",
" RA_TARG = 0.1826357301526E+03 / right ascension of the target (deg) (J2000)\n",
" DEC_TARG = 0.3940588837258E+02 / declination of the target (deg) (J2000)\n",
" = / \n",
" PROPOSID = 4220 / PEP proposal identifier\n",
" PEP_EXPO = '174.0000000 ' / PEP exposure identifier including sequence\n",
" LINENUM = '174.000 ' / PEP proposal line number\n",
" SEQLINE = ' ' / PEP line number of defined sequence\n",
" SEQNAME = ' ' / PEP define/use sequence name\n",
"\n",
" [1] ASCII TABLE ('y19g0309t.c2h.tab')\n",
" XTENSION = 'TABLE '\n",
" BITPIX = 8\n",
" NAXIS = 2\n",
" NAXIS1 = 336\n",
" NAXIS2 = 2\n",
" PCOUNT = 0\n",
" GCOUNT = 1\n",
" TFIELDS = 19\n",
" EXTNAME = 'y19g0309t.c2h.tab'\n",
" TTYPE1 = 'CRVAL1 '\n",
" CRVAL1 = 'pixel number'\n",
" TFORM1 = 'D25.16 '\n",
" TBCOL1 = 1\n",
" TTYPE2 = 'CRPIX1 '\n",
" CRPIX1 = 'pixel number of reference pixel'\n",
" TFORM2 = 'E15.7 '\n",
" TBCOL2 = 29\n",
" TTYPE3 = 'CD1_1 '\n",
" CD1_1 = 'pixel increment'\n",
" TFORM3 = 'E15.7 '\n",
" TBCOL3 = 45\n",
" TTYPE4 = 'DATAMIN '\n",
" DATAMIN = 'the minimum value of the data'\n",
" TFORM4 = 'E15.7 '\n",
" TBCOL4 = 61\n",
" TTYPE5 = 'DATAMAX '\n",
" DATAMAX = 'the maximum value of the data'\n",
" TFORM5 = 'E15.7 '\n",
" TBCOL5 = 77\n",
" TTYPE6 = 'RA_APER '\n",
" RA_APER = 'right ascension of aperture (deg)'\n",
" TFORM6 = 'D25.16 '\n",
" TBCOL6 = 93\n",
" TTYPE7 = 'DEC_APER'\n",
" DEC_APER = 'declination of aperture (deg)'\n",
" TFORM7 = 'D25.16 '\n",
" TBCOL7 = 121\n",
" TTYPE8 = 'FILLCNT '\n",
" FILLCNT = 'number of segments containing fill'\n",
" TFORM8 = 'I11 '\n",
" TBCOL8 = 149\n",
" TTYPE9 = 'ERRCNT '\n",
" ERRCNT = 'the error count of the data'\n",
" TFORM9 = 'I11 '\n",
" TBCOL9 = 161\n",
" TTYPE10 = 'FPKTTIME'\n",
" FPKTTIME = 'the time of the first packet'\n",
" TFORM10 = 'D25.16 '\n",
" TBCOL10 = 173\n",
" TTYPE11 = 'LPKTTIME'\n",
" LPKTTIME = 'the time of the last packet'\n",
" TFORM11 = 'D25.16 '\n",
" TBCOL11 = 201\n",
" TTYPE12 = 'CTYPE1 '\n",
" CTYPE1 = 'the first coordinate type'\n",
" TFORM12 = 'A8 '\n",
" TBCOL12 = 229\n",
" TTYPE13 = 'APER_POS'\n",
" APER_POS = 'aperture used'\n",
" TFORM13 = 'A8 '\n",
" TBCOL13 = 241\n",
" TTYPE14 = 'PASS_DIR'\n",
" PASS_DIR = 'polarization pass direction'\n",
" TFORM14 = 'I11 '\n",
" TBCOL14 = 253\n",
" TTYPE15 = 'YPOS '\n",
" YPOS = 'y-position on photocathode'\n",
" TFORM15 = 'E15.7 '\n",
" TBCOL15 = 265\n",
" TTYPE16 = 'YTYPE '\n",
" YTYPE = 'observation type: OBJ, SKY, BCK'\n",
" TFORM16 = 'A4 '\n",
" TBCOL16 = 281\n",
" TTYPE17 = 'EXPOSURE'\n",
" EXPOSURE = 'exposure time per pixel (seconds)'\n",
" TFORM17 = 'E15.7 '\n",
" TBCOL17 = 289\n",
" TTYPE18 = 'X_OFFSET'\n",
" X_OFFSET = 'x_offset for GIMP correction (diodes)'\n",
" TFORM18 = 'E15.7 '\n",
" TBCOL18 = 305\n",
" TTYPE19 = 'Y_OFFSET'\n",
" Y_OFFSET = 'y_offset for GIMP correction (defl.units)'\n",
" TFORM19 = 'E15.7 '\n",
" TBCOL19 = 321\n",
"\n"
]
}
],
"source": [
"hdu.Print(\"F+\");"
]
},
{
"cell_type": "markdown",
"id": "b6628598",
"metadata": {},
"source": [
"We now generate a TGraph from vectors"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0b8805b3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:53.282944Z",
"iopub.status.busy": "2026-05-19T20:13:53.282816Z",
"iopub.status.idle": "2026-05-19T20:13:53.488490Z",
"shell.execute_reply": "2026-05-19T20:13:53.487764Z"
}
},
"outputs": [],
"source": [
"std::unique_ptr Y(hdu.GetArrayRow(0));\n",
"std::unique_ptr X(hdu.GetArrayRow(1));\n",
"TGraph gr(*X,*Y);"
]
},
{
"cell_type": "markdown",
"id": "184cf1ac",
"metadata": {},
"source": [
"Show the graphic"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "96eea7c9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:53.490318Z",
"iopub.status.busy": "2026-05-19T20:13:53.490185Z",
"iopub.status.idle": "2026-05-19T20:13:53.693819Z",
"shell.execute_reply": "2026-05-19T20:13:53.693085Z"
}
},
"outputs": [],
"source": [
"auto c = new TCanvas(\"c1\", \"FITS tutorial #2\", 800, 800);\n",
"gr.SetFillColor(kRed);\n",
"gr.DrawClone(\"BA\");"
]
},
{
"cell_type": "markdown",
"id": "a538f2cc",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1a09a36b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:53.695837Z",
"iopub.status.busy": "2026-05-19T20:13:53.695711Z",
"iopub.status.idle": "2026-05-19T20:13:53.919961Z",
"shell.execute_reply": "2026-05-19T20:13:53.914162Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}