@@ -214,53 +214,51 @@ def read_moon_obs(
214214 Generated MoonObservation from the given datafile
215215 """
216216 try :
217- ds = xr_open_dataset (path , mask_limits = {"sat_pos " : False })
217+ ds = xr_open_dataset (path , mask_limits = {"irr_obs" : True , "__default__ " : False })
218218 _validate_schema_regular_moonobs (ds )
219- n_channels = len (ds ["channel_name" ])
220- ch_names = []
221- ch_irrs = {}
222- for i in range (n_channels ):
223- ch_name = str (ds ["channel_name" ][i ].values , "utf-8" ).rstrip ("\x00 " )
224- ch_names .append (ch_name )
219+ ch_names = np .char .rstrip (
220+ np .char .decode (ds ["channel_name" ].to_numpy (), "utf-8" ), "\x00 "
221+ )
225222 dt = ds ["date" ].values [0 ]
226223 if np .issubdtype (dt .dtype , np .datetime64 ):
227- dt = dt .astype ("datetime64[us]" ).astype (datetime )
228- dt = dt .replace (tzinfo = timezone .utc )
224+ dt = (
225+ dt .astype ("datetime64[us]" )
226+ .astype (datetime )
227+ .replace (tzinfo = timezone .utc )
228+ )
229229 else :
230230 dt = datetime .fromtimestamp (dt , tz = timezone .utc )
231- sat_pos_ref = ds ["sat_pos_ref" ].values
232- if str (sat_pos_ref .dtype .str ).startswith ("|S" ):
233- sat_pos_ref = str (sat_pos_ref , "utf-8" )
231+ sat_pos_ref = ds ["sat_pos_ref" ].to_numpy ()
232+ if isinstance (sat_pos_ref , np .ndarray ):
233+ sat_pos_ref = sat_pos_ref .item ()
234+ if isinstance (sat_pos_ref , (bytes , bytearray )):
235+ sat_pos_ref = sat_pos_ref .decode ("utf-8" , errors = "replace" )
234236 else :
235- sat_pos_ref = sat_pos_ref [ 0 ]
236- sat_pos_units : str = ds ["sat_pos" ].units
237- d_to_m = get_length_conversion_factor ( sat_pos_units , "m" )
238- to_correct_distance = False
239- if not np . isnan (ds ["sat_pos" ].values ). any ():
240- sat_pos = SatellitePosition (* list ( ds [ "sat_pos" ]. values * d_to_m ) )
237+ sat_pos_ref = str ( sat_pos_ref )
238+ sat_pos_vals = ds ["sat_pos" ].to_numpy ()
239+ has_sat_pos = ~ np . isnan ( sat_pos_vals ). any ( )
240+ if has_sat_pos :
241+ d_to_m = get_length_conversion_factor (ds ["sat_pos" ].units , "m" )
242+ sat_pos = SatellitePosition (* sat_pos_vals * d_to_m )
241243 md = None
244+ corr_dist = False
242245 else :
243246 sat_pos = None
244247 md = _get_moondata_from_moon_obs (ds , dt , kernels_path , eocfi_path )
245- if "to_correct_distance" in ds .attrs and int (ds .to_correct_distance ) == 1 :
246- to_correct_distance = True
247- irr_obs = ds ["irr_obs" ].values
248- irr_obs_units : str = ds ["irr_obs" ].units
249- d_to_nm = _calc_divisor_to_nm (irr_obs_units )
250- if to_correct_distance :
251- irr_obs = (
252- irr_obs
253- * ((1 / md .distance_sun_moon ) ** 2 )
254- * (DIST_EARTH_MOON_KM / md .distance_observer_moon ) ** 2
255- )
256- irr_obs = irr_obs / d_to_nm
257- for i , ch_irr in enumerate (irr_obs ):
258- if not np .isnan (ch_irr ):
259- ch_irrs [ch_names [i ]] = float (ch_irr )
260- data_source = ds .data_source
248+ corr_dist = bool (int (ds .attrs .get ("to_correct_distance" , 0 )))
249+ irr_obs = ds ["irr_obs" ].to_numpy ()
250+ d_to_nm = _calc_divisor_to_nm (ds ["irr_obs" ].units )
251+ data_source = ds .attrs .get ("data_source" )
261252 ds .close ()
253+ if corr_dist :
254+ irr_obs *= (1 / md .distance_sun_moon ) ** 2 * (
255+ DIST_EARTH_MOON_KM / md .distance_observer_moon
256+ ) ** 2
257+ irr_obs /= d_to_nm
258+ valid = ~ np .isnan (irr_obs )
259+ ch_irrs = dict (zip (ch_names [valid ], irr_obs [valid ].astype (float )))
262260 return LunarObservation (
263- ch_names , sat_pos_ref , ch_irrs , dt , sat_pos , data_source , md
261+ list ( ch_names ) , sat_pos_ref , ch_irrs , dt , sat_pos , data_source , md
264262 )
265263 except SchemaError as e :
266264 raise e
0 commit comments