/*
* Copyright (C) 1998 by Southwest Research Institute (SwRI)
*
* All rights reserved under U.S. Copyright Law and International Conventions.
*
* The development of this Software was supported by contracts NAG5-3148,
* NAG5-6855, NAS8-36840, NAG5-2323, and NAG5-7043 issued on behalf of
* the United States Government by its National Aeronautics and Space
* Administration. Southwest Research Institute grants to the Government,
* and others acting on its behalf, a paid-up nonexclusive, irrevocable,
* worldwide license to reproduce, prepare derivative works, and perform
* publicly and display publicly, by or on behalf of the Government.
* Other than those rights granted to the United States Government, no part
* of this Software may be reproduced in any form or by any means, electronic
* or mechanical, including photocopying, without permission in writing from
* Southwest Research Institute. All inquiries should be addressed to:
*
* Director of Contracts
* Southwest Research Institute
* P. O. Drawer 28510
* San Antonio, Texas 78228-0510
*
*
* Use of this Software is governed by the terms of the end user license
* agreement, if any, which accompanies or is included with the Software
* (the "License Agreement"). An end user will be unable to install any
* Software that is accompanied by or includes a License Agreement, unless
* the end user first agrees to the terms of the License Agreement. Except
* as set forth in the applicable License Agreement, any further copying,
* reproduction or distribution of this Software is expressly prohibited.
* Installation assistance, product support and maintenance, if any, of the
* Software is available from SwRI and/or the Third Party Providers, as the
* case may be.
*
* Disclaimer of Warranty
*
* SOFTWARE IS WARRANTED, IF AT ALL, IN ACCORDANCE WITH THESE TERMS OF THE
* LICENSE AGREEMENT. UNLESS OTHERWISE EXPLICITLY STATED, THIS SOFTWARE IS
* PROVIDED "AS IS", IS EXPERIMENTAL, AND IS FOR NON-COMMERCIAL USE ONLY,
* AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT
* SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
*
* Limitation of Liability
*
* SwRI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED AS A RESULT OF USING,
* MODIFYING, CONTRIBUTING, COPYING, DISTRIBUTING, OR DOWNLOADING THIS
* SOFTWARE. IN NO EVENT SHALL SwRI BE LIABLE FOR ANY INDIRECT, PUNITIVE,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGE (INCLUDING LOSS OF BUSINESS,
* REVENUE, PROFITS, USE, DATA OR OTHER ECONOMIC ADVANTAGE) HOWEVER IT ARISES,
* WHETHER FOR BREACH OF IN TORT, EVEN IF SwRI HAS BEEN PREVIOUSLY ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE. YOU HAVE SOLE RESPONSIBILITY FOR ADEQUATE
* PROTECTION AND BACKUP OF DATA AND/OR EQUIPMENT USED IN CONNECTION WITH THE
* SOFTWARE AND WILL NOT MAKE A CLAIM AGAINST SwRI FOR LOST DATA, RE-RUN TIME,
* INACCURATE OUTPUT, WORK DELAYS OR LOST PROFITS RESULTING FROM THE USE OF
* THIS SOFTWARE. YOU AGREE TO HOLD SwRI HARMLESS FROM, AND YOU COVENANT NOT
* TO SUE SwRI FOR, ANY CLAIMS BASED ON USING THE SOFTWARE.
*
* Local Laws: Export Control
*
* You acknowledge and agree this Software is subject to the U.S. Export
* Administration Laws and Regulations. Diversion of such Software contrary
* to U.S. law is prohibited. You agree that none of the Software, nor any
* direct product therefrom, is being or will be acquired for, shipped,
* transferred, or reexported, directly or indirectly, to proscribed or
* embargoed countries or their nationals, nor be used for nuclear activities,
* chemical biological weapons, or missile projects unless authorized by U.S.
* Government. Proscribed countries are set forth in the U.S. Export
* Administration Regulations. Countries subject to U.S embargo are: Cuba,
* Iran, Iraq, Libya, North Korea, Syria, and the Sudan. This list is subject
* to change without further notice from SwRI, and you must comply with the
* list as it exists in fact. You certify that you are not on the U.S.
* Department of Commerce's Denied Persons List or affiliated lists or on the
* U.S. Department of Treasury's Specially Designated Nationals List. You agree
* to comply strictly with all U.S. export laws and assume sole responsibilities
* for obtaining licenses to export or reexport as may be required.
*
* General
*
* These Terms represent the entire understanding relating to the use of the
* Software and prevail over any prior or contemporaneous, conflicting or
* additional, communications. SwRI can revise these Terms at any time
* without notice by updating this posting.
*
* Trademarks
*
* The SwRI logo is a trademark of SwRI in the United States and other countries.
*
*/
#ident "@(#) $Id: str_int_pnt_cont.C 18484 2007-07-12 18:55:07Z carrie $ SwRI"
#include
#include "user_defs.h"
#include "libIDFSMath.h"
/*****************************************************************************
* *
* IDFSMATH_STR_INT_POINT_CONT SUBROUTINE *
* *
* DESCRIPTION *
* This routine is called to calculate a trapezoidal integral (point) *
* using contiguous band values. Note that in this algorithm rather than *
* doing comparisions of the type A > (X[n] + X[n+1])/2.0 in comparing the *
* start or stop integration limits with the X values (we are doing *
* comparision only with the center value) we will use the form 2*A > *
* (X[n] + X[n+1]). Also, note that under normal situations start is less *
* than stop. If this is not the case then we assume the special case of *
* being asked to integrate over the entire X range - this is useful if you *
* are not sure of the X range. The IDFSMath_rmie() function converts the *
* values to radians for angular dimensions. *
* *
* INPUT VARIABLES *
* SDDAS_FLOAT *X pointer to the band values *
* SDDAS_FLOAT *Y pointer to the data values *
* SDDAS_LONG skip number of elements to add to get to next *
* data element *
* SDDAS_LONG terms number of bands *
* SDDAS_FLOAT start starting value to integrate over *
* SDDAS_FLOAT stop ending value to integrate over *
* SDDAS_CHAR which_dimen flag indicating which dimension is being *
* integrated over since multiple dimensions *
* make use of this code *
* SDDAS_CHAR norm flag indicating if the result is to be *
* normalized *
* SDDAS_INT power_sx the power of the extra "x" term or the power *
* of the sin term *
* SDDAS_INT power_c the power of the cos term *
* *
* USAGE *
* x = IDFSMath_str_int_point_cont (X, Y, skip, terms, start, stop, *
* which_dimen, norm, power_sx, power_c) *
* *
* NECESSARY SUBPROGRAMS *
* IDFSMath_rmie () returns the weighted values based on the *
* extra values that are in the integral *
* *
* EXTERNAL VARIABLES *
* None *
* *
* INTERNAL VARIABLES *
* std::vector WX weighted values for band values *
* reg SDDAS_DOUBLE *wf1, *wf2 pointers to weighted band values *
* reg SDDAS_FLOAT *f1, *f2 pointers to band values *
* reg SDDAS_FLOAT *f3, *f4 pointers to data values *
* reg SDDAS_FLOAT *f_end end of X array *
* SDDAS_DOUBLE ret_val resultant value returned to calling module *
* SDDAS_DOUBLE wstart weighted starting value for integration *
* SDDAS_DOUBLE wstop weighted ending value for integration *
* SDDAS_DOUBLE wa, wb weighted band values *
* SDDAS_DOUBLE wc, wd weighted variables for trapezoid integration *
* SDDAS_DOUBLE *w_end end of weighted X array *
* SDDAS_DOUBLE wtop weighted upper band value *
* SDDAS_DOUBLE wbot weighted lower band value *
* SDDAS_DOUBLE sum summation value of integration *
* SDDAS_FLOAT a, b center of first and second band *
* SDDAS_FLOAT c, d variables used for trapezoidal integration *
* SDDAS_FLOAT top upper band *
* SDDAS_FLOAT bot lower band *
* SDDAS_FLOAT sumx normalization factor *
* SDDAS_LONG loop looping variable *
* SDDAS_CHAR valid_data_found flag for data inclusion or missing data *
* *
* SUBSYSTEM *
* Display Level *
* *
****************************************************************************/
/*** TRAPAZOIDAL INTEGRAL, CONTIGUOUS X BANDS ***/
SDDAS_FLOAT IDFSMath_str_int_point_cont (SDDAS_FLOAT *X, SDDAS_FLOAT *Y,
SDDAS_LONG skip, SDDAS_LONG terms, SDDAS_FLOAT start, SDDAS_FLOAT stop,
SDDAS_CHAR which_dimen, SDDAS_CHAR norm, SDDAS_INT power_sx,
SDDAS_INT power_c)
{
std::vector WX;
register SDDAS_DOUBLE *wf1, *wf2;
register SDDAS_FLOAT *f1, *f2, *f3, *f4, *f_end;
SDDAS_DOUBLE ret_val = 0.0, wstart, wstop, wa, wb, wc, wd, *w_end;
SDDAS_DOUBLE wtop, wbot, sum = 0.0;
SDDAS_FLOAT a, b, c, d, top, bot, sumx = 0.0;
SDDAS_LONG loop;
SDDAS_CHAR valid_data_found;
valid_data_found = 0;
f_end = X + terms; /* end of X array */
/***********************************************************************/
/* Allocate space to hold weighted values returned by IDFSMath_rmie().*/
/* Need to account for the one extra bin that is needed. */
/* Get the weighted values based on the extra values that are in the */
/* integral, as indicated by power_sx and power_c arguments. */
/***********************************************************************/
WX.resize (terms + 1);
for (loop = 0; loop <= terms; ++loop)
WX[loop] = IDFSMath_rmie (*(X + loop), which_dimen, power_sx, power_c);
w_end = &WX[terms];
wstart = IDFSMath_rmie (start, which_dimen, power_sx, power_c);
wstop = IDFSMath_rmie (stop, which_dimen, power_sx, power_c);
wtop = (*w_end + *(w_end - 1)); /* weighted upper band */
wbot = (WX[0] + WX[1]); /* weighted lower band */
top = (*f_end + *(f_end - 1)); /* upper band */
bot = (X[0] + X[1]); /* lower band */
/************************************************************************/
/* Not that we have the weighted matrices and weighted positions, we */
/* can perform a linear trapezoidal integration so multiply BOTH sets */
/* of values by 2 to get rid of having to take the average at every */
/* iteration. */
/************************************************************************/
start *= 2.0; /* twice the start value */
stop *= 2.0; /* twice the end value */
wstart *= 2.0; /* twice the weighted start value */
wstop *= 2.0; /* twice the weighted end value */
/* Increasing array of values? Look at first and last instead of first */
/* 2 values. Indexing by terms is okay since the array of band values */
/* are used and there is always one more element at the end. */
if (X[terms] > X[0]) /* X values increasing */
{
if (start > stop) /* check for special request */
{ /* to integrate over all X */
start = bot; /* set start to least X */
stop = top; /* set stop to largest X */
wstart = wbot; /* set start to least X */
wstop = wtop; /* set stop to largest X */
}
if (start >= top || stop <= bot) /* are integ. limits in X array */
return (OUTSIDE_MIN); /* no? ok return OUTSIDE_MIN */
f1 = X + 1; /* top of first band */
f2 = X + 2; /* top of second band */
wf1 = &WX[1]; /* top of first band */
wf2 = &WX[2]; /* top of second band */
f3 = Y; /* Y value of first band */
f4 = Y + skip; /* Y value of second band */
wa = *wf1 + *(wf1 - 1); /* center of first band */
a = *f1 + *(f1 - 1); /* center of first band */
b = *f2 + *f1; /* center of second band */
wb = *wf2 + *wf1; /* center of second band */
if (start > a) /* if start in X find 1st band */
{
while ((start >= b) && (f2 < f_end)) /* begin search */
{
wa = wb; /* 1st band now 2nd band */
a = b; /* 1st band now 2nd band */
b = *(++f2) + *(++f1); /* new 2nd band center */
wb = *(++wf2) + *(++wf1); /* new 2nd band center */
f3 += skip; /* increment Y of 1st band */
f4 += skip; /* increment Y of 2nd band */
}
if (*f3 >= VALID_MIN && *f4 >= VALID_MIN) /* if data good */
{ /* compute 1st trap */
c = (b - a)/2.0;
d = (start - a)/2.0;
wc = (wb - wa)/2.0;
wd = (wstart - wa)/2.0;
ret_val = (*f4 - *f3) * (wc * wc - wd * wd) / 2.0 / wc;
ret_val += (wb - wstart)/2.0 * *f3;
valid_data_found = 1;
sumx += (c - d) * 2.0;
}
/* Added conditional since getting ABR errors from purify from f2. */
if (f2 != f_end)
{
wa = wb; /* 1st band now 2nd band */
a = b; /* 1st band now 2nd band */
b = *(++f2) + *(++f1); /* new 2nd band center */
wb = *(++wf2) + *(++wf1); /* new 2nd band center */
f3 += skip; /* increment Y of 1st band */
f4 += skip; /* increment Y of 2nd band */
}
}
while ((stop >= b) && (f2 < f_end))
{
if (*f3 >= VALID_MIN && *f4 >= VALID_MIN)
{
valid_data_found = 1;
sum += (wb - wa) * (*f3 + *f4);
sumx += b - a;
}
wa = wb;
a = b;
b = *(++f2) + *(++f1);
wb = *(++wf2) + *(++wf1);
f3 += skip;
f4 += skip;
}
ret_val += 0.25 * sum;
if ((stop < top) && (*f3 >= VALID_MIN) && (*f4 >= VALID_MIN))
{
wc = (wb - wa)/2.0;
d = (stop - a)/2.0;
wd = (wstop - wa)/2.0;
ret_val += (*f4 - *f3) * wd * wd / 2.0 / wc;
ret_val += (wstop - wa)/2.0 * *f3;
valid_data_found = 1;
sumx += d * 2.0;
}
}
else
{
if (start > stop) /* check for special request */
{ /* to integrate over all X */
start = top; /* set start to least X */
stop = bot; /* set stop to largest X */
wstart = wtop; /* set start to least X */
wstop = wbot; /* set stop to largest X */
}
if (start >= bot || stop <= top) /* are integ. limits in X array */
return (OUTSIDE_MIN); /* no? ok return OUTSIDE_MIN */
f1 = f_end - 1;
f2 = f_end - 2;
wf1 = w_end - 1;
wf2 = w_end - 2;
f3 = Y + (terms - 1) * skip;
f4 = Y + (terms - 2) * skip;
wa = *wf1 + *(wf1 + 1);
a = *f1 + *(f1 + 1);
b = *f2 + *f1;
wb = *wf2 + *wf1;
if (start > a)
{
while ((start >= b) && (f2 > X))
{
wa = wb;
a = b;
b = *(--f2) + *(--f1);
wb = *(--wf2) + *(--wf1);
f3 -= skip;
f4 -= skip;
}
if (*f3 >= VALID_MIN && *f4 >= VALID_MIN)
{
wc = (wb - wa)/2.0;
c = (b - a)/2.0;
d = (start - a)/2.0;
wd = (wstart - wa)/2.0;
ret_val = (*f4 - *f3) * (wc * wc - wd * wd ) / 2.0 / wc;
ret_val += (wb - wstart)/2.0 * *f3;
valid_data_found = 1;
sumx += (c - d) * 2.0;
}
/* Added conditional since getting ABR errors from purify from f2. */
if (f2 > X)
{
a = b;
wa = wb;
b = *(--f2) + *(--f1);
wb = *(--wf2) + *(--wf1);
f3 -= skip;
f4 -= skip;
}
}
while ((stop >= b) && (f2 > X))
{
if (*f3 >= VALID_MIN && *f4 >= VALID_MIN)
{
valid_data_found = 1;
sum += (wb - wa) * (*f3 + *f4);
sumx += b - a;
}
a = b;
wa = wb;
b = *(--f2) + *(--f1);
wb = *(--wf2) + *(--wf1);
f3 -= skip;
f4 -= skip;
}
ret_val += 0.25 * sum;
if ((stop < bot) && (*f3 >= VALID_MIN) && (*f4 >= VALID_MIN))
{
wc = (wb - wa)/2.0;
d = (stop - a)/2.0;
wd = (wstop - wa)/2.0;
ret_val += (*f4 - *f3) * wd * wd / 2.0 / wc;
ret_val += (stop - a)/2.0 * *f3;
sumx += d * 2.0;
valid_data_found = 1;
}
}
if (norm && sumx > 0.0)
{
ret_val /= sumx;
ret_val *= (stop - start);
}
/* If no data was found (all values set to OUTSIDE_MIN), */
/* return OUTSIDE_MIN so that possible later collapsing */
/* will also ignore this missing data. */
if (!valid_data_found)
return (OUTSIDE_MIN);
else
return ((SDDAS_FLOAT) ret_val);
}