function D = ims1280binary(fname)
% function D = ims1280binary(fname);



fid=fopen(fname);
if fid<0
    disp(['Cannot open ' fname])
    D=[];
    return
end
 
analysis_type=fread(fid,1,'int32',0,'b');
x_sample=fread(fid,1,'double',0,'b');
y_sample=fread(fid,1,'double',0,'b');
related_files_size=fread(fid,1,'uint32',0,'b'); % 4 bytes
related_files=cell(related_files_size,1);
for ii=1:related_files_size
    rfsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
    related_files{ii}=char(fread(fid,rfsz,'char',0,'b'))';
end
depth_profile.time_per_cycle=fread(fid,1,'uint16',0,'b');
depth_profile.number_of_cycles=fread(fid,1,'int32',0,'b');
depth_profile.analysis_time=fread(fid,1,'int32',0,'b');
isotope.time_per_cycle=fread(fid,1,'uint16',0,'b');
isotope.number_of_cycles=fread(fid,1,'int32',0,'b');
isotope.analysis_time=fread(fid,1,'int32',0,'b');
isotope.bloc_size=fread(fid,1,'int32',0,'b');
isotope.rejection=fread(fid,1,'int32',0,'b');
linescan.sample_beam=fread(fid,1,'uint16',0,'b');
linescan.xmin=fread(fid,1,'double',0,'b');
linescan.ymin=fread(fid,1,'double',0,'b');
linescan.xmax=fread(fid,1,'double',0,'b');
linescan.ymax=fread(fid,1,'double',0,'b');
linescan.number_of_steps=fread(fid,1,'int32',0,'b');
linescan.step_waiting_time=fread(fid,1,'double',0,'b');
linescan.analysis_time=fread(fid,1,'int32',0,'b');
linescan.stepvalue=fread(fid,1,'int32',0,'b');
linescan.mode=fread(fid,1,'int32',0,'b');
energy.slit=fread(fid,1,'uint16',0,'b');
energy.emin=fread(fid,1,'double',0,'b');
energy.emax=fread(fid,1,'double',0,'b');
energy.number_of_steps=fread(fid,1,'int32',0,'b');
energy.analysis_time=fread(fid,1,'int32',0,'b');
energy.egap=fread(fid,1,'double',0,'b');
spectrum.analysis_time=fread(fid,1,'double',0,'b');
spectrum.waiting_time=fread(fid,1,'double',0,'b');
spectrum.number_dM=fread(fid,1,'double',0,'b');
spectrum.number_measurement=fread(fid,1,'double',0,'b');
spectrum.step_resolution=fread(fid,1,'double',0,'b');
generalflag.presputtering=fread(fid,1,'int8',0,'b');
generalflag.referencesignal=fread(fid,1,'int8',0,'b');
generalflag.masscalibration=fread(fid,1,'int8',0,'b');
generalflag.hvsample=fread(fid,1,'int8',0,'b');
generalflag.overlappingcrater=fread(fid,1,'int8',0,'b');
generalflag.beamblanking=fread(fid,1,'int8',0,'b');
generalflag.emdriftcorrection=fread(fid,1,'int8',0,'b');
generalflag.beamcentering=fread(fid,1,'int8',0,'b');
scanparameter.mode=fread(fid,1,'uint16',0,'b');
scanparameter.valuemin=fread(fid,1,'double',0,'b');
scanparameter.valuemax=fread(fid,1,'double',0,'b');
scanparameter.number_of_steps=fread(fid,1,'int32',0,'b');
scanparameter.analysistime=fread(fid,1,'int32',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
scanparameter.parameterstring=fread(fid,strsz,'*char',0,'b')';
image.scanningrae=fread(fid,1,'uint16',0,'b');
image.number_of_cycles=fread(fid,1,'int32',0,'b');
image.analysistime=fread(fid,1,'int32',0,'b');
image.rastersizemicrons=fread(fid,1,'int32',0,'b');
commonparameters.waitingtime=fread(fid,1,'double',0,'b');
commonparameters.resolution=fread(fid,1,'double',0,'b');
commonparameters.dM=fread(fid,1,'double',0,'b');
arraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if arraysize>0
for ii=1:arraysize
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
massdescriptionmono(ii).symbol=fread(fid,strsz,'*char',0,'b')';
massdescriptionmono(ii).amu=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).matrix=fread(fid,1,'int8',0,'b');
massdescriptionmono(ii).detector=fread(fid,1,'int32',0,'b');
massdescriptionmono(ii).waitingtime=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).countingtime=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).offset=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).resolution=fread(fid,1,'double',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
massdescriptionmono(ii).symbol_h=fread(fid,strsz,'*char',0,'b')';
massdescriptionmono(ii).amu_h=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).dM_minus=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).dM_plus=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).Bfield_22bit=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).Bfield_MSB=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).Bfield_LSB=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).trolley_position=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).mono_multi=fread(fid,1,'int8',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
massdescriptionmono(ii).refmass=fread(fid,strsz,'*char',0,'b')';
massdescriptionmono(ii).refBfield_22bit=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).refBfield_MSB=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).refBfield_LSB=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).Bfield_RMN=fread(fid,1,'uint32',0,'b');
massdescriptionmono(ii).RMN=fread(fid,1,'int8',0,'b');
massdescriptionmono(ii).option1=fread(fid,1,'double',0,'b');
massdescriptionmono(ii).correction_em_drift=fread(fid,1,'int8',0,'b');
end
end
dataarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if dataarraysize>0
for ii=1:dataarraysize
xarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
 if xarraysize>0
