def write(self, file_name) : """Write stored data to file. Take all the data stored in the Writer (from added DataBlocks) and write it to a fits file with the passed file name. """ # Add the data Col = pyfits.Column(name='DATA', format=self.data_format, array=self.data) columns = [Col,] # Add all the other stored fields. for field_name in self.field.iterkeys() : Col = pyfits.Column(name=field_name, format=self.formats[field_name], array=self.field[field_name]) columns.append(Col) coldefs = pyfits.ColDefs(columns) # Creat fits header data units, one for the table and the mandatory # primary. tbhdu = pyfits.new_table(coldefs) prihdu = pyfits.PrimaryHDU() # Add the write history. fname_abbr = ku.abbreviate_file_path(file_name) self.history.add('Written to file.', ('File name: ' + fname_abbr,)) # Add the history to the header. bf.write_history_header(prihdu.header, self.history) # Combine the HDUs and write to file. hdulist = pyfits.HDUList([prihdu, tbhdu]) hdulist.writeto(file_name, clobber=True) if self.feedback > 0 : print 'Wrote data to file: ' + fname_abbr
def write(Maps, file_name, feedback=2): """Write a map to fits file. Map should be a map_data.MapData object. """ # If only a single Map was passed, make it iterable. if not hasattr(Maps, '__iter__'): Maps = (Maps, ) # First create a primary with the history and such: prihdu = pyfits.PrimaryHDU() # Add history to the primary. fname_abbr = ku.abbreviate_file_path(file_name) # Add final history entry and store in the primary header. history = base_data.merge_histories(*Maps) history.add('Written to file.', 'File name: ' + fname_abbr) bf.write_history_header(prihdu.header, history) list_of_hdus = [prihdu] for ii, Map in enumerate(Maps): # Creat an image HDU. map = sp.array(ma.filled(Map.data, float('nan'))) imhdu = pyfits.ImageHDU(sp.swapaxes(map, 0, 2), name='MAP%d' % ii) # Add extra data to the HDU for key in Map.field.iterkeys(): if Map.field_axes[key] != (): raise ce.DataError('Only 0D data can be written to a Fits Map ' 'Header.') card = pyfits.Card(key, Map.field[key].item()) imhdu.header.ascardlist().append(card) list_of_hdus.append(imhdu) # Creat the HDU list and write to file. hdulist = pyfits.HDUList(list_of_hdus) hdulist.writeto(file_name, clobber=True) if feedback > 0: print 'Wrote data to file: ' + fname_abbr
def write(Maps, file_name, feedback=2): """Write a map to fits file. Map should be a map_data.MapData object. """ # If only a single Map was passed, make it iterable. if not hasattr(Maps, "__iter__"): Maps = (Maps,) # First create a primary with the history and such: prihdu = pyfits.PrimaryHDU() # Add history to the primary. fname_abbr = ku.abbreviate_file_path(file_name) # Add final history entry and store in the primary header. history = base_data.merge_histories(*Maps) history.add("Written to file.", "File name: " + fname_abbr) bf.write_history_header(prihdu.header, history) list_of_hdus = [prihdu] for ii, Map in enumerate(Maps): # Creat an image HDU. map = sp.array(ma.filled(Map.data, float("nan"))) imhdu = pyfits.ImageHDU(sp.swapaxes(map, 0, 2), name="MAP%d" % ii) # Add extra data to the HDU for key in Map.field.iterkeys(): if Map.field_axes[key] != (): raise ce.DataError("Only 0D data can be written to a Fits Map " "Header.") card = pyfits.Card(key, Map.field[key].item()) imhdu.header.ascardlist().append(card) list_of_hdus.append(imhdu) # Creat the HDU list and write to file. hdulist = pyfits.HDUList(list_of_hdus) hdulist.writeto(file_name, clobber=True) if feedback > 0: print "Wrote data to file: " + fname_abbr