/* * 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 "@(#) pos_pback.c 1.11 05/08/19 SwRI" #include "gen_defs.h" #include #include #include "ret_codes.h" #include "libbase_idfs.h" #include "libVIDF.h" /* for header format comparison */ /****************************************************************************** * * * ir_position_playback SUBROUTINE * * * * DESCRIPTION * * This routine is called to position the data and header file pointers * * for the playback data files. A binary search is performed as a coarse * * search for the requested start time. A fine search is then performed in * * order to get as close to the requested start time as possible. * * * * INPUT VARIABLES * * SDDAS_ULONG data_key key which uniquely identifies the data set * * being processed * * SDDAS_CHAR *exten the filename extension for the data to be used * * SDDAS_USHORT vnum version number to be associated with this * * combination (allows for multiple opens) * * void *data_ptr ptr to the memory location for the structure * * that holds returned data values (either * * idf_data or tensor_data) * * SDDAS_SHORT btime_yr the start time requested (year component) * * SDDAS_SHORT btime_day the start time requested (day component) * * SDDAS_LONG btime_sec the start time requested (seconds component) * * SDDAS_LONG btime_nsec the start time requested (nanoseconds) * * SDDAS_SHORT etime_yr the stop time requested (year component) * * SDDAS_SHORT etime_day the stop time requested (day component) * * SDDAS_LONG etime_sec the stop time requested (seconds component) * * SDDAS_LONG etime_nsec the stop time requested (nanoseconds) * * * * USAGE * * x = ir_position_playback (data_key, exten, vnum, data_ptr, btime_yr, * * btime_day, btime_sec, btime_nsec, etime_yr, * * etime_day, etime_sec, etime_nsec) * * * * NECESSARY SUBPROGRAMS * * sizeof () the size of the specified object in bytes * * stat() obtains information ablout the file specified * * read() reads N bytes from the file associated with * * the file descriptor given * * lseek() moves the file pointer to a location within * * the file * * ir_alloc_exp_once() allocates memory that is needed in order to * * utilize the generic routines provided for * * retrieving experiment data * * ir_read_header() reads data from the header file * * ir_fine_search () positions the file pointer as close to the * * requested time as possible (within a record) * * ir_network_order() converts data read from data/header files * * between network byte order and host byte order * * ir_swap_data_array () swaps the data values if necessary to be * * in host byte order * * read_drec() the universal read routine that retrieves * * the data for the time sample being processed * * * * EXTERNAL VARIABLES * * struct general_info structure that holds information concerning * * ginfo the experiment that is being processed * * SDDAS_CHAR ir_swap_data flag that is used to determine when the data * * matrix is to be converted to host byte order * * * * INTERNAL VARIABLES * * struct idf_data structure that holds all of the currently * * *EXP_DATA returned data values to be processed * * struct experiment_info a pointer to the structure that holds specific * * *ex 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 * * struct stat stat_info structure which holds information concering * * the file specified * * int num_bytes the number of bytes requested from the file * * int ret_bytes the number of bytes actually read by READ() * * int low, high, mid variables used for the binary search on the * * data file to find the requested start time * * SDDAS_LONG num_records the number of data records in the data file * * SDDAS_LONG bytes no. of bytes to seek by for the LSEEK routine * * SDDAS_LONG ret_sec the composite time in seconds * * SDDAS_LONG ret_nsec nanoseconds component of the composite time * * SDDAS_LONG *hdr_off header offset value * * SDDAS_SHORT rval holds the value returned by the called routine * * SDDAS_BOOL one_read_performed flag indicating that at least one data / * * header read was performed * * SDDAS_BOOL data_gap flag indicating scenario when requested start * * time is in a gap between the end of a record * * and the start of the next * * char first_time flag indicating the first time through a loop * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_SHORT ir_position_playback (SDDAS_ULONG data_key, SDDAS_CHAR *exten, SDDAS_USHORT vnum, void *data_ptr, SDDAS_SHORT btime_yr, SDDAS_SHORT btime_day, SDDAS_LONG btime_sec, SDDAS_LONG btime_nsec, SDDAS_SHORT etime_yr, SDDAS_SHORT etime_day, SDDAS_LONG etime_sec, SDDAS_LONG etime_nsec) { extern struct general_info ginfo; extern SDDAS_CHAR ir_swap_data; struct idf_data *EXP_DATA; struct experiment_info *ex; struct ptr_rec *ptr; struct stat stat_info; int num_bytes, ret_bytes, low, high, mid; SDDAS_LONG num_records, bytes, ret_sec, ret_nsec, *hdr_off; SDDAS_SHORT rval; SDDAS_BOOL one_read_performed = sFalse, data_gap; char first_time; /**********************************************************************/ /* Determine the number of records in the data file. The last data */ /* record may be a partial record written by the listener, but ignore*/ /* this record. Use -2 instead of -1 to exclude trailer record */ /* written by IDFS builder program. */ /**********************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; num_bytes = stat (ex->data_name, &stat_info); num_records = stat_info.st_size / ptr->d_size; low = 0; high = num_records - 2; first_time = 1; /***********************************************************************/ /* Find the record closest to the requested start time using a binary */ /* search algorithm. */ /***********************************************************************/ while (low <= high) { mid = (low + high) / 2; bytes = ptr->d_size * mid; lseek (ex->fdd, (off_t) bytes, 0); /*******************************************************************/ /* Read a data record and the appropriate header record. Since */ /* the data and header files are copied and catalogued as is, a */ /* check still needs to be made for a short data record (written */ /* by listener). */ /*******************************************************************/ num_bytes = ptr->d_size; ret_bytes = read (ex->fdd, ex->DATA_MEM, num_bytes); if (ret_bytes < 0) return (POS_DATA_READ_ERROR); /********************************************************************/ /* The file closure flags are always in the first hdr_offset field.*/ /********************************************************************/ ir_swap_data = 1; ir_network_order (0, 1); if (ret_bytes != num_bytes) { hdr_off = (SDDAS_LONG *) (ex->DATA_MEM + 3 * sizeof (SDDAS_LONG)); if (*hdr_off == NO_MORE_DATA) return (PBACK_LOS); else if (*hdr_off == NEXT_FILE) return (PBACK_NEXT_FILE); else return (POS_DATA_READ_ERROR); } lseek (ex->fdh, (off_t) *(ptr->HDR_OFF), 0); if (first_time) { /****************************************************************/ /* Allocate memory needed in order to utilize generic routines */ /* provided for retrieving experiment data. This call will */ /* read the header record that is associated with data record. */ /****************************************************************/ rval = ir_alloc_exp_once (btime_yr, btime_day, btime_sec, btime_nsec, etime_yr, etime_day, etime_sec, etime_nsec, data_ptr); if (rval != ALL_OKAY) { if (rval == LOS_STATUS) return (PBACK_LOS); else if (rval == NEXT_FILE_STATUS) return (PBACK_NEXT_FILE); else return (rval); } first_time = 0; } /*****************************************************************/ /* Read the header associated with the data record. */ /*****************************************************************/ else { rval = ir_read_header (data_ptr); if (rval != ALL_OKAY) { if (rval == RHDR_READ_ERROR) return (POS_HDR_READ_ERROR); else if (rval == RHDR_HDR_MALLOC) return (POS_HDR_MALLOC); else if (rval == RHDR_HDR_REALLOC) return (POS_HDR_REALLOC); else if (rval == LOS_STATUS) return (PBACK_LOS); else if (rval == NEXT_FILE_STATUS) return (PBACK_NEXT_FILE); else return (rval); } } /********************************************************************/ /* Make sure the data array is in host byte order. Must be done */ /* AFTER the header record information has checked. */ /********************************************************************/ one_read_performed = sTrue; ir_swap_data_array (); /********************************************************************/ /* The requested start time is less than or equal to the time of */ /* the record. */ /********************************************************************/ ret_sec = *(ptr->TIME) / 1000; ret_nsec = (*(ptr->TIME) % 1000) * 1000000; /********************************************************************/ /* Add nanosecond time adjustment factor to dr_time, if one is */ /* defined for this virtual instrument. */ /********************************************************************/ if (ex->nano_defined) ret_nsec += *(ptr->NANO_RES); if (btime_yr < *ptr->YEAR || (btime_yr == *ptr->YEAR && btime_day < *ptr->DAYOFYEAR)) high = mid - 1; else if (btime_yr == *ptr->YEAR && btime_day == *ptr->DAYOFYEAR && btime_sec < ret_sec) high = mid - 1; else if (btime_yr == *ptr->YEAR && btime_day == *ptr->DAYOFYEAR && btime_sec == ret_sec && btime_nsec <= ret_nsec) high = mid - 1; /********************************************************************/ /* The requested start time is greater than the time of the record. */ /********************************************************************/ else low = mid + 1; } /**********************************************************************/ /* Check for empty data / header file situation. */ /**********************************************************************/ if (one_read_performed == sFalse) return (POS_DATA_READ_ERROR); /************************************************************************/ /* If the time is between records, the binary search returns the */ /* record with the time LARGER than the requested time, so back up */ /* to the previous record (if not at the beginning of the file) */ /* Assume the same year value. */ /************************************************************************/ if (high != -1 && (btime_day < *ptr->DAYOFYEAR || (btime_day == *ptr->DAYOFYEAR && btime_sec < ret_sec) || (btime_day == *ptr->DAYOFYEAR && btime_sec == ret_sec && btime_nsec < ret_nsec))) { /*********************************************************************/ /* Since the data and header files are copied and catalogued as is, */ /* a check still needs to be made for a short data record (written */ /* by listener). */ /*********************************************************************/ bytes = ptr->d_size * -2; lseek (ex->fdd, (off_t) bytes, 1); num_bytes = ptr->d_size; ret_bytes = read (ex->fdd, ex->DATA_MEM, num_bytes); if (ret_bytes < 0) return (POS_DATA_READ_ERROR); /********************************************************************/ /* The file closure flags are always in the first hdr_offset field.*/ /********************************************************************/ ir_swap_data = 1; ir_network_order (0, 1); if (ret_bytes != num_bytes) { hdr_off = (SDDAS_LONG *) (ex->DATA_MEM + 3 * sizeof (SDDAS_LONG)); if (*hdr_off == NO_MORE_DATA) return (PBACK_LOS); else if (*hdr_off == NEXT_FILE) return (PBACK_NEXT_FILE); else return (POS_DATA_READ_ERROR); } lseek (ex->fdh, (off_t) *(ptr->HDR_OFF), 0); /*********************************************************************/ /* Since ir_read_header() is called by more than one routine, return*/ /* an error code that indicates which calling routine resulted in */ /* the error and what the error was. */ /*********************************************************************/ rval = ir_read_header (data_ptr); if (rval != ALL_OKAY) { if (rval == RHDR_READ_ERROR) return (POS_HDR_READ_ERROR); else if (rval == RHDR_HDR_MALLOC) return (POS_HDR_MALLOC); else if (rval == RHDR_HDR_REALLOC) return (POS_HDR_REALLOC); else if (rval == LOS_STATUS) return (PBACK_LOS); else if (rval == NEXT_FILE_STATUS) return (PBACK_NEXT_FILE); else return (rval); } /********************************************************************/ /* Make sure the data array is in host byte order. Must be done */ /* AFTER the header record information has checked. */ /********************************************************************/ ir_swap_data_array (); ret_sec = *(ptr->TIME) / 1000; ret_nsec = (*(ptr->TIME) % 1000) * 1000000; /********************************************************************/ /* Add nanosecond time adjustment factor to dr_time, if one is */ /* defined for this virtual instrument. */ /********************************************************************/ if (ex->nano_defined) ret_nsec += *(ptr->NANO_RES); } /***********************************************************************/ /* Initialize variables pertinent to the ir_fine_search routine. */ /***********************************************************************/ ptr->time_row = 0; ptr->time_col = 0; ptr->cur_sen_set = 0; ex->accum_ss_sz = 0; ptr->reset_hdr = 1; ex->accum_ss_ms = 0; ex->accum_ss_ns = 0; ptr->chg_sen_set = 0; /***********************************************************************/ /* If requested time is greater than the start time of the current */ /* record, perform a fine search; otherwise, the requested time may */ /* be before the time of the first record (a data gap) or the time */ /* requested matches the start time of the data record. */ /***********************************************************************/ if (btime_yr > *ptr->YEAR || (btime_yr == *ptr->YEAR && btime_day > *ptr->DAYOFYEAR) || (btime_yr == *ptr->YEAR && btime_day == *ptr->DAYOFYEAR && btime_sec > ret_sec) || (btime_yr == *ptr->YEAR && btime_day == *ptr->DAYOFYEAR && btime_sec == ret_sec && btime_nsec > ret_nsec)) { /***********************************************************************/ /* Since ir_fine_search() calls read_drec(), make sure to reset the */ /* pointers for ex and ptr since reallocation may have transpired. */ /***********************************************************************/ if (ex->header_format == ORIGINAL_HEADER) { rval = ir_fine_search (data_key, exten, vnum, data_ptr, btime_yr, btime_day, btime_sec, btime_nsec, &data_gap); if (rval != ALL_OKAY) return (rval); ex = ginfo.expt; ptr = ex->info_ptr; /***********************************************************************/ /* Requested start time is within a gap between records. See if end */ /* time is too, in which case, report back no data. Take into account */ /* the situation where the gap includes a day boundary change, but as */ /* the last choice since less often. */ /***********************************************************************/ if (data_gap == sTrue) { EXP_DATA = (struct idf_data *) data_ptr; if ((etime_yr == EXP_DATA->eyear && etime_day == EXP_DATA->eday && EXP_DATA->esec > etime_sec) || (etime_yr == EXP_DATA->eyear && etime_day == EXP_DATA->eday && EXP_DATA->esec == etime_sec && EXP_DATA->ensec > etime_nsec) || (etime_yr == EXP_DATA->eyear && etime_day < EXP_DATA->eday)) return (FILE_POS_DATA_GAP); } } } else if (ex->header_format == ORIGINAL_HEADER) { /**************************************************************************/ /* Read the data record which contains the user requested time (use no */ /* advance) so that there is data in the data structure upon return. */ /**************************************************************************/ rval = read_drec (data_key, exten, vnum, data_ptr, *ptr->SENSOR_INDEX, 0, 0); if (rval < 0 || rval % 3 == 0) { if (rval == LOS_STATUS) return (PBACK_LOS); else if (rval == NEXT_FILE_STATUS) return (PBACK_NEXT_FILE); else return (rval); } /*********************************************************************/ /* Be sure to reset ex structure after calling read_drec() since */ /* multiple VIDF file crossing may cause a reallocation of the */ /* existing ex structures so address may change. */ /*********************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; /* CARRIE - This field is used only by the SCF code for a vector IDFS */ /* source. Leave it set at initialized value for tensor processing. */ ex->num_sample = (ex->smp_id == 2) ? 1 : *ptr->hdr_fmt1_ptr->N_SAMPLE; } else { /**************************************************************************/ /* Read the data record which contains the user requested time (use no */ /* advance) so that there is data in the data structure upon return. */ /**************************************************************************/ rval = read_tensor_data (data_key, exten, vnum, data_ptr, *ptr->SENSOR_INDEX, 0); if (rval < 0 || rval % 3 == 0) { if (rval == LOS_STATUS) return (PBACK_LOS); else if (rval == NEXT_FILE_STATUS) return (PBACK_NEXT_FILE); else return (rval); } /*********************************************************************/ /* Be sure to reset ex structure after calling read_drec() since */ /* multiple VIDF file crossing may cause a reallocation of the */ /* existing ex structures so address may change. */ /*********************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; } ex->fnext = 1; ptr->reset_hdr = 1; return (ALL_OKAY); }