/* * 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_drec_spin_src.c 1.8 05/08/19 SwRI" #include "libdb.h" #include "ret_codes.h" #include "libbase_idfs.h" /******************************************************************************* * * * IR_READ_DREC_SPIN_SRC SUBROUTINE * * * * DESCRIPTION * * This routine will return a full spin of data for the sensor specified * * for the data set of interest. This is done by comparing the times of * * each step within a sweep of data against the time period for the current * * spin, as defined by the designated start of spin data source. * * * * 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) * * SDDAS_SHORT sensor sensor value of interest * * SDDAS_USHORT *start_ele element number within the sweep where the spin * * starts for the sensor in question * * SDDAS_FLOAT *start_frac the percentage of the spin period covered * * by element flagged as the start of spin * * SDDAS_USHORT *stop_ele element number within the sweep where the spin * * stops for the sensor in question * * SDDAS_FLOAT *stop_frac the percentage of the spin period covered * * by element flagged as the end of spin * * SDDAS_LONG *num_sweeps the number of sweeps contained within the spin * * * * USAGE * * x = ir_read_drec_spin_src (data_key, exten, vnum, sensor, &start_ele, * * &start_frac, &stop_ele, &stop_frac, * * &num_sweeps) * * * * 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_check_idf_data_memory () makes sure that all allocated arrays in the * * idf_data structure are of sufficient size * * ir_find_start_of_spin_src () looks for the start of spin * * ir_find_end_of_spin_src () looks for the end of spin * * read_drec () universal read routine that retrieves data for * * the time sample being processed * * ir_create_spin_data_structure() creates the idf_data structure that will* * hold data from the sensor from this IDFS source * * for the current spin * * ir_spin_los_next_file () handles the case when an LOS_STATUS or * * NEXT_FILE_STATUS is encountered * * ir_check_start_time () determines if the current sample starts before * * the end time of the last sweep processed for * * the current spin * * ir_spin_sweep_times () computes the time period for each element of * * the sweep * * adjust_time () corrects time elements if too many seconds * * (day crossing) or too many days(year crossing) * * * * EXTERNAL VARIABLES * * struct general_info structure that holds information concerning * * ginfo the experiment that is being processed * * * * INTERNAL VARIABLES * * struct idf_data structure holding all of the currently * * *EXP_DATA returned data values to be processed * * struct idf_data structure holding the data for the start of * * *SPIN_SRC_DATA spin data source * * struct experiment_info a pointer to the structure that holds specific * * *ex, *new_ex experiment information * * struct start_spin_info *sptr a pointer to the structure which holds all * * spin information for the sensor being processed * * struct spin_src_info a pointer to the structure that holds * * *spin_src_ptr start of spin data source information * * struct time_span last_spin structure that holds the time period of the * * last spin processed * * SDDAS_FLOAT frac the percentage of the spin period covered * * by the current sample * * SDDAS_USHORT found_ele element at which start of spin begins * * SDDAS_USHORT num_steps_last_swp the sweep length of the previous sweep * * SDDAS_USHORT max_ele the number of elements in the sweep * * SDDAS_SHORT rval, read_code, value returned by called module * * spin_code * * SDDAS_CHAR full_swp flag that indicates if 1 value is being * * requested or all values for the record (for * * a scalar parameter only) * * SDDAS_CHAR found_end flag indicating end of spin found * * SDDAS_CHAR found_start flag indicating start of spin found * * SDDAS_CHAR time_adv flag indicating if time advanced or not * * SDDAS_CHAR same_times flag indicating if the same time range is * * applicable to all steps of the vector * * SDDAS_CHAR use_start flag which indicates if computation of * * fraction is with respect to the start of the * * spin or the end of the spin (0 = end) * * void *data_ptr ptr to the memory location for the structure * * that holds returned data values * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_SHORT ir_read_drec_spin_src (SDDAS_ULONG data_key, SDDAS_CHAR *exten, SDDAS_USHORT vnum, SDDAS_SHORT sensor, SDDAS_USHORT *start_ele, SDDAS_FLOAT *start_frac, SDDAS_USHORT *stop_ele, SDDAS_FLOAT *stop_frac, SDDAS_LONG *num_sweeps) { extern struct general_info ginfo; struct idf_data *EXP_DATA, *SPIN_SRC_DATA; struct experiment_info *ex, *new_ex; struct start_spin_info *sptr; struct spin_src_info *spin_src_ptr; struct time_span last_spin; SDDAS_FLOAT frac; SDDAS_USHORT found_ele, num_steps_last_swp, max_ele; SDDAS_SHORT rval, read_code, spin_code = ALL_OKAY; SDDAS_CHAR full_swp, found_end = 0, found_start = 0, time_adv, same_times; SDDAS_CHAR use_start; /* Leave variables as is */ void *data_ptr; /***************************************************************************/ /* Set a pointer to the structure which holds all pointers for header and */ /* data information for the experiment currently being processed. */ /***************************************************************************/ ex = ginfo.expt; /* spin_src_ptr = ex->spin_src; */ /**************************************************************************/ /* Read until the start of the next spin. Keep each data structure. */ /**************************************************************************/ sptr = ex->start_sptr + sensor; full_swp = (ex->smp_id == 2) ? 0 : 1; while (!found_end) { data_ptr = *(sptr->data_ptr + sptr->data_index - 1); EXP_DATA = (struct idf_data *) data_ptr; num_steps_last_swp = EXP_DATA->num_sample; /***********************************************************************/ /* Save time of this sample to make sure time does not go backwards. */ /***********************************************************************/ sptr->last_swp.byear = EXP_DATA->byear; sptr->last_swp.bday = EXP_DATA->bday; sptr->last_swp.bmilli = EXP_DATA->bmilli; sptr->last_swp.bnano = EXP_DATA->bnano; sptr->last_swp.eyear = EXP_DATA->eyear; sptr->last_swp.eday = EXP_DATA->eday; sptr->last_swp.emilli = EXP_DATA->emilli; sptr->last_swp.enano = EXP_DATA->enano; /************************************************************************/ /* Create an instance of the data structure, so that data and timing */ /* values can be preserved when the start/stop of spin is found. */ /************************************************************************/ rval = ir_create_spin_data_structure (data_key, exten, sptr); if (rval != ALL_OKAY) return (rval); data_ptr = *(sptr->data_ptr + sptr->data_index); ++sptr->data_index; /***********************************************************************/ /* Since read_drec makes use of the ginfo.called_locate flag, point */ /* to the correct ex structure for this sensor. */ /* Call read_drec to get the values needed to compute start of spin, */ /* Go ahead and advance in time since each sensor uses it's own ex */ /* structure. If an error was encountered, return to calling routine.*/ /***********************************************************************/ rval = ir_locate_ex (data_key, exten, sptr->spin_vnum, 0); if (rval != ALL_OKAY) return (READ_SPIN_SENSOR_NOT_FOUND); read_code = read_drec (data_key, exten, sptr->spin_vnum, data_ptr, sensor, 1, full_swp); if (read_code < 0) return (read_code); new_ex = ginfo.expt; spin_src_ptr = new_ex->spin_src; /***********************************************************************/ /* Header change so make sure memory allocated is sufficient. */ /***********************************************************************/ EXP_DATA = (struct idf_data *) data_ptr; if (EXP_DATA->hdr_change) { rval = ir_check_idf_data_memory (data_key, exten, sptr->spin_vnum, data_ptr); if (rval != ALL_OKAY) return (rval); } /**********************************************************************/ /* Make sure time did not go backwards. If it did, throw away data. */ /**********************************************************************/ time_adv = ir_check_start_time (sptr->last_swp, EXP_DATA->byear, EXP_DATA->bday, EXP_DATA->bmilli, EXP_DATA->bnano); if (!time_adv) { --sptr->data_index; /**********************************************************************/ /* If the cause was LOS or NEXT_FILE, set these elements for return. */ /* Will get overwritten if next sweep is processed. */ /**********************************************************************/ sptr->spin_stop_ele = num_steps_last_swp - 1; sptr->stop_ele_frac = 1.0; } else if (EXP_DATA->filled_data) { /**********************************************************************/ /* Compute the times for each element in the sweep. */ /**********************************************************************/ max_ele = EXP_DATA->num_sample; ir_spin_sweep_times (sensor, max_ele, &same_times, EXP_DATA->byear, EXP_DATA->bday, sptr->time_per_step.btime_yr, sptr->time_per_step.btime_day, sptr->time_per_step.btime_ms, sptr->time_per_step.btime_ns, sptr->time_per_step.etime_yr, sptr->time_per_step.etime_day, sptr->time_per_step.etime_ms, sptr->time_per_step.etime_ns); /**************************************************************************/ /* Send start and end time of the spin from the start of spin data source.*/ /**************************************************************************/ SPIN_SRC_DATA = (struct idf_data *) spin_src_ptr->idf_data_ptr; found_end = ir_find_end_of_spin_src (max_ele, &found_ele, same_times, &frac, SPIN_SRC_DATA->byear, SPIN_SRC_DATA->bday, SPIN_SRC_DATA->bmilli, SPIN_SRC_DATA->bnano, SPIN_SRC_DATA->eyear, SPIN_SRC_DATA->eday, SPIN_SRC_DATA->emilli, SPIN_SRC_DATA->enano, sptr->time_per_step.btime_yr, sptr->time_per_step.btime_day, sptr->time_per_step.btime_ms, sptr->time_per_step.btime_ns, sptr->time_per_step.etime_yr, sptr->time_per_step.etime_day, sptr->time_per_step.etime_ms, sptr->time_per_step.etime_ns, &use_start); #ifdef SPIN_SRC_CHECK #include printf ("\n found_start for sensor %d = %d frac = %f", sensor, found_end, frac); #endif if (found_end) { sptr->spin_start_ele = found_ele; sptr->stop_ele_use_start = use_start; /*******************************************************************/ /* End of the spin is at the beginning of the sweep? */ /*******************************************************************/ if (found_ele == 0) { sptr->last_spin_start_ele = *start_ele; /***************************************************************/ /* This sweep starts totally outside of the current spin. */ /***************************************************************/ if (frac < 0.0) { sptr->spin_stop_ele = num_steps_last_swp - 1; sptr->num_sweeps = sptr->data_index - 1; sptr->stop_ele_frac = 1.0; } /***************************************************************/ /* The first element of this sweep is partially contained */ /* within the current spin. */ /***************************************************************/ else { sptr->spin_stop_ele = found_ele; sptr->num_sweeps = sptr->data_index; sptr->stop_ele_frac = frac; } } /*******************************************************************/ /* End of current spin is in the middle of the sweep. */ /*******************************************************************/ else { sptr->num_sweeps = sptr->data_index; /***************************************************************/ /* This step starts totally outside of the current spin. */ /***************************************************************/ if (frac < 0.0) { sptr->spin_stop_ele = found_ele - 1; sptr->stop_ele_frac = 1.0; } else { sptr->spin_stop_ele = found_ele; sptr->stop_ele_frac = frac; } } *start_frac = sptr->start_ele_frac; *stop_frac = sptr->stop_ele_frac; sptr->last_spin_start_ele_frac = sptr->start_ele_frac; /*******************************************************************/ /* Find the spin which contains this sweep. */ /*******************************************************************/ while (!found_start) { /*******************************************************************/ /* Save the time range for the current spin. */ /*******************************************************************/ SPIN_SRC_DATA = (struct idf_data *) spin_src_ptr->idf_data_ptr; last_spin.byear = SPIN_SRC_DATA->byear; last_spin.bday = SPIN_SRC_DATA->bday; last_spin.bmilli = SPIN_SRC_DATA->bmilli; last_spin.bnano = SPIN_SRC_DATA->bnano; last_spin.eyear = SPIN_SRC_DATA->eyear; last_spin.eday = SPIN_SRC_DATA->eday; last_spin.emilli = SPIN_SRC_DATA->emilli; last_spin.enano = SPIN_SRC_DATA->enano; /*******************************************************************/ /* Has the data source for the spin determination reached the end?*/ /* Try to move on. */ /*******************************************************************/ if (spin_src_ptr->los_flag) { spin_src_ptr->los_flag = 0; rval = ir_spin_los_next_file (spin_src_ptr->data_key, spin_src_ptr->exten, spin_src_ptr->version, spin_src_ptr->idf_data_ptr, spin_src_ptr->sensor); if (rval == NO_DATA) { found_start = 1; found_ele = 0; frac = 0.0; use_start = 0; spin_code = READ_SPIN_TERMINATE; break; /* break out of while loop */ } else if (rval != ALL_OKAY) return (READ_SPIN_DSRC_READ); } /*******************************************************************/ /* Since read_drec makes use of the ginfo.called_locate flag, */ /* point to the correct ex structure and get the next spin period.*/ /* Then put back the correct ex structure for the source being */ /* processed. */ /*******************************************************************/ rval = ir_locate_ex (spin_src_ptr->data_key, spin_src_ptr->exten, spin_src_ptr->version, 0); if (rval != ALL_OKAY) return (READ_SPIN_SENSOR_NOT_FOUND); spin_code = read_drec (spin_src_ptr->data_key, spin_src_ptr->exten, spin_src_ptr->version, spin_src_ptr->idf_data_ptr, spin_src_ptr->sensor, 1, 0); rval = ir_locate_ex (data_key, exten, sptr->spin_vnum, 0); if (rval != ALL_OKAY) return (READ_SPIN_SENSOR_NOT_FOUND); /*****************************************************************/ /* Since data is read AND then advanced, wait until next */ /* iteration to get next data file. */ /*****************************************************************/ if (spin_code == LOS_STATUS || spin_code == NEXT_FILE_STATUS) spin_src_ptr->los_flag = 1; else if (spin_code != ALL_OKAY) return (READ_SPIN_DSRC_READ); /*******************************************************************/ /* Adjust this spin period by the correction factors defined in */ /* the VIDF file. These time correction factors can be either */ /* positive or negative, so make sure adjustments are made */ /* correctly. Work on second boundaries for time adjustments. */ /* 1 second = 1000 millisecond = 1000000000 nanoseconds. */ /*******************************************************************/ SPIN_SRC_DATA = (struct idf_data *) spin_src_ptr->idf_data_ptr; SPIN_SRC_DATA->bmilli += spin_src_ptr->msec_adj; SPIN_SRC_DATA->bnano += spin_src_ptr->nsec_adj; while (SPIN_SRC_DATA->bnano < 0) { SPIN_SRC_DATA->bmilli -= 1000; SPIN_SRC_DATA->bnano += 1000000000; } while (SPIN_SRC_DATA->bnano >= 1000000000) { SPIN_SRC_DATA->bmilli += 1000; SPIN_SRC_DATA->bnano -= 1000000000; } adjust_time (&SPIN_SRC_DATA->byear, &SPIN_SRC_DATA->bday, &SPIN_SRC_DATA->bmilli, 2); SPIN_SRC_DATA->bsec = (SPIN_SRC_DATA->bmilli + (SPIN_SRC_DATA->bnano / 1000000)) / 1000; SPIN_SRC_DATA->bnsec = (SPIN_SRC_DATA->bmilli % 1000) * 1000000 + SPIN_SRC_DATA->bnano; SPIN_SRC_DATA->emilli += spin_src_ptr->msec_adj; SPIN_SRC_DATA->enano += spin_src_ptr->nsec_adj; while (SPIN_SRC_DATA->enano < 0) { SPIN_SRC_DATA->emilli -= 1000; SPIN_SRC_DATA->enano += 1000000000; } while (SPIN_SRC_DATA->enano >= 1000000000) { SPIN_SRC_DATA->emilli += 1000; SPIN_SRC_DATA->enano -= 1000000000; } adjust_time (&SPIN_SRC_DATA->eyear, &SPIN_SRC_DATA->eday, &SPIN_SRC_DATA->emilli, 2); SPIN_SRC_DATA->esec = (SPIN_SRC_DATA->emilli + (SPIN_SRC_DATA->enano / 1000000)) / 1000; SPIN_SRC_DATA->ensec = (SPIN_SRC_DATA->emilli % 1000) * 1000000 + SPIN_SRC_DATA->enano; /*******************************************************************/ /* Make sure next spin period does not go back in time. */ /*******************************************************************/ time_adv = ir_check_start_time (last_spin, SPIN_SRC_DATA->byear, SPIN_SRC_DATA->bday, SPIN_SRC_DATA->bmilli, SPIN_SRC_DATA->bnano); if (!time_adv) return (READ_SPIN_DSRC_BACK_SPIN); /*******************************************************************/ /* See if the start of spin is in this sweep. */ /*******************************************************************/ found_start = ir_find_start_of_spin_src (max_ele, &found_ele, same_times, &frac, SPIN_SRC_DATA->byear, SPIN_SRC_DATA->bday, SPIN_SRC_DATA->bmilli, SPIN_SRC_DATA->bnano, SPIN_SRC_DATA->eyear, SPIN_SRC_DATA->eday, SPIN_SRC_DATA->emilli, SPIN_SRC_DATA->enano, sptr->time_per_step.btime_yr, sptr->time_per_step.btime_day, sptr->time_per_step.btime_ms, sptr->time_per_step.btime_ns, sptr->time_per_step.etime_yr, sptr->time_per_step.etime_day, sptr->time_per_step.etime_ms, sptr->time_per_step.etime_ns, &use_start); #ifdef SPIN_SRC_CHECK printf ("\n new beginning for sensor %d element = %d frac = %f", sensor, found_ele, frac); #endif } sptr->start_ele_use_start = use_start; sptr->spin_start_ele = found_ele; sptr->start_ele_frac = frac; } } /*****************************************************************/ /* Handle file crossing condition. This is valid since data is */ /* processed and then the file pointer is advanced. */ /*****************************************************************/ if (read_code == LOS_STATUS || read_code == NEXT_FILE_STATUS) { /***********************************************************************/ /* Need another data structure to read the record from the next file. */ /* Don't increment sptr->data_index so last "good" structure is the */ /* previous one. */ /***********************************************************************/ rval = ir_create_spin_data_structure (data_key, exten, sptr); if (rval != ALL_OKAY) return (rval); data_ptr = *(sptr->data_ptr + sptr->data_index); rval = ir_spin_los_next_file (data_key, exten, sptr->spin_vnum, data_ptr, sensor); if (rval == NO_DATA) { /************************************************************/ /* Go ahead and set number of sweeps so far in case user */ /* wants to access what is available. */ /************************************************************/ *num_sweeps = sptr->data_index; *start_frac = sptr->start_ele_frac; *stop_frac = sptr->stop_ele_frac; return (READ_SPIN_PARTIAL); } else if (rval != ALL_OKAY) return (rval); } } if (spin_code == READ_SPIN_TERMINATE) return (spin_code); else return (ALL_OKAY); }