datavalueX{ii,:}=fread(fid,xarraysize,'double',0,'b');
end
yarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if yarraysize>0
datavalueY{ii,:}=fread(fid,yarraysize,'double',0,'b');
end
end
end
data.valueX=datavalueX;
data.valueY=datavalueY;
externalconditions.presputtering_sputtertime=fread(fid,1,'double',0,'b');
externalconditions.presputtering_rastersizestart=fread(fid,1,'double',0,'b');
externalconditions.presputtering_rastersizeend=fread(fid,1,'double',0,'b');
externalconditions.presputtering_prim_intensity_start=fread(fid,1,'uint8',0,'b');
externalconditions.presputtering_prim_intensity_end=fread(fid,1,'uint8',0,'b');
externalconditions.presputtering_FC_offsets_measurement_boolean=fread(fid,1,'int8',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
externalconditions.referencesignal_symbol=fread(fid,strsz,'*char',0,'b')';
externalconditions.referencesignal_offset=fread(fid,1,'double',0,'b');
externalconditions.referencesignal_detector=fread(fid,1,'int32',0,'b');
externalconditions.referencesignal_mass=fread(fid,1,'double',0,'b');
externalconditions.masscalibration_period=fread(fid,1,'int32',0,'b');
arraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if arraysize>0
externalconditions.masscalibration_masslist=fread(fid,arraysize,'int32',0,'b');
end
externalconditions.masscalibration_mode=fread(fid,1,'uint16',0,'b'); % Mass recalibration by .., peak centering, correlation, peak shift
externalconditions.masscalibration_applycyclenumber=fread(fid,1,'int32',0,'b');
externalconditions.masscalibration_numberofmeasurementsperdM=fread(fid,1,'uint32',0,'b');
externalconditions.masscalibration_massrange=fread(fid,1,'double',0,'b');
externalconditions.masscalibration_cyclenumber=fread(fid,1,'uint32',0,'b');
externalconditions.masscalibration_countingtime=fread(fid,1,'double',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
externalconditions.hvsample_symbol=fread(fid,strsz,'*char',0,'b')';
externalconditions.hvsample_numberitem=fread(fid,1,'int32',0,'b');
externalconditions.hvsample_applycyclenumber=fread(fid,1,'int32',0,'b');
externalconditions.hvsample_period=fread(fid,1,'int32',0,'b');
externalconditions.hvsample_emin=fread(fid,1,'double',0,'b');
externalconditions.hvsample_emax=fread(fid,1,'double',0,'b');
externalconditions.hvsample_estep=fread(fid,1,'double',0,'b');
externalconditions.hvsample_eband=fread(fid,1,'double',0,'b');
externalconditions.hvsample_countingtime=fread(fid,1,'double',0,'b');
externalconditions.hvsample_egap=fread(fid,1,'double',0,'b');
externalconditions.hvsample_mode=fread(fid,1,'uint16',0,'b');
externalconditions.overlappingcrater_xsize=fread(fid,1,'double',0,'b');
externalconditions.overlappingcrater_ysize=fread(fid,1,'double',0,'b');
externalconditions.hvsample_applycyclenumber=fread(fid,1,'int32',0,'b');
externalconditions.em_drift_period=fread(fid,1,'int32',0,'b');
arraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if arraysize>0
externalconditions.em_drift_masslist=fread(fid,arraysize,'int32',0,'b');
end
externalconditions.em_drift_applycyclenumber=fread(fid,1,'int32',0,'b');
externalconditions.em_drift_cyclenumber=fread(fid,1,'uint32',0,'b');
externalconditions.em_drift_countingtime=fread(fid,1,'double',0,'b');
externalconditions.em_drift_param1=fread(fid,1,'double',0,'b');
externalconditions.em_drift_param2=fread(fid,1,'double',0,'b');
externalconditions.em_drift_param3=fread(fid,1,'double',0,'b');
externalconditions.beamcentering_period=fread(fid,1,'uint32',0,'b');
arraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if arraysize>0
externalconditions.beamcentering_masslist=fread(fid,arraysize,'int32',0,'b');
end
externalconditions.beamcentering_apply_at_cycle=fread(fid,1,'uint32',0,'b');
externalconditions.beamcentering_cyclenumber=fread(fid,1,'uint32',0,'b');
externalconditions.beamcentering_countingtime=fread(fid,1,'double',0,'b');
externalconditions.beamcentering_waitingtime=fread(fid,1,'double',0,'b');
externalconditions.beamcentering_mode=fread(fid,1,'int32',0,'b');
externalconditions.beamcentering_energyslits_bool=fread(fid,1,'int8',0,'b');
externalconditions.beamcentering_contrastaperture_bool=fread(fid,1,'int8',0,'b');
externalconditions.beamcentering_entranceslits_bool=fread(fid,1,'int8',0,'b');
externalconditions.beamcentering_fieldapp_bool=fread(fid,1,'int8',0,'b');
externalconditions.beamcentering_DeltaToScan_EnS=fread(fid,1,'int32',0,'b');
externalconditions.beamcentering_DeltaToScan_CA=fread(fid,1,'int32',0,'b');
externalconditions.beamcentering_DeltaToScan_ES=fread(fid,1,'int32',0,'b');
externalconditions.beamcentering_DeltaToScan_FA=fread(fid,1,'int32',0,'b');
arraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if arraysize>0
for ii=1:arraysize
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
Ratio_1{ii}=fread(fid,strsz,'*char',0,'b')';
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
Ratio_2{ii}=fread(fid,strsz,'*char',0,'b')';
end
end
detectionmode_bool=fread(fid,1,'int8',0,'b');
arraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if arraysize>0
for ii=1:arraysize
mass_scan_parameters(ii).ANALYSIS_TIME=fread(fid,1,'double',0,'b');
mass_scan_parameters(ii).number_DM=fread(fid,1,'double',0,'b');
mass_scan_parameters(ii).number_Measurement=fread(fid,1,'double',0,'b');
mass_scan_parameters(ii).Counting_time_seconds=fread(fid,1,'double',0,'b');
mass_scan_parameters(ii).Max_boolean=fread(fid,1,'int8',0,'b');
end
end
massdescriptionmultiarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if massdescriptionmultiarraysize>0
for ii=1:massdescriptionmultiarraysize
    for jj=1:8
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
massdescriptionmulti(ii,jj).symbol=fread(fid,strsz,'*char',0,'b')';
massdescriptionmulti(ii,jj).amu=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).matrix_boolean=fread(fid,1,'int8',0,'b')';
massdescriptionmulti(ii,jj).detector=fread(fid,1,'int32',0,'b')';
massdescriptionmulti(ii,jj).waiting_time=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).counting_time=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).offset=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).resolution=fread(fid,1,'double',0,'b')';
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
massdescriptionmulti(ii,jj).symbol_h=fread(fid,strsz,'*char',0,'b')';
massdescriptionmulti(ii,jj).amu_h=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).dM_minus=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).dM_plus=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).Bfield_22bit=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).Bfield_MSB=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).Bfield_LSB=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).trolleyposition=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).mono_multi_boolean=fread(fid,1,'int8',0,'b')';
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
massdescriptionmulti(ii,jj).ref_mass=fread(fid,strsz,'*char',0,'b')';
massdescriptionmulti(ii,jj).refBfield_22bit=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).refBfield_MSB=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).refBfield_LSB=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).Bfield_RMN=fread(fid,1,'uint32',0,'b')';
massdescriptionmulti(ii,jj).RMN_boolean=fread(fid,1,'int8',0,'b')';
massdescriptionmulti(ii,jj).option1=fread(fid,1,'double',0,'b')';
massdescriptionmulti(ii,jj).corretion_em_drift_boolean=fread(fid,1,'int8',0,'b')';
    end
