char		*fget_next_line(FILE *fd)
{
  t_struct	tbl;
  static char	buf[BUF_SIZE];
  static int	i = 0;
  static int	lu = 0;

  if (!init_struct(&tbl))
    return (0);
  while (1)
    {
      if (i >= lu)
	{
	  lu = (int)fread(buf, 1, BUF_SIZE, fd);
	  if (lu == 0)
	    return ((tbl.s[0]) ? (end_of(tbl.s, tbl.j)) : (0));
	  i = 0;
	}
      if ((tbl.j + 1) == tbl.taille)
	realloc_s(&tbl);
      if (buf[i] == '\n' || !buf[i])
	{
	  i++;
	  return (end_of(tbl.s, tbl.j));
	}
      tbl.s[tbl.j++] = buf[i++];
    }
}
Exemple #2
0
int histogram_insert(struct histogram *h, double value) {
	uint64_t bucket = bucket_of(h, value);

	struct box_count *box = itable_lookup(h->buckets, bucket);
	if(!box) {
		box = calloc(1, sizeof(*box));
		itable_insert(h->buckets, bucket, box);
	}

	h->total_count++;
	box->count++;

	int mode_count = histogram_count(h, histogram_mode(h));

	if(value > h->max_value || h->total_count < 1) {
		h->max_value = value;
	}

	if(value < h->min_value || h->total_count < 1) {
		h->min_value = value;
	}

	if(box->count > mode_count) {
		h->mode       = end_of(h, bucket);
	}

	return box->count;
}
static void spool_cdr(const char *path, const char *log_line)
{
    cdr_fd_t *fd = NULL;
    char *log_line_lf = NULL;
    unsigned int bytes_in, bytes_out;
    int loops = 0;

    if (!(fd = switch_core_hash_find(globals.fd_hash, path))) {
        fd = switch_core_alloc(globals.pool, sizeof(*fd));
        switch_assert(fd);
        memset(fd, 0, sizeof(*fd));
        fd->fd = -1;
        switch_mutex_init(&fd->mutex, SWITCH_MUTEX_NESTED, globals.pool);
        fd->path = switch_core_strdup(globals.pool, path);
        switch_core_hash_insert(globals.fd_hash, path, fd);
    }

    if (end_of(log_line) != '\n') {
        log_line_lf = switch_mprintf("%s\n", log_line);
    } else {
        switch_strdup(log_line_lf, log_line);
    }
    assert(log_line_lf);

    switch_mutex_lock(fd->mutex);
    bytes_out = (unsigned) strlen(log_line_lf);

    if (fd->fd < 0) {
        do_reopen(fd);
        if (fd->fd < 0) {
            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening %s\n", path);
            goto end;
        }
    }

    if (fd->bytes + bytes_out > UINT_MAX) {
        do_rotate(fd);
    }

    while ((bytes_in = write(fd->fd, log_line_lf, bytes_out)) != bytes_out && ++loops < 10) {
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Write error to file %s %d/%d\n", path, (int) bytes_in, (int) bytes_out);
        do_rotate(fd);
        switch_yield(250000);
    }

    if (bytes_in > 0) {
        fd->bytes += bytes_in;
    }

end:

    switch_mutex_unlock(fd->mutex);
    switch_safe_free(log_line_lf);
}
Exemple #4
0
static char *gen_fmtp(opus_codec_settings_t *settings, switch_memory_pool_t *pool)
{
	char buf[256] = "";
    
	if (settings->useinbandfec) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "useinbandfec=1; ");
	}
    
	if (settings->usedtx) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "usedtx=1; ");
	}
    
	if (settings->maxaveragebitrate) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "maxaveragebitrate=%d; ", settings->maxaveragebitrate);
	}
    
	if (settings->maxplaybackrate) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "maxplaybackrate=%d; ", settings->maxplaybackrate);
	}
    
	if (settings->ptime) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "ptime=%d; ", settings->ptime);
	}
    
	if (settings->minptime) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "minptime=%d; ", settings->minptime);
	}

	if (settings->maxptime) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "maxptime=%d; ", settings->maxptime);
	}
    
	if (settings->samplerate) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "samplerate=%d; ", settings->samplerate);
	}

	if (settings->stereo) {
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "stereo=%d; ", settings->stereo);
	}

	if (end_of(buf) == ' ') {
		*(end_of_p(buf) - 1) = '\0';
	}
	
	return switch_core_strdup(pool, buf);
    
}
Exemple #5
0
double *histogram_buckets(struct histogram *h) {

	int n = histogram_size(h);

	if(n < 1) {
		return NULL;
	}

	double *values = calloc(histogram_size(h), sizeof(double));

	int i = 0;
	uint64_t key;
	struct box_count *box;

	itable_firstkey(h->buckets);
	while(itable_nextkey(h->buckets, &key, (void **) &box)) {
		values[i] = end_of(h, key);
		i++;
	}

	qsort(values, n, sizeof(double), cmp_double);

	return values;
}
Exemple #6
0
/*-------------------------
 *  Start of HydroGlacial
 *-------------------------*/
