/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/hydrotrend/hydroreadhypsom.c

Go to the documentation of this file.
00001 /*
00002  *      HydroReadHypsom.c
00003  *
00004  *      Reads digitized hypsometeric integral data from file.
00005  *
00006  *    The variables maxalt and totalarea are initialized at
00007  *    the end using values from the input file.
00008  *    Note that maxalt and totalarea can change with sea-level,
00009  *    and hence with the epoch, so these are arrays.
00010  *
00011  *      Author:         M.D. Morehead (June 1998)
00012  *      Author2:        S.D. Peckham  (September 2001)
00013  *  Author3:        A.J. Kettner  (October-November 2002)
00014  *
00015  */
00016 
00017 #include <string.h>
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include "hydroinout.h"
00021 #include "hydroparams.h"       /* includes <stdio.h> */
00022 #include "hydroalloc_mem.h"
00023 
00024 /*----------------------------
00025  *  Start of HydroReadHypsom
00026  *----------------------------*/
00027 int hydroreadhypsom()
00028 {
00029 double dumdbl, dummyelev, adjusttosealevel;
00030 int dumint, err, kk, i, k, ep;
00031 char chs[120], dummystring[100];
00032 err = 0;
00033 
00034 /*-----------------------
00035  *  Open the input file
00036  *-----------------------*/
00037 if (webflag == 0){
00038         fidhyps = allocate_1d_F( nepochs );
00039         for (ep=0; ep<nepochs; ep++ ){
00040                 sprintf(dummystring,"%s%d",fnamehyps,ep);
00041                 strcpy(ffnamehyps,dummystring);
00042                 strcat(ffnamehyps,fnamehypsext);
00043                 if ( (fidhyps[ep] = fopen(ffnamehyps,"r")) == NULL) {
00044             fprintf(stderr, "  openfiles ERROR: Unable to open the hypsometeric integral data file %s \n", ffnamehyps);
00045                 fprintf(stderr, "    Make sure the input file name is all in capitals\n");
00046                 fprintf(stderr, "    program aborted \n");
00047             exit(1);
00048                 }
00049         }
00050 }
00051 
00052 /*------------------------
00053  *  Take off file header
00054  *------------------------*/
00055         for (ep=0; ep<nepochs; ep++ )
00056             for (k=0; k < 8; k++)
00057             fgets( chs, 120, fidhyps[ep] );
00058 
00059 /*------------------
00060  *  Alocate memory
00061  *------------------*/
00062     hypsarea = (double**)malloc((nepochs)*sizeof(double*));
00063     if (!hypsarea){
00064         perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00065         exit(-1);
00066     }
00067     hypselev = (double**)malloc((nepochs)*sizeof(double*));
00068     if (!hypselev){
00069         perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00070         exit(-1);
00071     }
00072 
00073 /*--------------------------------------------
00074  *  Read the hypsometric data for each epoch
00075  *--------------------------------------------*/
00076     k = -1;
00077     for (i=0; i<nepochs; i++){
00078         adjusttosealevel = 0.0;
00079         fgets( chs, 120, fidhyps[i] );
00080         dumint = sscanf( chs, "%d\n", &nhypts[i] );
00081         if (dumint == 1){
00082             hypselev[i] = (double*)malloc((nhypts[i])*sizeof(double));
00083             if (!hypselev[i]){
00084                 perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00085                 exit(-1);
00086             }
00087             hypsarea[i] = (double*)malloc((nhypts[i])*sizeof(double));
00088             if (!hypsarea[i]){
00089                 perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00090                 exit(-1);
00091             }
00092             for (kk=0; kk<nhypts[i]; kk++){
00093                 fgets( chs, 120, fidhyps[i] );
00094                 dumint = sscanf( chs, "%lf %lf\n", &dummyelev, &dumdbl );
00095                 if (dumint != 2) {
00096                     printf("hydrotrend error; hydroreadhypsom.c\n");
00097                     exit(-1);
00098                 }
00099                 if (kk==0 && dummyelev < 0.0)
00100                         adjusttosealevel = dummyelev;
00101                 hypselev[i][kk] = dummyelev - adjusttosealevel;
00102                 hypsarea[i][kk] = dumdbl * 1e6;  /* Convert km^2 to m^2 */
00103 
00104            /*-----------------------------------------------------
00105             *  Make sure the elevation bin size are all the same
00106             *-----------------------------------------------------*/
00107                 if (kk == 1)
00108                     elevbinsize = hypselev[i][kk] - hypselev[i][kk-1];
00109                 if (kk > 0)
00110                     if (elevbinsize != hypselev[i][kk] - hypselev[i][kk-1]){
00111                         printf("\nhydrotrend ERROR; hydroreadhypsom.c\n");
00112                         printf("   The elevation bin size is not equal in the hypsometry file\n");
00113                         printf("   at epoch: %d at number %d.\n",i+1,kk+1);
00114                         printf("   Elevation bin size was set to %.4g, calculated binsize at %d is %.4g.\n",elevbinsize,kk+1,hypselev[i][kk] - hypselev[i][kk-1]);
00115                                                 printf("   Elevation = %.4g\n\n",hypselev[i][kk] );
00116                         exit(-1);
00117                     }
00118             }
00119             fgets (chs, 120, fidhyps[i] );
00120         }
00121         else i = nepochs;
00122         k++;
00123     }
00124 
00125 /*------------------
00126  *  Close the file
00127  *------------------*/
00128         for (i=0; i<nepochs; i++)
00129         fclose( fidhyps[i] );
00130 
00131 /*----------------------------------------------------------
00132  *  Warning messages if nepochs != nr. of hyps. data files
00133  *----------------------------------------------------------*/
00134     if (k == 0 && nepochs >1){
00135         fprintf(stderr, "\n WARNING in HydroReadHypsom: \n");
00136         fprintf(stderr, "    Hydrotrend found only 1 hypsometric data file for  \n");
00137         fprintf(stderr, "    multiple Epochs. \n");
00138         fprintf(stderr, "    The original hypsometric data file will be used for ALL \n");
00139         fprintf(stderr, "    epochs.  Changes in maximum elevation (or sealevel) may cause\n");
00140         fprintf(stderr, "    subtle (or obvious) errors. \n\n");
00141 
00142         fprintf(fidlog, "\n WARNING in HydroReadHypsom: \n");
00143         fprintf(fidlog, "    Hydrotrend found only 1 hypsometric data file for  \n");
00144         fprintf(fidlog, "    multiple Epochs. \n");
00145         fprintf(fidlog, "    The original hypsometric data file will be used for ALL \n");
00146         fprintf(fidlog, "    epochs.  Changes in maximum elevation (or sealevel) may cause\n");
00147         fprintf(fidlog, "    subtle (or obvious) errors. \n\n");
00148     }
00149     if (k != 0 && k < nepochs-1){
00150         fprintf(stderr, "\n WARNING in HydroReadHypsom: \n");
00151         fprintf(stderr, "    Hydrotrend found only less hypsometric data files then  \n");
00152         fprintf(stderr, "    the number of Epochs. \n");
00153         fprintf(stderr, "    The last found hypsometric data file will be used for \n");
00154         fprintf(stderr, "    remaining epochs.  Changes in maximum elevation (or sealevel)\n");
00155         fprintf(stderr, "    may cause subtle (or obvious) errors. \n\n");
00156 
00157         fprintf(fidlog, "\n WARNING in HydroReadHypsom: \n");
00158         fprintf(fidlog, "    Hydrotrend found only less hypsometric data files then  \n");
00159         fprintf(fidlog, "    the number of Epochs. \n");
00160         fprintf(fidlog, "    The last found hypsometric data file will be used for \n");
00161         fprintf(fidlog, "    remaining epochs.  Changes in maximum elevation (or sealevel)\n");
00162         fprintf(fidlog, "    may cause subtle (or obvious) errors. \n\n");
00163     }
00164 
00165 /*-----------------------------------------------------------------------------
00166  *  Adusting nr. of hyps. data files to the nr. of epochs if they don't match
00167  *-----------------------------------------------------------------------------*/
00168     if (k==0 && nepochs >1){
00169         for (k=0; k<nepochs; k++){
00170             hypselev[k+1] = (double*)malloc((nhypts[k])*sizeof(double));
00171             if (!hypselev[k+1]){
00172                 perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00173                 exit(-1);
00174             }
00175             hypsarea[k+1] = (double*)malloc((nhypts[k])*sizeof(double));
00176             if (!hypsarea[k+1]){
00177                 perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00178                 exit(-1);
00179             }
00180             for (kk=0; kk<nhypts[k]; kk++) {
00181                 hypsarea[k+1][kk] = hypsarea[k][kk];
00182                 hypselev[k+1][kk] = hypselev[k][kk];
00183             }
00184             nhypts[k+1] = nhypts[k];
00185         }
00186     }
00187     if (k != 0 && i < nepochs-1){
00188         for (k=k; k<nepochs; k++){
00189             hypselev[k+1] = (double*)malloc((nhypts[k])*sizeof(double));
00190             if (!hypselev[k+1]){
00191                 perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00192                 exit(-1);
00193             }
00194             hypsarea[k+1] = (double*)malloc((nhypts[k])*sizeof(double));
00195             if (!hypsarea[k+1]){
00196                 perror("HYDROTREND ERROR; hydroreadhypsom.c\n");
00197                 exit(-1);
00198             }
00199             for (kk=0; kk<nhypts[k]; kk++) {
00200                 hypsarea[k+1][kk] = hypsarea[k][kk];
00201                 hypselev[k+1][kk] = hypselev[k][kk];
00202             }
00203             nhypts[k+1] = nhypts[k];
00204         }
00205     }
00206 
00207 /*-----------------------
00208  *  Is # of points > 1?         
00209  *----------------------*/
00210     for (i=0;i<nepochs;i++){
00211         if (nhypts[i] <= 1) {
00212             fprintf(stderr,"   HydroReadHypsom ERROR: \n");
00213             fprintf(stderr,"     Number of hypsometric points read is less than 2. \n");
00214             fprintf(stderr,"     nhypts = %d in epoch=%d \n", nhypts[i],i+1);
00215             err++;
00216         }
00217     }
00218 
00219 /*--------------------------------------------------------
00220  *  Are the hypsometric curves monotonically increasing?
00221  *--------------------------------------------------------*/
00222     for (i=0;i<nepochs;i++)
00223         for (kk=0; kk<nhypts[i]; kk++) {
00224             if (kk > 0 && (hypselev[i][kk] <= hypselev[i][kk-1] || hypsarea[i][kk] <= hypsarea[i][kk-1])) {
00225                         fprintf(stderr,"   HydroReadHypsom ERROR: \n");
00226                 fprintf(stderr,"         Altitude and area in digitized hypsometric  \n");
00227                 fprintf(stderr,"         data file must be monotonically increasing. \n");
00228                 fprintf(stderr,"         kk = %d \n", kk);
00229                 fprintf(stderr,"         epoch = %d \n", i+1);
00230                 fprintf(stderr,"         hypselev[ep][kk-1] \t = %f \n", hypselev[i][kk-1]);
00231                 fprintf(stderr,"         hypselev[ep][kk]   \t = %f \n", hypselev[i][kk]);
00232                 fprintf(stderr,"         hypsarea[ep][kk-1] \t = %f \n", hypsarea[i][kk-1]);
00233                 fprintf(stderr,"         hypsarea[ep][kk]   \t = %f \n", hypsarea[i][kk]);
00234                 err++;
00235             }
00236         }
00237 
00238 /*----------------------------------------------------
00239  *  Use hypsometric data to set maxalt and totalarea
00240  *  per epoch.
00241  *----------------------------------------------------*/
00242     for (i=0; i<nepochs; i++) {
00243         totalarea[i] = hypsarea[i][nhypts[i] - 1];
00244         maxalt[i]    = hypselev[i][nhypts[i] - 1];
00245     }
00246     return(err);
00247 }  /* end of HydroReadHypsom */
00248 

Generated on Fri Jan 4 18:04:14 2008 for sedflux by  doxygen 1.5.2