/* * 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 "@(#) conv_cal.c 1.35 05/08/19 SwRI" #include #include "ret_codes.h" #include "gen_defs.h" #include "user_defs.h" #include "libbase_idfs.h" /******************************************************************************* * * * IR_CONVERT_CAL_DATA SUBROUTINE * * * * DESCRIPTION * * This routine is called to convert the calibration data for the requested * * sensor into units by applying the specified tables and the table operations* * in the specified order to achieve the desired output. The order is implied* * by the table specification. If the chk_fill flag is set to 1, the data * * values will be compared against the specified FILL value. If the data * * value is identified as a fill value, the value OUTSIDE_MIN will be * * returned and it is up to the user to decide what is to be done with this * * value. The FILL value must be specified in terms of raw telemetry. * * * * INPUT VARIABLES * * SDDAS_SHORT sensor the sensor being processed * * SDDAS_CHAR cal_set calibration set number * * SDDAS_CHAR num_tbls number of tables to apply * * SDDAS_CHAR *tbls_to_apply array of tables to be applied * * SDDAS_LONG *tbl_oper array of operations to be applied to tables * * SDDAS_CHAR chk_fill flag indicating if the data is to be checked* * for FILL values * * SDDAS_FLOAT *ret_data array in which the data is returned in the * * converted units * * SDDAS_LONG fill_value the value identified as the FILL value * * void *idf_data_ptr ptr to the memory location for the structure* * that holds returned data values (read_drec) * * * * USAGE * * x = ir_convert_cal_data (sensor, cal_set, num_tbls, tbls_to_apply, * * tbl_oper, &ret_data, chk_fill, fill_value, idf_data_ptr) * * * * NECESSARY SUBPROGRAMS * * ir_transfer_data () converts the raw integer telemetry to the * * raw units (integer or SDDAS_FLOAT) * * abs() returns the absolute value of a number * * ir_idf_ex_func () performs higher order table operations * * IDF_OPER[]._math_drv () performs the arithmetic operation defined * * for the table being processed * * COMBINE_OPER[]._math_drv() performs the arithmetic operation defined * * to combine the permanent and temporary * * accumulators * * sizeof () the size of the specified object in bytes * * malloc() allocates memory * * free () frees previously allocated memory * * * * EXTERNAL VARIABLES * * struct general_info ginfo structure that holds information concerning * * the experiment that is being processed * * struct def_oper IDF_OPER[] array of pointers to the routines that * * perform the arithmetic operation defined * * for the sensor, sweep, and cal. data tables * * struct def_combine_oper array of pointers to the routines that * * COMBINE_OPER[] perform the arithmetic operation defined * * to combine the permanent and temporary * * accumulators * * void *ir_tmp_space scratch space for various uses * * * * INTERNAL VARIABLES * * struct idf_data *EXP_DATA structure that holds all of the currently * * returned data values to be processed * * struct experiment_info *ex a pointer to the structure that holds * * specific experiment information * * struct sensor_tables *sptr a pointer to the structure which holds the * * various information for the tables utilized * * by sensor-table combination being processed * * struct ptr_rec *ptr a pointer to the structure which holds all * * pointers to the header and data for the * * experiment of interest * * struct inst_tbl_info a pointer to the structure which holds the * * *tbl_info_ptr non-array table definition information for * * each table defined for the data source * * register SDDAS_LONG j, k looping variables * * register SDDAS_ULONG *ul1 pointer to number in each cal set info. * * register SDDAS_CHAR i looping variable * * SDDAS_FLOAT *which_data pointer to the accumulator of interest * * SDDAS_FLOAT *hold_data pointer to the temporary array that holds * * the cal. data as it is being converted * * SDDAS_FLOAT *from_data_ptr pointer to the accumulator specified as the * * from data buffer (source) * * SDDAS_FLOAT *to_data_ptr pointer to the accumulator specified as the * * to data buffer (destination) * * SDDAS_LONG data_start, offset into the data at which to start * * dstart picking up data values (used for cal.) * * SDDAS_LONG num_data the number of data values being returned * * SDDAS_LONG hold_val the number of successive data values within * * a data set that one calibration value is to * * be applied to * * SDDAS_LONG num_ele_in_tbl no. of elements in the table being applied * * SDDAS_LONG stop_ind the stopping value for the loop * * SDDAS_LONG time_row the element within a packed scalar being * * processed * * SDDAS_LONG data_offset offset to get to correct advanced buffer * * SDDAS_LONG tbl_operator the table operator being accessed * * SDDAS_LONG num_left_to_do no. of values that still need processing * * SDDAS_ULONG *ul_end loop termination variable * * SDDAS_LONG oper_index the table operation to be performed * * SDDAS_SHORT from_buf_num accumulator defined as the from data buffer * * SDDAS_SHORT to_buf_num accumulator defined as the to data buffer * * SDDAS_CHAR *sen pointer to sensor data from READ_DREC() * * SDDAS_CHAR *swp pointer to sweep data from READ_DREC() * * SDDAS_CHAR *cal pointer to the cal. data from READ_DREC() * * SDDAS_CHAR cur_tbl the current table being processed * * SDDAS_CHAR dtype the format of the data - uns int, int, float* * SDDAS_CHAR *data_tbl pointer to the data values which the table * * values are to be applied to * * SDDAS_CHAR which_cal the calibration set being processed * * SDDAS_CHAR packed_scalar flag indicating if a packed scalar is being * * processed * * SDDAS_CHAR tbl_var the functional dependence of the table * * SDDAS_CHAR *sc_pot ptr to spacecraft potential data from * * READ_DREC() * * SDDAS_CHAR *background ptr to background data from READ_DREC() * * void *tmp_ptr pointer which holds address passed back by * * the call to the MALLOC routine * * size_t bytes the number of bytes to allocate * * size_t num_bytes_sfloat the number of bytes needed for a SDDAS_FLOAT* * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_SHORT ir_convert_cal_data (SDDAS_SHORT sensor, SDDAS_CHAR cal_set, SDDAS_CHAR num_tbls, SDDAS_CHAR *tbls_to_apply, SDDAS_LONG *tbl_oper, SDDAS_FLOAT *ret_data, SDDAS_CHAR chk_fill, SDDAS_LONG fill_value, void *idf_data_ptr) { extern struct general_info ginfo; extern struct def_oper IDF_OPER[]; extern struct def_combine_oper COMBINE_OPER[]; extern void *ir_tmp_space; struct idf_data *EXP_DATA; struct experiment_info *ex; struct sensor_tables *sptr; struct ptr_rec *ptr; struct inst_tbl_info *tbl_info_ptr; register SDDAS_LONG j, k; register SDDAS_ULONG *ul1; register SDDAS_CHAR i; SDDAS_FLOAT *which_data, *hold_data, *from_data_ptr, *to_data_ptr; SDDAS_LONG data_start, dstart, num_data, hold_val, num_ele_in_tbl, stop_ind; SDDAS_LONG time_row, data_offset, oper_index, tbl_operator, num_left_to_do; SDDAS_ULONG *ul_end; SDDAS_SHORT from_buf_num, to_buf_num; SDDAS_CHAR *sen, *swp, *cal, cur_tbl, dtype, *data_tbl; SDDAS_CHAR which_cal, packed_scalar, tbl_var, *sc_pot, *background; void *tmp_ptr; size_t bytes, num_bytes_sfloat; /**********************************************************************/ /* Set pointers to sensor, sweep step, and calibration data returned */ /* by the READ_DREC() routine. */ /**********************************************************************/ EXP_DATA = (struct idf_data *) idf_data_ptr; sen = (SDDAS_CHAR *) EXP_DATA->sen_data; swp = (SDDAS_CHAR *) EXP_DATA->swp_data; cal = (SDDAS_CHAR *) EXP_DATA->cal_data; sc_pot = (SDDAS_CHAR *) EXP_DATA->potential; background = (SDDAS_CHAR *) EXP_DATA->background; /**********************************************************************/ /* Allocate space to temporarily hold cal. values since data needs */ /* to be expanded out to vector length (one cal per data returned). */ /**********************************************************************/ num_bytes_sfloat = sizeof (SDDAS_FLOAT); bytes = num_bytes_sfloat * EXP_DATA->num_sample; if ((tmp_ptr = malloc (bytes)) == NO_MEMORY) return (CONV_CAL_MALLOC); hold_data = (SDDAS_FLOAT *) tmp_ptr; /**********************************************************************/ /* Set pointers to the current sensor being processed. */ /**********************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; sptr = ex->sen_tbl_ptr + *(ex->index_sen_tbl + sensor); /***********************************************************************/ /* Determine the index into the calibration data array at which this */ /* calibration set starts. Transfer the integer raw values into */ /* the data array to be returned to the user. There is no D_TYPE for */ /* calibration data. It is always an unsigned long. */ /***********************************************************************/ for (j = 0, data_start = 0; j < cal_set; ++j) data_start += *(EXP_DATA->cset_num + j); /******************************************************************/ /* For a packed scalar, if the call to read_drec() did not */ /* request a full_swp (all samples packed into a sensor set) */ /* then only one value from a packed scalar with n_sample */ /* elements must be extracted one at a time, just like the data. */ /* The value for time_row must be carefully set since read_drec */ /* has already advanced it to the next row or next sensor set if */ /* the last row was processed. */ /******************************************************************/ time_row = 0; packed_scalar = 0; num_data = *(EXP_DATA->cset_num + cal_set); if (ex->smp_id == 2 && EXP_DATA->num_sample == 1 && num_data != 1) { if (ptr->time_advanced) time_row = ptr->time_row - 1; else time_row = ptr->time_row; if (time_row < 0) time_row = num_data - 1; data_start += time_row; num_data = EXP_DATA->num_sample; packed_scalar = 1; } ir_transfer_data (hold_data, cal, num_data, *(ex->cal_d_type + cal_set), *(ex->cal_wlen + cal_set), data_start); /*********************************************************************/ /* If tables are to be applied, convert to the data accordingly. */ /*********************************************************************/ for (i = 0; i < num_tbls; ++i) { /*******************************************************************/ /* Check for combination of accumulators since operations are NOT */ /* table specific. */ /*******************************************************************/ tbl_operator = *(tbl_oper + i); if (tbl_operator > 2000 && tbl_operator < 3000) { which_data = (SDDAS_FLOAT *) ir_tmp_space; oper_index = tbl_operator % 2000; COMBINE_OPER[oper_index % 10]._math_drv (hold_data, which_data, num_data); /************************************************************/ /* Perform the higher order operations. */ /************************************************************/ if (oper_index > 9) ir_idf_ex_func (oper_index/10, hold_data, num_data, idf_data_ptr); continue; } /*******************************************************************/ /* Check for combination of advanced buffers since operations are */ /* NOT table specific. */ /*******************************************************************/ if (tbl_operator > 10000) { /*******************************************************************/ /* Divisions are costly - see if we can change this. */ /* Should call to COMBINE_OPER and ir_idf_ex_func use hold_data? */ /*******************************************************************/ from_buf_num = tbl_operator / 10000; to_buf_num = (tbl_operator - from_buf_num * 10000) / 1000; oper_index = tbl_operator - (from_buf_num * 10000) - (to_buf_num * 1000); /* Cast ir_tmp_space to a char. ptr since it is void and offset is in bytes.*/ data_offset = (from_buf_num - 1) * ex->swp_len * num_bytes_sfloat; from_data_ptr = (SDDAS_FLOAT *) ((SDDAS_CHAR *) ir_tmp_space + data_offset); /*******************************************************************/ /* FROM buffer cannot be primary buffer, but TO buffer can be. */ /*******************************************************************/ if (to_buf_num == 0) to_data_ptr = ret_data; else { /* Cast ir_tmp_space to char. ptr since it's void & offset in bytes.*/ data_offset = (to_buf_num - 1) * ex->swp_len * num_bytes_sfloat; to_data_ptr = (SDDAS_FLOAT *) ((SDDAS_CHAR *) ir_tmp_space + data_offset); } COMBINE_OPER[oper_index % 10]._math_drv (to_data_ptr, from_data_ptr, num_data); /************************************************************/ /* Perform the higher order operations. */ /************************************************************/ if (oper_index > 9) ir_idf_ex_func (oper_index/10, to_data_ptr, num_data, idf_data_ptr); continue; } cur_tbl = *(tbls_to_apply + i); to_buf_num = tbl_operator / 1000; tbl_info_ptr = ex->tbl_info_ptr + cur_tbl; tbl_var = tbl_info_ptr->tbl_var; /********************************************************************/ /* A check is made upfront for ASCII tables and tables associated */ /* with status mode information. */ /********************************************************************/ if (tbl_info_ptr->tbl_type == ASCII_TBL) continue; else if (tbl_var == FN_RAW_MODE || tbl_var == FN_PROCESSED_MODE) continue; /********************************************************************/ /* Is this table to be applied for the conversion of the data? */ /* A size of 0 indicates that no table is defined (tbl_fmt < 0); */ /* therefore, this table is bypassed for data conversion. */ /********************************************************************/ if (*(sptr->tbl_size + cur_tbl) != 0) { if (to_buf_num == 0) which_data = hold_data; else { /* Cast ir_tmp_space to char. ptr since it's void & offset in bytes.*/ data_offset = (to_buf_num - 1) * ex->swp_len * num_bytes_sfloat; which_data = (SDDAS_FLOAT *) ((SDDAS_CHAR *) ir_tmp_space + data_offset); } oper_index = tbl_operator % 1000; /*****************************************************************/ /* This table is a function of a calibration set. Determine the */ /* index value to get to the calibration values in question. */ /*****************************************************************/ dstart = 0; if (tbl_var < 0) { which_cal = abs (tbl_var) - 1; if (ex->smp_id != 2 && *(EXP_DATA->cset_num + which_cal) != num_data) { free (tmp_ptr); return (CONV_CAL_VECTOR_MISMATCH); } /****************************************************************/ /* Okay for data_start and num_data to be SDDAS_LONG and not */ /* SDDAS_ULONG since this module is used to convert non-tensor */ /* IDFS data. */ /****************************************************************/ ul1 = EXP_DATA->cset_num; ul_end = ul1 + which_cal; for (; ul1 < ul_end; ++ul1) dstart += *ul1; /***************************************************************/ /* Point to the correct location within the packed scalar. */ /***************************************************************/ if (packed_scalar) dstart += time_row; /***************************************************************/ /* Determine the number of data elements each cal. value is */ /* to be applied to. A single cal. value is to be applied to */ /* each data element if CAL_USE = 0. */ /***************************************************************/ if (*(ex->CAL_USE + which_cal) == 0) hold_val = num_data; else hold_val = *(ex->CAL_USE + which_cal); } /***************************************************************/ /* Start at the beginning of the data array and there is a one-*/ /* to-one mapping between table elements and data elements. */ /***************************************************************/ else hold_val = 1; /************************************************************/ /* The table is a function of raw sensor data. */ /************************************************************/ if (tbl_var == FN_RAW_SENSOR) { data_tbl = sen; dtype = *(ex->d_type + sensor); free (tmp_ptr); return (CONV_CAL_VECTOR_MISMATCH); } /**************************************************************/ /* The table is a function of raw sweep step data, which is */ /* always unsigned integer binary data. */ /**************************************************************/ else if (tbl_var == FN_RAW_SCAN) { data_tbl = swp; dtype = 0; free (tmp_ptr); return (CONV_CAL_VECTOR_MISMATCH); } /**************************************************************/ /* The table is a function of a calibration set. */ /**************************************************************/ else if (tbl_var < 0) { data_tbl = cal; which_cal = abs (tbl_var) - 1; dtype = *(ex->cal_d_type + which_cal); } /****************************************************************/ /* The table is a function of spacecraft potential data, which */ /* is always floating point data within the EXP_DATA structure.*/ /****************************************************************/ else if (tbl_var == FN_SC_POTENTIAL) { /*************************************************************/ /* No spacecraft potential data? */ /*************************************************************/ if (EXP_DATA->num_potential == 0) continue; data_tbl = sc_pot; dtype = 2; free (tmp_ptr); return (CONV_CAL_VECTOR_MISMATCH); } /****************************************************************/ /* The table is a function of background data, which is always */ /* floating point data within the EXP_DATA structure. */ /****************************************************************/ else if (tbl_var == FN_BACKGROUND) { /*************************************************************/ /* No background data? */ /*************************************************************/ if (EXP_DATA->num_background == 0) continue; data_tbl = background; dtype = 2; free (tmp_ptr); return (CONV_CAL_VECTOR_MISMATCH); } /***************************************************************/ /* The table is a function of a processed data, which is */ /* always floating point data (since processed). Took out the */ /* automatic return of error code CONV_CAL_VECTOR_MISMATCH */ /* since it made sense to allow for this once the data has been*/ /* currectly placed into the "working buffer", which is true */ /* since the data is processed data. Leave the hard error in */ /* all other cases and examine on an as needed basis. */ /***************************************************************/ else { if (to_buf_num == 0) data_tbl = (SDDAS_CHAR *) hold_data; else { data_offset = (to_buf_num - 1) * ex->swp_len * num_bytes_sfloat; data_tbl = (SDDAS_CHAR *) (ir_tmp_space + data_offset); } dtype = 2; } /****************************************************************/ /* Perform the operation defined for the table. */ /****************************************************************/ if (tbl_info_ptr->tbl_type == INTEGER_SCAN_TBL) num_ele_in_tbl = *(sptr->tbl_size + cur_tbl) / ex->swp_len; else num_ele_in_tbl = *(sptr->tbl_size + cur_tbl); IDF_OPER[oper_index % 10]._math_drv (which_data, data_tbl, dstart, *(sptr->tbl_ptr + cur_tbl), num_ele_in_tbl, num_data, *(sptr->tbl_expand + cur_tbl), dtype, hold_val, tbl_info_ptr->cal_fn_of_raw, *(ex->sen_tdw_len + sensor), tbl_info_ptr->tbl_type, EXP_DATA->swp_data, ex->swp_len); /************************************************************/ /* Perform the higher order operations. */ /************************************************************/ if (oper_index > 9) ir_idf_ex_func (oper_index/10, which_data, num_data, idf_data_ptr); } } /************************************************************************/ /* Check the data for FILL values. If the data is found to be fill */ /* data, the value returned is set to OUTSIDE_MIN. */ /************************************************************************/ if (chk_fill) for (j = 0; j < num_data; ++j) { if (*(EXP_DATA->cal_data + data_start + j) == fill_value) *(hold_data + j) = OUTSIDE_MIN; } /***************************************************************/ /* Determine the number of data elements each cal. value is */ /* to be applied to. A single cal. value is to be applied to */ /* each data element if CAL_USE = 0. */ /***************************************************************/ if (*(ex->CAL_USE + cal_set) == 0) hold_val = EXP_DATA->num_sample; else hold_val = *(ex->CAL_USE + cal_set); /************************************************************************/ /* For optimization purposes, it's advised to use ++x instead of x++. */ /************************************************************************/ for (j = 0; j < EXP_DATA->num_sample;) { num_left_to_do = EXP_DATA->num_sample - j; stop_ind = (num_left_to_do < hold_val) ? num_left_to_do : hold_val; for (k = 0; k < stop_ind; ++k, ++ret_data) *ret_data = *hold_data; j += stop_ind; ++hold_data; } free (tmp_ptr); return (ALL_OKAY); }