xData_2d_xshared_yhistogram.cc

Go to the documentation of this file.
00001 /*
00002 # <<BEGIN-copyright>>
00003 # Copyright (c) 2010, Lawrence Livermore National Security, LLC. 
00004 # Produced at the Lawrence Livermore National Laboratory 
00005 # Written by Bret R. Beck, beck6@llnl.gov. 
00006 # CODE-461393
00007 # All rights reserved. 
00008 #  
00009 # This file is part of GIDI. For details, see nuclear.llnl.gov. 
00010 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov. 
00011 # 
00012 # Redistribution and use in source and binary forms, with or without modification, 
00013 # are permitted provided that the following conditions are met: 
00014 #
00015 #      1) Redistributions of source code must retain the above copyright notice, 
00016 #         this list of conditions and the disclaimer below.
00017 #      2) Redistributions in binary form must reproduce the above copyright notice, 
00018 #         this list of conditions and the disclaimer (as noted below) in the 
00019 #          documentation and/or other materials provided with the distribution.
00020 #      3) Neither the name of the LLNS/LLNL nor the names of its contributors may be 
00021 #         used to endorse or promote products derived from this software without 
00022 #         specific prior written permission. 
00023 #
00024 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
00025 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00026 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT 
00027 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR 
00028 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00029 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00030 # OR SERVICES;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
00031 # AND ON  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00032 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00034 # <<END-copyright>>
00035 */
00036 #include <stdlib.h>
00037 #include <limits.h>
00038 #include <ctype.h>
00039 #include "xData.h"
00040 
00041 #if defined __cplusplus
00042 namespace GIDI {
00043 using namespace GIDI;
00044 #endif
00045 
00046 //char const * const xData_twod_xShared_yHistogram_ID = "2d_xShared_yHistogram";
00047 
00048 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
00049 static char *toString( statusMessageReporting *smr, xDataType *xDT );
00050 static int release( statusMessageReporting *smr, xDataType *xDT );
00051 /*
00052 ************************************************************
00053 */
00054 int xData_init_2d_xShared_yHistogram( statusMessageReporting *smr, xData_element *element ) {
00055 
00056     xDataType *xDT = &(element->xDataTypeInfo);
00057 
00058     xDT->status = xData_xDataType_Ok;
00059     xDT->typeString = xData_twod_xShared_yHistogram_ID;
00060     xDT->element = element;
00061     xDT->toData = toData;
00062     xDT->toString = toString;
00063     xDT->release = release;
00064     xDT->data = NULL;
00065     return( xData_xDataTypeConvertAttributes( smr, element ) );
00066 }
00067 /*
00068 ************************************************************
00069 */
00070 int xData_is_2d_xShared_yHistogram( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
00071 
00072     return( xData_is_xDataType( smr, xDT, xData_twod_xShared_yHistogram_ID, setMsg ) );
00073 }
00074 /*
00075 ************************************************************
00076 */
00077 int xData_isElement_2d_xShared_yHistogram( statusMessageReporting *smr, xData_element *element, int setMsg ) {
00078 
00079     return( xData_is_2d_xShared_yHistogram( smr, &(element->xDataTypeInfo), setMsg ) );
00080 }
00081 /*
00082 ************************************************************
00083 */
00084 //double *xData_2d_xShared_yHistogram_copyData( statusMessageReporting *smr, xData_element *element, xData_Int *n ) {
00085 double *xData_2d_xShared_yHistogram_copyData( statusMessageReporting *, xData_element *element, xData_Int *n ) {
00086 
00087     xDataType *xDT = &(element->xDataTypeInfo);
00088     xData_Int i;
00089     double *p, *values, *d = (double *) xDT->data;
00090 
00091     *n = xDT->end - xDT->start;
00092     if( xDT->length == 0 ) return( NULL );
00093     if( *n == 0 ) return( NULL );
00094     //if(( values = xData_malloc2( NULL, *n * sizeof( double ), 0, "values" ) )) {
00095     if(( values = (double*) xData_malloc2( NULL, *n * sizeof( double ), 0, "values" ) )) {
00096         p = values;
00097         for( i = 0; i < *n; i++, p++, d++ ) *p = *d;
00098     }
00099     return( values );
00100 }
00101 /*
00102 ************************************************************
00103 */
00104 int xData_2d_xShared_yHistogram_free_copyData( statusMessageReporting *smr, void *data ) {
00105 
00106     xData_free( smr, data );
00107     return( 0 );
00108 }
00109 /*
00110 ************************************************************
00111 */
00112 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
00113 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
00114 
00115     xData_Int i, n = xDT->end - xDT->start, status = 0;
00116     char *e;
00117     const char *s;
00118     double *p;
00119     void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
00120 
00121     if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
00122     release( smr, xDT );
00123     if( ( xDT->data = xData_malloc2( smr, n * sizeof( double ), 0, "data" ) ) == NULL ) return( 1 );
00124     for( i = 0, s = text, p = (double *) xDT->data; i < n; i++, p++, s = e ) {
00125         if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
00126     }
00127     if( status == 0 ) {
00128         while( isspace( *e ) ) e++;
00129         if( *e != 0 ) {
00130             smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "text contains extra data = %s", e );
00131             status = 1;
00132         }
00133     }
00134     if( status != 0 ) release( smr, xDT );
00135     return( status );
00136 }
00137 /*
00138 ************************************************************
00139 */
00140 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
00141 static char *toString( statusMessageReporting *, xDataType *xDT ) {
00142 
00143     xData_Int i, n = xDT->length, recordSize = 16 + 1;
00144     char *str, *p, fmt[32] = " %15.7e\n";
00145     //double *d = xDT->data;
00146     double *d = (double*) xDT->data;
00147 
00148     if( n < 0 ) n = 0;
00149     if( ( str = (char *) malloc( recordSize * ( n + 1 ) ) ) == NULL ) return( NULL );
00150     for( i = 0, p = str; i < n; i++, p += recordSize, d++ ) sprintf( p, fmt, d ); *p = 0;
00151     return( str );
00152 }
00153 /*
00154 ************************************************************
00155 */
00156 static int release( statusMessageReporting *smr, xDataType *xDT ) {
00157 
00158     if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
00159     return( xDT->status = xData_xDataType_Ok );
00160 }
00161 /*
00162 ************************************************************
00163 */
00164 double *xData_2d_xShared_yHistogram_toFilledXYs( xDataType *xDT, xData_Int nXs, double *Xs ) {
00165 /*
00166 *   Returns NULL if length is 0, memory could not be allocated, or nXs != xDT->length + 1.
00167 */
00168     xData_Int i;
00169     //double *p, *values, *d = xDT->data;
00170     double *p, *values, *d = (double*) xDT->data;
00171 
00172     if( xDT->length == 0 ) return( NULL );
00173     if( ( xDT->length + 1 ) != nXs ) return( NULL );
00174     //if( ( values = xData_malloc2( NULL, 4 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
00175     if( ( values = (double*) xData_malloc2( NULL, 4 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
00176     p = values;
00177     for( i = 0; i < xDT->length; i++ ) {
00178         *(p++) = Xs[i];
00179         *(p++) = 0.;
00180         *(p++) = Xs[i+1];
00181         *(p++) = 0.;
00182     }
00183     p = &(values[4 * xDT->start + 1]);
00184     for( i = xDT->start; i < xDT->end; i++, d++, p += 2 ) {
00185         *p = *d;
00186         p += 2;
00187         *p = *d;
00188     }
00189     return( values );
00190 }
00191 
00192 #if defined __cplusplus
00193 }
00194 #endif

Generated on Mon May 27 17:50:36 2013 for Geant4 by  doxygen 1.4.7