/* * 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_units.c 1.32 05/08/19 SwRI" #include #include "user_defs.h" #include "ret_codes.h" #include "libbase_idfs.h" #include "gen_defs.h" #include "libVIDF.h" /******************************************************************************* * * * CONVERT_TO_UNITS SUBROUTINE * * * * DESCRIPTION * * This routine is called in order to convert the raw telemetry values * * read by the READ_DREC() routine into the units specified by the calling * * routine. This routine will convert sensor data, sweep data, calibration * * and status (mode) data into the units specified by applying the tables * * specified by the calling routine. It was decided that there would be no * * tables to be applied to the data quality flag. It is assumed that the * * calling routine has allocated enough space to hold all values being * * returned. Angular information has been added, but there are no tables to * * be applied. Later versions of the software will allow for table definition* * to convert from degrees (what is always returned from read_drec) to * * radians. * * * * INPUT VARIABLES * * SDDAS_ULONG data_key key which uniquely identifies the data set * * being 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) * * void *idf_data_ptr ptr to memory location for the structure * * that holds returned data values (read_drec) * * SDDAS_SHORT sensor the sensor being processed * * SDDAS_CHAR data_type the type of data being processed - sensor, * * sweep step, cal, mode, or d_qual * * 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_FLOAT *ret_data array in which the data is returned in the * * converted units * * SDDAS_CHAR chk_fill flag indicating if the data is to be checked* * for FILL values * * SDDAS_LONG fill_value the value identified as the FILL value * * * * USAGE * * x = convert_to_units (data_key, exten, vnum, idf_data_ptr, sensor, * * data_type, cal_set, num_tbls, tbls_to_apply, * * tbl_oper, &ret_data, chk_fill, fill_value) * * * * NECESSARY SUBPROGRAMS * * sizeof () the size of the specified object in bytes * * realloc() reallocates previously allocated memory * * ir_locate_ex() determines if the requested combination has * * already been processed and points to the * * correct structure allocated for the combo * * ir_convert_sensor_data () converts sensor data into requested units * * ir_convert_sweep_data () converts sweep data into requested units * * ir_convert_cal_data () converts cal. data into requested units * * ir_convert_mode_data () converts mode data into requested units * * ir_convert_spacecraft_potential_data () converts spacecraft potential * * data into requested units * * * * EXTERNAL VARIABLES * * struct general_info ginfo structure holding information concerning * * the experiment that is being processed * * SDDAS_UINT ir_tmp_bytes number of bytes allocated for scratch space * * 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 a pointer to the structure that holds * * *ex specific experiment information * * reg SDDAS_FLOAT *new_data pointer to data values passed back to user * * reg SDDAS_FLOAT *dptr pointer to data values * * reg SDDAS_FLOAT *stop_dptr loop termination variable * * register SDDAS_LONG i looping variable * * SDDAS_LONG loop_end loop terminator * * SDDAS_LONG tbl_operator the table operator being accessed * * SDDAS_LONG oper_index the table operation to be performed * * SDDAS_LONG remove_from_buf used to compute TO advanced buffer number * * and advanced buffer operation * * size_t bytes the number of bytes to allocate space for * * SDDAS_SHORT rval holds value returned by called routine * * SDDAS_SHORT tmp_only number of tables that use intermediate * * accumulator * * SDDAS_SHORT tmp_final number of times the primary and intermediate* * accumulator are combined * * SDDAS_SHORT adv_only number of tables that use advanced buffers * * SDDAS_SHORT adv_final number of times the advanced data buffers * * are combined * * 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 data_loaded[] flag indicating data loaded into advanced * * buffers * * SDDAS_CHAR buffer_used[] flag indicating advanced buffer is needed * * for computations * * char reset_called flag indicating if LOCATE_EX was called * * void *tmp_ptr pointer which holds address passed back by * * the call to the MALLOC routine * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_SHORT convert_to_units (SDDAS_ULONG data_key, SDDAS_CHAR *exten, SDDAS_USHORT vnum, void *idf_data_ptr, SDDAS_SHORT sensor, SDDAS_CHAR data_type, 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) { extern struct general_info ginfo; extern SDDAS_UINT ir_tmp_bytes; extern void *ir_tmp_space; struct idf_data *EXP_DATA; struct experiment_info *ex; register SDDAS_FLOAT *new_data, *dptr, *stop_dptr; register SDDAS_LONG i; SDDAS_LONG loop_end, tbl_operator, oper_index, remove_from_buf; SDDAS_SHORT rval, tmp_only, tmp_final, adv_only, adv_final; SDDAS_SHORT from_buf_num, to_buf_num; SDDAS_CHAR data_loaded[MAX_UNITS_BUFFERS], buffer_used[MAX_UNITS_BUFFERS]; size_t bytes; char reset_called; void *tmp_ptr; /****************************************************************************/ /* Since void pointer, make sure correct type of data structure being used.*/ /* Data structures define the same elements at the beginning of the */ /* structure definition. */ /****************************************************************************/ EXP_DATA = (struct idf_data *) idf_data_ptr; if (EXP_DATA->header_format != ORIGINAL_HEADER) return (WRONG_DATA_STRUCTURE); /***************************************************************************/ /* Check to see if the combination being processed has been processed */ /* before. If not, an error condition - probably didn't call FILE_OPEN. */ /* Since ir_locate_ex() is called by more than one routine, return an */ /* error code that indicates which calling routine resulted in the error. */ /* Since a 0 is passed for the last parameter, the only possible error is */ /* that the requested combination was not found among processed combos. */ /***************************************************************************/ if (!ginfo.called_locate) { rval = ir_locate_ex (data_key, exten, vnum, 0); if (rval != ALL_OKAY) return (CNVT_NOT_FOUND); ginfo.called_locate = 1; reset_called = 1; } else reset_called = 0; rval = ALL_OKAY; ex = ginfo.expt; /***************************************************************************/ /* Bad data type? */ /***************************************************************************/ if (data_type > MAX_DATA_TYPE) return (CNVT_BAD_DTYPE); /***************************************************************************/ /* Reset data loaded into buffer flags. */ /***************************************************************************/ for (i = 0; i < MAX_UNITS_BUFFERS; ++i) { data_loaded[i] = 0; buffer_used[i] = 0; } /***************************************************************************/ /* Loop over all tables to see if intermediate accumulator is needed. If */ /* the user is combining the permanent and intermediate accumulator, no */ /* look-up operator is defined. */ /***************************************************************************/ tmp_only = tmp_final = 0; adv_only = adv_final = 0; for (i = 0; i < num_tbls; ++i) { /***********************************************************************/ /* Check for bad table number (should be from 0 - num_tbls). */ /***********************************************************************/ if (*(tbls_to_apply + i) >= ex->num_tbls) return (CNVT_BAD_TBL_NUM); /***********************************************************************/ /* Operators for primary buffer can be bypassed. */ /***********************************************************************/ tbl_operator = *(tbl_oper + i); if (tbl_operator < 1000) continue; if (tbl_operator >= 1000 && tbl_operator <= 1999) { ++tmp_only; data_loaded[0] = 1; } else if (tbl_operator > 2000 && tbl_operator <= 2999) ++tmp_final; else if (tbl_operator == 2000) return (CNVT_BAD_TBL_OPER); /***********************************************************************/ /* If using advanced buffers, need to perform some combination also. */ /***********************************************************************/ else if (tbl_operator >= 3000 && tbl_operator <= 9999) { ++adv_only; to_buf_num = (tbl_operator / 1000) - 1; data_loaded[to_buf_num] = 1; } else if (tbl_operator > 10000 && tbl_operator <= 99999) { ++adv_final; from_buf_num = tbl_operator / 10000; remove_from_buf = from_buf_num * 10000; to_buf_num = (tbl_operator - remove_from_buf) / 1000; oper_index = tbl_operator - remove_from_buf - (to_buf_num * 1000); /********************************************************************/ /* There is no buffer 2 - reserved for combo. operators. */ /********************************************************************/ if (from_buf_num == 2 || to_buf_num == 2) return (CNVT_BAD_BUF_NUM); /********************************************************************/ /* From and To buffer numbers cannot be the same value. */ /********************************************************************/ if (from_buf_num == to_buf_num) return (CNVT_SAME_BUF_NUM); /********************************************************************/ /* Look-up operator not allowed for FROM - TO manipulation. */ /********************************************************************/ if (oper_index % 10 == 0) return (CNVT_BAD_TBL_OPER); /********************************************************************/ /* Make sure data placed into intermediate or advanced buffers. */ /* 0 is not a proper value for the FROM buffer. */ /********************************************************************/ if (from_buf_num == 1) buffer_used[0] = 1; else buffer_used[from_buf_num - 1] = 1; /********************************************************************/ /* Set flag to indicate data placed into the TO buffer. */ /********************************************************************/ if (to_buf_num != 0) data_loaded[to_buf_num - 1] = 1; } } /****************************************************************************/ /* User wanted to combine the permanent and intermediate accumulators but */ /* nothing will be stored in the intermediate accumulator. */ /****************************************************************************/ if (tmp_final != 0 && tmp_only == 0) return (CNVT_NO_TMP); /****************************************************************************/ /* User wanted to utilize the from - to buffer operations. */ /****************************************************************************/ if (adv_final != 0) { /*************************************************************************/ /* Make sure if accessed intermediate and / or advanced buffers, that */ /* data has been placed into these buffers. */ /*************************************************************************/ for (i = 0; i < MAX_UNITS_BUFFERS; ++i) { if (buffer_used[i] && !data_loaded[i]) return (CNVT_NO_ADV); } } /***************************************************************************/ /* Space for the intermediate accumulator and / or the advanced buffers */ /* may need to be allocated. */ /***************************************************************************/ if (tmp_only != 0 || adv_only != 0) { bytes = MAX_UNITS_BUFFERS * sizeof (SDDAS_FLOAT) * ex->swp_len; if (bytes > ir_tmp_bytes) { if ((tmp_ptr = realloc (ir_tmp_space, bytes)) == NO_MEMORY) return (CNVT_TMP_MALLOC); ir_tmp_space = tmp_ptr; ir_tmp_bytes = (SDDAS_UINT) bytes; } /***********************************************************************/ /* Initialize temporary accumulator and advanced buffers for help */ /* with debugging. For optimization purposes, it's advised to use */ /* ++x instead of x++. */ /***********************************************************************/ new_data = (SDDAS_FLOAT *) ir_tmp_space; loop_end = MAX_UNITS_BUFFERS * ex->swp_len; stop_dptr = new_data + loop_end; for (; new_data < stop_dptr; ++new_data) *new_data = OUTSIDE_MAX; } /***************************************************************************/ /* Convert the sensor data. */ /***************************************************************************/ if (data_type == SENSOR) ir_convert_sensor_data (sensor, num_tbls, tbls_to_apply, tbl_oper, ret_data, chk_fill, fill_value, idf_data_ptr); /***************************************************************************/ /* Convert the sweep step data. */ /***************************************************************************/ else if (data_type == SWEEP_STEP) ir_convert_sweep_data (sensor, num_tbls, tbls_to_apply, tbl_oper, ret_data, idf_data_ptr); /***************************************************************************/ /* Convert the calibration data. */ /***************************************************************************/ else if (data_type == CAL_DATA) rval = ir_convert_cal_data (sensor, cal_set, num_tbls, tbls_to_apply, tbl_oper, ret_data, chk_fill, fill_value, idf_data_ptr); /***************************************************************************/ /* Convert the status (mode) data. */ /***************************************************************************/ else if (data_type == MODE) rval = ir_convert_mode_data (sensor, num_tbls, tbls_to_apply, tbl_oper, ret_data, idf_data_ptr); /***************************************************************************/ /* Simply copy the data quality value into the data array being returned. */ /* It is already the correct one for the sensor of interest, assuming */ /* read_drec has been called. */ /***************************************************************************/ else if (data_type == D_QUAL) { new_data = ret_data; *new_data = (SDDAS_FLOAT) EXP_DATA->d_qual; } /***************************************************************************/ /* All angle values are in degrees. Must define a table to convert to */ /* radians. */ /***************************************************************************/ else if (data_type == PITCH_ANGLE) { /***********************************************************************/ /* If no pitch angles were computed, set the data that is returned to */ /* a value that indicates a problem with pitch angles. Since there */ /* one value per sweep step (per data element), use num_sample. */ /***********************************************************************/ new_data = ret_data; if (EXP_DATA->num_pitch == 0) { stop_dptr = new_data + EXP_DATA->num_sample; for (; new_data < stop_dptr; ++new_data) *new_data = OUTSIDE_MIN; } else { dptr = EXP_DATA->pitch_angles; stop_dptr = dptr + EXP_DATA->num_pitch; for (; dptr < stop_dptr; ++new_data, ++dptr) *new_data = *dptr; } } else if (data_type == START_AZ_ANGLE) { new_data = ret_data; dptr = EXP_DATA->start_az; stop_dptr = dptr + EXP_DATA->num_angle; for (; dptr < stop_dptr; ++new_data, ++dptr) *new_data = *dptr; } else if (data_type == STOP_AZ_ANGLE) { new_data = ret_data; dptr = EXP_DATA->stop_az; stop_dptr = dptr + EXP_DATA->num_angle; for (; dptr < stop_dptr; ++new_data, ++dptr) *new_data = *dptr; } else if (data_type == SC_POTENTIAL) { /***********************************************************************/ /* If no spacecraft potential values were computed, set the data that */ /* is returned to a value that indicates a problem with potential. */ /* Since there is one value per sweep step (per data element), use */ /* num_sample. */ /***********************************************************************/ new_data = ret_data; if (EXP_DATA->num_potential == 0) { stop_dptr = new_data + EXP_DATA->num_sample; for (; new_data < stop_dptr; ++new_data) *new_data = OUTSIDE_MIN; } /***********************************************************************/ /* Parent source can define more units that MODIFY the potential */ /* data - that is, take the "processed" potential value and modify it */ /* even further. This is different from the sensor, sweep and cal. */ /* data which is always stored as raw data in the EXP_DATA structure. */ /***********************************************************************/ else ir_convert_spacecraft_potential_data (sensor, num_tbls, tbls_to_apply, tbl_oper, ret_data, idf_data_ptr); } else if (data_type == BACKGROUND) { /*************************************************************************/ /* If no background values were computed, set the data that is returned */ /* to a value that indicates a problem with this data. Since there is */ /* one value per sweep step (per data element), use num_sample. */ /*************************************************************************/ new_data = ret_data; if (EXP_DATA->num_background == 0) { stop_dptr = new_data + EXP_DATA->num_sample; for (; new_data < stop_dptr; ++new_data) *new_data = OUTSIDE_MIN; } /************************************************************************/ /* Parent source can define more units that MODIFY the background */ /* data - that is, take the "processed" background value and modify it */ /* even further. This is different from the sensor, sweep and cal. */ /* data which is always stored as raw data in the EXP_DATA structure. */ /************************************************************************/ else ir_convert_background_data (sensor, num_tbls, tbls_to_apply, tbl_oper, ret_data, idf_data_ptr); } if (reset_called) ginfo.called_locate = 0; return (rval); }