16
16
.. module:: model
17
17
"""
18
18
19
- from .util import read_kwargs
20
19
from .star import Star
21
20
22
21
@@ -204,29 +203,37 @@ def read(cls, fits, extname):
204
203
205
204
:returns: a model built with a information in the FITS file.
206
205
"""
207
- assert extname in fits
208
- assert 'type' in fits [extname ].get_colnames ()
209
- model_type = fits [extname ].read ()['type' ]
210
- assert len (model_type ) == 1
211
- try :
212
- model_type = str (model_type [0 ].decode ())
213
- except AttributeError :
214
- # fitsio 1.0 returns strings
215
- model_type = model_type [0 ]
206
+ from .readers import FitsReader
207
+ return cls ._read (FitsReader (fits , None ), extname )
208
+
209
+ @classmethod
210
+ def _read (cls , reader , name ):
211
+ """Read a Model from a FITS file.
212
+
213
+ Note: the returned Model will not have its parameters set. This just initializes a fresh
214
+ model that can be used to interpret interpolated vectors.
215
+
216
+ :param reader: A reader object that encapsulates the serialization format.
217
+ :param name: Name associated with this model in the serialized output.
218
+
219
+ :returns: a model built with a information in the FITS file
220
+ """
221
+ kwargs = reader .read_struct (name )
222
+ assert kwargs is not None
223
+ assert 'type' in kwargs
224
+ model_type = kwargs .pop ('type' )
216
225
217
226
# Check that model_type is a valid Model type.
218
227
if model_type not in Model .valid_model_types :
219
228
raise ValueError ("model type %s is not a valid Piff Model" % model_type )
220
229
model_cls = Model .valid_model_types [model_type ]
221
230
222
- kwargs = read_kwargs (fits , extname )
223
- kwargs .pop ('type' ,None )
224
231
if 'force_model_center' in kwargs : # pragma: no cover
225
232
# old version of this parameter name.
226
233
kwargs ['centered' ] = kwargs .pop ('force_model_center' )
227
234
model_cls ._fix_kwargs (kwargs )
228
235
model = model_cls (** kwargs )
229
- model ._finish_read (fits , extname )
236
+ model ._finish_read (reader )
230
237
return model
231
238
232
239
@classmethod
@@ -243,14 +250,13 @@ def _fix_kwargs(cls, kwargs):
243
250
"""
244
251
pass
245
252
246
- def _finish_read (self , fits , extname ):
253
+ def _finish_read (self , reader ):
247
254
"""Finish the reading process with any class-specific steps.
248
255
249
256
The base class implementation doesn't do anything, which is often appropriate, but
250
257
this hook exists in case any Model classes need to read extra information from the
251
258
fits file.
252
259
253
- :param fits: An open fitsio.FITS object.
254
- :param extname: The base name of the extension.
260
+ :param reader: A reader object that encapsulates the serialization format.
255
261
"""
256
262
pass
0 commit comments