/* * 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 "@(#) col_dimen.c 1.32 05/08/19 SwRI" #include "ret_codes.h" #include "gen_defs.h" #include "user_defs.h" #include "libtrec_idfs.h" #include "libIDFSMath.h" /******************************************************************************* * * * COLLAPSE_DIMENSIONS SUBROUTINE * * * * DESCRIPTION * * This routine is called in order to collapse data over the specified * * dimensions using the requested ranges. This routine will work on one * * data level or "unit", which is specified by the user. If there are many * * data levels being processed, there should be multiple calls to this * * routine. The address of the space that holds the result is passed back to * * the user. Before the actual collapsing is performed, the missing bins are * * filled in according to the user-specified action. The data is then * * collapsed over the specified ranges for the requested dimensions. If the * * last plot is being processed for this data set, reset the bin stat values * * to the values that existed before adjustment for filling in missing bins. * * This should be done only once the last plot is being processed for the * * combination being requested; otherwise, erroneous calculations will result.* * * * INPUT VARIABLES * * SDDAS_ULONG data_key key which uniquely identifies the data set * * being processed * * SDDAS_SHORT sensor the sensor being requested * * SDDAS_SHORT order the order of the fit (1, 2, 3, etc) * * SDDAS_SHORT unit_index value specifying which data level is to be * * processed * * SDDAS_CHAR *exten filename extension for the data to be used * * SDDAS_USHORT vnum version number to be associated with this * * combination (allows for multiple opens) * * SDDAS_CHAR *dimen_status the requested dimensions to collapse over * * SDDAS_CHAR avg_type averaging scheme to use for calculations * * SDDAS_CHAR int_type integration scheme to use for calculations * * SDDAS_CHAR cyclic flag indicating if the data is cyclic * * SDDAS_CHAR last_plot flag indicating if the last plot for this * * combo is being processed * * SDDAS_CHAR norm_res flag indicating if the result is to be * * normalized * * SDDAS_CHAR bin_project flag indicating if the data is to be * * projected to the end of the bins * * SDDAS_CHAR dlevel flag indicating if the data is to be reduced* * to a single value or to an array of values * * 1 - data to be reduced to a single value * * 2 - data to be reduced to an array * * SDDAS_FLOAT *s_range starting range values for each dimension * * SDDAS_FLOAT *e_range ending range value for each dimension * * SDDAS_FLOAT **ret_data pointer to the resultant * * SDDAS_FLOAT tension the weighting of the data * * SDDAS_LONG need_filled number of filled bins needed to perform the * * calculations to determine the missing value * * SDDAS_CHAR cur_buf the buffer being processed (0 - 4) * * * * USAGE * * x = collapse_dimensions (data_key, exten, vnum, sensor, &dimen_status, * * &s_range, &e_range, avg_type, int_type, &ret_data, cyclic, order, * * need_filled, tension, norm_res, bin_project, unit_index, last_plot, * * dlevel, cur_buf) * * * * NECESSARY SUBPROGRAMS * * ir_locate_ex() determines if the requested combination has * * already been processed and points to the * * correct structure allocated for the combo * * ir_phi_bin_stat () sets/resets the bin status values for the * * normalization matrix which is associated * * with the 4D matrix (highest order is PHI) * * ir_theta_bin_stat () sets/resets the bin status values for the * * normalization matrix which is associated * * with the 3D matrix (highest order is THETA) * * ir_fill_bin_elements () fills in missing bin elements * * ir_collapse_chrg () collapses data over the CHARGE dimension * * ir_collapse_mass () collapses data over the MASS dimension * * ir_collapse_phi () collapses data over the PHI dimension * * ir_collapse_theta () collapses data over the THETA dimension * * ir_collapse_sweep () collapses data over the sweep dimension * * ir_collapse_scalar () averages sensor data for scalar instruments * * ir_transfer_sensor () transfers data from the 3D matrix to the 2D * * matrix if no theta averaging was performed * * ir_transfer_to_3d_binned () combines those sensors that are defined * * to be mounted at the same theta angles, as * * defined in the VIDF file * * IDFSMath_get_assumption_factor() returns hemisphere assumption factor * * ir_add_phi_matrices () combines 2 phi matrices * * * * EXTERNAL VARIABLES * * struct general_info structure that holds information concerning * * ginfo the experiment that is being processed * * * * INTERNAL VARIABLES * * reg struct experiment_info a pointer to the structure that holds * * *ex specific experiment information * * reg struct collapse_data pointer to the collapse_data structure * * *cptr being processed * * struct bin_info *bptr a pointer to the structure that holds sweep * * binning information * * register SDDAS_SHORT loop looping variable * * SDDAS_FLOAT *dptr pointer to the data to be passed into called* * routine * * SDDAS_FLOAT assumption_factor constant multiplier, dimension dependent * * 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 * * SDDAS_SHORT ret_val the value returned by the called routine * * SDDAS_CHAR chrg_dimen flag indicating if the CHARGE dimension is * * to be collapsed over * * SDDAS_CHAR mass_dimen flag indicating if the MASS dimension is to * * be collapsed over * * SDDAS_CHAR phi_dimen flag indicating if the PHI dimension is to * * be collapsed over * * SDDAS_CHAR theta_dimen flag indicating if the THETA dimension is to* * be collapsed over * * SDDAS_CHAR sweep_dimen flag indicating if the SWEEP dimension is to* * be collapsed over * * SDDAS_CHAR scalar_avg flag indicating if sensor data averaging * * is to be performed * * SDDAS_CHAR buf_num current buffer being processed * * SDDAS_CHAR prev_buf previous buffer processed * * char theta_done flag indicating if averaging was performed * * over the theta bins * * char processed flag indicating if any of the dimensions * * retrieved data * * char reset_called flag indicating if LOCATE_EX was called * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_SHORT collapse_dimensions (SDDAS_ULONG data_key, SDDAS_CHAR *exten, SDDAS_USHORT vnum, SDDAS_SHORT sensor, SDDAS_CHAR *dimen_status, SDDAS_FLOAT *s_range, SDDAS_FLOAT *e_range, SDDAS_CHAR avg_type, SDDAS_CHAR int_type, SDDAS_FLOAT **ret_data, SDDAS_CHAR cyclic, SDDAS_SHORT order, SDDAS_LONG need_filled, SDDAS_FLOAT tension, SDDAS_CHAR norm_res, SDDAS_CHAR bin_project, SDDAS_SHORT unit_index, SDDAS_CHAR last_plot, SDDAS_CHAR dlevel, SDDAS_CHAR cur_buf) { extern struct general_info ginfo; register struct experiment_info *ex; register struct collapse_data *cptr; register struct bin_info *bptr; register SDDAS_SHORT loop; SDDAS_FLOAT *dptr, assumption_factor; SDDAS_INT power_sx, power_c; SDDAS_SHORT ret_val; SDDAS_CHAR chrg_dimen, mass_dimen, phi_dimen, theta_dimen, sweep_dimen; SDDAS_CHAR scalar_avg, buf_num, prev_buf; char theta_done, processed, reset_called; /**************************************************************************/ /* Check to see if the combination being processed has been processed */ /* before. If not, an error condition - probably didn't call FILE_OPEN. */ /**************************************************************************/ if (!ginfo.called_locate) { ret_val = ir_locate_ex (data_key, exten, vnum, 0); if (ret_val != ALL_OKAY) return (CDIMEN_NOT_FOUND); ginfo.called_locate = 1; reset_called = 1; } else reset_called = 0; ex = ginfo.expt; cptr = ex->collapse_ptr; bptr = ex->bin_ptr; processed = 0; /***************************************************************************/ /* For Moments Integration, user must call moments_computations(). */ /***************************************************************************/ if (avg_type == MOMENTS_INT) { if (reset_called) ginfo.called_locate = 0; return (COMPUTE_MOMENTS); } /************************************************************************/ /* Check if memory for collapsing over multi-dimension data exists. */ /************************************************************************/ if (ex->collapse_ptr == NO_MEMORY) { if (reset_called) ginfo.called_locate = 0; return (CDIMEN_COLLAPSE); } scalar_avg = *(dimen_status + SCALAR_INDEX); chrg_dimen = *(dimen_status + CHARGE_INDEX); mass_dimen = *(dimen_status + MASS_INDEX); phi_dimen = *(dimen_status + PHI_INDEX); theta_dimen = *(dimen_status + THETA_INDEX); sweep_dimen = *(dimen_status + SCAN_INDEX); theta_done = 0; /*************************************************************************/ /* Only one scan range can be defined for the virtual instrument. If */ /* more than one exists (FIXED_SWEEP and TBL_OFF values for sweep table */ /* differ amongst the sensor used), return a hard error ONLY if */ /* dimension is past the sweep dimension. */ /*************************************************************************/ if (ex->bin_ptr->num_center_band != 1 && (theta_dimen == DIMEN_ON || phi_dimen == DIMEN_ON || mass_dimen == DIMEN_ON || chrg_dimen == DIMEN_ON)) return (CDIMEN_MANY_SCAN); /***************************************************************************/ /* Merge together phi buffers that were processed by combining neighboring */ /* phi buffers. Do it ONCE for the current buffer being processed, in */ /* case multiple sensors are being collapsed. Do it here instead of at */ /* the end of fill_data() since need the negative bin stat indicators, */ /* which are only set after the buffer has been collapsed over. Fill_data */ /* will set the indicator. */ /***************************************************************************/ if (cptr->add_phi_matrix[(SDDAS_SHORT) cur_buf]) { prev_buf = cur_buf - 1; if (prev_buf < 0) prev_buf += NUM_BUFFERS; ret_val = ir_add_phi_matrices (prev_buf, cur_buf); if (ret_val != ALL_OKAY) return (ret_val); cptr->add_phi_matrix[(SDDAS_SHORT) cur_buf] = 0; ir_clear_phi_bins (prev_buf); } /*************************************************************************/ /* If the missing data bins have not been filled in for the combo being */ /* processed, do so at this time. This is triggered once by the first */ /* plot using the specified combo. */ /*************************************************************************/ if (!cptr->filled_phi_matrix[(SDDAS_SHORT) cur_buf]) { if (cptr->phi_bins != 1) ir_phi_bin_stat (0, cur_buf); else ir_theta_bin_stat (0); ir_fill_bin_elements (cyclic, order, need_filled, tension, bin_project, cur_buf); } /**************************************************************************/ /* Collapse over the charge dimension. */ /**************************************************************************/ if (chrg_dimen == DIMEN_ON) { /************************************************************************/ /* Currently, only one module is called to handle this case and it */ /* accepts only contiguous bin values. It is anticipated that charge */ /* will be defined like theta so it has non-contiguous bin values */ /* (distinct upper and lower arrays). */ /************************************************************************/ if (avg_type == STRAIGHT_AVG_AZ) return (CHRG_PA_ERROR); processed = 1; ir_collapse_chrg (*(s_range + CHARGE_INDEX), *(e_range + CHARGE_INDEX), avg_type, unit_index, ret_data); } /**************************************************************************/ /* Collapse over the mass dimension. */ /**************************************************************************/ if (mass_dimen == DIMEN_ON) { /***********************************************************************/ /* Currently, only one module is called to handle this case and it */ /* accepts only contiguous bin values. It is anticipated that mass */ /* will be defined like theta so it has non-contiguous bin values */ /* (distinct upper and lower arrays). */ /***********************************************************************/ if (avg_type == STRAIGHT_AVG_AZ) return (MASS_PA_ERROR); processed = 1; ir_collapse_mass (*(s_range + MASS_INDEX), *(e_range + MASS_INDEX), avg_type, unit_index, ret_data); } /**************************************************************************/ /* The user can only collapse over phi if more than 1 phi bin exists. */ /**************************************************************************/ if (phi_dimen == DIMEN_ON && cptr->phi_bins != 1) { /**********************************************************************/ /* According to notes, all types of integrations that call */ /* IDFSMath_trapezoidal_int () or IDFSMath_str_int_band_cont () use */ /* the same values for power_sx and power_c. */ /**********************************************************************/ power_sx = 0; power_c = 0; processed = 1; ir_collapse_phi (*(s_range + PHI_INDEX), *(e_range + PHI_INDEX), avg_type, int_type, unit_index, ret_data, norm_res, power_sx, power_c, cur_buf); } /**************************************************************************/ /* The user can only collapse over theta if the theta constants were */ /* defined for the virtual instrument being processed. */ /**************************************************************************/ if (theta_dimen == DIMEN_ON && cptr->theta_bins != 0) { /**********************************************************************/ /* For those data sets with the duplicate theta issue, combine the */ /* individual sensors back to the theta groups they belong to. */ /**********************************************************************/ if (cptr->theta_bins < 0) ir_transfer_to_3d_binned (unit_index); processed = 1; theta_done = 1; /***********************************************************************/ /* Set values of power_sx and power_c for use by */ /* IDFSMath_trapezoidal_int() or IDFSMath_str_int_band_cont (). */ /***********************************************************************/ if (avg_type == SPHERICAL_INT) { power_sx = 1; power_c = 0; } else if (avg_type == FLUX_INT) { power_sx = 1; power_c = 1; } else { power_sx = 0; power_c = 0; } ir_collapse_theta (*(s_range + THETA_INDEX), *(e_range + THETA_INDEX), avg_type, unit_index, norm_res, ret_data, power_sx, power_c); /***********************************************************************/ /* Pointer returned by ir_collapse_theta is to the beginning of the */ /* data matrix, not the unit processed, so reset in case no other */ /* collapsing is necessary. */ /***********************************************************************/ *ret_data = cptr->data_2d + (unit_index * ex->bin_ptr->num_bins); } /**************************************************************************/ /* If some higher dimension (MASS or PHI) has been processed and the 2D */ /* matrix is being requested (for a SPECTROGRAM plot), ensure that the */ /* data is transferred from the 3D matrix to the 2D matrix. */ /**************************************************************************/ else if (processed && dlevel == 2) { ir_transfer_sensor (sensor, unit_index); *ret_data = cptr->data_2d + (unit_index * ex->bin_ptr->num_bins); } /***************************************************************************/ /* Collapse over the sweep values. If no theta averaging transpired, the */ /* data for the requested sensor must be copied into the 2d data matrix. */ /***************************************************************************/ if (sweep_dimen == DIMEN_ON) { processed = 1; if (!theta_done) ir_transfer_sensor (sensor, unit_index); dptr = cptr->data_2d + unit_index * bptr->num_bins; /***********************************************************************/ /* Set values of power_sx and power_c for use by */ /* IDFSMath_trapezoidal_int() or IDFSMath_str_int_band_cont (). */ /***********************************************************************/ if (avg_type == FLUX_INT) { power_sx = 0; power_c = 0; } else { power_sx = 0; power_c = 0; } ir_collapse_sweep (*(s_range + SCAN_INDEX), *(e_range + SCAN_INDEX), avg_type, int_type, unit_index, norm_res, ret_data, dptr, power_sx, power_c); } /***************************************************************************/ /* Averaging of sensor data for a scalar instrument is being requested. */ /***************************************************************************/ if (scalar_avg == DIMEN_ON) { processed = 1; ir_collapse_scalar (*(s_range + SCALAR_INDEX), *(e_range + SCALAR_INDEX), unit_index, ret_data); } /*************************************************************************/ /* If none of the dimensions were exercised, return the data for the */ /* sensor the units in question. */ /*************************************************************************/ if (!processed) { ir_transfer_sensor (sensor, unit_index); *ret_data = cptr->data_2d + (unit_index * ex->bin_ptr->num_bins); } /************************************************************************/ /* Scalar value is returned. If the value is OUTSIDE_MIN, leave as is.*/ /************************************************************************/ dptr = *ret_data; if (dlevel == 1 && *dptr >= VALID_MIN) { /*********************************************************************/ /* Get the selected dimension "constants". */ /*********************************************************************/ assumption_factor = IDFSMath_get_assumption_factor (avg_type, phi_dimen, theta_dimen); *dptr *= assumption_factor; } /************************************************************************/ /* 1-D array of returned values. */ /************************************************************************/ else if (dlevel == 2) { /*********************************************************************/ /* Get the selected dimension "constants". */ /*********************************************************************/ assumption_factor = IDFSMath_get_assumption_factor (avg_type, phi_dimen, theta_dimen); for (loop = 0; loop < bptr->num_bins; ++loop) if (*(dptr + loop) >= VALID_MIN) *(dptr + loop) *= assumption_factor; } /**************************************************************************/ /* Reset the bin stat values to previous values before adjustment. This */ /* should be done only once the last plot is being processed for the */ /* combination being requested; otherwise, erroneous calculations will */ /* result. */ /**************************************************************************/ if (last_plot) { if (cptr->phi_bins != 1) { /********************************************************************/ /* Since the last plot, reset phi matrices individually IF set. */ /********************************************************************/ for (buf_num = 0; buf_num < NUM_BUFFERS; ++buf_num) { if (cptr->filled_phi_matrix[(SDDAS_SHORT) buf_num]) { ir_phi_bin_stat (1, buf_num); cptr->filled_phi_matrix[(SDDAS_SHORT) buf_num] = 0; } } } else { ir_theta_bin_stat (1); cptr->filled_phi_matrix[(SDDAS_SHORT) cur_buf] = 0; } } if (reset_called) ginfo.called_locate = 0; return (ALL_OKAY); }