from__future__importannotationsimportjsonfromtypingimportTYPE_CHECKINGimportnumpyasnpfromqcodes.dataset.measurementsimportDataSaver,MeasurementifTYPE_CHECKING:frompathlibimportPathfromqcodes_loop.data.data_arrayimportDataArrayfromqcodes_loop.data.data_setimportDataSetasOldDataSetfromqcodes.dataset.experiment_containerimportExperimentdefsetup_measurement(dataset:OldDataSet,exp:Experiment|None=None)->Measurement:""" Register parameters for all :class:.`DataArrays` in a given QCoDeS legacy dataset This tries to infer the `name`, `label` and `unit` along with any `setpoints` for the given array. Args: dataset: Legacy dataset to register parameters from. exp: experiment that the legacy dataset should be bound to. If None the default experiment is used. See the docs of :class:`qcodes.dataset.Measurement` for more details. """meas=Measurement(exp=exp)forarrayname,arrayindataset.arrays.items():ifarray.is_setpoint:setarrays=Noneelse:setarrays=[setarray.array_idforsetarrayinarray.set_arrays]meas.register_custom_parameter(name=array.array_id,label=array.label,unit=array.unit,setpoints=setarrays)returnmeasdefstore_array_to_database(datasaver:DataSaver,array:DataArray)->int:assertarray.shapeisnotNonedims=len(array.shape)assertarray.array_idisnotNoneifdims==2:forindex1,iinenumerate(array.set_arrays[0]):forindex2,jinenumerate(array.set_arrays[1][index1]):datasaver.add_result((array.set_arrays[0].array_id,i),(array.set_arrays[1].array_id,j),(array.array_id,array[index1,index2]),)elifdims==1:forindex,iinenumerate(array.set_arrays[0]):datasaver.add_result((array.set_arrays[0].array_id,i),(array.array_id,array[index]))else:raiseNotImplementedError("The exporter only currently handles 1 and 2 Dimensional data")returndatasaver.run_iddefstore_array_to_database_alt(meas:Measurement,array:DataArray)->int:assertarray.shapeisnotNonedims=len(array.shape)assertarray.array_idisnotNoneifdims==2:outer_data=np.empty(array.shape[1]# pyright: ignore[reportGeneralTypeIssues])withmeas.run()asdatasaver:forindex1,iinenumerate(array.set_arrays[0]):outer_data[:]=idatasaver.add_result((array.set_arrays[0].array_id,outer_data),(array.set_arrays[1].array_id,array.set_arrays[1][index1,:]),(array.array_id,array[index1,:]),)elifdims==1:withmeas.run()asdatasaver:forindex,iinenumerate(array.set_arrays[0]):datasaver.add_result((array.set_arrays[0].array_id,i),(array.array_id,array[index]))else:raiseNotImplementedError("The exporter only currently handles 1 and 2 Dimensional data")returndatasaver.run_id
[docs]defimport_dat_file(location:str|Path,exp:Experiment|None=None)->list[int]:""" This imports a QCoDeS legacy :class:`qcodes.data.data_set.DataSet` into the database. Args: location: Path to file containing legacy dataset exp: Specify the experiment to store data to. If None the default one is used. See the docs of :class:`qcodes.dataset.Measurement` for more details. """try:fromqcodes_loop.data.data_setimportload_dataexceptImportErrorase:raiseImportError("The legacy importer requires qcodes_loop to be installed.")fromeloaded_data=load_data(str(location))meas=setup_measurement(loaded_data,exp=exp)run_ids=[]withmeas.run()asdatasaver:datasaver.dataset.add_metadata("snapshot",json.dumps(loaded_data.snapshot()))forarrayname,arrayinloaded_data.arrays.items():ifnotarray.is_setpoint:run_id=store_array_to_database(datasaver,array)run_ids.append(run_id)returnrun_ids