function out = skyline_in(ins)

% this function implements the topographic-shielding calculator.
%
% Syntax: out = skyline_in(ins)
% 
% the input structure ins has the following fields, which all ought to be strings:
%
% ins.str_strike -- strike of surface in degrees
% ins.str_dip -- dip of surface in degrees
% ins.str_az -- space-separated string of azimuths
% ins.str_el -- space-separated string of elevations
%
% calls skyline.m to do the calculation
%
% Written by Greg Balco -- UW Cosmogenic Nuclide Lab
% balcs@u.washington.edu
% March, 2006
% Part of the CRONUS-Earth online calculators: 
%      http://hess.ess.washington.edu/math
%
% Copyright 2001-2007, University of Washington
% All rights reserved
% Developed in part with funding from the National Science Foundation.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License, version 2,
% as published by the Free Software Foundation (www.fsf.org).
%

% ------------------------------------------------

% What's the version?

ver = '1.1';

% get in proper directory.

cd /var/www/html/math/general

% ------------------------------------------------

% INPUT DATA CHECKING....

% Decide if anything was submitted

%if ~( (length(deblank(ins.str_strike)) == 0) | (str2double(ins.str_strike) == 0) );
if ~(length(deblank(ins.str_strike)) == 0);
    isStrike = 1; 
else;
    isStrike = 0;
end;
%if ~( (length(deblank(ins.str_dip)) == 0) | (str2double(ins.str_dip) == 0) );
if ~(length(deblank(ins.str_dip)) == 0);
    isDip = 1; 
else;
    isDip = 0;
end;
if ~( (length(deblank(ins.str_az)) == 0) | (str2double(ins.str_az) == 0) );
    isAz = 1; 
else;
    isAz = 0;
end;
if ~( (length(deblank(ins.str_el)) == 0) | (str2double(ins.str_el) == 0) );
    isEl = 1; 
else;
    isEl = 0;
end;

% check proper combinations

if ~((isStrike & isDip) == (isStrike | isDip));
    error('Gotta have both strike and dip');
end;

if ~((isAz & isEl) == (isAz | isEl));
    error('Gotta have both azimuths and elevations');
end;

% check for proper strike and dip values

if isStrike;
    if (isnan(str2double(ins.str_strike)) | (size(str2double(ins.str_strike)) ~= [1 1]));
        error('Something wrong with strike value');
    end;
end;

if isDip;
    if (isnan(str2double(ins.str_dip)) | (size(str2double(ins.str_dip)) ~= [1 1]));
        error('Something wrong with dip value');
    end;
end;

% make strike and dip values

strike = str2double(ins.str_strike);
dip = str2double(ins.str_dip);

% check azimuth and elevation values
% note that decimal points are not allowed - whole number degrees only. 

az = str2double(regexp(ins.str_az,'[0-9]{1,3}','match'));
el = str2double(regexp(ins.str_el,'[0-9]{1,3}','match'));

if length(find(az > 360));
    error('azimuths out of range');
elseif length(find(el > 90));
    error('elevations out of range');
end;

if length(find((size(az) ~= size(el))));
    emsg = (['Azimuth and elevation arrays not the same size. ' ...
         'Check for decimal points, commas, etc.']);
    error(emsg);
end;

if length(find(isnan(az))) | length(find(isnan(el)));
        emsg = (['Something wrong with azimuth or elevation array. ' ...
         'Check for decimal points, commas, etc.']);
    error(emsg);
end;

% Done checking input data. 

% do shielding calculation

[sf,horiz,skyline_ver] = skyline(az,el,strike,dip);

% make the plot

data = [(0:360)' horiz']';

% Now see if we can make raster images using GMT and ghostscript. 

rand('seed',sum(100*clock));
uniqueid = int2str(round(rand(1)*1e6));
% no effort made to see if this really is unique. 
% I mean, what are the odds?

% open file for write 
% everything goes on in the scratch directory. 
fname = ['/var/www/html/scratch/skyline' uniqueid '.gmt'];

fid = fopen(fname,'w');

% write some GMT setup code
fprintf(fid,'%s\n','gmtset PAPER_MEDIA square8');
fprintf(fid,'%s\n','gmtset ANOT_FONT 3 ANOT_FONT_SIZE 14');
fprintf(fid,'%s\n','gmtset LABEL_FONT 3 LABEL_FONT_SIZE 14');
fprintf(fid,'%s\n','gmtset FRAME_PEN 8');

% write some GMT code that actually does something
temp_string = ['psxy -R0/360/0/90 -JX5i/2i -P -Ba60g60:"Azimuth":/a30g30:"Elevation":WeSn -W1p/255/0/0 << EOF >> /var/www/html/scratch/skyline' uniqueid '.ps'];
fprintf(fid,'%s\n',temp_string);
fprintf(fid,'%0.5g %0.5g\n',data);
fprintf(fid,'%s\n','EOF');

% write the ImageMagick conversion commands
temp_string = ['convert -trim /var/www/html/scratch/skyline' uniqueid '.ps /var/www/html/scratch/skyline' uniqueid '.jpg'];
fprintf(fid,'%s\n',temp_string);

% close file
fclose(fid);

% run the script you just made
temp_string = ['chmod a+x /var/www/html/scratch/skyline' uniqueid '.gmt'];
system(temp_string);
temp_string = ['/var/www/html/scratch/skyline' uniqueid '.gmt'];
system(temp_string);

% return the name of the jpeg you just made
out.plotName = ['/scratch/skyline' uniqueid '.jpg'];

% assemble other output data
out.s_factor = sprintf('%10.9f',sf);
out.wrapper_version = ver;
out.skyline_version = skyline_ver;

% Write to the log

% validate requesting_ip form input 

ip_pattern = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if regexp(ins.requesting_ip,ip_pattern);
	this_ip = ins.requesting_ip;
else;
	this_ip = 'badAddress';
end;

% do the log entry

log_entry = [this_ip ' skyline 0 0' ];
log_entry = [log_entry ' ' sprintf('%i ',fix(clock))];
	
write_to_log(log_entry);

% increment the call count

increment_call_count();

% return
templatefile = '/var/www/html/math/general/skyline_out.html';
out = htmlrep(out, templatefile);

