function msql_prof ( ptrfile, lat_range, lon_range, time_range, depth_range, dbase )
% MSQL_PROF:  Query the msql database for profile data.
%
% USAGE: msql_prof ( ptrfile, latrange, lonrange, trange, drange, dbase );
%
% PARAMETERS
%    ptrfile:
%        name of pointer file to write.  Default is /tmp/msql_ptr.
%    latrange:  
%        should be 2-vector, default is [0 90]
%    lonrange:
%        should be 2-vector, default is [0 360], can be negative
%    time_range:
%        should be contain two gregorian dates.  Eventually is converted 
%        into a 2-vector in julian date format, default being [1 2460000]. 
%        This corresponds to a starting day in 4713 BC and ending in 2023, 
%        so it should be sufficient.
%    depth_range:
%        should be 2-vector, default is [0 100000]
%    dbase:
%        name of database to extract data from.  Run
%        "/usr/local/epic/bin/list_tbl epic | grep prof" 
%        to see all available databases.  Default is 
%        "bpr_prof_all".
%



if ( nargin > 6 )
    help msql_prof;
    return;
end

%
% set defaults
%def_dbase = 'xrmrp_prof_z94gom9a';
def_dbase = 'bpr_prof_all';
def_depth_range = [0 100000];
def_time_range = [1 2460000];
def_time_range = [1 2460000];
def_lon_range = [0 360];
def_lat_range = [0 90];
def_ptrfile = '/tmp/msql_ptr';

if ( nargin > 6 )
    fprintf ( 1, 'Too many input arguments.\n' );
    help msql_prof;  
end

%
% choose the default ptrfile
if ( nargin == 5 )
    dbase = def_dbase;
end



%
% chose the database and depth range from defaults
if ( nargin == 4 )
    dbase = def_dbase;
    depth_range = def_depth_range;
end


%
% choose defaults for database and depth range, time_range
if ( nargin == 3 )
    dbase = def_dbase;
    time_range = def_time_range;
    depth_range = def_depth_range;
end

%
% choose defaults for database, depth range, time_range, lon_range
if ( nargin == 2)
    dbase = def_dbase;
    depth_range = def_depth_range;
    time_range = def_time_range;
    lon_range = def_lon_range;
end

%
% choose defaults for database, depth range, time_range, lon_range, lat_range
if ( nargin == 1 )
    dbase = def_dbase;
    depth_range = def_depth_range;
    time_range = def_time_range;
    lon_range = def_lon_range;
    lat_range = def_lat_range;
end


%
% choose defaults for database, depth range, time_range, lon_range, lat_range
if ( nargin == 0 )
   ptrfile = '/tmp/msql_ptr';
    dbase = def_dbase;
    depth_range = def_depth_range;
    time_range = def_time_range;
    lon_range = def_lon_range;
    lat_range = def_lat_range;
end


%
% if the time_range is NOT the default, convert it to julian
if ( nargin >= 4 )
    [m,n] = size(time_range);
    if ( (m==2) & (n==6) )
        tr1 = time_range(1,:);
        tr2 = time_range(2,:);
        clear time_range;
        time_range(1) = julian(tr1);
        time_range(2) = julian(tr2);
    elseif ( (m==1) & (n==12) )
        tr1 = time_range(1:6);
        tr2 = time_range(7:12);
        clear time_range;
        time_range(1) = julian(tr1);
        time_range(2) = julian(tr2);
    else
        fprintf ( 2, 'I don''t understand the time range you chose.\n' );
        fprintf ( 2, 'Make each date a 6-vector.\n' );
        help msql_prof;
        return;
    end
end

if ( findstr(dbase,'time') )
    fprintf ( 2, 'The database ''%s'' is ''time series'', not ''profile''.\n', dbase );
    return;
end



%
% If the longitudes are negative, make them positive
if ( (lon_range(1) < 0) & (lon_range(2) < 0 ) )
    lon_range = lon_range + 360;
end


%
% Write the shell script that will produce the pointer file with the 
% selected files.
msql_script = '/tmp/msql_script';
selection_file = '/tmp/msql_select';
fprintf ( 1, 'Writing the shell script %s to produce msql selection file %s.\n', msql_script, selection_file );
afid = fopen ( msql_script, 'w' );


fprintf ( afid, '#!/bin/sh\n\n' );
fprintf ( afid, '#\n# This script produces a pointer file from the profile database.\n' );
fprintf ( afid, 'PATH=$PATH":/usr/local/epic/bin:/usr/local/Minerva/bin:."\n\n' );
fprintf ( afid, 'tmpfile=%s\n\n', selection_file );

fprintf ( afid, '#\n# Query the database for the filename and path\n' );
fprintf ( afid, 'msql epic << EOF > $tmpfile\n' );
fprintf ( afid, 'select path,filename from %s\n', dbase );
fprintf ( afid, 'where lati >= %f and lati <= %f\n', lat_range(1), lat_range(2) );
fprintf ( afid, 'and longi >= %f and longi <= %f\n', lon_range(1), lon_range(2) );
fprintf ( afid, 'and timiday >= %d and timiday <= %d\n', round(time_range(1)), round(time_range(2)) );
fprintf ( afid, 'and zi >= %f and zi <= %f\n', depth_range(1), depth_range(2) );
fprintf ( afid, '\\g\n' );
fprintf ( afid, '\\q\n' );
fprintf ( afid, 'EOF\n\n\n' );




fclose ( afid );


unix ( sprintf ( 'chmod a+x %s', msql_script ) );
command = msql_script;
unix ( msql_script );


%
% Ok, the selection has been made, now we filter the
% msql selection file to reduce it down to a full pathname
fprintf ( 1, 'Filtering the selection file to produce pointer file %s...\n', ptrfile );
command = sprintf ( 'grep "|" < %s | grep -v filename | awk ''{ print $2$4}'' > %s', selection_file, ptrfile );
unix ( command );
fprintf ( 1, 'Done.\n' );





