function create_storage_matrices
%works on one raw (.hdf) file at a time;  calls patchfile_analysis
%to extract the relevant info each trial, inner routine stores everything as tab-delineated ascii  -- to save time it can be set to save on the last trial only

hdfml('closeall'), clear all, close all,tic
%_________________________
patchlog_name='/Users/tathagatagarbha/dwn_data_analysis_system/raw_data_dwn/patch20070308T145028.hdf'
hy=[12.5 25 50 100 150 200 400 800];
mcc_output_voltage_scaling_factor=100     %(pA/V) -- the data recorded from the BNC block is in volts;  the pA/V ratio is set in multiclamp commander 
stim_artifact_threshold=50;
stim_artifact_jitter=3;     %in multiples of pulse duration (which is stored in the values appended at the beginning of the data trace)
IPI_jitter=1;               %in ms
baseline_time_window=[-150 -5];%in ms
PSC_time_window=[5 20];      %in ms
PSCbegin_threshold=2.5;       %in SD
PSCend_threshold=4.5         %in SD
minimum_possible_decay_time=3 %in ms  -- note this is a time window, not a time constant
maximum_possible_decay_time=25  %in ms
user_vetting=0
minimum_possible_psc_duration=3%in ms
Q_time_window=[5 20];       %in ms
Q_btwn_thresholds=0;        %counts btwn the two PSC thresholds if 1, otherwise counts over Q_time_window
asynchronous_window=[25 125]; %in ms
raw_window=[-200:7000];    %in samples, relative to the first presyn AP
fastemplate=monoexprisedecay_template_generator([14.16371820548567  11.31185487577630  19.35826590564763 0 1],1:50);fastemplate=[0 fastemplate];
%fastemplate=lsqfit_template_generator([3 8 52 225 0 0],[1:50]);fastemplate=[0 fastemplate]; %this template is much more accurate, at least for some cases (eg 20060413) than the above
do_careful_latency_measuring=1;
    plot_latency_measures=0;
    remove_entire_stim_artifact=0;
        extradeadsamples=7;
do_detecting=0
do_fitting=0
do_riseplateaumeasurement=0
do_montecarlojitteredevent=0
    threshforfitting=1
    fitendtime=400  %in samples
    successful_sims_required=10
    maximum_possible_simulation_loops=1000000
do_multipeak_detecting=0
do_fitting_to_fast_and_slow_templates=0;
    fastandslow_criteria_window=[-9999:10000];  %measured in samples, centered on first AP time
do_smoothing=[0 10]         %second value is binwidth, in samples, for boxcar smoothing
do_asynch_carefuldetection=[0 3 510 2 10]      %[do it or not, begin window (in ms), end window (in ms), criterion, samples preceding peak to include in fitting]
do_I_Na_checking=0
do_PSCclass_measuring=0
do_controls_measuring=0
    do_access_input_measuring=0
    plot_controls=0
do_raw_measuring=0
save_that_sht=0;            %whether tis nobler to save from the inner routine, or not to save at all
    save_after_each_trial=0;
display_traces=0;

%_________________________
%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)        %will take the first dataset in the .hdf file
[ds_name, ds_ndims, ds_dims, ds_type, ds_atts, stat]=hdfsd('getinfo',converter_sds_id) %will get info on the dataset
stat_dataset_end_post=hdfsd('endaccess',converter_sds_id)
%_______________________
real_time_mode=0;
samples_per_ms=10;samples_per_second=samples_per_ms*1000;
amillion=1000000;atrillion=amillion^2;
IPI_jitter=IPI_jitter*samples_per_ms;
baseline_time_window=[baseline_time_window(1)*samples_per_ms:baseline_time_window(2)*samples_per_ms];
PSC_time_window=[PSC_time_window(1)*samples_per_ms:PSC_time_window(2)*samples_per_ms];
minimum_possible_psc_duration=minimum_possible_psc_duration*samples_per_ms;
Q_time_window=[Q_time_window(1)*samples_per_ms:Q_time_window(2)*samples_per_ms];
asynch_window_fractionofsecond=(asynchronous_window(2)-asynchronous_window(1))/1000;
asynchronous_window=[asynchronous_window(1)*samples_per_ms:asynchronous_window(2)*samples_per_ms];
minimum_possible_decay_time=minimum_possible_decay_time*samples_per_ms;
maximum_possible_decay_time=maximum_possible_decay_time*samples_per_ms;

