## Copyright 
## 
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
 
## -*- texinfo -*-
## @deftypefn {Function File} {} antipode (@var{lat}, @var{long})
##
## Returns the antipode of the given latitude and longitude (degrees)
## @example
## >> [lat,long]=antipode(45,90)
## lat = -45
## long = -90
## @end example
## It will correct for numbers greater than 360
## @example
## >> [lat, long] = antipode(-987.3, 400083)
## lat = -87.300
## long = -57
## @end example
## Vectors are also possible
## @example
## >> [lat, long] =antipode([-34, 190], [187, -15])
## lat =
##
##   34   10
## long =
##
##     7   165
## @end example  
## @end deftypefn 

function [alat, along] = antipode(lat, long)  
# Get the values in the correct range 
# There probably is a faster algorithm
# Latitude
  lat = mod(lat, 360);
  for i=1:length(lat)  
    x=lat(i);
    if x <= 90
	   lat(i) = x;
    elseif ( x > 90 && x <= 180 )
	   lat(i) = 180 - x;
    elseif ( x > 180 && x <= 270 )
	   lat(i) = 180 - x;
    else 
	   lat(i) = x - 360;
    end	
  end		

#Longitude
  long = mod( -long, 360 ); #The negative came from trial and error
 
# Now it can be solved
  alat = -lat; 
  along = 180 - long;
  endfunction
