/* * 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 "@(#) read_tensor.c 1.1 05/08/19 SwRI" #include #include "ret_codes.h" #include "gen_defs.h" #include "libbase_idfs.h" #include "libVIDF.h" /* for header format comparison */ /**************************************************************************** * * * READ_TENSOR_DATA SUBROUTINE * * * * DESCRIPTION * * This is the read routine used by all instruments to retrieve tensor * * data sets. The sensor data for the data set of interest is retrieved, * * if applicable, and placed in the data structure that is allocated to * * hold the tensor values to be returned to the user. All data is * * returned in final stored units. For the time being, tensor data can * * not be converted to any other physical unit since it is unclear how * * to define/apply tables for tensor data. For this routine, the fwd flag * * determines when the NEXT time sample is to be processed. If ALL * * sensors for a given data set are being requested, the user should set * * the fwd flag to 1 for the last sensor in the group, the rest should use * * a 0 for the fwd flag. This ensures that all data taken at the same * * time will be processed before advancing to the next time sample. For * * tensor data sets, there is no "packing" of multiple samples within a * * single sensor set (like scalar under original IDFS definition). * * * * 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 *tensor_data_ptr ptr to memory location for the structure * * that holds returned tensor data values * * SDDAS_SHORT sen the sensor for which data is requested * * SDDAS_CHAR fwd flag that indicates when to advance to the * * time sample * * * * USAGE * * x = read_tensor_data (data_key, exten, vnum, tensor_data_ptr, sen, * * fwd) * * * * NECESSARY SUBPROGRAMS * * strcpy() copies a string to another string variable * * ir_locate_ex() determines if requested combination has * * already been pand points to the * * correct structure allocated for the combo * * ir_next_idf_file() determines if a new VIDF file needs to be * * opened and utilized for the data set being * * processed * * ir_reset_tensor_header() sets data elements that are dependent upon * * the information in the header file * * ir_new_data_record () retrieves the next data record * * ir_new_header_record () retrieves the next header record * * ir_read_tensor_times () fills in the time values returned by this * * module * * ir_read_tensor_time_advance() advances to the next time sample * * ir_read_tensor_data_values () retrieves data for sensor requested * * * * EXTERNAL VARIABLES * * struct general_info structure holding information concerning * * ginfo the experiment that is being processed * * SDDAS_CHAR ir_reset_sensors flag indicating if a new header was read,* * in which case, new sensors may be returned * * SDDAS_CHAR ir_block_adv flag indicating if time advancement should * * be blocked * * * * INTERNAL VARIABLES * * struct tensor_data structure that holds all of the currently * * *TENSOR_DATA returned tensor data values to be processed* * struct experiment_info a pointer to the structure that holds * * *ex specific experiment information * * struct ptr_rec *ptr a pointer to the structure which holds all * * pointers to the header and data for the * * experiment of interest * * register SDDAS_LONG *l1 pointer to long variables * * reg SDDAS_LONG *l_end loop termination variable * * register SDDAS_SHORT *s1 pointer to SDDAS_SHORT variables * * reg SDDAS_SHORT *s_end loop termination variable * * reg SDDAS_UCHAR *uc1 pointer to process mode values * * reg SDDAS_UCHAR *uc_end loop termination variable * * SDDAS_LONG sen_index index value that tells the position of the * * sensor within the sensors returned * * SDDAS_LONG hdr_offset indicates which header record to access * * SDDAS_SHORT rval holds called routine status flags * * SDDAS_CHAR found_sensor flag indicating if the requested sensor is * * in the sensors returned and is associated * * with the time period being processed * * char reset_called flag indicating if LOCATE_EX was called * * * * SUBSYSTEM * * Display Level * * * ***************************************************************************/ SDDAS_SHORT read_tensor_data (SDDAS_ULONG data_key, SDDAS_CHAR *exten, SDDAS_USHORT vnum, void *data_ptr, SDDAS_SHORT sen, SDDAS_CHAR fwd) { extern struct general_info ginfo; extern SDDAS_CHAR ir_reset_sensors, ir_block_adv; struct tensor_data *TENSOR_DATA; struct experiment_info *ex; struct ptr_rec *ptr; register SDDAS_LONG *l1, *l_end; register SDDAS_SHORT *s1, *s_end; register SDDAS_UCHAR *uc1, *uc_end; SDDAS_LONG sen_index, hdr_offset; SDDAS_SHORT rval; SDDAS_CHAR found_sensor; char reset_called; /****************************************************************************/ /* 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. */ /****************************************************************************/ TENSOR_DATA = (struct tensor_data *) data_ptr; if (TENSOR_DATA->header_format != TENSOR_SINGLE_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. */ /*************************************************************************/ TENSOR_DATA->filled_data = 0; if (!ginfo.called_locate) { rval = ir_locate_ex (data_key, exten, vnum, 0); if (rval != ALL_OKAY) return (TENSOR_NOT_FOUND); ginfo.called_locate = 1; reset_called = 1; } else reset_called = 0; /************************************************************************/ /* This module should only be called for single-valued tensor data. */ /************************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; if (ex->header_format != TENSOR_SINGLE_HEADER) return (WRONG_HEADER_FORMAT); /************************************************************************/ /* Check file descriptors for the data and header files. If either of */ /* the file descriptors is less than 0, the file has not been opened. */ /************************************************************************/ if (ex->fdh < 0 || ex->fdd < 0) { if (reset_called) ginfo.called_locate = 0; return (TENSOR_NO_FILES); } ptr->time_advanced = 0; if (ptr->chg_sen_set) TENSOR_DATA->hdr_change = 0; /************************************************************************/ /* Set the time values to last time calculated in case EOF's are hit */ /* since fill_data() uses ALL sweeps, even EOF sweeps. */ /************************************************************************/ TENSOR_DATA->bmilli = ex->btime_ms % 86400000; TENSOR_DATA->bnano = ex->btime_ns; TENSOR_DATA->bsec = (TENSOR_DATA->bmilli + (TENSOR_DATA->bnano / 1000000)) / 1000; TENSOR_DATA->bnsec = (TENSOR_DATA->bmilli % 1000) * 1000000 + TENSOR_DATA->bnano; TENSOR_DATA->emilli = ex->etime_ms % 86400000; TENSOR_DATA->enano = ex->etime_ns; TENSOR_DATA->esec = (TENSOR_DATA->emilli + (TENSOR_DATA->enano / 1000000)) / 1000; TENSOR_DATA->ensec = (TENSOR_DATA->emilli % 1000) * 1000000 + TENSOR_DATA->enano; ir_reset_sensors = 0; /**************************************************************************/ /* Was there a problem reading from the data file last time? Send the */ /* TENSOR_EOF flag to ir_new_header_record() to be sure that the same */ /* status is used by both routines. Send no_sensor to new_data_record */ /* and new_header_record so return code will be TENSOR_EOF_NO_SENSOR if */ /* data still not there since no data contained in TENSOR_DATA to be */ /* plotted. */ /**************************************************************************/ if (ex->drec_eof != 0) { ex->drec_eof = 0; if ((rval = ir_new_data_record (0)) != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } if ((rval = ir_new_header_record (0, data_ptr)) != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } ir_reset_sensors = 1; } /************************************************************************/ /* Was there a problem reading from the header file last time? */ /************************************************************************/ if (ex->hrec_eof != 0) { ex->hrec_eof = 0; if ((rval = ir_new_header_record (0, data_ptr)) != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } ir_reset_sensors = 1; } /************************************************************************/ /* Find the position of the sensor requested in the sensor index array. */ /************************************************************************/ found_sensor = 0; l1 = &sen_index; s1 = ptr->SENSOR_INDEX; s_end = s1 + *ptr->N_SEN; for (*l1 = 0; s1 < s_end; ++(*l1), ++s1) if (*s1 == sen) { found_sensor = 1; if (ex->sen_mode < 2 || ex->sen_mode == 4 || ex->sen_mode == 5) { if (*l1 != ptr->time_col) found_sensor = 0; } break; } /*************************************************************************/ /* Was a new header read on the previous call to this routine? If so, */ /* certain data elements need to be reset. */ /*************************************************************************/ if (ptr->reset_hdr) { /**********************************************************************/ /* This check is made in two places to facilitate plotting of data. */ /* (allow the plotting of a partial sweep) */ /**********************************************************************/ hdr_offset = (*ptr->NSS > 0) ? *(ptr->HDR_OFF + ptr->cur_sen_set) : *(ptr->HDR_OFF + 0); if (hdr_offset == NO_MORE_DATA) { if (reset_called) ginfo.called_locate = 0; return (LOS_STATUS); } else if (hdr_offset == NEXT_FILE) { if (reset_called) ginfo.called_locate = 0; return (NEXT_FILE_STATUS); } if ((rval = ir_reset_tensor_header (data_ptr, sen)) != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } } /**************************************************************************/ /* These two fields are updated if a header changes, but if the user is */ /* swapping between various virtuals, or the same virtual, but different */ /* time spans, and a header change was not detected for the virtual in */ /* question, user gets wrong YEAR/DAY values being returned. */ /**************************************************************************/ else { TENSOR_DATA->byear = *ptr->YEAR; TENSOR_DATA->bday = *ptr->DAYOFYEAR; TENSOR_DATA->eyear = *ptr->YEAR; TENSOR_DATA->eday = *ptr->DAYOFYEAR; TENSOR_DATA->mode_byear = *ptr->YEAR; TENSOR_DATA->mode_bday = *ptr->DAYOFYEAR; TENSOR_DATA->mode_eyear = *ptr->YEAR; TENSOR_DATA->mode_eday = *ptr->DAYOFYEAR; /***********************************************************************/ /* Set the beginning time and ending time for this time sample. */ /* Adjust the time components for day boundary crossings. The byear */ /* and eyear elements are the same value BEFORE time adjustment. */ /***********************************************************************/ ir_read_tensor_times (data_ptr, sen); } /*************************************************************************/ /* Instrument mode flags are returned every read. */ /* For optimization purposes, it's advised to use ++x instead of x++. */ /************************************************************************/ TENSOR_DATA->mode_len = *ptr->I_MODE; l1 = TENSOR_DATA->mode; uc1 = ptr->MODE_INDEX; uc_end = uc1 + *ptr->I_MODE; for (; uc1 < uc_end; ++l1, ++uc1) *l1 = *uc1; /*************************************************************************/ /* If the sensor was found, retrieve the data for the requested sensor. */ /*************************************************************************/ TENSOR_DATA->num_dqual = *ptr->hdr_fmt2_ptr->DQUAL_SIZE; if (found_sensor) { l1 = TENSOR_DATA->d_qual; uc1 = ptr->DQUAL + (sen_index * *ptr->hdr_fmt2_ptr->DQUAL_SIZE); uc_end = uc1 + *ptr->hdr_fmt2_ptr->DQUAL_SIZE; for (; uc1 < uc_end; ++l1, ++uc1) *l1 = *uc1; rval = ir_read_tensor_data_values (data_ptr, sen, sen_index); if (rval != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } } else { l1 = TENSOR_DATA->d_qual; l_end = l1 + *ptr->hdr_fmt2_ptr->DQUAL_SIZE; for (; l1 < l_end; ++l1) *l1 = -1; /* set to -1 to mean BOGUS value since 0-255 */ } /*************************************************************************/ /* If more than 1 IDF file is to be utilized, check the time to see if */ /* the next IDF file should be read. Be sure to reset ex structure */ /* since VIDF file crossing may mean a reallocation of the existing ex */ /* structures so address may change (do this regardless of error code). */ /*************************************************************************/ if (ex->de_year != -1) { rval = ir_next_idf_file (TENSOR_DATA->bmilli, TENSOR_DATA->bnano); ex = ginfo.expt; if (rval != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } } /*************************************************************************/ /* Save various data elements associated with this call to the generic */ /* read routine. */ /*************************************************************************/ ex->last_sensor = sen; TENSOR_DATA->data_key = data_key; TENSOR_DATA->version = vnum; TENSOR_DATA->sensor = sen; TENSOR_DATA->spin_rate = *(ptr->SPIN); TENSOR_DATA->sun_sen = *(ptr->SUN_SEN); strcpy (TENSOR_DATA->exten, exten); /******************************************************************/ /* Do we need to advance the pointers to the next time sample? */ /******************************************************************/ if (fwd && (!ir_reset_sensors || !ir_block_adv)) { rval = ir_read_tensor_time_advance (data_ptr, sen, found_sensor); if (rval != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (rval); } } /*************************************************************************/ /* If the sensor requested was not found, return a different code from */ /* code meaning the sensor was found and data is being returned. */ /*************************************************************************/ if (reset_called) ginfo.called_locate = 0; if (!found_sensor) return (TENSOR_NO_SENSOR); else return (ALL_OKAY); }