yanera_data.c

Go to the documentation of this file.
00001 /*==============================================================================
00002 Copyright 2006 Thad Harroun, Kevin Yager
00003 
00004 This file is part of "yanera 2.0".
00005 
00006 "yanera 2.0" is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
00007 
00008 "yanera 2.0" is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00009 
00010 You should have received a copy of the GNU General Public License along with "yanera 2.0"; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
00011 ==============================================================================*/ 
00012 /*============================================================================*/
00018 #define _CRT_SECURE_NO_DEPRECATE
00019 
00020 #include "yanera.h"
00021 #include "yanera_data.h"
00022 
00023 /*=========================================================================*/
00024 void yanera_load_data(yanera_container *yanera)
00025 {
00026   unsigned int  i, j, k;
00027   char          buff[120];
00028   unsigned int  ndat;
00029   double        dtmp;
00030   FILE         *fp = NULL;
00031 
00032   for (k=0; k<yanera->number_of_models; k++)
00033   {
00034     /*--------------------------------------------
00035      * Open the data file
00036      */
00037     fp = fopen(yanera->data[k].file_name, "r");
00038     if (fp==NULL)
00039     { 
00040       yanera_error("Cannot open data file.", __FILE__, __LINE__, yanera);
00041     }
00042     
00043     /*--------------------------------------------
00044      *  Count the number of data points
00045      */ 
00046     ndat= 0;
00047     memset(buff, '\0', 120);
00048     while(fgets(buff, 119, fp) != NULL)
00049     {
00050       j = sscanf(buff, "%lf %lf %lf", &dtmp, &dtmp, &dtmp);
00051       if ((j == 2) || (j == 3)) ndat++;
00052       memset(buff, '\0', 120);
00053     }
00054     rewind(fp);
00055 
00056     /*--------------------------------------------
00057      *  Allocate memory for the data
00058      */ 
00059     yanera->data[k].n = ndat;
00060     yanera->data[k].q = (double *)calloc(yanera->data[k].n,sizeof(double));
00061     yanera->data[k].R = (double *)calloc(yanera->data[k].n,sizeof(double));
00062     yanera->data[k].e = (double *)calloc(yanera->data[k].n,sizeof(double));
00063   
00064     /*--------------------------------------------
00065      *  Read in  the data
00066      */ 
00067     ndat= 0;
00068     memset(buff, '\0', 120);
00069     while(fgets(buff, 119, fp) != NULL)
00070     {
00071       j = sscanf(buff, "%lf %lf %lf", &yanera->data[k].q[ndat], \
00072                                       &yanera->data[k].R[ndat], \
00073                                       &yanera->data[k].e[ndat]);
00074       /* 
00075        *  If we want error in R from file, but don't get it,
00076        *  what should we do? For now, set e=1.
00077        */
00078       if ((j == 2) && (yanera->misc.fit_weighting == WEIGHT_DATA)) 
00079       { yanera->data[k].e[ndat] = 1.0; }
00080       if ((j == 2) || (j == 3)) ndat++;
00081       memset(buff, '\0', 120);
00082     }
00083     fclose(fp);
00084   
00085     /*--------------------------------------------
00086      *  Set the error in R for other cases
00087      */ 
00088     for (i=0; i<yanera->data[k].n; i++)
00089     {
00090       if      (yanera->misc.fit_weighting == WEIGHT_NONE) 
00091       { yanera->data[k].e[i] = 1.0; }
00092       else if (yanera->misc.fit_weighting == WEIGHT_R)    
00093       { yanera->data[k].e[i] = yanera->data[k].R[i]; }
00094       else if (yanera->misc.fit_weighting == WEIGHT_SQRT) 
00095       { yanera->data[k].e[i] = sqrt(fabs(yanera->data[k].R[i])); }
00096     }
00097   }
00098   
00099   return;
00100 }
00101 /*============================================================================*/
00102 void yanera_read_resolution(yanera_container *yanera) 
00103 {
00104   unsigned int     j, k;
00105   char             buff[120];
00106   unsigned int     ndat;
00107   double           dtmp;
00108   FILE            *fp = NULL;
00109 
00110   for (k=0; k<yanera->number_of_models; k++)
00111   {
00112     if ((yanera->data[k].resolution.type == RESOLUTION_ARRAY_CONSTANT) ||
00113         (yanera->data[k].resolution.type == RESOLUTION_ARRAY_RELATIVE))
00114       {
00115     
00116       if (yanera->data[k].resolution.n > 0) 
00117       { 
00118         yanera_error("Resolution cannot be both array and value.", \
00119                       __FILE__, __LINE__, yanera);
00120       }
00121      /*--------------------------------------------
00122        * Open the data file
00123        */
00124       fp = fopen(yanera->data[k].resolution.file_name, "r");
00125       if (fp==NULL)
00126       { 
00127         yanera_error("Cannot open resultion array file.", \
00128                      __FILE__, __LINE__, yanera);
00129       }
00130 
00131       /*--------------------------------------------
00132        *  Count the number of data points
00133        */ 
00134       ndat= 0;
00135       memset(buff, '\0', 120);
00136       while(fgets(buff, 119, fp) != NULL)
00137       {
00138         j = sscanf(buff, "%lf %lf", &dtmp, &dtmp);
00139         if (j == 2) ndat++;
00140         memset(buff, '\0', 120);
00141       }
00142       rewind(fp);
00143 
00144       /*--------------------------------------------
00145        *  Allocate memory for the data
00146        */ 
00147       yanera->data[k].resolution.n     = ndat;
00148       yanera->data[k].resolution.value = \
00149         (double *)calloc(yanera->data[k].resolution.n, sizeof(double));
00150       yanera->data[k].resolution.q     = \
00151         (double *)calloc(yanera->data[k].resolution.n, sizeof(double));
00152   
00153       /*--------------------------------------------
00154        *  Read in  the data
00155        */ 
00156       ndat= 0;
00157       memset(buff, '\0', 120);
00158       while(fgets(buff, 119, fp) != NULL)
00159       {
00160         j = sscanf(buff, "%lf %lf", &yanera->data[k].resolution.q[ndat], \
00161                     &yanera->data[k].resolution.value[ndat]);
00162         if (j == 2) ndat++;            
00163         memset(buff, '\0', 120);
00164       }
00165       fclose(fp);
00166     }
00167     
00168     if (yanera->data[k].resolution.type != RESOLUTION_NONE)
00169     {
00170       if (yanera->data[k].resolution.n == 0) 
00171       { 
00172         yanera_error("Missing resolution value.", \
00173                      __FILE__, __LINE__, yanera);
00174       }
00175     }
00176     
00177   }
00178   
00179   return;
00180 }

Generated on Thu May 29 10:56:33 2008 by  doxygen 1.5.5