#NEWLINE# @ classmethod #NEWLINE# def from_columns( cls , columns , nrows = 0 , fill = False ) : #NEWLINE# #INDENT# '\n        Given a `ColDefs` object of unknown origin, initialize a new `FITS_rec`\n        object.\n\n        .. note::\n\n            This was originally part of the `new_table` function in the table\n            module but was moved into a class method since most of its\n            functionality always had more to do with initializing a `FITS_rec`\n            object than anything else, and much of it also overlapped with\n            ``FITS_rec._scale_back``.\n\n        Parameters\n        ----------\n        columns : sequence of `Column` or a `ColDefs`\n            The columns from which to create the table data.  If these\n            columns have data arrays attached that data may be used in\n            initializing the new table.  Otherwise the input columns\n            will be used as a template for a new table with the requested\n            number of rows.\n\n        nrows : int\n            Number of rows in the new table.  If the input columns have data\n            associated with them, the size of the largest input column is used.\n            Otherwise the default is 0.\n\n        fill : bool\n            If `True`, will fill all cells with zeros or blanks.  If\n            `False`, copy the data from input, undefined cells will still\n            be filled with zeros/blanks.\n        ' #NEWLINE# if ( not isinstance ( columns , ColDefs ) ) : #NEWLINE# #INDENT# columns = ColDefs ( columns ) #NEWLINE# #UNINDENT# for column in columns : #NEWLINE# #INDENT# arr = column . array #NEWLINE# if isinstance ( arr , Delayed ) : #NEWLINE# #INDENT# if ( arr . hdu . data is None ) : #NEWLINE# #INDENT# column . array = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# column . array = _get_recarray_field ( arr . hdu . data , arr . field ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# del columns . _arrays #NEWLINE# if ( nrows == 0 ) : #NEWLINE# #INDENT# for arr in columns . _arrays : #NEWLINE# #INDENT# if ( arr is not None ) : #NEWLINE# #INDENT# dim = arr . shape [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dim = 0 #NEWLINE# #UNINDENT# if ( dim > nrows ) : #NEWLINE# #INDENT# nrows = dim #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# raw_data = np . empty ( ( columns . dtype . itemsize * nrows ) , dtype = np . uint8 ) #NEWLINE# raw_data . fill ( ord ( columns . _padding_byte ) ) #NEWLINE# data = np . recarray ( nrows , dtype = columns . dtype , buf = raw_data ) . view ( cls ) #NEWLINE# columns . _add_listener ( data ) #NEWLINE# data . _coldefs = columns #NEWLINE# if fill : #NEWLINE# #INDENT# return data #NEWLINE# #UNINDENT# for ( idx , column ) in enumerate ( columns ) : #NEWLINE# #INDENT# arr = column . array #NEWLINE# if ( arr is None ) : #NEWLINE# #INDENT# array_size = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# array_size = len ( arr ) #NEWLINE# #UNINDENT# n = min ( array_size , nrows ) #NEWLINE# if ( not n ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# field = _get_recarray_field ( data , idx ) #NEWLINE# name = column . name #NEWLINE# fitsformat = column . format #NEWLINE# recformat = fitsformat . recformat #NEWLINE# outarr = field [ : n ] #NEWLINE# inarr = arr [ : n ] #NEWLINE# if isinstance ( recformat , _FormatX ) : #NEWLINE# #INDENT# if ( inarr . shape [ ( - 1 ) ] == recformat . repeat ) : #NEWLINE# #INDENT# _wrapx ( inarr , outarr , recformat . repeat ) #NEWLINE# continue #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( recformat , _FormatP ) : #NEWLINE# #INDENT# data . _cache_field ( name , _makep ( inarr , columns , recformat , nrows = nrows ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( recformat [ ( - 2 ) : ] == FITS2NUMPY [ 'L' ] ) and ( inarr . dtype == bool ) ) : #NEWLINE# #INDENT# field [ : ] = ord ( 'F' ) #NEWLINE# converted = np . zeros ( field . shape , dtype = bool ) #NEWLINE# converted [ : n ] = inarr #NEWLINE# data . _cache_field ( name , converted ) #NEWLINE# inarr = np . where ( ( inarr == False ) , ord ( 'F' ) , ord ( 'T' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( columns [ idx ] . _physical_values and columns [ idx ] . _pseudo_unsigned_ints ) : #NEWLINE# #INDENT# bzero = column . bzero #NEWLINE# converted = np . zeros ( field . shape , dtype = inarr . dtype ) #NEWLINE# converted [ : n ] = inarr #NEWLINE# data . _cache_field ( name , converted ) #NEWLINE# if ( n < nrows ) : #NEWLINE# #INDENT# field [ n : ] = ( - bzero ) #NEWLINE# #UNINDENT# inarr = ( inarr - bzero ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( columns , _AsciiColDefs ) : #NEWLINE# #INDENT# if fitsformat . _pseudo_logical : #NEWLINE# #INDENT# outarr = field . view ( np . uint8 , np . ndarray ) [ : n ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( arr . dtype . kind not in ( 'S' , 'U' ) ) : #NEWLINE# #INDENT# data . _cache_field ( name , np . zeros ( nrows , dtype = arr . dtype ) ) #NEWLINE# outarr = data . _converted [ name ] [ : n ] #NEWLINE# #UNINDENT# #UNINDENT# outarr [ : ] = inarr #NEWLINE# continue #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( inarr . shape != outarr . shape ) : #NEWLINE# #INDENT# if ( ( inarr . dtype . kind == outarr . dtype . kind ) and ( inarr . dtype . kind in ( 'U' , 'S' ) ) and ( inarr . dtype != outarr . dtype ) ) : #NEWLINE# #INDENT# inarr_rowsize = inarr [ 0 ] . size #NEWLINE# inarr = inarr . flatten ( ) . view ( outarr . dtype ) #NEWLINE# #UNINDENT# if ( outarr . ndim > 1 ) : #NEWLINE# #INDENT# inarr_rowsize = inarr [ 0 ] . size #NEWLINE# inarr = inarr . reshape ( ( n , inarr_rowsize ) ) #NEWLINE# outarr [ : , : inarr_rowsize ] = inarr #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# outarr [ : n ] = inarr . ravel ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# outarr [ : ] = inarr #NEWLINE# #UNINDENT# #UNINDENT# for idx in range ( len ( columns ) ) : #NEWLINE# #INDENT# columns . _arrays [ idx ] = data . field ( idx ) #NEWLINE# #UNINDENT# return data