% gf_apmoon February 5, 2013
% apogee and perigee of the Moon
% MICE geometry finder routines
% Orbital Mechanics with MATLAB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
% name of kernels data file
meta = 'apmoon.tm';
% maximum number of event windows
maxwin = 200000;
% maximum number of event intervals
maxivl = maxwin / 2;
% define format of event date and time display
timfmt = 'YYYY MON DD HR:MN:SC.###### TDB';
timlen = 35;
% load kernels for this task
cspice_furnsh (meta);
% create a confinement window (cnfine) for the distance search. this window
% contains the start and stop times of the search interval.
et0 = cspice_str2et ('2013 dec 1');
et1 = cspice_str2et ('2014 apr 1');
cnfine = cspice_wninsd (et0, et1);
% set the observer and target
obsrvr = 'earth';
target = 'moon';
% we're looking for the distance given by the planetary ephemeris, not the
% apparent distance, so we'll use geometric states (no corrections).
abcorr = 'none';
clc; home;
fprintf('\n\nprogram gf_apmoon - apogee and perigee of the moon\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute 'local' perigee of the Moon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the relational operator for this search is "local minimum." the reference
% value is unused; simply initialize it to zero.
relate = 'locmin';
refval = 0.0;
% set the step size for this search. the step must be shorter than the
% shortest interval over which the distance is increasing or decreasing.
% we pick a conservative value of 5 days. units expected by spice are seconds.
step = 5.0 * cspice_spd;
% the adjustment value isn't used for this search; set it to 0.
adjust = 0.0;
% the number of intervals to be accommodated in the workspace windows to be
% dynamically allocated by cspice_gfdist is specified by the parameter maxivl.
result = cspice_gfdist (target, abcorr, obsrvr, relate, refval, adjust, ...
step, maxivl, cnfine);
% display the times of perigee and geocentric distance
fprintf ('\ntimes of local perigee and geocentric distance of the moon\n\n');
for i = 1 : cspice_wncard(result)
% fetch the start and stop times of the ith
% interval from the window `result'.
[start, finish] = cspice_wnfetd (result, i);
begstr = cspice_timout (start, timfmt);
fprintf (' %s ', begstr);
% compute geocentric distance at perigee
starg = mice_spkezr(target, start, 'J2000', abcorr, obsrvr);
rmag = norm(starg.state(1:3));
fprintf('%16.8f kilometers\n', rmag);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute 'local' apogee of the Moon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the relational operator for this search is "local maximum." the reference
% value is unused; simply initialize it to zero.
relate = 'locmax';
refval = 0.0;
result = cspice_gfdist (target, abcorr, obsrvr, relate, refval, adjust, ...
step, maxivl, cnfine);
% display the times of apogee and geocentric distance
fprintf ('\ntimes of local apogee and geocentric distance of the moon\n\n');
for i = 1 : cspice_wncard(result)
% fetch the start and stop times of the ith
% interval from the window `result'.
[start, finish] = cspice_wnfetd (result, i);
begstr = cspice_timout (start, timfmt);
fprintf (' %s ', begstr);
% compute geocentric distance at apogee
starg = mice_spkezr(target, start, 'J2000', abcorr, obsrvr);
rmag = norm(starg.state(1:3));
fprintf('%16.8f kilometers\n', rmag);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute 'global' perigee of the Moon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the relational operator for this search is "absolute minimum." the reference
% value is unused; simply initialize it to zero.
relate = 'absmin';
refval = 0.0;
result = cspice_gfdist (target, abcorr, obsrvr, relate, refval, adjust, ...
step, maxivl, cnfine);
% display the times of perigee and geocentric distance
fprintf ('\ntime of global perigee and geocentric distance of the moon\n\n');
for i = 1 : cspice_wncard(result)
% fetch the start and stop times of the ith
% interval from the window `result'.
[start, finish] = cspice_wnfetd (result, i);
begstr = cspice_timout (start, timfmt);
fprintf (' %s ', begstr);
% compute geocentric distance at perigee
starg = mice_spkezr(target, start, 'J2000', abcorr, obsrvr);
rmag = norm(starg.state(1:3));
fprintf('%16.8f kilometers\n', rmag);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute 'global' apogee of the Moon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the relational operator for this search is "absolute maximum." the reference
% value is unused; simply initialize it to zero.
relate = 'absmax';
refval = 0.0;
result = cspice_gfdist (target, abcorr, obsrvr, relate, refval, adjust, ...
step, maxivl, cnfine);
% display the times of apogee and geocentric distance
fprintf ('\ntime of global apogee and geocentric distance of the moon\n\n');
for i = 1 : cspice_wncard(result)
% fetch the start and stop times of the ith
% interval from the window `result'.
[start, finish] = cspice_wnfetd (result, i);
begstr = cspice_timout (start, timfmt);
fprintf (' %s ', begstr);
% compute geocentric distance at apogee
starg = mice_spkezr(target, start, 'J2000', abcorr, obsrvr);
rmag = norm(starg.state(1:3));
fprintf('%16.8f kilometers\n', rmag);
end
fprintf('\n\n');
% unload kernels and clean up the kernel pool.
cspice_kclear;