function [raw_values,pulse_durations,action_current_onsets,voltage_step_onsets]=create_basic_text_file_for_asynch_rel_INa_onset(file_to_read,first_trial_only,...
    electroaudio,endpoint,remove_stim_artifacts,plot_removal)
%description: opens the raw (.hdf) file, opens and reads out each trace, omits the prepended metadata, subtracts leak, finds the action current onsets and voltage step onsets,...
        %optionally removes artifacts from the postsyn trace, closes each dataset and then the .hdf file
    %returns the leak-corrected, unscaled raw values, presyn pulse durations, action current onset times, and voltage step onset times
    %if electroaudio==1, patch traces only (columns 0 and 1 from dataset), if electroaudio==2, audio trace only (col 2 from dataset), if electroaudio==3, all three traces
    %if any(remove_stim_artifacts), will use the presyn voltage step start and end times + [-1 0 1] as the points to be replaced
        %to randomly choose two from among five points from before and five points from after these, use [2 -6 -5 -4 -3 -2 2 3 4 5 6]
    %plot_removal only makes sense if first_trial_only, as a single 4-AP sweep will generate 24 plots
%called by: command line
%calls: find_peaks_specifically_ends_of_peaks, find_peaks_within_threshold_crossing_regions, artifact_camouflager
%notes:
%code confirmed date: 2 feb 2013

hdfml('closeall')

if ismac
    patchlog_name=strcat('/Users/tathagatagarbha/dwn_research/raw_data_dwn_old/',file_to_read,'.hdf')
elseif isunix
    patchlog_name=strcat('/home/dwn/Desktop/dwn_produ/raw_data_dwn_old/',file_to_read,'.hdf')
end

%open the patchlog created by update_patchlog at the time of the recording
converting_hdf=hdfsd('start',patchlog_name,'read');
[datasets_for_converted,nglobal_atts,stat_readinghdf]=hdfsd('fileinfo',converting_hdf)
number_of_trials=datasets_for_converted
converter_sds_id=hdfsd('select',converting_hdf,0);        %take the first dataset in the .hdf file
[ds_name, ds_ndims, ds_dims, ds_type, ds_atts, stat_getinfo_dataset]=hdfsd('getinfo',converter_sds_id) %get info on the dataset
stat_endgetinfo_dataset=hdfsd('endaccess',converter_sds_id)

raw_values=[];pulse_durations=[];action_current_onsets=[];voltage_step_onsets=[];
if first_trial_only, number_of_trials=1,end
for trial=1:number_of_trials
    converter_sds_id=hdfsd('select',converting_hdf,(trial-1)); %trial, corrected for zeroth indexing, tells which dataset to read
    [ds_name, ~, ds_dims, ~, ~, ~]=hdfsd('getinfo',converter_sds_id); 
    samples_per_trigger=ds_dims(2);
    measured_ITI=str2double(ds_name)   %get the ITI which preceded the current trial, which was stored as the ds_name by update_patchlog
    ds_stride=[];       %these lines tell how the dataset should be read
    ds_edges=[1, samples_per_trigger];
    if any(electroaudio==[1 3])
        %read in the recording from the presynaptic neuron
        ds_start=[0 0];   %starts reading from the first row in the first column, with the row and column switched ala .hdf's indexing system 
        [ds_dataein,~]=hdfsd('readdata',converter_sds_id,ds_start,ds_stride,ds_edges);
        if trial==1,output_channel_to_presyn=ds_dataein(7),end
        %read in the recording from the postsynaptic neuron
        ds_start=[1 0];   %starts reading from the first row in the second column
        [ds_datazwei,~]=hdfsd('readdata',converter_sds_id,ds_start,ds_stride,ds_edges); 
        
        samples_per_ms=ds_dataein(5)/1000;
        pulse_duration=ds_dataein(3)*samples_per_ms
        pulse_durations=[pulse_durations pulse_duration];
        
        ein=ds_dataein(10:endpoint)-mean(ds_dataein(10:25000));
        action_current_onsets_thistrial=find_peaks_specifically_ends_of_peaks(4.8,50,ein)
        action_current_onsets=[action_current_onsets action_current_onsets_thistrial];
        
        zwei=ds_datazwei(10:endpoint)-mean(ds_datazwei(10:25000));
        zwei_peaks=find_peaks_specifically_ends_of_peaks(4.8,50,zwei); if ~isempty(zwei_peaks),error('zwei peaks!'),end
        
        voltage_step_onsets_thistrial=find_peaks_within_threshold_crossing_regions(4.8,50,ein)
        voltage_step_onsets=[voltage_step_onsets voltage_step_onsets_thistrial'];
        if any(remove_stim_artifacts)
            for artifact=1:length(voltage_step_onsets_thistrial)
                for artifact_end=0:1  %each presyn voltage step produces both onset and offset artifacts
                    periartifact_indices=voltage_step_onsets_thistrial(artifact)+(artifact_end*pulse_duration)+remove_stim_artifacts(2:end);
                    for artifact_sample=-1:1  %this should suffice to ensure that all postsynaptic membrane current values distorted by the presyn voltage step are camouflaged
                        if plot_removal,figure,hold on, plot(zwei,'k'),end
                        artifact_index=voltage_step_onsets_thistrial(artifact)+(artifact_end*pulse_duration)+artifact_sample;
                        newdata=artifact_camouflager(zwei(periartifact_indices),remove_stim_artifacts(1));
                        zwei(artifact_index)=newdata;
                        if plot_removal,plot(zwei,'r'),end
                    end
                end             
            end
        end                            
    end
    %-----for microphone input
    if any(electroaudio==[2 3])
        ds_start=[2 0];   %starts reading from the first row in the third column
        [ds_datadrei,~]=hdfsd('readdata',converter_sds_id,ds_start,ds_stride,ds_edges);
        drei=ds_datadrei(10:endpoint);
    end
    %------
    if electroaudio==1, raw_values=[raw_values ein zwei];
    elseif electroaudio==2, raw_values=[raw_values drei];pulse_durations=[];action_current_onsets=[];voltage_step_onsets=[];
    elseif electroaudio==3, raw_values=[raw_values ein zwei drei];
    end
    stat_endread_dataset=hdfsd('endaccess',converter_sds_id)  %closing the data set after having read it
end
stat_hdfend=hdfsd('end',converting_hdf) %closing the patchlog file