int hydroglacial()
{

/*-------------------
 *  Local Variables
 *-------------------*/
#ifdef DBG
FILE *fid;
#endif

int err, ii, jj, kk, indx, glacierind, meltflag;
int dumint;

double elabin, elaerror, approxarea, Parea;
double massavailable, maxmelt, meltday[maxday], shldday[maxday];
double smallgapprox;
double totalmelt, Tcorrection, Tfix, Mice, Mgw, Mout, Mwrap, Minput;
double Tmean;
double Volumelast, Volumeglacierarea;
double lastareakm, glacierareakm;
err = 0;
glacierind = 0;
elabin = 0.0;
Tmean = 0.0;


/*---------------------------------------------------------
 *  Approximate the ELA and glaciated area for the
 *  year prior to the model run.
 *  If (floodtry > 0) then keep that lastela and lastarea
 *  from the first time through
 ----------------------------------------------------------*/
if (floodtry == 0) {
    if (ep == 0 && yr == syear[ep]) {
        /*----------------------------------------
         *  Find the closest elevbin to the ela;
         *  this may be above or below the ela
         *----------------------------------------*/
        if (ELAstart[ep] > maxalt[ep]) {
            lastela = ELAstart[ep] - ELAchange[ep];
            elaerror = 0.0;
            elabin = ELAstart[ep];
            ELAindex = 2*nelevbins;
            smallg = 0.0;
            bigg = 0.0;
            lastarea = 0.0;
        }
        else {
	      lastela  = ELAstart[ep] - ELAchange[ep];
	      elaerror = maxalt[ep];
	      for (kk=0; kk<nelevbins; kk++)
	         if (fabs(lastela - elevbins[kk]) < elaerror) {
                   elabin = elevbins[kk];
                   ELAindex = kk;
                   elaerror = fabs(lastela - elevbins[kk]);
	         }

	 /*-------------------------------------------------
	  *  Calculate the glaciated area above the elabin
	  *  by summing the areas above that point
	  *-------------------------------------------------*/
	 smallgapprox = 0.0;
	 for (kk=ELAindex; kk<nelevbins; kk++)
	      smallgapprox += areabins[kk];

	 /*---------------------------------------------------
	  *  Determine which bin the ela actually resides in
	  *---------------------------------------------------*/
	 if (elabin > lastela)
	     indx = ELAindex - 1;
	 else
	     indx = ELAindex;

	 /*--------------------------------------------
	  *  Correct the area by linear interpolation
	  *--------------------------------------------*/
	 smallg = smallgapprox + areabins[indx]*(elabin-lastela)/elevbinsize;

	 /*--------------------------------------------------------
	  *	Calculate the glaciated area below the ela
	  *	Assume area below ela = 35% of total area
	  *	G = 0.35*Atotal	 g = 0.65*Atotal	G = (0.35/0.65)*g
	  *--------------------------------------------------------*/
	 bigg = (0.35/0.65)*smallg;

	 /*------------------------------------
	  *  Find the estimate glaciated area
	  *------------------------------------*/
	 lastarea = smallg + bigg;

      } /* end if ELA > maxalt */
   }

 /*-----------------------------------------------------------------
  *  Keep last years Glaciated area for Mass Balance and Discharge
  *-----------------------------------------------------------------*/
	else {
		if ( ep != 0 && yr == syear[ep] && setstartmeanQandQs == 0 ){
			initiallastela = ela;
			initiallastarea = glacierarea;
			lastela  = ela;
			lastarea = glacierarea;
		}
		else if (ep != 0 && yr == syear[ep] && setstartmeanQandQs > 0 ){
			lastela = initiallastela;
			lastarea = initiallastarea;
		}
		else if (yr != syear[ep]){
			lastela  = ela;
			lastarea = glacierarea;
		}
   }
}	/* endif floodtry==0 */

/*-------------------------
 *  Calculate the new ELA
 *-------------------------*/
ela = ELAstart[ep] + ELAchange[ep]*((yr-syear[ep]));

/*-------------------------------------
 *  Simulate a basin with NO glaciers
 *-------------------------------------*/
if (ela > maxalt[ep]) {
    if (lastela < maxalt[ep]) {
        fprintf(stderr, "\nHydroGlacial WARNING: epoch = %d, year = %d \n", ep+1, yr );
        fprintf(stderr, "   The Glacier completely melted. \n");
        fprintf(stderr, "   This has not been accounted for yet. \n");
        fprintf(stderr, "   There will be a mass balance error for \n");
        fprintf(stderr, "   the remaining part of the glacier. \n");
    }
    glacierelev = maxalt[ep]+elevbinsize;  /* make sure it is above the basin */
    glacierarea = 0.0;
    approxarea = 0.0;
    bigg = 0.0;
    smallg = 0.0;
    smallgapprox = 0.0;
}

/*----------------------------------
 *  Simulate a basin with glaciers
 *----------------------------------*/
else {
    for (ii=0; ii<maxday; ii++) {
        meltday[ii] = 0.0;
        shldday[ii] = 0.0;
    }

    /*----------------------------------------
     *  Find the closest elevbin to the ela;
     *  this may be above or below the ela
     *----------------------------------------*/
    elaerror = maxalt[ep];
    for (kk=0; kk<nelevbins; kk++)
        if (fabs(ela - elevbins[kk]) < elaerror) {
            elabin = elevbins[kk];
            ELAindex = kk;
            elaerror = fabs(ela - elevbins[kk]);
        }

   /*--------------------------------------------------
    *	 Calculate the glaciated area above the elabin
    *	 by summing the areas above that point
    *--------------------------------------------------*/
   smallgapprox = 0.0;
   for (kk=ELAindex; kk<nelevbins; kk++)
       smallgapprox += areabins[kk];

   /*---------------------------------------------------
    *  Determine which bin the ela actually resides in
    *---------------------------------------------------*/
   if (elabin > ela)
       indx = ELAindex - 1;
   else
       indx = ELAindex;

   /*--------------------------------------------
    *  Correct the area by linear interpolation
    *--------------------------------------------*/
   smallg = smallgapprox + areabins[indx]*(elabin-ela)/elevbinsize;
        
   /*--------------------------------------------------------
    *	 Calculate the glaciated area below the ela
    *	 Assume area below ela = 35% of total area
    *	 G = 0.35*Atotal	 g = 0.65*Atotal	G = (0.35/0.65)*g
    *--------------------------------------------------------*/
   bigg = (0.35/0.65)*smallg;

   /*--------------------------------------------------
    *  Find the actual glaciated area (glacierarea)
    *  and elevation of the glacier toe (glacierelev)
    *  and glacierelev's elevbins index (glacierind)
    *--------------------------------------------------*/
   glacierarea = smallg + bigg;
   approxarea = 0.0;
   kk = nelevbins-1;
   while (approxarea <= glacierarea) {
       approxarea += areabins[kk];
       glacierelev = elevbins[kk];
       glacierind = kk;
       kk--;
   }

   /*---------------------------------------------------------------------------
    *  Sum the Precip which is above BOTH:
    *	i) the ELA
    *	ii) the Freezing line altitude (FLAindex)
    *
    *	if( FLAindex == FLAflag ) then no T<0 occured on that day at any elev.
    *---------------------------------------------------------------------------*/
   MPglacial = 0.0;   
   for (ii=0; ii<daysiy; ii++) {
        /*  Find area above the ELA */
        if( FLAindex[ii] < ELAindex )
            Parea = smallgapprox;
        /* Find area above the FLA */
        else if( FLAindex[ii] < FLAflag ) {
            Parea = 0.0;
            for (kk=nelevbins-1; kk>=FLAindex[ii]; kk-- )
                 Parea += areabins[kk];
        }
      	else{
      	 Parea = 0.0; 				/* FLA is above the basin */
      	}
      	MPglacial += Pdaily[ii]*Parea;
   }

   /*---------------------------------------------------------
    *  Track the actual changes in glacier mass so there are
    *  no step changes in mass between years
    *---------------------------------------------------------*/
	if (glacierarea < lastarea){
		lastareakm =(lastarea/1e6);
		glacierareakm =(glacierarea/1e6);
		Volumelast = bethaglacier * pow(lastareakm,bethaexpo);
		Volumeglacierarea = bethaglacier * pow (glacierareakm,bethaexpo);
		Gmass = ((Volumelast*1e6) - (Volumeglacierarea*1e6));	
	}
	else
		Gmass = 0.0;
	
   /*--------------------------------------------------------
    *  Calculate the total water available for E, Q, and Gw
    *  = sum( ice balance + Precip in )
    *--------------------------------------------------------*/
   massavailable = Gmass + MPglacial;

   /*-------------------------------------------------------
    *  Check if there is sufficient mass to grow a glacier
    *-------------------------------------------------------*/
	if( massavailable < 0.0 ) {
		fprintf( stderr, "HydroGlacial ERROR: year = %d, ep =%d \n", yr, ep );
		fprintf( stderr, " \t There is insufficient precipitation on the \n" );
		fprintf( stderr, " \t glaciated area to grow the glacier at the \n" );
		fprintf( stderr, " \t prescribed rate. \n" );
		fprintf( stderr, " \t massavailable < 0.0 \n" );
		fprintf( stderr, " \t massavailable = Gmass + MPglacial \n" );
		fprintf( stderr, " \t massavailable \t = %e \n", massavailable );
		fprintf( stderr, " \t Gmass \t = %e (m^3) \n", Gmass );
		fprintf( stderr, " \t MPglacial  \t = %e (m^3) \n", MPglacial );
		fprintf( stderr, " \t Gmass = (lastarea-glacierarea)*fabs(lastela-ela) \n" );
		fprintf( stderr, " \t lastarea   \t = %e (m^2) \n", lastarea );
		fprintf( stderr, " \t glacierarea\t = %e (m^2) \n", glacierarea );
		fprintf( stderr, " \t lastela    \t = %e  \n", lastela );
		fprintf( stderr, " \t ela        \t = %e  \n", ela );
		fprintf( stderr, " \t Parea       \t = %e  \n", Parea);
		fprintf( stderr, " \t ELAstart[ep]      \t = %e  \n", ELAstart[ep]);
		fprintf( stderr, " \t ELAchange[ep]      \t = %e  \n", ELAchange[ep]);
		fprintf( stderr, " \t setstartmeanQandQs  \t = %d \n", setstartmeanQandQs);
		exit(-1);
	}

   /*-----------------------------------------------------------------------
    *  Calculate the amount of ice lost to evaporation
    *  divide by the glaciated area, to get units of m of water equivelant
    *-----------------------------------------------------------------------*/
   Eiceannual = massavailable*dryevap[ep]/glacierarea;

   /*------------------------------------------------------------------
    *  Loop through the year and determine the melt days
    *  scale each event by the Temperature and Precip
    *  Later the scaled days will be assigned the appropriate
    *  amount of runoff
    *
    *  Also check to insure that we melt enough ice, and do not flood
    *  the basin.  Note that meltday will be scaled down further
    *  by the shoulder events.
    *
    *  The ranarray factor will add some randomness to the events
    *  ranarry has a mean of zero and std of 1
    *------------------------------------------------------------------*/
   meltflag = 1;
   maxmelt = 0.0;
   Tfix = 0.0;
	while( meltflag == 1 ) {
		totalmelt = 0.0;
		for( ii=0; ii<daysiy; ii++ ) {
			Tcorrection = 0.0;
			if( Pdaily[ii] > 0.0 )
				Tcorrection = 1.0;
			if( Televday[glacierind][ii]+Tfix > 0.0 ) {
				meltday[ii]=mx(Televday[glacierind][ii]+ranarray[nran]-Tcorrection+Tfix,0.0);
				nran++;
				totalmelt += meltday[ii];
				maxmelt = mx( meltday[ii], maxmelt );
			}
		}
		if (totalmelt == 0.0 ) {
			Tmean = 0.0;
//			for (ii=daystrm[5]; ii<dayendm[7]; ii++ )
			for (ii=start_of(Jun); ii<end_of(Aug); ii++ )
				Tmean += Televday[glacierind][ii];
//			Tmean/=(dayendm[7]-daystrm[5]);
			Tmean/=(end_of(Aug)-start_of(Jun));
			Tfix += mx( -Tmean, 1.0 );	
		}
		else
			meltflag = 0;
	}
	
	if (Tfix > 0.0) {
		fprintf( stderr, "\n HydroGlacial Warning: epoch = %d, year = %d \n", ep+1, yr );
		fprintf( stderr, " \t The basin was too cold to melt enough glacial ice. \n" );
		fprintf( stderr, " \t The daily temperatures used to melt ice were increased. \n" );
		fprintf( stderr, " \t Tfix = %f (degC) \n", Tfix );
		fprintf( stderr, " \t Tmean = %f (degC) \n", Tmean );
		fprintf( fidlog, " HydroGlacial Warning: \n" );
		fprintf( fidlog, " \t The basin was too cold to melt enough glacial ice. \n" );
		fprintf( fidlog, " \t The daily temperatures used to melt ice were increased. \n" );
		fprintf( fidlog, " \t Tfix = %f (degC) \n", Tfix );
		fprintf( fidlog, " \t Tmean = %f (degC) \n", Tmean );
	}	

   /*---------------------------------------------------------------------------
    *  Create the shoulder events (Murray's version of flood wave attenuation)
    *  there is one left (preceeding) day scaled as:
    *   shoulderleft*event
    *  the main event is scaled down to:
    *   shouldermain*event
    *  there are 1 or more right (following days) scaled to:
    *   shoulderright[]*event
    *  1.0 = Sum(shoulderleft+shouldermain+shoulderright[])
    *---------------------------------------------------------------------------*/
   ii = 0;
   if (meltday[ii] > 0.0) {
       shldday[ii] += shoulderleft*meltday[ii];
       for (jj=0; jj<shouldern-2; jj++)
            shldday[ii+jj+1] += shoulderright[jj]*meltday[ii];
       meltday[ii] = shouldermain*meltday[ii];
   }

   for (ii=1; ii<daysiy; ii++){
   		Qice[ii-1] = 0.0;
   		Qice[ii] = 0.0;
        if (meltday[ii] > 0.0) {
	      shldday[ii-1] += shoulderleft*meltday[ii];
	      for (jj=0; jj<shouldern-2; jj++)
                shldday[ii+jj+1] += shoulderright[jj]*meltday[ii];
	      meltday[ii] = shouldermain*meltday[ii];
        }
   }

   /*-----------------------------------------------------------
    *	 Add the shoulder events and the main events
    *	 to get the total ice derived discharge.
    *	 Also scale the discharges to match the actual ice melt.
    *	 Convert to m^3/s
    *-----------------------------------------------------------*/
   for (ii=0; ii<maxday-distbins[ELAindex]; ii++) {

      /*---------------------------------------------------------------------
       *  (Mark's version of routing)
       *  Add the time lag for the distance up the basin (distbins[elabin])
       *  Convert to m^3/s
       *---------------------------------------------------------------------*/
      dumint = distbins[ELAindex];
      Qice[ ii+dumint ] += (meltday[ii]+shldday[ii])
      				*(massavailable-Eiceannual*glacierarea)
      				/(totalmelt*dTOs);
   }

   /*---------------------------------------
    *  Add the carryover from the previous
    *  year and track it's mass
    *---------------------------------------*/
   Mwrap = 0.0;
   for (ii=0; ii<maxday-daysiy; ii++) {
       Qice[ii] += Qicewrap[ii];
       Mwrap += Qicewrap[ii]*dTOs;
   }

   /*---------------------------------------------------------
    *  Add to the flux to the Groundwater pool
    *  Actual addition to the GW pool is done in HydroRain.c
    *---------------------------------------------------------*/
   for (ii=0; ii<daysiy; ii++) {
       Qicetogw[ii] += percentgw[ep]*Qice[ii];
       Qice[ii]     -= Qicetogw[ii];
   }

   /*--------------------------
    *  Check the mass balance
    *--------------------------*/
   Mice = 0.0;
   Mgw  = 0.0;
   for (ii=0; ii<maxday; ii++)
       Mice += Qice[ii]*dTOs;
   for (ii=0; ii<daysiy; ii++)
       Mgw  += Qicetogw[ii]*dTOs;

   Mout = Mice + Mgw + Eiceannual*glacierarea;
   Minput = massavailable + Mwrap;
   if ( (fabs(Mout-Minput)/Minput) > masscheck ) {
       fprintf( stderr, "ERROR in HydroGlacial: \n");
       fprintf( stderr, "  Mass Balance error: Mout != Minput \n\n");
       fprintf( stderr, "\t fabs(Mout-Minput)/Minput > masscheck \n" );
       fprintf( stderr, "\t note: masscheck set in HydroParams.h \n" );
       fprintf( stderr, "\t masscheck = %f (%%) \n", masscheck );
       fprintf( stderr, "\t fabs(Mout-Minput)/Minput = %f (%%) \n\n", fabs(Mout-Minput)/Minput );
       fprintf( stderr, " \t Minput = massavailable + Mwrap \n" );
       fprintf( stderr, " \t Minput \t\t = %e \n", Minput );
       fprintf( stderr, " \t massavailable \t = %e \n", massavailable );
       fprintf( stderr, " \t Mwrap \t\t = %e \n\n", Mwrap );
       fprintf( stderr, " \t Mout = Mice + Mgw + Eiceannual*glacierarea \n" );
       fprintf( stderr, " \t Mout \t\t = %e \n", Mout );
       fprintf( stderr, " \t Mice \t\t = %e \n", Mice );
       fprintf( stderr, " \t Mgw \t\t = %e \n", Mgw );
       fprintf( stderr, " \t Eiceannual \t = %e \n\n", Eiceannual*glacierarea );
       exit(-1);
   }

}   /* endif glacial */

#if DBG
   fprintf( fidlog,"\n HydroGlacial: \t year = %d \n\n", yr );

   fprintf( fidlog," \t Mass Wrap \t = %e (m^3) \n\n", Mwrap );

   fprintf( fidlog," \t ela \t\t = %f (m) \n", ela );
   fprintf( fidlog," \t elabin \t = %f (m) \n", elabin );
   fprintf( fidlog," \t elevbinsize \t = %f (m) \n\n", elevbinsize );

   fprintf( fidlog," \t maxalt[ep] \t = %f (m) \n", maxalt[ep] );
   fprintf( fidlog," \t glacierelev \t = %f (m) \n\n", glacierelev );

   fprintf( fidlog," \t smallg \t = %e (m^2) \n", smallg );
   fprintf( fidlog," \t bigg \t\t = %e (m^2) \n", bigg );
   fprintf( fidlog," \t glacierarea \t = %e (m^2) \n", glacierarea );
   fprintf( fidlog," \t approxarea \t = %e (m^2) \n\n", approxarea );

   fprintf( fidlog," \t ela \t\t = %f (m) \n", ela );
   fprintf( fidlog," \t lastela \t = %f (m) \n", lastela );
   fprintf( fidlog," \t diff(ela) \t = %f (m) \n\n", fabs(lastela-ela) );

   fprintf( fidlog," \t lastarea \t = %e (m^2) \n", lastarea );
   fprintf( fidlog," \t glacierarea \t = %e (m^2) \n", glacierarea );
   fprintf( fidlog," \t diff(area) \t = %e (m^2) \n\n", lastarea-glacierarea );

   fprintf( fidlog," \t MPglacial \t = %e (m^3) \n", MPglacial );
   fprintf( fidlog," \t GMass \t\t = %e (m^3) \n", Gmass );
   fprintf( fidlog," \t Mass available\t = %e (m^3) \n\n", massavailable );

   fprintf( fidlog, " \t Mout \t\t = %e \n", Mout );
   fprintf( fidlog, " \t Mout = Mice + Mgw + Eiceannual*glacierarea \n" );
   fprintf( fidlog, " \t Mice \t\t = %e \n", Mice );
   fprintf( fidlog, " \t Mgw \t\t = %e \n", Mgw );
   fprintf( fidlog, " \t Eiceannual \t = %e \n", Eiceannual*glacierarea );

  /*----------------------------------------------------------------------------
   *  Print out glacial Q for error checking.
   *  The % at the begining of each text line is for a comment line in matlab.
   *  This enables the file to be read directly into matlab for plotting.
   *----------------------------------------------------------------------------*/
  if( tblstart[ep] <= yr && yr <= tblend[ep] ) {
    if( (fid = fopen("hydro.ice","a+")) == NULL) {
      printf("  HydroGlacial ERROR: Unable to open the Qice file hydro.ice \n");
      printf("     non-fatal error, continueing. \n\n");
    }
    else {
	fprintf( fid,"%%\n%%\n%% HydroGlacial output: \n%%\n" );
	fprintf( fid,"%%Daily predicted Qice for epoch %d \n%%\n", ep+1 );
	fprintf( fid,"%%Year \t Day \t Qice(m^3/s) \t Qicetogw(m^3/s) \n" );
	fprintf( fid,"%%---- \t --- \t ----------- \t ------------ \n" );
	for(ii=0; ii<daysiy; ii++ )
           fprintf( fid,"%d \t %d \t %f \t %f \n", yr, ii+1, Qice[ii], Qicetogw[ii] );
	for(ii=daysiy; ii<maxday; ii++ )
           fprintf( fid,"%d \t %d \t %f \t NaN \n", yr, ii+1, Qice[ii] );
        fclose(fid);
    }
  }
#endif

return(err);
}  /* end of HydroGlacial */
static switch_status_t load_config(switch_memory_pool_t *pool)
{
	char *cf = "cdr_csv.conf";
	switch_xml_t cfg, xml, settings, param;
	switch_status_t status = SWITCH_STATUS_SUCCESS;

	memset(&globals, 0, sizeof(globals));
	switch_core_hash_init(&globals.fd_hash, pool);
	switch_core_hash_init(&globals.template_hash, pool);

	globals.pool = pool;

	switch_core_hash_insert(globals.template_hash, "default", default_template);
	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding default template.\n");
	globals.legs = CDR_LEG_A;

	if ((xml = switch_xml_open_cfg(cf, &cfg, NULL))) {

		if ((settings = switch_xml_child(cfg, "settings"))) {
			for (param = switch_xml_child(settings, "param"); param; param = param->next) {
				char *var = (char *) switch_xml_attr_soft(param, "name");
				char *val = (char *) switch_xml_attr_soft(param, "value");
				if (!strcasecmp(var, "debug")) {
					globals.debug = switch_true(val);
				} else if (!strcasecmp(var, "legs")) {
					globals.legs = 0;

					if (strchr(val, 'a')) {
						globals.legs |= CDR_LEG_A;
					}

					if (strchr(val, 'b')) {
						globals.legs |= CDR_LEG_B;
					}
				} else if (!strcasecmp(var, "log-base")) {
					globals.log_dir = switch_core_sprintf(pool, "%s%scdr-csv", val, SWITCH_PATH_SEPARATOR);
				} else if (!strcasecmp(var, "rotate-on-hup")) {
					globals.rotate = switch_true(val);
				} else if (!strcasecmp(var, "default-template")) {
					globals.default_template = switch_core_strdup(pool, val);
				} else if (!strcasecmp(var, "master-file-only")) {
					globals.masterfileonly = switch_true(val);
				}
			}
		}

		if ((settings = switch_xml_child(cfg, "templates"))) {
			for (param = switch_xml_child(settings, "template"); param; param = param->next) {
				char *var = (char *) switch_xml_attr(param, "name");
				if (var) {
					char *tpl;
					size_t len = strlen(param->txt) + 2;
					if (end_of(param->txt) != '\n') {
						tpl = switch_core_alloc(pool, len);
						switch_snprintf(tpl, len, "%s\n", param->txt);
					} else {
						tpl = switch_core_strdup(pool, param->txt);
					}

					switch_core_hash_insert(globals.template_hash, var, tpl);
					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding template %s.\n", var);
				}
			}
		}
		switch_xml_free(xml);
	}


	if (zstr(globals.default_template)) {
		globals.default_template = switch_core_strdup(pool, "default");
	}

	if (!globals.log_dir) {
		globals.log_dir = switch_core_sprintf(pool, "%s%scdr-csv", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
	}

	return status;
}
Exemple #8
0
abyss_bool auth_hook(TSession * r)
{
	char *domain_name, *e;
	abyss_bool ret = FALSE;

	if (!strncmp(r->requestInfo.uri, "/domains/", 9)) {
		domain_name = strdup(r->requestInfo.uri + 9);
		switch_assert(domain_name);

		if ((e = strchr(domain_name, '/'))) {
			*e++ = '\0';
		}

		if (!strcmp(domain_name, "this")) {
			free(domain_name);
			domain_name = strdup(r->requestInfo.host);
		}

		ret = !http_directory_auth(r, domain_name);

		free(domain_name);
	} else {
		char tmp[512];
		const char *list[2] = { "index.html", "index.txt" };
		int x;

		if (!strncmp(r->requestInfo.uri, "/pub", 4)) {
			char *p = (char *) r->requestInfo.uri;
			char *new_uri = p + 4;
			if (!new_uri) {
				new_uri = "/";
			}

			switch_snprintf(tmp, sizeof(tmp), "%s%s", SWITCH_GLOBAL_dirs.htdocs_dir, new_uri);

			if (switch_directory_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
				for (x = 0; x < 2; x++) {
					switch_snprintf(tmp, sizeof(tmp), "%s%s%s%s",
									SWITCH_GLOBAL_dirs.htdocs_dir, new_uri, end_of(new_uri) == *SWITCH_PATH_SEPARATOR ? "" : SWITCH_PATH_SEPARATOR, list[x]
						);

					if (switch_file_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
						switch_snprintf(tmp, sizeof(tmp), "%s%s%s", new_uri, end_of(new_uri) == '/' ? "" : "/", list[x]
							);
						new_uri = tmp;
						break;
					}
				}
			}

			r->requestInfo.uri = strdup(new_uri);
			free(p);

		} else {
			if (globals.realm && strncmp(r->requestInfo.uri, "/pub", 4)) {
				ret = !http_directory_auth(r, NULL);
			}
		}
	}
	return ret;
}
Exemple #9
0
abyss_bool auth_hook(TSession * r)
{
	char *domain_name, *e;
	abyss_bool ret = FALSE;

	if (globals.enable_websocket && !strncmp(r->requestInfo.uri, "/socket", 7)) {
		// Chrome has no Authorization support yet
		// https://code.google.com/p/chromium/issues/detail?id=123862
		return websocket_hook(r);
	}

	if (!strncmp(r->requestInfo.uri, "/portal", 7) && strlen(r->requestInfo.uri) <= 8) {
		ResponseAddField(r, "Location", "/portal/index.html");
		ResponseStatus(r, 302);
		return TRUE;
	}

	if (!strncmp(r->requestInfo.uri, "/domains/", 9)) {
		domain_name = strdup(r->requestInfo.uri + 9);
		switch_assert(domain_name);

		if ((e = strchr(domain_name, '/'))) {
			*e++ = '\0';
		}

		if (!strcmp(domain_name, "this")) {
			free(domain_name);
			domain_name = strdup(r->requestInfo.host);
		}

		ret = !http_directory_auth(r, domain_name);

		free(domain_name);
	} else {
		char tmp[512];
		const char *list[2] = { "index.html", "index.txt" };
		int x;

		if (!strncmp(r->requestInfo.uri, "/pub", 4)) {
			char *p = (char *) r->requestInfo.uri;
			char *new_uri = p + 4;
			if (!new_uri) {
				new_uri = "/";
			}

			switch_snprintf(tmp, sizeof(tmp), "%s%s", SWITCH_GLOBAL_dirs.htdocs_dir, new_uri);

			if (switch_directory_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
				for (x = 0; x < 2; x++) {
					switch_snprintf(tmp, sizeof(tmp), "%s%s%s%s",
									SWITCH_GLOBAL_dirs.htdocs_dir, new_uri, end_of(new_uri) == *SWITCH_PATH_SEPARATOR ? "" : SWITCH_PATH_SEPARATOR, list[x]
						);

					if (switch_file_exists(tmp, NULL) == SWITCH_STATUS_SUCCESS) {
						switch_snprintf(tmp, sizeof(tmp), "%s%s%s", new_uri, end_of(new_uri) == '/' ? "" : "/", list[x]
							);
						new_uri = tmp;
						break;
					}
				}
			}

			r->requestInfo.uri = strdup(new_uri);
			free(p);

		} else {
			if (globals.realm && strncmp(r->requestInfo.uri, "/pub", 4)) {
				ret = !http_directory_auth(r, NULL);
			}
		}
	}
	return ret;
}