|
2 | 2 |
|
3 | 3 | import sqlite3 |
4 | 4 |
|
| 5 | +import astropy.io.fits as fits |
| 6 | +import pandas as pd |
| 7 | +from astropy.table import Table |
| 8 | + |
5 | 9 | # The following SQL will create am SQLite database |
6 | 10 | FILENAME_TABLE = """ |
7 | 11 | CREATE TABLE IF NOT EXISTS filename ( |
@@ -133,3 +137,113 @@ def print_summary(self) -> str: |
133 | 137 | # Add more detail here later? - could break down by fields, etc. |
134 | 138 |
|
135 | 139 | return summary_message |
| 140 | + |
| 141 | + def write_to_alternate_format(self, save_format: str) -> list[str]: |
| 142 | + """ |
| 143 | + Write out the SQLite DB as an alternate format. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + save_format : str |
| 148 | + Format to save output: 'csv', 'excel', 'html', or 'fits' |
| 149 | +
|
| 150 | + Returns |
| 151 | + -------- |
| 152 | + files_written: list[str]: List of file names written out |
| 153 | + """ |
| 154 | + # Check that input is a valid option |
| 155 | + supported_formats = ["csv", "excel", "fits", "html"] |
| 156 | + if save_format.lower() not in supported_formats: |
| 157 | + msg = f"Save Format '{save_format}' is not supported." |
| 158 | + msg += f"Please choose from {supported_formats}" |
| 159 | + raise ValueError(msg) |
| 160 | + |
| 161 | + # Construct new filename to save to |
| 162 | + fileroot = self.db_file.strip(".db") |
| 163 | + |
| 164 | + # Read DB file as pandas dataframe |
| 165 | + self.conn = sqlite3.connect(self.db_file) |
| 166 | + filename_data = pd.read_sql_query("SELECT * FROM filename", self.conn) |
| 167 | + fields_data = pd.read_sql_query("SELECT * FROM fields", self.conn) |
| 168 | + # Close connection |
| 169 | + self.conn.close() |
| 170 | + |
| 171 | + # Save out data in new format |
| 172 | + # CSV files |
| 173 | + if save_format == "csv": |
| 174 | + ouput_filename1 = f"{fileroot}_filenames.csv" |
| 175 | + ouput_filename2 = f"{fileroot}_fields.csv" |
| 176 | + filename_data.to_csv(ouput_filename1) |
| 177 | + fields_data.to_csv(ouput_filename2) |
| 178 | + files_written = [ouput_filename1, ouput_filename2] |
| 179 | + # Excel spreadsheet |
| 180 | + elif save_format == "excel": |
| 181 | + output_filename = f"{fileroot}.xlsx" |
| 182 | + # Style dataframe: change color of cell depending on verdict |
| 183 | + filename_data = filename_data.style.map(color_formatter) |
| 184 | + fields_data = fields_data.style.map(color_formatter) |
| 185 | + # Save as one Excel document with two sheets: |
| 186 | + # One for filenames table and one for fields |
| 187 | + with pd.ExcelWriter(output_filename) as excel_writer: |
| 188 | + filename_data.to_excel(excel_writer, sheet_name="FileNames") |
| 189 | + fields_data.to_excel(excel_writer, sheet_name="Fields") |
| 190 | + files_written = [output_filename] |
| 191 | + |
| 192 | + # Fits table |
| 193 | + elif save_format == "fits": |
| 194 | + # Save as one fits file with two table extensions: |
| 195 | + # One for filenames table and one for fields |
| 196 | + output_filename = f"{fileroot}.fits" |
| 197 | + hdu_list = fits.HDUList( |
| 198 | + [ |
| 199 | + fits.PrimaryHDU(), # TODO: add some metadata? |
| 200 | + fits.table_to_hdu(Table.from_pandas(filename_data), name="FILENAMES"), |
| 201 | + fits.table_to_hdu(Table.from_pandas(fields_data), name="FIELDS"), |
| 202 | + ] |
| 203 | + ) |
| 204 | + hdu_list.writeto(output_filename, overwrite=True) |
| 205 | + files_written = [output_filename] |
| 206 | + |
| 207 | + # Html table |
| 208 | + elif save_format == "html": |
| 209 | + ouput_filename1 = f"{fileroot}_filenames.html" |
| 210 | + ouput_filename2 = f"{fileroot}_fields.html" |
| 211 | + # Style dataframe: change color of cell depending on verdict |
| 212 | + filename_data = filename_data.style.map(color_formatter) |
| 213 | + fields_data = fields_data.style.map(color_formatter) |
| 214 | + # Then write to html file |
| 215 | + filename_data.to_html( |
| 216 | + ouput_filename1, |
| 217 | + header=True, |
| 218 | + ) |
| 219 | + fields_data.to_html( |
| 220 | + ouput_filename2, |
| 221 | + header=True, |
| 222 | + ) |
| 223 | + files_written = [ouput_filename1, ouput_filename2] |
| 224 | + |
| 225 | + else: |
| 226 | + # No alternate format - only DB file |
| 227 | + files_written = [self.db_file] |
| 228 | + |
| 229 | + return files_written |
| 230 | + |
| 231 | + |
| 232 | +def color_formatter(value: str) -> str: |
| 233 | + """Color mapping for use in write_to_alternate_format(). |
| 234 | + Color-codes table cells based on verdict: green for "PASS", red for "FAIL", etc. |
| 235 | +
|
| 236 | + Parameters |
| 237 | + ------ |
| 238 | + value: str |
| 239 | +
|
| 240 | + """ |
| 241 | + if str(value).upper() == "PASS": |
| 242 | + color = "lightgreen" |
| 243 | + elif str(value).upper() == "FAIL": |
| 244 | + color = "red" |
| 245 | + elif str(value).upper() == "NEEDS REVIEW": |
| 246 | + color = "yellow" |
| 247 | + else: |
| 248 | + color = None |
| 249 | + return "background-color: %s" % color |
0 commit comments