end
end
dataIparraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if dataIparraysize>0
for ii=1:dataIparraysize
dataIpxarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
dataIp(ii).valueX=fread(fid,dataIpxarraysize,'double',0,'b');
dataIpyarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
dataIp(ii).valueY=fread(fid,dataIpyarraysize,'double',0,'b');
end
end
% 2-D Array ????
paramspresetlistarraysize1=fread(fid,1,'uint32',0,'b'); % 4 bytes
paramspresetlistarraysize2=fread(fid,1,'uint32',0,'b'); % 4 bytes
if paramspresetlistarraysize1>0 && paramspresetlistarraysize2>0
    for ii=1:paramspresetlistarraysize1
        for jj=1:paramspresetlistarraysize2
        params_preset_list(ii,jj).Numeric=fread(fid,1,'double',0,'b')';
        end
    end
end
paramIDlistarraysize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if paramIDlistarraysize>0
param_ID_list.Numeric=fread(fid,paramIDlistarraysize,'double',0,'b')';
end
OIP.slits_entrance=fread(fid,1,'uint16',0,'b');
OIP.slits_energy=fread(fid,1,'uint16',0,'b');
OIP.slits_exit=fread(fid,1,'uint16',0,'b');
OIP.Apertures_Contrast=fread(fid,1,'uint16',0,'b');
OIP.Apertures_Field=fread(fid,1,'uint16',0,'b');
OIP.Apertures_Apert_L4=fread(fid,1,'uint16',0,'b');
OIP.Apertures_Apert_L2=fread(fid,1,'uint16',0,'b');
OIP.MultiCollectionEMSetting_L2_boolean=fread(fid,1,'int8',0,'b')';
OIP.MultiCollectionEMSetting_L1_boolean=fread(fid,1,'int8',0,'b')';
OIP.MultiCollectionEMSetting_C_boolean=fread(fid,1,'int8',0,'b')';
OIP.MultiCollectionEMSetting_H1_boolean=fread(fid,1,'int8',0,'b')';
OIP.MultiCollectionEMSetting_H2_boolean=fread(fid,1,'int8',0,'b')';
OIP.MultiCollectionEMSetting_ALL_ON_boolean=fread(fid,1,'int8',0,'b')';
OIP.MultiCollectionEMSetting_ALL_OFF_boolean=fread(fid,1,'int8',0,'b')';
OIP.DetectorsPresets_Det=fread(fid,1,'uint16',0,'b');
OIP.DetectorsPresets_PresetSelection=fread(fid,1,'uint16',0,'b');
OIP.Beam_Pri_Beam=fread(fid,1,'uint16',0,'b');
OIP.Beam_Max_Area=fread(fid,1,'uint16',0,'b');
OIP.Beam_Raster_microns=fread(fid,1,'uint16',0,'b');
OIP.Beam_egate_percent=fread(fid,1,'uint16',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
OIP.OIP_Label=fread(fid,strsz,'*char',0,'b')';
OIP.DTOS=fread(fid,1,'uint16',0,'b');
OIP.E_Beam=fread(fid,1,'uint16',0,'b');
OIP.Apply_OIP_Presets=fread(fid,1,'uint16',0,'b');
OIP.OIP_Number=fread(fid,1,'double',0,'b');
OIP.Ip_Sampling=fread(fid,1,'uint16',0,'b');
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
OIP.Isf_filename=fread(fid,strsz,'*char',0,'b')';
OIP.eGate=fread(fid,1,'uint16',0,'b');
analytical_parameters.PRIMARY_ion_species=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_Arc_current_mA=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_Primary_current_begin_A=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_Primary_current_end_A=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_HV_kV=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_raster_size_microns=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_Primary_beam_size_microns=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_Duo_pressure_mbar=fread(fid,1,'double',0,'b');
analytical_parameters.PRIMARY_HR_Aperture_boolean=fread(fid,4,'int8',0,'b')';
analytical_parameters.PRIMARY_L4_Aperture_boolean=fread(fid,4,'int8',0,'b')';
analytical_parameters.SECONDARY_ESA_inner_V=fread(fid,1,'int32',0,'b')';
analytical_parameters.SECONDARY_ESA_outer_V=fread(fid,1,'int32',0,'b')';
analytical_parameters.SECONDARY_Transfer_lens_microns=fread(fid,1,'int32',0,'b')';
analytical_parameters.SECONDARY_Sample_HV_V=fread(fid,1,'int32',0,'b')';
analytical_parameters.Contrast_Aperture_boolean=fread(fid,4,'int8',0,'b')';
analytical_parameters.Field_Aperture_boolean=fread(fid,4,'int8',0,'b')';
analytical_parameters.Energy_slit_width_eV=fread(fid,1,'int32',0,'b')';
analytical_parameters.Exit_slit_width_microns=fread(fid,1,'int32',0,'b')';
analytical_parameters.Mass_Resolution=fread(fid,1,'int32',0,'b')';
analytical_parameters.Entrance_slit_width_microns=fread(fid,1,'int32',0,'b')';
analytical_parameters.Anal_pressure_mbar=fread(fid,1,'double',0,'b')';
analytical_parameters.Gate_DTOS_Y=fread(fid,1,'int32',0,'b')';
analytical_parameters.Gate_DTOS_X=fread(fid,1,'int32',0,'b')';
analytical_parameters.Gate_SII_Y=fread(fid,1,'int32',0,'b')';
analytical_parameters.Gate_SII_X=fread(fid,1,'int32',0,'b')';
analytical_parameters.Field_Aperture_Size_microns=fread(fid,1,'int32',0,'b')';
strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
analytical_parameters.Comments=fread(fid,strsz,'*char',0,'b')';
Reference_data=fread(fid,1,'double',0,'b')';
detectorsparameterssize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if detectorsparameterssize>0
    for ii=1:detectorsparameterssize
    detectors_parameters(ii).yield=fread(fid,1,'double',0,'b')';
    detectors_parameters(ii).Dead_time_nanoseconds=fread(fid,1,'double',0,'b')';
    detectors_parameters(ii).background_counts_per_second=fread(fid,1,'double',0,'b')';
    end
end
driftoffsetindexsize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if driftoffsetindexsize>0
Drift_offset.index=fread(fid,driftoffsetindexsize,'uint32',0,'b');
end
driftoffsetdriftsize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if driftoffsetdriftsize>0
Drift_offset.drift_volts=fread(fid,driftoffsetdriftsize,'double',0,'b');
end
driftfieldsize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if driftfieldsize>0
    
    for ii=1:driftfieldsize
        driftfieldindexsize=fread(fid,1,'uint32',0,'b'); % 4 bytes
        Drift_field(ii,:).index=fread(fid,driftfieldindexsize,'uint32',0,'b');
        driftfielddriftsize=fread(fid,1,'uint32',0,'b'); % 4 bytes
        Drift_field(ii,:).drift_volts=fread(fid,driftfielddriftsize,'double',0,'b');
    end

end

% 2-D Array ????
emdriftdatasize1=fread(fid,1,'uint32',0,'b'); % 4 bytes
emdriftdatasize2=fread(fid,1,'uint32',0,'b'); % 4 bytes
if emdriftdatasize1>0 && emdriftdatasize2>0
    for ii=1:emdriftdatasize1
        for jj=1:emdriftdatasize2
            relyieldsize=fread(fid,1,'uint32',0,'b');
            if relyieldsize>0
            for kk=1:relyieldsize
                em_drift_data(ii,jj,kk).Yield=fread(fid,1,'double',0,'b');
                em_drift_data(ii,jj,kk).dY_per_Y_over_dSigma_per_Sigma=fread(fid,1,'double',0,'b');
                em_drift_data(ii,jj,kk).Sigma=fread(fid,1,'double',0,'b');
                em_drift_data(ii,jj,kk).STD_error_Sigma=fread(fid,1,'double',0,'b');
            end
            end
        em_drift_data.Enabled_detectors_Lp2_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_L2_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_L1_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_C_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_H1_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_H2_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_Hp2_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_FC_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_EM_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.Enabled_detectors_FC2_boolean(ii,jj)=fread(fid,4,'int8',0,'b');
        em_drift_data.measurement_mass_mass_index(ii,jj)=fread(fid,4,'uint32',0,'b');
        strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
        em_drift_data.measurement_mass_symbol{ii,jj}=fread(fid,strsz,'*char',0,'b')';
        em_drift_data.measurement_mass_acq_cycle(ii,jj)=fread(fid,4,'uint32',0,'b');
        end
    end
end

fitcoefssize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if fitcoefssize>0
    for ii=1:fitcoefssize
    FitCoefs(ii).Lambda=fread(fid,1,'double',0,'b');
    FitCoefs(ii).Beta=fread(fid,1,'double',0,'b');
    FitCoefs(ii).Alpha=fread(fid,1,'double',0,'b');
    FitCoefs(ii).Sigma=fread(fid,1,'double',0,'b');
    end    
end
BeamCenteringData.FieldApp_yesno_boolean=fread(fid,1,'int8',0,'b');
BeamCenteringData.FieldApp_DeltaToScan=fread(fid,1,'int32',0,'b');
resultssize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if resultssize>0
    for ii=1:resultssize
    BeamCenteringData.BeamCenteringDataFieldAppResults(ii).X=fread(fid,1,'double',0,'b');
    BeamCenteringData.BeamCenteringDataFieldAppResults(ii).Y=fread(fid,1,'double',0,'b');
    end
end
BeamCenteringData.FieldApp_Detector=fread(fid,1,'uint16',0,'b');

BeamCenteringData.EntranceSlits_yesno_boolean=fread(fid,1,'int8',0,'b');
BeamCenteringData.EntranceSlits_DeltaToScan=fread(fid,1,'int32',0,'b');
resultssize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if resultssize>0
    for ii=1:resultssize
    BeamCenteringData.BeamCenteringDataEntranceSlitsResults(ii).X=fread(fid,1,'double',0,'b');
    BeamCenteringData.BeamCenteringDataEntranceSlitsResults(ii).Y=fread(fid,1,'double',0,'b');
    end    
end
BeamCenteringData.EntranceSlits_Detector=fread(fid,1,'uint16',0,'b');

BeamCenteringData.ContrastAperture_yesno_boolean=fread(fid,1,'int8',0,'b');
BeamCenteringData.ContrastAperture_DeltaToScan=fread(fid,1,'int32',0,'b');
resultssize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if resultssize>0
    for ii=1:resultssize
    BeamCenteringData.BeamCenteringDataContrastApertureResults(ii).X=fread(fid,1,'double',0,'b');
    BeamCenteringData.BeamCenteringDataContrastApertureResults(ii).Y=fread(fid,1,'double',0,'b');
    end    
end
BeamCenteringData.ContrastAperture_Detector=fread(fid,1,'uint16',0,'b');


BeamCenteringData.OPTION_yesno_boolean=fread(fid,1,'int8',0,'b');
BeamCenteringData.OPTION_DeltaToScan=fread(fid,1,'int32',0,'b');
resultssize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if resultssize>0
    for ii=1:resultssize
    BeamCenteringData.BeamCenteringDataOPTIONResults(ii).X=fread(fid,1,'double',0,'b');
    BeamCenteringData.BeamCenteringDataOPTIONResults(ii).Y=fread(fid,1,'double',0,'b');
    end    
end
BeamCenteringData.OPTION_Detector=fread(fid,1,'uint16',0,'b');

detectorsparsize=fread(fid,1,'uint32',0,'b'); % 4 bytes
if detectorsparsize>0
    for ii=1:detectorsparsize
    Detectors_par(ii).detector_identifier=fread(fid,1,'uint8',0,'b');
    Detectors_par(ii).detector_type=fread(fid,1,'uint8',0,'b');
    strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
    Detectors_par(ii).detector_name=fread(fid,strsz,'*char',0,'b')';
    Detectors_par(ii).detector_position=fread(fid,1,'double',0,'b');
    Detectors_par(ii).Bfield=fread(fid,1,'double',0,'b');
    Detectors_par(ii).detectorMass=fread(fid,1,'double',0,'b');
    strsz=fread(fid,1,'uint32',0,'b'); % 4 bytes
    Detectors_par(ii).symbol_Mass=fread(fid,strsz,'*char',0,'b')';
    slitsizesize=fread(fid,1,'uint32',0,'b'); % 4 bytes
    if slitsizesize>0
    Detectors_par(ii).slit_size=fread(fid,slitsizesize,'int16',0,'b')';
    end
    Detectors_par(ii).slit_nb=fread(fid,1,'int16',0,'b');
    Detectors_par(ii).Yield=fread(fid,1,'double',0,'b');
    Detectors_par(ii).DeadTime=fread(fid,1,'double',0,'b');
    Detectors_par(ii).HV_multi=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).Threshold=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_X_input_plates=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_ESA_outer=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_ESA_inner=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_X_detector_plates=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_Y_detector_plates=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_repeller_a=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).plates_repeller_b=fread(fid,1,'uint16',0,'b');
    Detectors_par(ii).number_counter=fread(fid,1,'uint8',0,'b');
    Detectors_par(ii).lateral_detector=fread(fid,1,'double',0,'b');
    Detectors_par(ii).Background=fread(fid,1,'double',0,'b');
    end
end

fclose(fid);

clearvars datavalueX datavalueY fid related_files_size ii rfsz strsz ...
    arraysize dataarraysize xarraysize yarraysize massdescriptionmultiarraysize...
    jj dataIparraysize dataIpxarraysize dataIpyarraysize paramspresetlistarraysize1...
    paramspresetlistarraysize2 paramIDlistarraysize detectorsparameterssize...
    driftoffsetindexsize driftoffsetdriftsize driftfieldsize driftfieldindexsize...
    driftfielddriftsize emdriftdatasize1 emdriftdatasize2 fitcoefssize resultssize...
    detectorsparsize slitsizesize


TMP_NAME = tempname;
save(TMP_NAME, '-regexp', '^(?!(TMP_NAME)$).')
D=load(TMP_NAME);

    