%
%L_ORBIT  Script for loading Aquarius satellite orbit data.
%
%   Try 'type l_orbit'.
%   _________________________________________________________________
%   This is part of JLAB --- type 'help jlab' for more information 
%   (C) 2007 J.M. Lilly --- type 'help jlab_license' for details

dirname=jlab_settings('dirnames.aquarius');

f_orbit=fopen([dirname '/sim_orbit0.dat'], 'r');

[orbit, count]=fread(f_orbit, [8,100801], 'real*8','ieee-le');

fclose(f_orbit);

clear lat lon
lat(:,1)=orbit(3,:)'; lon(:,1)=orbit(4,:)';
lat(:,2)=orbit(5,:)'; lon(:,2)=orbit(6,:)';
lat(:,3)=orbit(7,:)'; lon(:,3)=orbit(8,:)';
lat(:,4)=orbit(1,:)'; lon(:,4)=orbit(2,:)';

%Compute heading
cv=latlon2uv((1:size(lat,1))',lat(:,4),lon(:,4));
hdg=jrad2deg(angle(cv));

vindex(lat,lon,hdg,(1:length(lat)-1),1);
vshift(lat,lon,hdg,min(find(vshift(lon(:,4),1,1)-lon(:,4)>300)),1);
index=find(vshift(lon(:,4),1,1)-lon(:,4)>300);

%Advance to nearest turning point
bool=turningpoint(lat(:,4));
vshift(bool,lat,lon,hdg,min(find(bool))-1,1);

%Give each segment a unique number
num=cumsum(bool);
num=osum(num,(0:3)'*(maxmax(num)));

lon=deg180(lon);

%colbreaks(num,lat,lon);
clear orbit

description='Beam groundtracks and nadir point for the future Aquarius satellite';
link='http://aquarius.nasa.gov/';
citation='Orbit data generated by F. Wentz of Remote Sensing Systems';
permissions= 'Permission to redistribute this data was given to J. M. Lilly by F. Wentz';
creator='Created by the script L_ORBIT by J. M. Lilly' ;
timestamp=datestr(now);
notes=[    'Varibles are defined as follows:' char(13) char(13) ...
           'lat --- latitude' char(13) ... 
           'lon --- longitude' char(13) ... 
           'num --- orbit number, incremented at every northern or southern turning point' char(13) ... 
           'hdg --- local angle of groundtrack with respect to east' char(13) ...
           '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' char(13) ... 
           'Columns 1, 2, and 3 are the altimeter beam locations ' char(13) ...
           'Column 4 is the nadir point'];
       
           
matsave -v6 orbit description link citation permissions creator timestamp notes lat lon num hdg