%make the empty matrices      
if  size(hy,1)>1 %if more than one row -- to distinguish stim_philosophy 1 type hy from stim_phil 6 type hy        %~isempty(hy)
    permutations_of_IPIs=hy(1,:);
    max_length_raw=samples_per_ms*max(((hy(1,:)-1).*hy(3,:))+hy(2,:));
	min_length_raw=samples_per_ms*min(hy(2,:));
else 
    permutations_of_IPIs=[length(hy) 1];     
    max_length_raw=1500;
	min_length_raw=1500;
end
number_of_IPIs=length(permutations_of_IPIs);
possible_repetitions_of_permutation=3*ceil(number_of_trials/prod(permutations_of_IPIs));%need the coefficient b/c some trials are removed, etc.    
APs_in_the_train=number_of_IPIs+1;

metrics_of_interest=7;   %trial, PSC, Q, latency, rise time, decay time, asynch
%____________________________
%number_of_trials=69
%z=[]
%if strcmp('F:\work_drive_f\patch20060629T192419.hdf',patchlog_name),number_of_trials=77,end
for trial=1:number_of_trials
    %if strcmp('F:\work_drive_f\patch20060629T180454.hdf',patchlog_name) & isempty(find(trial==[92]))
%     if isempty(find(trial== [71]))       %used for skipping trials if needed, but the resultant all-zero columns in the ascii matrices should be deleted before further analysis
    %choose the dataset for this trial
    converter_sds_id=hdfsd('select',converting_hdf,(trial-1)); %trial, corrected for zeroth indexing, tells which data set to read
    [ds_name, ds_ndims, ds_dims, ds_type, ds_atts, stat]=hdfsd('getinfo',converter_sds_id) %can desemicolon this line for checking
    samples_per_trigger=ds_dims(2)
    %get the ITI which preceded the current trial, which was stored as the ds_name by update_patchlog
    measured_ITI=str2num(ds_name)
    ds_stride=[];       %these lines tell how the dataset should be read
    ds_edges=[1, samples_per_trigger];
    %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 and its zeroth indexing system 
    [ds_dataein,status]=hdfsd('readdata',converter_sds_id,ds_start,ds_stride,ds_edges);
    %read in the recording from the postsynaptic neuron
    ds_start=[1 0];   %starts reading from the first row in the second column
    [ds_datazwei,status]=hdfsd('readdata',converter_sds_id,ds_start,ds_stride,ds_edges); 
    %--------
    ds_start=[2 0];   %starts reading from the first row in the second column
    [ds_datadrei,status]=hdfsd('readdata',converter_sds_id,ds_start,ds_stride,ds_edges);
    %---------
    ddata=( [ds_dataein ds_datazwei]);    
    %close the dataset
    stat=hdfsd('endaccess',converter_sds_id);  %closing the data set after having read it
      
    patchfile_analysis(trial,ddata,patchlog_name,hy,stim_artifact_threshold,stim_artifact_jitter,IPI_jitter,raw_window,baseline_time_window,PSC_time_window,Q_time_window,...
    Q_btwn_thresholds,PSCbegin_threshold,PSCend_threshold,asynchronous_window,mcc_output_voltage_scaling_factor,samples_per_ms,permutations_of_IPIs,number_of_IPIs,possible_repetitions_of_permutation,...
    APs_in_the_train,max_length_raw,min_length_raw,metrics_of_interest,measured_ITI,real_time_mode,amillion,atrillion,samples_per_second,number_of_trials,plot_controls,save_that_sht,...
    asynch_window_fractionofsecond,minimum_possible_decay_time,maximum_possible_decay_time,user_vetting,do_careful_latency_measuring,plot_latency_measures,remove_entire_stim_artifact,...
    extradeadsamples, fastemplate, do_detecting,do_fitting,do_riseplateaumeasurement,do_montecarlojitteredevent,...
    threshforfitting,fitendtime,successful_sims_required,maximum_possible_simulation_loops,do_smoothing,do_multipeak_detecting,display_traces,save_after_each_trial,...
    do_asynch_carefuldetection,do_I_Na_checking,do_PSCclass_measuring,do_controls_measuring,do_raw_measuring,do_access_input_measuring,do_fitting_to_fast_and_slow_templates,fastandslow_criteria_window)

%     end
    a=clock;
    save('clockcheck','a','-ascii','-tabs','-double') 
end

%closing the patchlog file
stat_hdfend=hdfsd('end',converting_hdf);
toc