/* * 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: trap_int_scan.C 21649 2011-12-05 20:50:45Z carrie $ SwRI" #include #include #include "user_defs.h" #include "libIDFSMath.h" /******************************************************************************* * * * IDFSMATH_TRAPEZOIDAL_INT_SCAN SUBROUTINE * * * * DESCRIPTION * * This routine is called to calculate a trapezoidal integration method on * * data which exists in a band, which means that X[n] is the starting location* * of the band, X[n+1] is the ending location of the band, and Y[n] is the * * ampltude of the band. Assume that the Y[n] values are measured at the * * centers of the bands. There are terms number of bands, so there are terms * * values of Y and terms+1 values of X. When converted to trapezoids, there * * are terms-1 trapezoids. Points are measured as X, Y pairs. This module * * is utilized when collapsing over the IDFS scan dimension. * * * * INPUT VARIABLES * * SDDAS_FLOAT *X pointer to the band values (x component) * * SDDAS_FLOAT *Y pointer to the data values (y component) * * SDDAS_LONG skip number of elements to add to get to next * * data element * * SDDAS_LONG terms number of bands or terms to integrate * * SDDAS_FLOAT start starting value to integrate over in X * * SDDAS_FLOAT stop ending value to integrate over in X * * 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 * * * * USAGE * * x = IDFSMath_trapezoidal_int_scan (&X, &Y, skip, terms, start, stop, * * norm, power_sx) * * * * NECESSARY SUBPROGRAMS * * pow() returns double-precision power function of * * the arguments * * * * EXTERNAL VARIABLES * * None * * * * INTERNAL VARIABLES * * std::vector X_centers center of the X bins * * std::vector wX_centers weighted center of the X bins * * std::vector width the widths between X bin centers * * std::vector M the slope of each Y between centers * * std::vector B the intercept of each Y between centers * * reg SDDAS_FLOAT *low_x, *high_x ptr to edges of X band - speed-up issue * * reg SDDAS_LONG i, next_i looping variables * * SDDAS_DOUBLE power_sx_plus_one, modified power_sx values * * power_sx_plus_two * * SDDAS_DOUBLE c_factor multiplication factor * * SDDAS_DOUBLE slope_term triangle area * * SDDAS_DOUBLE intercept_term constant area * * SDDAS_DOUBLE sum integration amp. * * SDDAS_DOUBLE sumx X covered area * * SDDAS_DOUBLE wstart weighted start value * * SDDAS_DOUBLE wstop weighted stop value * * SDDAS_DOUBLE x1, x2 integration limits * * SDDAS_FLOAT *y1, *y2 pointers to the first and last data values * * SDDAS_LONG terms_minus_one modified terms values used for indexing * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_FLOAT IDFSMath_trapezoidal_int_scan (SDDAS_FLOAT *X, SDDAS_FLOAT *Y, SDDAS_LONG skip, SDDAS_LONG terms, SDDAS_FLOAT start, SDDAS_FLOAT stop, SDDAS_CHAR norm, SDDAS_INT power_sx) { std::vector X_centers, wX_centers, width, M, B; register SDDAS_FLOAT *low_x, *high_x; register SDDAS_LONG i, next_i; SDDAS_DOUBLE power_sx_plus_one, power_sx_plus_two; SDDAS_DOUBLE c_factor, slope_term, intercept_term; SDDAS_DOUBLE sum, sumx, wstart, wstop, x1, x2; SDDAS_FLOAT *y1, *y2; SDDAS_LONG terms_minus_one; /***********************************************************************/ /* Allocate terms space for centers and weighted centers; allocate */ /* one less space for widths, slopes and intercepts. */ /***********************************************************************/ terms_minus_one = terms - 1; X_centers.resize (terms); wX_centers.resize (terms); width.resize (terms_minus_one); M.resize (terms_minus_one); B.resize (terms_minus_one); /*****************************************************************************/ /* Find the centers of the bins. Divide by 2 is costly, so multiply by 0.5 */ /*****************************************************************************/ c_factor = 0.5; low_x = &X[0]; high_x = &X[1]; for (i = 0; i < terms; ++i, ++low_x, ++high_x) X_centers[i] = (*high_x + *low_x) * c_factor; /*****************************************************************************/ /* Find the widths between bin centers, the slope of each Y between centers,*/ /* and determine the intercept for each Y between centers. */ /*****************************************************************************/ y1 = Y; /* Y value of first band */ y2 = Y + skip; /* Y value of second band */ for (i = 0, next_i = 1; i < terms_minus_one; ++i, ++next_i, y1 += skip, y2 += skip) { width[i] = X_centers[next_i] - X_centers[i]; /*****************************************************************************/ /* Are both data values valid? */ /*****************************************************************************/ if (*y1 >= VALID_MIN && *y2 >= VALID_MIN) { M[i] = (*y2 - *y1) / width[i]; B[i] = (X_centers[next_i] * *y1 - X_centers[i] * *y2) / width[i]; } /*****************************************************************************/ /* Set as flags for later usage by code in order to ignore these elements. */ /*****************************************************************************/ else { M[i] = OUTSIDE_MIN; B[i] = OUTSIDE_MIN; } } /*****************************************************************************/ /* Determine the power weight for each bin: set x**0=1 or x**(n+1) for n>0. */ /*****************************************************************************/ power_sx_plus_one = power_sx + 1.0; power_sx_plus_two = power_sx + 2.0; if (power_sx == 0) for (i = 0; i < terms; ++i) wX_centers[i] = X_centers[i]; else { for (i = 0; i < terms; ++i) wX_centers[i] = pow (X_centers[i], power_sx_plus_one); } /****************************************************************************/ /* Integration over scan, from x1 to X2, (M*x + B)*(x**n) dx */ /****************************************************************************/ wstart = pow (start, power_sx_plus_one); wstop = pow (stop, power_sx_plus_one); sumx = 0.0; /* initialize X covered area */ sum = 0.0; /* initialize integration amp*/ /*****************************************************************************/ /* Data above integrate stop. */ /****************************************************************************/ if (stop <= X[0]) return ((SDDAS_FLOAT) sum); /*****************************************************************************/ /* Data below integrate start. */ /****************************************************************************/ else if (start >= X[terms]) return ((SDDAS_FLOAT) sum); /********************************************************/ /* Start is below first center. */ /********************************************************/ if (start < X_centers[0]) { /********************************************************/ /* Integrate from the first data or start if bigger. */ /********************************************************/ if (start <= X[0]) x1 = X[0]; else x1 = start; /*******************************************************************/ /* Integrate to the first data center or the stop if it is less. */ /*******************************************************************/ if (stop >= X_centers[0]) x2 = X_centers[0]; else x2 = stop; if (Y[0] >= VALID_MIN) { sumx += x2 - x1; /* accumulate the width */ sum += Y[0] * (x2 - x1); /* accumulate the rect area */ } } /*************************************************************/ /* Search through all of the data bands - accumulate them. */ /*************************************************************/ for (i = 0, next_i = 1; i < terms_minus_one; ++i, ++next_i) { if (M[i] < VALID_MIN) continue; /*************************************************************/ /* Band is between start and stop, so accumulate this area. */ /*************************************************************/ if (start < X_centers[i] && X_centers[next_i] < stop) { /*************************************************************/ /* Determine constant area and then triangle area. */ /*************************************************************/ intercept_term = B[i] * (wX_centers[next_i] - wX_centers[i]) / power_sx_plus_one; slope_term = M[i] * (wX_centers[next_i] * X_centers[next_i] - wX_centers[i] * X_centers[i]) / power_sx_plus_two; sumx += X[next_i] - X[i]; /* accumulate the width */ sum += slope_term + intercept_term; /* accumulate trapazoid area */ } /*************************************************************/ /* The start is within the trapazoid of the data. */ /*************************************************************/ else if (X_centers[i] <= start && start < X_centers[next_i]) { /*************************************************************/ /* The stop is within the trapazoid of the data. */ /*************************************************************/ if (stop <= X_centers[next_i]) { /*************************************************************/ /* Determine constant area for limits between trap. and */ /* then triangle area. */ /*************************************************************/ intercept_term = B[i] * (wstop - wstart) / power_sx_plus_one; slope_term = M[i] * (wstop * stop - wstart * start) / power_sx_plus_two; sumx += stop - start; /* accumulate the width */ sum += slope_term + intercept_term; /* accumulate trapazoid area */ } /*************************************************************/ /* The start is within the trapazoid, but stop is not. */ /*************************************************************/ else { /*************************************************************/ /* Determine constant area and then triangle area. */ /*************************************************************/ intercept_term = B[i] * (wX_centers[next_i] - wstart) / power_sx_plus_one; slope_term = M[i] * (wX_centers[next_i] * X_centers[next_i] - wstart * start) / power_sx_plus_two; sumx += X[next_i] - start; /* accumulate the width */ sum += slope_term + intercept_term; /* accumulate trapazoid area */ } } /*************************************************************/ /* The stop is within the trapazoid of the data. */ /*************************************************************/ else if (X_centers[i] < stop && stop <= X_centers[next_i]) { /*************************************************************/ /* The start is within the trapazoid of the data. */ /*************************************************************/ if (start >= X_centers[i]) { /**************************************************************/ /* Determine constant area for limits between trap. and then */ /* triangle area. */ /**************************************************************/ intercept_term = B[i] * (wstop - wstart) / power_sx_plus_one; slope_term = M[i] * (wstop * stop - wstart * start) / power_sx_plus_two; sumx += stop - start; /* accumulate the width */ sum += slope_term + intercept_term; /* accumulate trapazoid area */ } /*************************************************************/ /* The stop is within the trapazoid, but start is not. */ /*************************************************************/ else { /*************************************************************/ /* Determine constant area and then triangle area. */ /*************************************************************/ intercept_term = B[i] * (wstop - wX_centers[i]) / power_sx_plus_one; slope_term = M[i] * (wstop * stop - wX_centers[i] * X_centers[i]) / power_sx_plus_two; sumx += stop - X[i]; /* accumulate the width */ sum += slope_term + intercept_term; /* accumulate trapazoid area */ } } } /********************************************************/ /* Stop is above last center. */ /********************************************************/ if (stop > X_centers[terms_minus_one]) { /********************************************************/ /* Integrate from the last data or stop if its bigger. */ /********************************************************/ if (stop >= X[terms]) x2 = X[terms]; else x2 = stop; /******************************************************************/ /* Integrate to the first data center or the stop if it is less. */ /******************************************************************/ if (start <= X_centers[terms_minus_one]) x1 = X_centers[terms_minus_one]; else x1 = start; y2 = Y + terms_minus_one * skip; /* ptr to Y value of last band */ if (*y2 >= VALID_MIN) { sumx += x2 - x1; /* accumulate the width */ sum += *y2 * (x2 - x1); /* accumulate the rect area */ } } /**************************************************************************/ /* Normalize the data to the start and stop distance. Note that this is */ /* 1.0 when start and stop lie within the data range with no gap. */ /**************************************************************************/ if (norm && sumx > 0.0) { sum /= sumx; sum *= (stop - start); } /**************************************************************************/ /* Return the final integral value. */ /* If no data was found (all values set to OUTSIDE_MIN), return */ /* OUTSIDE_MIN as a flag to the end user that the data matrix was not */ /* filled in totally (there is still missing data). The old way would */ /* return a 0.0. */ /**************************************************************************/ if (sumx > 0.0) return ((SDDAS_FLOAT) sum); else return (OUTSIDE_MIN); }