/* * 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_angle.c 1.6 05/08/19 SwRI" #include #include "libdb.h" #include "ret_codes.h" #include "libbase_idfs.h" /******************************************************************************* * * * IR_READ_DREC_SPIN_ANGLE SUBROUTINE * * * * DESCRIPTION * * This routine will return a full spin of data for the sensor specified * * for the data set of interest using the angular method. This method uses * * the azimuthal angle value 0.0 as the "start of spin" indicator, looking * * for this value to indicate the end of the current spin and the beginning * * of the next spin. This method has the flaw of not recognizing a gap in * * spin number, assuming that the end of one spin is the beginning of the * * next. * * * * 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_angle (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_angle () looks for the start of spin within the * * set of angular information passed in * * 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_period_exceeded () determines if current sweep is in another spin* * * * 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 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 start_spin_info *sptr a pointer to the structure which holds all * * spin information for the sensor being processed * * SDDAS_DOUBLE diff_samp the time covered by the sweep in milliseconds * * SDDAS_FLOAT tolerance number of degrees per sample * * SDDAS_LONG diff_milli the milliseconds difference between the * * start and end time values being utilized * * SDDAS_LONG diff_nano the nanoseconds difference between the * * start and end time values being utilized * * SDDAS_LONG spin_duration the spin period in milliseconds * * 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_SHORT rval, read_code value returned by called module * * SDDAS_SHORT diff_year the year difference between the start and * * the end of the sweep time period * * SDDAS_SHORT diff_day the day difference between the start and * * the end of the sweep time period * * SDDAS_SHORT days_in_yr the number of days in the year - takes leap * * year into account * * 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_start flag indicating start of spin found * * SDDAS_CHAR time_adv flag indicating if time advanced or not * * SDDAS_CHAR first_time flag indicating first time code encountered * * SDDAS_CHAR next_spin flag indicating sweep is in another spin * * 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_angle (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; struct experiment_info *ex; struct ptr_rec *ptr; struct start_spin_info *sptr; SDDAS_DOUBLE diff_samp; SDDAS_FLOAT tolerance; SDDAS_LONG diff_milli, diff_nano, spin_duration; SDDAS_USHORT found_ele, num_steps_last_swp; SDDAS_SHORT rval, read_code, diff_year, diff_day, days_in_yr; SDDAS_CHAR full_swp, found_start = 0, time_adv, first_time = 1, next_spin = 0; /* Leave variables as is */ void *data_ptr; /**************************************************************************/ /* Read until the start of the next spin. Keep each data structure. */ /**************************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; sptr = ex->start_sptr + sensor; full_swp = (ex->smp_id == 2) ? 0 : 1; while (!found_start) { data_ptr = *(sptr->data_ptr + sptr->data_index - 1); EXP_DATA = (struct idf_data *) data_ptr; num_steps_last_swp = EXP_DATA->num_sample; /***********************************************************************/ /* Spin duration is based upon 1st sweep in the spin. */ /***********************************************************************/ if (first_time) { spin_duration = abs (EXP_DATA->spin_rate); first_time = 0; } /***********************************************************************/ /* 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); /***********************************************************************/ /* 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) { /*******************************************************************/ /* Figure out time duration covered by this sweep in milliseconds.*/ /*******************************************************************/ diff_year = EXP_DATA->eyear - EXP_DATA->byear; diff_day = EXP_DATA->eday - EXP_DATA->bday; diff_milli = EXP_DATA->emilli - EXP_DATA->bmilli; diff_nano = EXP_DATA->enano - EXP_DATA->bnano; if (diff_nano < 0) { --diff_milli; diff_nano += 1000000; /* 1 milli = 1000000 nanoseconds */ } /******************************************************************/ /* The sample's time range covers a day or more? */ /******************************************************************/ if (diff_day != 0 || diff_year != 0) { /******************************************************************/ /* Assumption is no more than a year difference. */ /******************************************************************/ if ((EXP_DATA->byear % 4 == 0 && EXP_DATA->byear % 100 != 0) || EXP_DATA->byear % 400 == 0) days_in_yr = 366; else days_in_yr = 365; diff_samp = (diff_year * days_in_yr * 86400000) + diff_day * 86400000 + diff_milli + (diff_nano / 1000000.0); } else diff_samp = diff_milli + (diff_nano / 1000000.0); /*******************************************************************/ /* Now, convert tolerance to degrees per sample for direct */ /* comparison against the azimuthal angles that are computed. */ /* Make sure to include the nanosecond component as a fraction */ /* of time for better accuracy so that tolerance is not TOO wide. */ /* Loop over all returned start (stop) azimuthal angle, looking */ /* for 0 degrees, within tolerance. */ /*******************************************************************/ tolerance = ptr->deg_per_msec; tolerance *= diff_samp / EXP_DATA->num_sample; found_start = ir_find_start_of_spin_angle (data_ptr, tolerance, &found_ele, ptr->increasing_angles, sptr->azimuthal_cross_over, sptr->last_swp_last_step_az); if (found_start) { /*****************************************************************/ /* For angle determination, use 100% of begin and end elements. */ /* If any code within this block changes, modify down below */ /* where checking if spin period was exceeded (time). */ /*****************************************************************/ sptr->stop_ele_frac = 1.0; sptr->spin_start_ele = found_ele; sptr->last_spin_start_ele = *start_ele; sptr->stop_ele_use_start = 0; /*******************************************************************/ /* Start of NEXT spin is at the beginning of the sweep? If so, */ /* then stop_ele for current spin is last step of previous sweep. */ /*******************************************************************/ if (found_ele == 0) { sptr->num_sweeps = sptr->data_index - 1; sptr->spin_stop_ele = num_steps_last_swp - 1; } else { sptr->num_sweeps = sptr->data_index; sptr->spin_stop_ele = found_ele - 1; } *start_frac = sptr->start_ele_frac; *stop_frac = sptr->stop_ele_frac; sptr->last_spin_start_ele_frac = sptr->start_ele_frac; } /**********************************************************************/ /* Make sure time did not go past anticipate spin duration. This */ /* is necessary because a large time gap could have been encountered */ /* and since the code is simply checking angle, could have incorrect */ /* combination of data from different spins. Check needs to be made */ /* AFTER call to ir_find_start_of_spin_angle () so that natural */ /* start of spin test is made first (angle) before time check. */ /**********************************************************************/ else { next_spin = ir_spin_period_exceeded (*(sptr->data_ptr + 0), data_ptr, spin_duration); if (next_spin) { found_ele = 0; found_start = 1; /*****************************************************************/ /* For angle determination, use 100% of begin and end elements. */ /*****************************************************************/ sptr->stop_ele_frac = 1.0; sptr->spin_start_ele = found_ele; sptr->last_spin_start_ele = *start_ele; sptr->stop_ele_use_start = 0; sptr->partial_spin = 1; /*******************************************************************/ /* Start of NEXT spin is at the beginning of the sweep so */ /* stop_ele for current spin is last step of previous sweep. */ /*******************************************************************/ sptr->num_sweeps = sptr->data_index - 1; sptr->spin_stop_ele = num_steps_last_swp - 1; *start_frac = sptr->start_ele_frac; *stop_frac = sptr->stop_ele_frac; sptr->last_spin_start_ele_frac = sptr->start_ele_frac; } } /*******************************************************************/ /* Set this regardless if found start element or not. */ /*******************************************************************/ sptr->last_swp_last_step_az = EXP_DATA->stop_az[EXP_DATA->num_sample-1]; } /*****************************************************************/ /* 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); } } /**************************************************************************/ /* Current spin is flagged as a partial spin? */ /**************************************************************************/ if (sptr->partial_spin) { /**************************************************************************/ /* Last sweep processed is in another spin in the future. */ /**************************************************************************/ if (next_spin) { /**************************************************************************/ /* Change this to to reflect the stop az value for the last sweep in the */ /* partial sweep. */ /**************************************************************************/ data_ptr = *(sptr->data_ptr + sptr->data_index - 2); EXP_DATA = (struct idf_data *) data_ptr; sptr->last_swp_last_step_az = EXP_DATA->stop_az[num_steps_last_swp-1]; return (READ_SPIN_DATA_GAP); } /*****************************************************************************/ /* The next complete start of spin has been found at the end of this current*/ /* partial so reset the flag BUT send status code indicating partial. */ /*****************************************************************************/ else { sptr->partial_spin = 0; return (READ_SPIN_DATA_GAP); } } else return (ALL_OKAY); }