read.m will still work, but to use load_raw, must have set up these files

(0) make directories to hold stuff
   assume: raw/ binfiles/ gbinfiles are all in the same root directory,
   easiest if inside adcp/ (i.e. parallel to adcpdb/, nav/, edit/,...)
   - linking is a perfectly reasonable way to get there

   raw/
      raw/adcp_pings  
      raw/adu2  
      raw/gyro
      raw/pcode
      raw/sndspd


   make these:

   binfiles/
      binfiles/adu2  
      binfiles/gyro
      binfiles/pcode
      binfiles/sndspd


   gbinfiles/
      gbinfiles/adu2  
      gbinfiles/gyro
      gbinfiles/pcode
      gbinfiles/sndspd
      gbinfiles/time


(1) have to set up all the .bin files

   cd raw/pcode
   serasc2bin.py -o ../../binfiles/pcode *.gps

   cd ../adu2
   serasc2bin.py -o ../../binfiles/adu2 *.adu

   cd ../gyro
   serasc2bin.py -o ../../binfiles/gyro *.hdg

   cd ../sndspd
   serasc2bin.py -o ../../binfiles/sndspd *.spd

   cd ..


(2) for each adcp_pings/*.raw, have to make all the gbin files
    - they are expected to end up in the directories: 
                   [adu2, gyro, pcode, sndspd, time]
    - defaults are set to run from the root directory that contains
            raw/, binfiles/, and /gbinfiles
    -  gps2bin;   xxx2gbin(rawfilebase, 'binfpath', 'gyro')
    - (can run mk_allgbin from this root directory)



### example for vg0104 ###

set cruiseid = vg0104
set dbname = a0104
set rawdir = '..';
set demo  = /home/noio/programs/codas3/adcp/vmdas_demos/lta_demo/load

(3) ## set up the cruise directory 
   adcptree vg0104 /home/noio/programs/matlab
    cd vg0104
    ln -s $rawdir/gbinfiles .
    ln -s $rawdir/raw .

   ## copy these files to load/ subdirectory
    cp -p  $demo/ldcodas.cnt           load
    cp -p  $demo/load_lta.m            load
    cp -p  $demo/load_lta_manual.m     load
    cp -p  $demo/vmadcp.def            load


   ## now we have gbinfiles alongside ping/ adcpdb/ and all the rest


(4)# scan: get all timeranges 
   for instance, do this from adcp/scan to get a file (alltimes.asc) 

   %% do this in matlab
   tr2ilist([], 'gbinpath','../gbinfiles/time', ...
                'file_exten', 'tim',...
                'ascii_file', 'alltimes.scn');

(5)  ## load 
(5a) ##editing and ping-averaging:

   %%in matlab; edit and run load_pings.m
    cd  load  
   load_pings %%see appendix
   
(5b) ## load:  
     ## edit ldcodas.cnt to contain the appropriate list of "*.cmd" files
     ## command line: run: 
           ldcodas ldcodas.cnt

(6)  ## up for quick_adcp.prl
(6a) ## replicate ubprint by creating a0104.ags
   cd ../nav
   cat ../load/*.gps1 > lta.gps1
   cat ../load/*.gps2 > lta.gps2

   ## for quick_adcp.prl to be happy:
   ln -s lta.gps1 ${dbname}.ags


(6b) ## replicate the time range file (need a better scheme than this!!)

   cd ../load
   set tr = ../scan/${dbname}.tr
   set file1 =  `ls *cmd | head -1`
   grep new_profile $file1 | head -1 | awk '{printf("%s %s", $2, $3)}' >> $tr
   echo -n " to " >> $trfile
   set file2 =  `ls *cmd | tail -1`
   grep new_profile $file2 | tail -1 | awk '{printf("%s %s\n", $2, $3)}' >> $tr


(7) ## now run quick_adcp.prl
   cd ..
    quick_adcp.prl -yearbase 2001 -noashtech -rlstart 3 -rlend 15 -progdir /home/ulili/programs -zapsetup 


(it says:)                                (you say)      (what you're doing)

Proceed with this dbname? (y/n/q)                 y        (accept database)
setup.m exists; make basic setup.m again? (y/n/q) y        (make new setup.m)
Proceed with ubprint? (y/n/q)                     n        (do not run ubprint)
Proceed to rotate? (y/n/q)                        y
Proceed with adcpsect? (y/n/q)                    y
Proceed with refabs? (y/n/q)                      y         
Proceed with smoothr? (y/n/q)                     y
Proceed with putnav? (y/n/q)                      y
Proceed with adcpsect (for autoedit)? (y/n/q)     y
Proceed with water track calibration? (y/n/q)     y
Proceed with bottom track calibration? (y/n/q)    y
list temperature? (y/n/q)                         y
set up autoedit files? (y/n/q)                    y
make matlab files?? (y/n/q)                       y


edit/

   autoedit
   list

   %reapply editing

   dbupdate ../adcpdb/atest abottom.asc
   dbupdate ../adcpdb/atest abadprf.asc
   badbin ../adcpdb/atest abadbin.asc
   set_lgb ../adcpdb/atest
   setflags setflags.tmp

nav/
%rerun navsteps
    adcpsect as_nav.tmp
    refabs refabs.tmp
    smoothr smoothr.tmp
    putnav putnav.tmp


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rotation is not getting in right
amplitude is bad too:


unedited: 11 points
edited:   11 points, 2.0 min speed, 2.5 max dev
            median     mean      std
amplitude   1.7088   1.7431   0.0699
phase      -43.7810  -46.1751   6.0069

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 also ran into an error here.

0 out of 20 (../raw/adcp_pings/nbp2001_206_43200.raw) 
closing the att bin file ./nbp2001_206_43200.attstats.bin
starting a new configuration segment: resetting leftover to []
calculating iranges
1 out of 4 (../raw/adcp_pings/nbp2001_206_49231.raw) 
1 out of 293 bad headings in ensemble
elapsed_time =
    2.1785
2 out of 4 (../raw/adcp_pings/nbp2001_206_49231.raw) 
elapsed_time =
    2.1767
3 out of 4 (../raw/adcp_pings/nbp2001_206_49231.raw) 
elapsed_time =
    2.1939
4 out of 4 (../raw/adcp_pings/nbp2001_206_49231.raw) 
closing the att bin file ./nbp2001_206_49231.attstats.bin
calculating iranges
1 out of 25 (../raw/adcp_pings/nbp2001_206_50400.raw) 
??? Error using ==> -
Matrix dimensions must agree.

Error in ==> /home/ulili/programs/matlab/rawadcp/att_stats.m
On line 43  ==> m_dh = abs(dh - bl_filt(dh, 20)) > c.max_dh_dev;

Error in ==> /home/ulili/programs/matlab/rawadcp/rdi/@nb/load_raw.m
On line 337  ==>       [attstats, att_rownames] = att_stats(aux);




--------------------------  2002/01/08 got to here ------------------
   ### encapsulated in get_rndata 
   finfo = tr2ilist(allfinfo{4,2} + [0 300/86400],  'gbinpath','gbinfiles');
   fullrawfile = fullfile('adcp_pings',finfo{1});
   ilist = finfo{2};
   data = read(nb, fullrawfile, 'yearbase', 2001, 'ilist', ilist,...
         'use_ext_heading', 1, 'vars', 'all')
   aux_struct = get_serfields(data,'verbose',1,...
      'gpspath', 'gbinfiles/pcode',  'adupath', 'gbinfiles/adu2',...
      'hdgpath', 'gbinfiles/fyro',   'spdpath', 'gbinfiles/sndspd')        
   data = update_raw(data, aux_struct);


   data.depth = mk_depth(data, 5);
   OH = 45.0 + .55;  %Mounting Angle +  phase 
   hdg = data.heading;  
   [data.enu data.xyze] = beam_enu(data.vel, OH + hdg, 0*hdg, 0*hdg', ...
                   'geometry','convex');

   [dd, nbconfig] = restruct(nb, data);
   ###-----------


   [dd,config] = get_rndata(94 + [0 5*300/86400], 'gbinfiles');
   rlookit(dd, config, 1, [15])
   rlookit2(dd, config, [15])


=============================================================================

params.h_align  =  45 + .55;
params.h_bias   =  0;
params.ilayer   = [2:20];
params.interval = 300;
params.geometry = 'concave';
params.yearbase =  2001;
params.add_to_depth  = 5;


instclass  = nb;
rawadcppath = '../raw/adcp_pings';
files = dirs(fullfile(rawadcppath, '*.raw'), 'fullpath', 1);

adcppath

r = [];
for ifile = 1:length(files)
   file = files(ifile).name;
   r = load_raw(instclass, file, params, 'finish', 0, 'leftover', r, ...
                'use_external_gyro', 1);
end
