0

I'm new to python implementation and would like to do the following process.

Read a set of data from .fits files, go through all of them (a few hundred .fits files) and present in a dataframe (preferably) the following HEADERS: File, IMAGE, OBJECT, FILTER, EXPTIME, DATE-OBS, RA and DEC

This dataframe must display the Headers selected from all headers in the dataset.

Below is the block I made manually and typing the values ​​directly. This is clearly not feasible when we are working with hundreds of files

import pandas as pd

df = pd.DataFrame({'File':["Bias","Flat","F0A","VOph","VOSgr1","VOSgr2","V6OSgr1", "VZSR1", "VZSR2"], 
                   'IMAGE':["Bias_0.fits","FlatB_0.fits","FOAqrR1_0.fits","V2051OphR1_0.fits","V4140SgrR1_0.fits",
                            "V4140SgrR2_0.fits","617SgrR1_0.fits","foco_008.fits","VZSclR2_0.fits"], 
                   'OBJECT':["Bias","Flat","FO Aqr","V2051 Oph","V4140 Sgr","V4140 Sgr","V617 Sgr","VZ Scl","VZ Scl"],
                   'FILTER':["Dark","B","B","B","B","B","B", "B", "B"], 
                   'EXPTIME':[(1,00000),(7,00000),(10,00000),(20,00000),(20,00000),(20,00000),(10,00000),(20,00000),(20,00000)], 
                   'DATE-OBS':["2018-09-11T21:33:05.129", "2018-09-11T21:42:12.854", "2018-09-11T23:04:28.561", 
                               "2018-09-11T21:52:46.914", "2018-09-11T22:37:46.847", "2018-09-12T00:04:03.136", 
                               "2018-09-12T00:26:25.792", "2018-09-12T01:15:04.282", "2018-09-12T05:05:14.991"], 
                   'RA':["16:04:36", "16:04:36", "22:17:55", "17:08:19", "19:58:50", "19:58:50", "18:07:51", "23:50:09","23:50:09"],
                   'DEC':["-37:51:47", "-37:51:47", "-08:21:0", "-25:48:32", "-38:56:13", "-38:56:13", "-35:10:25", 
                          "-26:22:5", "-26:22:53"]}) 

That is, I need to read each of the .fits of the dataset, and present the values ​​of each head that I mentioned in a single dataframe

I only know how to do it manually, that is, presenting the data directly, my need is for this process to be done automatically, reading the .fits files individually, and printing the 8 headers that I mentioned for each of these files, where everything must appear in a single table or dataframe that I can traverse through the data.

  • It sounds like you need to read the files directly. Maybe this answer would be helpful: https://stackoverflow.com/a/40112001/11578996 – ciaran haines Apr 27 '23 at 17:05
  • 1
    Have you learned about loops yet? – Iguananaut May 01 '23 at 17:16
  • Iguananaut. I learned superficially, I managed to solve this problem, maybe there are better ways, but it solved for the purpose I wanted. # Definition of the headers that will be extracted. headers = ["FILE", "IMAGE", "OBJECT", "FILTER", "EXPTIME", "DATE-OBS", "RA", "DEC"] – Henrique Lima May 09 '23 at 21:35
  • # Function to extract headers from a .fits file def extract_headers(filename): try: with fits.open(filename) as hdul: header = {} for h in headers: try: header[h] = hdul[0].header[h] except KeyError: continue return header except: return None – Henrique Lima May 09 '23 at 21:35
  • # Function to read all .fits files from a directory and generate the dataframe def process_directory(directory): data = [] for root, dirs, files in os.walk(directory): for f in files: if f.endswith(".fits"): filename = os.path.join(root, f) header = extract_headers(filename) if header is not None: data.append(header) df = pd.DataFrame(data) return df – Henrique Lima May 09 '23 at 21:35
  • # Reading the .fits files from the folder. directory = "simple-dataopd" df = process_directory(directory) # Presentation of the dataframe in a structured table. print(df) – Henrique Lima May 09 '23 at 21:35
  • Updates with code in comments are unreadable. Please [edit] your question to add the update. Or, since you mention "managed to solve this", you can self-answer your question: that makes it clear your problem is solved, and may be helpful to others as well. If you prefer a "better" solution, answer anyway, and add a comment asking for potential better answers. – 9769953 May 11 '23 at 10:04

0 Answers0