/* * 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 "@(#) fill_data_env.c 1.4 05/08/19 SwRI" #include "ret_codes.h" #include "gen_defs.h" #include "libtrec_idfs.h" #include "user_defs.h" /**************************************************************************** * * * FILL_DATA_ENVELOPE SUBROUTINE * * * * DESCRIPTION * * This routine returns the minimum and maximum data values that are * * encountered during the time interval being processed. That is, given a * * specified time duration (time per pixel) and a base pixel, this routine * * will return the data envelope (min/max) and pixel locations for the * * data envelope for all sensors. All sensors in the sensor set are * * processed at the same time sample. Some sensors may have data at the * * current sample time, others may not. The number of sensors being * * processed is returned to the user, along with an array of sensor * * numbers indicating which sensor returns data (-1 indicates no data * * found, values 0-n are sensor numbers). Upon return to this module * * all data buffers that should have been processed by the calling module * * are reset and reused. For each sensor, there are NUM_BUFFER (5) * * buffers that hold the data. To indicate which data set is ready to be * * plotted, a status flag for each buffer is passed back to the calling * * module - BUFFER_READY indicates data is ready to be plotted and * * PARTIAL_WORKING indicates fill routine needs to continue processing * * this data set. In addition, there are N many sub-buffers that hold * * the data in each of the data levels requested. * * * * INPUT VARIABLES * * SDDAS_ULONG data_key key which uniquely identifies the data * * set being processed * * SDDAS_CHAR *exten filename extension of data to be used * * SDDAS_USHORT vnum version number associated with this * * combination (allows for multiple opens) * * void *idf_data_ptr ptr to memory location for structure that* * holds returned data values (read_drec) * * SDDAS_CHAR **bin_stat array holding the status of the sweep * * bins (data stored in bin or empty bin) * * SDDAS_SHORT **ret_sensors an array holding the sensor number(s) * * for which data is being returned * * SDDAS_FLOAT **ret_min an array holding the minimum data value * * associated with the returned pixel range * * (data for all sensors) * * SDDAS_FLOAT **ret_max an array holding the maximum data value * * associated with the returned pixel range * * (data for all sensors) * * SDDAS_LONG **bpix an array holding the starting pixel * * location for each data value returned * * SDDAS_LONG **epix an array holding the ending pixel * * location for each data value returned * * SDDAS_CHAR **ret_stat an array holding the buffer status flags * * SDDAS_SHORT *num_sen the number of sensors processed * * SDDAS_SHORT **num_units array holding the number of data sets * * to bypass to get to the data for the * * specified sensor * * SDDAS_SHORT *block_size max. number of elements in the data set * * (used for indexing into data array) * * SDDAS_SHORT **s_time_yr an array holding the start year for each * * data value returned * * SDDAS_SHORT **s_time_day an array holding the start day for each * * data value returned * * SDDAS_LONG **s_time_sec an array holding the start time for each * * data value returned (in seconds) * * SDDAS_LONG **s_time_nsec an array holding the start time for each * * data value returned (nanosecond residual)* * SDDAS_SHORT **e_time_yr an array holding the stop year for each * * data value returned * * SDDAS_SHORT **e_time_day an array holding the stop day for each * * data value returned * * SDDAS_LONG **e_time_sec an array holding the stop time for each * * data value returned (in seconds) * * SDDAS_LONG **e_time_nsec an array holding the stop time for each * * data value returned (nanosecond residual)* * SDDAS_CHAR *hdr_change flag indicating a header change occurred * * SDDAS_UCHAR exclude_dqual data is to be excluded if the d_qual flag* * for the data is set to the specified * * value * * * * USAGE * * x = fill_data_envelope (data_key, exten, vnum, idf_data_ptr, * * &ret_sensors, &ret_min, &ret_max, &bin_stat, &bpix, * * &epix, &ret_stat, &num_sen, &num_units, &block_size, * * &s_time_yr, &s_time_day, &s_time_sec, &s_time_nsec, * * &e_time_yr, &e_time_day, &e_time_sec, &e_time_nsec, * * &hdr_change, exclude_dqual) * * * * NECESSARY SUBPROGRAMS * * ir_locate_ex() determines if requested combination has * * already been processed and points to the * * correct structure allocated for combo * * ir_alloc_fill_arrays () allocates structure(s) utilized by this * * module, one per experiment_info structure* * ir_alloc_fill_info () allocates space needed to hold the info * * that is being returned by this module * * ir_reset_buffers () resets (reinitializes) buffer information* * read_drec () universal read routine that retrieves * * data for the time sample being processed * * ir_set_buffer_status () resets buffer status flags when pertinent* * ir_process_fill_data () processes the data for the sensor * * * * EXTERNAL VARIABLES * * struct general_info ginfo structure holding information concern- * * ing 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 idf_data *EXP_DATA structure holding all of the currently * * returned data values to be processed * * struct experiment_info a pointer to the structure holding * * *ex specific experiment information * * struct ptr_rec *ptr a pointer to structure which holds all * * pointers to the header and data for the * * experiment of interest * * struct fill_data *fptr pointer to the fill_data structure being * * processed * * register SDDAS_SHORT *s1 pointer to the sensor values * * register SDDAS_SHORT *s_beg pointer to the first sensor value * * register SDDAS_SHORT *s_end pointer to the last sensor value * * SDDAS_FLOAT save_frac[] array that holds the fractions associated* * with the pixel information * * SDDAS_LONG save_info[] array that holds the pixel start, stop * * and time values * * SDDAS_SHORT ret_val the value returned by the called routine * * SDDAS_SHORT send_code status code returned to calling module * * SDDAS_SHORT read_code status code returned from READ_DREC() * * SDDAS_SHORT sensor sensor for which data is to be retrieved * * 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 fwd flag indicating when to advance to the * * time sample * * SDDAS_CHAR more_data terminate data processing flag * * SDDAS_CHAR found_data flag indicating if data was returned for * * requested sensor * * SDDAS_CHAR last_buf the last buffer processed (0 - 4) * * SDDAS_CHAR chk_buffers flag indicating if there is a need to * * check if all buffers have been utilized * * SDDAS_CHAR recalc_swp recalculate sweep values flag * * SDDAS_CHAR find_pix flag indicating if the pixel locations * * need to be determined * * SDDAS_CHAR cur_buf the buffer being processed (0 - 4) * * char reset_called flag indicating if LOCATE_EX was called * * char chk_reset flag indicating if the sensors need to * * be reset since header changed * * * * SUBSYSTEM * * Display Level * * * ***************************************************************************/ SDDAS_SHORT fill_data_envelope (SDDAS_ULONG data_key, SDDAS_CHAR *exten, SDDAS_USHORT vnum, void *idf_data_ptr, SDDAS_SHORT **ret_sensors, SDDAS_FLOAT **ret_min, SDDAS_FLOAT **ret_max, SDDAS_CHAR **bin_stat, SDDAS_LONG **bpix, SDDAS_LONG **epix, SDDAS_CHAR **ret_stat, SDDAS_SHORT *num_sen, SDDAS_SHORT **num_units, SDDAS_SHORT *block_size, SDDAS_SHORT **s_time_yr, SDDAS_SHORT **s_time_day, SDDAS_LONG **s_time_sec, SDDAS_LONG **s_time_nsec, SDDAS_SHORT **e_time_yr, SDDAS_SHORT **e_time_day, SDDAS_LONG **e_time_sec, SDDAS_LONG **e_time_nsec, SDDAS_CHAR *hdr_change, SDDAS_UCHAR exclude_dqual) { extern struct general_info ginfo; extern SDDAS_CHAR ir_reset_sensors, ir_block_adv; struct idf_data *EXP_DATA; struct experiment_info *ex; struct ptr_rec *ptr; struct fill_data *fptr; register SDDAS_SHORT *s1, *s_beg, *s_end; SDDAS_FLOAT save_frac[3]; SDDAS_LONG save_info[6]; SDDAS_SHORT ret_val, send_code = ALL_OKAY, read_code, sensor; SDDAS_CHAR full_swp = 0, fwd, more_data = 1, found_data, last_buf; SDDAS_CHAR chk_buffers, recalc_swp, find_pix, cur_buf; char reset_called, chk_reset; /***********************************************************************/ /* Check to see if requested combination exists. Since a 0 is passed */ /* for last parameter, the only possible error is that the combo was */ /* not found. */ /***********************************************************************/ if (!ginfo.called_locate) { ret_val = ir_locate_ex (data_key, exten, vnum, 0); if (ret_val != ALL_OKAY) return (FILL_ENV_NOT_FOUND); ginfo.called_locate = 1; reset_called = 1; } else reset_called = 0; /***********************************************************************/ /* Set pointers to the correct data set. */ /***********************************************************************/ EXP_DATA = (struct idf_data *) idf_data_ptr; ex = ginfo.expt; ptr = ex->info_ptr; *hdr_change = 0; /***********************************************************************/ /* Only scalar sources can call this routine. If this changes later, */ /* modules similar to ir_point_buffer () and ir_band_buffer () will */ /* need to be developed. */ /***********************************************************************/ if (ex->smp_id != 2) return (FILL_ENV_SCALAR); /***********************************************************************/ /* Allocate a fill_data structure for the data set being processed */ /* if the memory has yet to be allocated. */ /***********************************************************************/ if (ex->bmem.base_fill_arrays == NO_MEMORY) if ((ret_val = ir_alloc_fill_arrays ()) != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (ret_val); } /*************************************************************************/ /* Set_time_values MUST be called by the user BEFORE fill_data_envelope */ /* can be called. */ /*************************************************************************/ if (ex->time_values.base_year == 0) { if (reset_called) ginfo.called_locate = 0; return (FILL_ENV_BASE_TIME_MISSING); } /**********************************************************************/ /* Set_bin_info MUST be called by the user BEFORE fill_data_envelope */ /* can be called. */ /**********************************************************************/ if (ex->bin_ptr == NO_MEMORY) { if (reset_called) ginfo.called_locate = 0; return (FILL_ENV_BIN_MISSING); } /**********************************************************************/ /* Center_and_band_values MUST be called by the user BEFORE */ /* fill_data_envelope can be called IF VARIABLE_SWEEP is selected */ /* for binning. */ /**********************************************************************/ if (ex->bin_ptr->swp_type == VARIABLE_SWEEP && ex->bin_ptr->band_low == NO_MEMORY) { if (reset_called) ginfo.called_locate = 0; return (FILL_ENV_CENTER_BAND_MISSING); } /***********************************************************************/ /* Allocate the space needed to hold the information for processing if */ /* the memory for this particular combination has yet to be allocated. */ /***********************************************************************/ fptr = ex->fill_arrays; if (fptr->base == NO_MEMORY) { if ((ret_val = ir_alloc_fill_info (1)) != ALL_OKAY) { if (reset_called) ginfo.called_locate = 0; return (ret_val); } } else if (fptr->buf_stat == NO_MEMORY) return (FILL_ENV_WITH_SWEEP); /**********************************************************************/ /* Free up any buffers that can be utilized this iteration. */ /**********************************************************************/ ir_reset_buffers (1); last_buf = -1; recalc_swp = 1; /**********************************************************************/ /* For this type of plot, there can be no data averaging over any of */ /* the dimensions. Just leave in since check up above traps for */ /* non-scalar source. */ /**********************************************************************/ if (ex->collapse_ptr != NO_MEMORY) { for (cur_buf = 0; cur_buf < NUM_BUFFERS; ++cur_buf) { ex->collapse_ptr->filled_phi_matrix[(SDDAS_SHORT) cur_buf] = 0; ex->collapse_ptr->add_phi_matrix[(SDDAS_SHORT) cur_buf] = 0; } } /***********************************************************************/ /* Keep processing data until the amount of time asked for has been */ /* accumulated. */ /***********************************************************************/ while (more_data) { /********************************************************************/ /* Loop over those sensors contained within the current sensor set. */ /********************************************************************/ chk_reset = 0; chk_buffers = 1; s1 = ptr->SENSOR_INDEX; s_beg = ptr->SENSOR_INDEX; s_end = ptr->SENSOR_INDEX + *ptr->N_SEN - 1; for (; s1 <= s_end; ++s1) { /******************************************************************/ /* Advance to the next data set only if the last sensor is */ /* being processed to ensure all samples which occur at the same */ /* time are processed simultaneously. */ /******************************************************************/ sensor = *s1; find_pix = (ex->all_or_one == 1 || s1 == s_beg) ? 1 : 0; fwd = (s1 == s_end) ? 1 : 0; ir_block_adv = 1; read_code = read_drec (data_key, exten, vnum, idf_data_ptr, sensor, fwd, full_swp); ir_block_adv = 0; /*******************************************************************/ /* Be sure to reset ex structure after calling read_drec() since */ /* VIDF file crossing means a reallocation of the existing ex */ /* structures so address may change (do this regardless of code). */ /*******************************************************************/ ex = ginfo.expt; ptr = ex->info_ptr; fptr = ex->fill_arrays; if (EXP_DATA->hdr_change) { *hdr_change = 1; recalc_swp = 1; } /******************************************************************/ /* Header changed at top of read_drec since data and/or header */ /* record was missing at time of last access (real-time). Break */ /* out to get a possible new set of sensors. */ /******************************************************************/ if (ir_reset_sensors) { chk_reset = 1; break; } /*****************************************************************/ /* If an error was encountered, return to the calling routine. */ /*****************************************************************/ if (read_code < 0) { if (reset_called) ginfo.called_locate = 0; return (read_code); } /*****************************************************************/ /* If no more data is forthcoming, terminate the partial working */ /* buffer for all sensors that have been processed. */ /*****************************************************************/ else if (read_code == LOS_STATUS && !EXP_DATA->filled_data) { more_data = 0; send_code = ir_set_buffer_status (0, idf_data_ptr); if (send_code == ALL_OKAY) send_code = read_code; break; } /******************************************************************/ /* If the next file needs to be opened, have the caller plot the */ /* full buffers (if any) and call the module to open the next set */ /* of files. When this module is re-entered, the previous */ /* working buffer stays the current working buffer and processing */ /* continues as usual. */ /******************************************************************/ else if (read_code == NEXT_FILE_STATUS && !EXP_DATA->filled_data) { more_data = 0; send_code = read_code; break; } /******************************************************************/ /* If the first sensor returned EOF status and there are more */ /* than one sensor in the set, all will return EOF since no */ /* advancement (fwd) till the last sensor. If only one sensor, */ /* go ahead and plot this data. */ /******************************************************************/ else if (s1 == s_beg && fwd == 0 && (read_code == DREC_EOF_SENSOR || read_code == DREC_EOF_NO_SENSOR)) { more_data = 0; send_code = read_code; break; } /******************************************************************/ /* If the EXP_DATA was not refilled by read_drec, typically */ /* because of EOF conditions, EXP_DATA holds data that is NOT */ /* pertinent to the current data set. */ /******************************************************************/ else if (data_key != EXP_DATA->data_key) { more_data = 0; send_code = read_code; break; } found_data = (read_code == DREC_NO_SENSOR || read_code == DREC_EOF_NO_SENSOR || EXP_DATA->d_qual == exclude_dqual) ? 0 : 1; ret_val = ir_process_fill_data (sensor, &more_data, found_data, fptr->start_buf, &last_buf, chk_buffers, recalc_swp, save_info, save_frac, find_pix, idf_data_ptr, 1); if (ret_val != ALL_OKAY && ret_val != NO_EMPTY_BUFFERS) { if (reset_called) ginfo.called_locate = 0; return (ret_val); } chk_buffers = 0; /******************************************************************/ /* EOF was encountered for last sensor in sensor set. */ /******************************************************************/ if ((read_code == DREC_EOF_SENSOR || read_code == DREC_EOF_NO_SENSOR) && s1 == s_end) { more_data = 0; send_code = read_code; break; } else if (read_code == LOS_STATUS) { more_data = 0; send_code = ir_set_buffer_status (0, idf_data_ptr); if (send_code == ALL_OKAY) send_code = read_code; break; } else if (read_code == NEXT_FILE_STATUS) { more_data = 0; send_code = read_code; break; } recalc_swp = 0; } if (!chk_reset && last_buf != -1) fptr->start_buf = last_buf; } /*****************************************************************/ /* Compute the time of the last data sample in the data record. */ /*****************************************************************/ last_buf = (*(fptr->buf_stat + fptr->start_buf) == BUFFER_READY) ? fptr->start_buf : fptr->start_buf - 1; if (last_buf < 0) last_buf = NUM_BUFFERS - 1; /*********************************************************************/ /* Set pointers to the time range associated with each buffer. */ /*********************************************************************/ *s_time_yr = fptr->btime_yr; *s_time_day = fptr->btime_day; *s_time_sec = fptr->btime_sec; *s_time_nsec = fptr->btime_nsec; *e_time_yr = fptr->etime_yr; *e_time_day = fptr->etime_day; *e_time_sec = fptr->etime_sec; *e_time_nsec = fptr->etime_nsec; /*********************************************************************/ /* Set pointers to return data, bin status, start pixel, stop pixel, */ /* buffer status, and sensors processed to calling routine. */ /*********************************************************************/ *ret_min = fptr->data; *ret_max = fptr->tot_frac; *bin_stat = fptr->bin_stat; *bpix = fptr->bpix; *epix = fptr->epix; *ret_stat = fptr->buf_stat; *ret_sensors = fptr->sensors; *num_sen = fptr->sensors_used; *num_units = fptr->num_units; *block_size = ex->bin_ptr->num_bins; if (reset_called) ginfo.called_locate = 0; return (send_code); }