@@ -53,70 +53,79 @@ def process(self, input_data):
5353 SlitModel or MultiSlitModel
5454 The resampled output, one slit per source.
5555 """
56- with datamodels .open (input_data ) as input_new :
57- # Check if input_new is a MultiSlitModel
58- model_is_msm = isinstance (input_new , MultiSlitModel )
59-
60- # If input is a 3D rateints MultiSlitModel (unsupported) skip the step
61- if model_is_msm and len ((input_new [0 ]).shape ) == 3 :
62- log .warning ("Resample spec step will be skipped" )
63- result = input_new .copy ()
64- result .meta .cal_step .resample = "SKIPPED"
65-
66- return result
67-
68- # Convert ImageModel to SlitModel (needed for MIRI LRS)
69- if isinstance (input_new , ImageModel ):
70- input_new = datamodels .SlitModel (input_new )
71-
72- if isinstance (input_new , ModelContainer ):
73- input_models = input_new
74-
75- try :
76- output = input_models .meta .asn_table .products [0 ].name
77- except AttributeError :
78- # NIRSpec MOS data goes through this path, as the container
79- # is only ModelContainer-like, and doesn't have an asn_table
80- # attribute attached. Output name handling gets done in
81- # _process_multislit() via the update method
82- # TODO: the container-like object should retain asn_table
83- output = None
84- else :
85- input_models = ModelContainer ([input_new ])
86- output = input_new .meta .filename
87- self .blendheaders = False
56+ output_model = self .prepare_output (input_data )
8857
89- # Setup drizzle-related parameters
90- kwargs = self .get_drizpars ()
91- kwargs ["output" ] = output
92- self .drizpars = kwargs
58+ # Check if input model is a MultiSlitModel
59+ model_is_msm = isinstance (output_model , MultiSlitModel )
9360
94- # Call resampling
95- if isinstance (input_models [0 ], MultiSlitModel ):
96- result = self ._process_multislit (input_models )
61+ # If input is a 3D rateints MultiSlitModel (unsupported) skip the step
62+ if model_is_msm and len ((output_model [0 ]).shape ) == 3 :
63+ log .warning ("Resample spec step will be skipped" )
64+ output_model .meta .cal_step .resample = "SKIPPED"
65+ return output_model
9766
98- elif len (input_models [0 ].data .shape ) != 2 :
99- # resample can only handle 2D images, not 3D cubes, etc
100- raise TypeError (f"Input { input_models [0 ]} is not a 2D image." )
67+ # Convert ImageModel to SlitModel (may be needed for older MIRI LRS data)
68+ if isinstance (output_model , ImageModel ):
69+ slit_model = datamodels .SlitModel (output_model )
70+ if output_model is not input_data :
71+ output_model .close ()
72+ output_model = slit_model
10173
102- else :
103- # result is a SlitModel
104- result = self ._process_slit (input_models )
105-
106- # Update ASNTABLE in output
107- result .meta .cal_step .resample = "COMPLETE"
108- result .meta .asn .table_name = input_models [0 ].meta .asn .table_name
109- result .meta .asn .pool_name = input_models [0 ].meta .asn .pool_name
110-
111- # populate the result wavelength attribute for MultiSlitModel
112- if isinstance (result , MultiSlitModel ):
113- for slit_idx , _slit in enumerate (result .slits ):
114- wl_array = get_wavelengths (result .slits [slit_idx ])
115- result .slits [slit_idx ].wavelength = wl_array
116- else :
117- # populate the result wavelength attribute for SlitModel
118- wl_array = get_wavelengths (result )
119- result .wavelength = wl_array
74+ if isinstance (output_model , ModelContainer ):
75+ input_models = output_model
76+
77+ try :
78+ output = input_models .meta .asn_table .products [0 ].name
79+ except AttributeError :
80+ # NIRSpec MOS data goes through this path, as the container
81+ # is only ModelContainer-like, and doesn't have an asn_table
82+ # attribute attached. Output name handling gets done in
83+ # _process_multislit() via the update method
84+ # TODO: the container-like object should retain asn_table
85+ output = None
86+ else :
87+ input_models = ModelContainer ([output_model ])
88+ output = output_model .meta .filename
89+ self .blendheaders = False
90+
91+ # Setup drizzle-related parameters
92+ kwargs = self .get_drizpars ()
93+ kwargs ["output" ] = output
94+ self .drizpars = kwargs
95+
96+ # Call resampling
97+ if isinstance (input_models [0 ], MultiSlitModel ):
98+ result = self ._process_multislit (input_models )
99+
100+ elif len (input_models [0 ].data .shape ) != 2 :
101+ # resample can only handle 2D images, not 3D cubes, etc
102+ raise TypeError (f"Input { input_models [0 ]} is not a 2D image." )
103+
104+ else :
105+ # result is a SlitModel
106+ result = self ._process_slit (input_models )
107+
108+ # Update ASNTABLE in output
109+ result .meta .cal_step .resample = "COMPLETE"
110+ result .meta .asn .table_name = input_models [0 ].meta .asn .table_name
111+ result .meta .asn .pool_name = input_models [0 ].meta .asn .pool_name
112+
113+ # populate the result wavelength attribute for MultiSlitModel
114+ if isinstance (result , MultiSlitModel ):
115+ for slit_idx , _slit in enumerate (result .slits ):
116+ wl_array = get_wavelengths (result .slits [slit_idx ])
117+ result .slits [slit_idx ].wavelength = wl_array
118+ else :
119+ # populate the result wavelength attribute for SlitModel
120+ wl_array = get_wavelengths (result )
121+ result .wavelength = wl_array
122+
123+ # Output is a new datamodel.
124+ # Clean up the input model(s) if they were opened here.
125+ if output_model is not input_data :
126+ del output_model
127+ if input_models is not input_data :
128+ del input_models
120129
121130 return result
122131
0 commit comments