Ejemplo n.º 1
0
void display_prc_rec_mod(PRC_REC *tmp, float tdtutc_offset)
{
    double          dtmp;
    hms_time        tmp_time;
    ymd_date        tmp_date;
    double          offset;

    /* tmp_time to hold 1/1/2000 12h
     ------------------------------*/
    tmp_time.hour = 12;
    tmp_time.min = 0;
    tmp_time.sec = 0.0;

    tmp_date.year = 2000;
    tmp_date.month = 1;
    tmp_date.day = 1;

    /* offset by given julian days
     ----------------------------*/
    offset = tmp->ttagd * 86400.0;
    if (offset < 0.0)       sub_time(-1.0*offset,&tmp_date,&tmp_time);
    else if (offset > 0.0)  add_time(offset,&tmp_date,&tmp_time);

    /* offset by given second of day
     ------------------------------*/
    offset = tmp->ttagms/1000000.0;
    if (offset < 0.0)       sub_time(-1.0*offset,&tmp_date,&tmp_time);
    else if (offset > 0.0)  add_time(offset,&tmp_date,&tmp_time);

    sub_time(tdtutc_offset,&tmp_date,&tmp_time);

    asfPrintStatus("   Date/Time of state vector: %.4i%.2i%.2iT%.2i%.2i%5.3f\n",
                   tmp_date.year, tmp_date.month, tmp_date.day,
                   tmp_time.hour, tmp_time.min,tmp_time.sec);


    dtmp = tmp->xsat / 1000.0;
    asfPrintStatus("   X-Coordinate          : %f\n",dtmp);
    dtmp = tmp->ysat / 1000.0;
    asfPrintStatus("   Y-Coordinate          : %f\n",dtmp);
    dtmp = tmp->zsat / 1000.0;
    asfPrintStatus("   Z-Coordinate          : %f\n",dtmp);
    dtmp = tmp->xdsat / 1000000.0;
    asfPrintStatus("   X-Velocity            : %f\n",dtmp);
    dtmp = tmp->ydsat / 1000000.0;
    asfPrintStatus("   Y-Velocity            : %f\n",dtmp);
    dtmp = tmp->zdsat / 1000000.0;
    asfPrintStatus("   Z-Velocity            : %f\n",dtmp);
}
Ejemplo n.º 2
0
/*
 ******************************************************************************
 * Function : delay
 *
 * Abstract : Wait for some time (during some delay)
 *
 * Decisions : Loop while EINTR
 *
 * Input  : p_timeout  The delay to wait
 *
 * Output : None
 *
 * Return : None
 *
 * Errors : None
 *
 * History :
 *
 *******************************************************************************
*/
extern void delay ( timeout_t *p_timeout ) {
    timeout_t cur_time, exp_time, delta_time;
    int cr;

  /* Compute expiration time */
  get_time (&exp_time);
  (void) add_time (&exp_time, p_timeout);


  for (;;) {

    /* Compute delay until expiration */
    delta_time = exp_time;
    get_time (&cur_time);
    if (sub_time (&delta_time, &cur_time) > 0) {
      cr = select (0, NULL, NULL, NULL, &delta_time);
    } else {
      cr = 0;
    }

    /* Done or interrupted */
    if (cr == 0) {
      return;
    } else if (errno != EINTR) {
      perror ("select");
    }
  }
}
Ejemplo n.º 3
0
/*
 ******************************************************************************
 * Function : wait_until
 *
 * Abstract : Wait until the given time is reached
 *
 * Decisions : None
 *
 * Input  : p_time  The time to wait until it is reached
 *
 * Output : None
 *
 * Return : None
 *
 * Errors : None
 *
 * History :
 *
 *******************************************************************************
*/
extern void wait_until ( timeout_t *p_time ) {
  timeout_t current_time, timeout;

  get_time (&current_time);
  timeout = *p_time;
  if (sub_time (&timeout, &current_time) <= 0) {
    /* Time is reached */
    return;
  }
  delay (&timeout);
}
Ejemplo n.º 4
0
void prc_date_time(PRC_REC *tmp,float tdtutc_offset, ymd_date *date, hms_time *time)
{
    hms_time  tmp_time;
    ymd_date  tmp_date;
    double    offset;

    /* tmp_time to hold 1/1/2000 12h
     ------------------------------*/
    tmp_time.hour = 12;
    tmp_time.min = 0;
    tmp_time.sec = 0.0;

    tmp_date.year = 2000;
    tmp_date.month = 1;
    tmp_date.day = 1;

    /* offset by given julian days
     ----------------------------*/
    offset = tmp->ttagd * 86400.0;

    if (offset < 0.0)       sub_time(-1.0*offset,&tmp_date,&tmp_time);
    else if (offset > 0.0)  add_time(offset,&tmp_date,&tmp_time);

    /* offset by given second of day
     ------------------------------*/
    offset = tmp->ttagms/1000000.0;

    if (offset < 0.0)       sub_time(-1.0*offset,&tmp_date,&tmp_time);
    else if (offset > 0.0)  add_time(offset,&tmp_date,&tmp_time);

    sub_time(tdtutc_offset,&tmp_date,&tmp_time);

    date->year = tmp_date.year;
    date->month = tmp_date.month;
    date->day = tmp_date.day;
    time->hour = tmp_time.hour;
    time->min = tmp_time.min;
    time->sec = tmp_time.sec;
}
Ejemplo n.º 5
0
/* Compute time remaining */
extern void evt_time_remaining (timeout_t *remaining, timeout_t *exp_time) {

  timeout_t cur_time;

  if ( (remaining->tv_sec > 0) && (remaining->tv_usec > 0) ) {
    get_time (&cur_time);
    *remaining = *exp_time;
    if (sub_time (remaining, &cur_time) < 0) {
      remaining->tv_sec = 0;
      remaining->tv_usec = 0;
    }
  }
}
Ejemplo n.º 6
0
/*
 * Function:    set_time
 * Purpose:     Set the time in a ``pio_time'' object.
 * Return:      Pointer to the passed in ``pio_time'' object.
 * Programmer:  Bill Wendling, 01. October 2001
 * Modifications:
 */
pio_time *
set_time(pio_time *pt, timer_type t, int start_stop)
{
    if (pt) {
        if (pt->type == MPI_TIMER) {
            if (start_stop == TSTART) {
                pt->mpi_timer[t] = MPI_Wtime();

    /* When we start the timer for HDF5_FINE_WRITE_FIXED_DIMS or HDF5_FINE_READ_FIXED_DIMS
     * we compute the time it took to only open the file */
    if(t == HDF5_FINE_WRITE_FIXED_DIMS)
        pt->total_time[HDF5_FILE_WRITE_OPEN] += pt->mpi_timer[t] - pt->mpi_timer[HDF5_GROSS_WRITE_FIXED_DIMS];
    else if(t == HDF5_FINE_READ_FIXED_DIMS)
        pt->total_time[HDF5_FILE_READ_OPEN] += pt->mpi_timer[t] - pt->mpi_timer[HDF5_GROSS_READ_FIXED_DIMS];

            } else {
                pt->total_time[t] += MPI_Wtime() - pt->mpi_timer[t];
    pt->mpi_timer[t] = MPI_Wtime();

    /* When we stop the timer for HDF5_GROSS_WRITE_FIXED_DIMS or HDF5_GROSS_READ_FIXED_DIMS
     * we compute the time it took to close the file after the last read/write finished */
    if(t == HDF5_GROSS_WRITE_FIXED_DIMS)
        pt->total_time[HDF5_FILE_WRITE_CLOSE] += pt->mpi_timer[t] - pt->mpi_timer[HDF5_FINE_WRITE_FIXED_DIMS];
    else if(t == HDF5_GROSS_READ_FIXED_DIMS)
        pt->total_time[HDF5_FILE_READ_CLOSE] += pt->mpi_timer[t] - pt->mpi_timer[HDF5_FINE_READ_FIXED_DIMS];
            }
        } else {
            if (start_stop == TSTART) {
                HDgettimeofday(&pt->sys_timer[t], NULL);

    /* When we start the timer for HDF5_FINE_WRITE_FIXED_DIMS or HDF5_FINE_READ_FIXED_DIMS
     * we compute the time it took to only open the file */
    if(t == HDF5_FINE_WRITE_FIXED_DIMS)
        pt->total_time[HDF5_FILE_WRITE_OPEN] += sub_time(&(pt->sys_timer[t]), &(pt->sys_timer[HDF5_GROSS_WRITE_FIXED_DIMS]));
    else if(t == HDF5_FINE_READ_FIXED_DIMS)
        pt->total_time[HDF5_FILE_READ_OPEN] += sub_time(&(pt->sys_timer[t]), &(pt->sys_timer[HDF5_GROSS_READ_FIXED_DIMS]));


            } else {
                struct timeval sys_t;

                HDgettimeofday(&sys_t, NULL);
                pt->total_time[t] += sub_time(&sys_t, &(pt->sys_timer[t]));

/*                    ((double)sys_t.tv_sec +
                                ((double)sys_t.tv_usec) / MICROSECOND) -
                    ((double)pt->sys_timer[t].tv_sec +
                            ((double)pt->sys_timer[t].tv_usec) / MICROSECOND);*/

    /* When we stop the timer for HDF5_GROSS_WRITE_FIXED_DIMS or HDF5_GROSS_READ_FIXED_DIMS
     * we compute the time it took to close the file after the last read/write finished */
    if(t == HDF5_GROSS_WRITE_FIXED_DIMS)
        pt->total_time[HDF5_FILE_WRITE_CLOSE] += sub_time(&(pt->sys_timer[t]), &(pt->sys_timer[HDF5_FINE_WRITE_FIXED_DIMS]));
    else if(t == HDF5_GROSS_READ_FIXED_DIMS)
        pt->total_time[HDF5_FILE_READ_CLOSE] += sub_time(&(pt->sys_timer[t]), &(pt->sys_timer[HDF5_FINE_READ_FIXED_DIMS]));

            }
        }

        if (pio_debug_level >= 4) {
            const char *msg;
            int myrank;

            MPI_Comm_rank(pio_comm_g, &myrank);

            switch (t) {
            case HDF5_FILE_OPENCLOSE:
                msg = "File Open/Close";
                break;
            case HDF5_DATASET_CREATE:
                msg = "Dataset Create";
                break;
            case HDF5_MPI_WRITE:
                msg = "MPI Write";
                break;
            case HDF5_MPI_READ:
                msg = "MPI Read";
                break;
            case HDF5_FINE_WRITE_FIXED_DIMS:
                msg = "Fine Write";
                break;
            case HDF5_FINE_READ_FIXED_DIMS:
                msg = "Fine Read";
                break;
            case HDF5_GROSS_WRITE_FIXED_DIMS:
                msg = "Gross Write";
                break;
            case HDF5_GROSS_READ_FIXED_DIMS:
                msg = "Gross Read";
                break;
            case HDF5_RAW_WRITE_FIXED_DIMS:
                msg = "Raw Write";
                break;
            case HDF5_RAW_READ_FIXED_DIMS:
                msg = "Raw Read";
                break;
            default:
                msg = "Unknown Timer";
                break;
            }

            fprintf(output, "    Proc %d: %s %s: %.2f\n", myrank, msg,
                    (start_stop == TSTART ? "Start" : "Stop"),
                    pt->total_time[t]);
        }
    }

    return pt;
}
Ejemplo n.º 7
0
int main (int argc, char *argv[]) {

  timeout_t timeout;
  int timeout_ms;
  int accuracy_ms;
  soc_token soc = init_soc;
  soc_port port_no;
  char lan_name[80];
  synchro_msg_t synchro_msg;
  soc_length length;
  int cr, fd;
  boolean read;
  unsigned int travel_ms;

  timeout_t request_time, reply_time, travel_delta;
  timeout_t accuracy_timeout, wait_timeout;


  if ( (argc != 2) && (argc != 3) && (argc != 4) ) {
    fprintf (stderr, "SYNTAX error : Wrong number of arguments\n");
    USAGE();
    exit (1);
  }

  if (argc >= 3) {
    timeout_ms = atoi(argv[2]);
    if (timeout_ms <= 0) {
      fprintf (stderr, "SYNTAX error : Wrong timeout value\n");
      USAGE();
      exit (1);
    }
  } else {
    timeout_ms = DEFAULT_TIMEOUT_MS;
  }
  wait_timeout.tv_sec = timeout_ms / 1000; 
  wait_timeout.tv_usec = (timeout_ms % 1000) * 1000;

  if (argc == 4) {
    accuracy_ms = atoi(argv[3]);
    if (accuracy_ms <= 0) {
      fprintf (stderr, "SYNTAX error : Wrong accuracy value\n");
      USAGE();
      exit (1);
    }
  } else {
    accuracy_ms = DEFAULT_ACCURACY_MS;
  }
  accuracy_timeout.tv_sec = accuracy_ms / 1000;
  accuracy_timeout.tv_usec = (accuracy_ms % 1000) * 1000;


  if (soc_get_local_lan_name(lan_name, sizeof(lan_name)) != SOC_OK) {
    perror ("getting lan name");
    exit (1);
  }

  if (soc_open(&soc, udp_socket) != SOC_OK) {
    perror ("opening socket");
    exit (1);
  }

  port_no = atoi(argv[1]);
  if (port_no <= 0) {
    if (soc_set_dest_name_service(soc, lan_name, true, argv[1]) != SOC_OK) {
      perror ("setting destination service");
      exit (1);
    }
  } else {
    if (soc_set_dest_name_port(soc, lan_name, true, port_no) != SOC_OK) {
      perror ("setting destination port");
      exit (1);
    }
  }

  if (soc_get_dest_port(soc, &port_no) != SOC_OK) {
    perror ("getting destination port no");
    exit (1);
  }

  if (soc_link_port(soc, port_no) != SOC_OK) {
    perror ("linking socket");
    exit (1);
  }

  /* List for wait */
  if (soc_get_id(soc, &fd) != SOC_OK) {
    perror ("getting socket id");
    exit (1);
  }
  if (evt_add_fd(fd, TRUE) != WAIT_OK) {
    perror("Adding fd");
    exit (1);
  }

  for (;;) {
    synchro_msg.magic_number = magic_request_value;
    get_time (&(synchro_msg.request_time));

    cr = soc_send (soc, (soc_message) &synchro_msg, sizeof(synchro_msg));
    if (cr != SOC_OK) {
      perror ("sending request");
      exit (1);
    }

    request_time = synchro_msg.request_time;

    for (;;) {

      timeout = wait_timeout;
      if (evt_wait (&fd, &read, &timeout) != WAIT_OK) {
        perror ("waiting for event");
        exit (1);
      }

      if (fd == NO_EVENT) {
        printf ("Timeout expired. Giving up.\n");
        exit (2);
      } else if (fd <= 0) {
        /* Other non fd event */
        continue;
      }

      length = sizeof (synchro_msg);
      cr = soc_receive (soc, (soc_message) &synchro_msg, length, FALSE);
      get_time (&reply_time);

      if ( cr == sizeof (synchro_msg) ) {
        if (synchro_msg.magic_number == magic_request_value) {
          ;
        } else if ( (synchro_msg.magic_number == magic_reply_value)
                 && (synchro_msg.request_time.tv_sec == request_time.tv_sec)
                 && (synchro_msg.request_time.tv_usec == request_time.tv_usec) ) {

          travel_delta = reply_time;
          (void) sub_time (&travel_delta, &request_time);
          if (comp_time(&travel_delta, &accuracy_timeout) > 0) {
              printf ("Insuficient accuracy. Skipping...\n");
              break;
          }

          /* Compute time (reply + travel/2) and settimofday */
          travel_ms = travel_delta.tv_sec * 1000;
          travel_ms += travel_delta.tv_usec / 1000;
          incr_time (&synchro_msg.server_time, travel_ms / 2);

          (void) sub_time (&reply_time, &synchro_msg.server_time);
          printf ("Synchro %ld.%06d s\n", reply_time.tv_sec,
                                         (int)reply_time.tv_usec);

          if (settimeofday (&synchro_msg.server_time, NULL) < 0) {
             perror ("settimeofday");
             exit (1);
          }
          exit (0);
        } else {
          fprintf (stderr, "Error : wrong reply received");
        }

      } else if (cr != SOC_OK) {
        perror ("receiving reply");
      } else {
        fprintf (stderr, "Error : wrong reply received");
      }
    }

    /* Reset dest */
    if (soc_set_dest_name_port(soc, lan_name, true, port_no) != SOC_OK) {
      perror ("linking socket");
      exit (1);
    }
  }
}
void DataStatisticsWindow::displaySelectedRideStats(int idx_start, int idx_end)
{
	assert(_user);
	assert(_data_log);

	if (idx_start > 0 && idx_end > 0)
	{
		_selection_begin_idx = idx_start;
		_selection_end_idx = idx_end;

		// Compute totals
		double time = _data_log->time(idx_end) - _data_log->time(idx_start);
		double dist = _data_log->dist(idx_end) - _data_log->dist(idx_start);
		double elev_gain = DataProcessing::computeGain(_data_log->altFltd().begin() + idx_start, _data_log->altFltd().begin() + idx_end);
		double elev_loss = DataProcessing::computeLoss(_data_log->altFltd().begin() + idx_start, _data_log->altFltd().begin() + idx_end);
		
		// Compute avgs
		double avg_speed = DataProcessing::computeAverage(_data_log->speedFltd().begin() + idx_start, _data_log->speedFltd().begin() + idx_end);
		double avg_hr = DataProcessing::computeAverage(_data_log->heartRateFltd().begin() + idx_start, _data_log->heartRateFltd().begin() + idx_end);
		double avg_grad = DataProcessing::computeAverage(_data_log->gradientFltd().begin() + idx_start, _data_log->gradientFltd().begin() + idx_end);
		double avg_cadence = DataProcessing::computeAverage(_data_log->cadenceFltd().begin() + idx_start, _data_log->cadenceFltd().begin() + idx_end);
		double avg_power = DataProcessing::computeAverage(_data_log->powerFltd().begin() + idx_start, _data_log->powerFltd().begin() + idx_end);
		
		// Compute maxs
		double max_speed = DataProcessing::computeMax(_data_log->speedFltd().begin() + idx_start, _data_log->speedFltd().begin() + idx_end);
		double max_hr = DataProcessing::computeMax(_data_log->heartRateFltd().begin() + idx_start, _data_log->heartRateFltd().begin() + idx_end);
		double max_gradient = DataProcessing::computeMax(_data_log->gradientFltd().begin() + idx_start, _data_log->gradientFltd().begin() + idx_end);
		double max_cadence = DataProcessing::computeMax(_data_log->cadenceFltd().begin() + idx_start, _data_log->cadenceFltd().begin() + idx_end);
		double max_power = DataProcessing::computeMax(_data_log->powerFltd().begin() + idx_start, _data_log->powerFltd().begin() + idx_end);
		
		// Compute HR zone times
		std::vector<double> sub_heart_rate(_data_log->heartRate().begin() + idx_start, _data_log->heartRate().begin() + idx_end);
		std::vector<double> sub_time(_data_log->time().begin() + idx_start, _data_log->time().begin() + idx_end);
		double hr_zone1 = DataProcessing::computeTimeInHRZone(sub_heart_rate, sub_time, _user->zone1(), _user->zone2());
		double hr_zone2 = DataProcessing::computeTimeInHRZone(sub_heart_rate, sub_time, _user->zone2(), _user->zone3());
		double hr_zone3 = DataProcessing::computeTimeInHRZone(sub_heart_rate, sub_time, _user->zone3(), _user->zone4());
		double hr_zone4 = DataProcessing::computeTimeInHRZone(sub_heart_rate, sub_time, _user->zone4(), _user->zone5());
		double hr_zone5 = DataProcessing::computeTimeInHRZone(sub_heart_rate, sub_time, _user->zone5(), 1000.0);

		// Set selection column
		_table->item(0,1)->setText(DataProcessing::minsFromSecs(time));
		_table->item(1,1)->setText(DataProcessing::kmFromMeters(dist));
		_table->item(2,1)->setText(QString::number(elev_gain, 'f', 1));
		_table->item(3,1)->setText(QString::number(elev_loss, 'f', 1));

		_table->item(4,1)->setText(QString::number(avg_speed, 'f', 1));
		_table->item(5,1)->setText(QString::number(avg_hr, 'f', 1));
		_table->item(6,1)->setText(QString::number(avg_grad, 'f', 2));
		_table->item(7,1)->setText(QString::number(avg_cadence, 'f', 1));
		_table->item(8,1)->setText(QString::number(avg_power, 'f', 1));

		_table->item(9,1)->setText(QString::number(max_speed, 'f', 1));
		_table->item(10,1)->setText(QString::number(max_hr, 'f', 0));
		_table->item(11,1)->setText(QString::number(max_gradient, 'f', 2));
		_table->item(12,1)->setText(QString::number(max_cadence, 'f', 0));
		_table->item(13,1)->setText(QString::number(max_power, 'f', 2));

		_table->item(14,1)->setText(DataProcessing::minsFromSecs(hr_zone1));
		_table->item(15,1)->setText(DataProcessing::minsFromSecs(hr_zone2));
		_table->item(16,1)->setText(DataProcessing::minsFromSecs(hr_zone3));
		_table->item(17,1)->setText(DataProcessing::minsFromSecs(hr_zone4));
		_table->item(18,1)->setText(DataProcessing::minsFromSecs(hr_zone5));
	}
}
Ejemplo n.º 9
0
/*
 * HEADER:100:fix_CFSv2_fcst:misc:3:fixes CFSv2 monthly fcst  X=daily or 00/06/12/18 Y=pert no. Z=number ens fcsts  v1.0
 */
int f_fix_CFSv2_fcst(ARG3) {

    int subcenter, n_time_ranges, fcst_time_0, fcst_time_1, i;
    int dtime, unit, id;

    int ref_year, ref_month, ref_day, ref_hour, ref_minute, ref_second;
    int fcst0_year, fcst0_month, fcst0_day, fcst0_hour, fcst0_minute, fcst0_second;
    int fcst1_year, fcst1_month, fcst1_day, fcst1_hour, fcst1_minute, fcst1_second;

    static unsigned char new_sec4[61];

    struct local_struct {
        int fixed;			// number of fields processed
	int dt;				// dt either 6 or 24
	int hour0;			// initial hour
        int pert;			// perturbation no, 0 = ctl
        int num_ensemble_members;
    };
    struct local_struct *save;

    if (mode == -1) {
	*local = save = (struct local_struct *)malloc( sizeof(struct local_struct));
	save->fixed = 0;
	save->dt = 24;
	if (strcmp(arg1,"daily") == 0) {
	    save->dt = 6;
	    save->hour0 = 0;
	}
	else if (strcmp(arg1,"00") == 0) {
	    save->hour0 = 0;
	}
	else if (strcmp(arg1,"06") == 0) {
	    save->hour0 = 6;
	}
	else if (strcmp(arg1,"12") == 0) {
	    save->hour0 = 12;
	}
	else if (strcmp(arg1,"18") == 0) {
	    save->hour0 = 1;
	}
	else fatal_error("fix_CFSv2_fcst: bad arg %s, wanted daily,00,06,12 or 18",arg1);

	save->pert = atoi(arg2);
	save->num_ensemble_members = atoi(arg2);

    }
    save = *local;

    if (mode == -2) sprintf(inv_out,"fix_CFSRv2_fcst %d fields fixed" , save->fixed);

    if (mode < 0) return 0;

    // only process NCEP CFSv2 monthly forecasts

    // must be NCEP generated
    if (GB2_Center(sec) != NCEP) return 0;

    // subcenter should be 0, 1 (reanalysis), 3 (EMC) or 4 (NCO)
    subcenter = GB2_Subcenter(sec);
    if (subcenter != 0 && subcenter != 1 && subcenter != 3 && subcenter != 4) return 0;

    // must have process id = 82 or 98
    id = analysis_or_forecast_generating_process_identifier(sec);
    if (id != 82 && id != 98) return 0;

    // product defn table must be 8 (fcst average)
    if ( code_table_4_0(sec) != 8) return 0;

    // n_time_ranges should be 1
    if ( (n_time_ranges = sec[4][41]) != 1) {
        fprintf(stderr,"unexected CFSv2 type forecast field .. pdt=8, n=%d\n", n_time_ranges);
	return 0;
    }

    // forecast time units should be months
    if (sec[4][17] != 3 || sec[4][48] != 3) return 0;

    fcst_time_0  = int4(sec[4]+18);
    fcst_time_1  = int4(sec[4]+49) + fcst_time_0;

    // get reference time
    reftime(sec, &ref_year, &ref_month, &ref_day, &ref_hour, &ref_minute, &ref_second);

    if (ref_minute != 0 || ref_second != 0) {
        fprintf(stderr,"unexected CFSv2 minute/second value != 0\n");
	return 0;
    }

    if (fcst_time_0 != 0) {	// start at day=1 hour=save->hour0 of proper month
	fcst0_year = ref_year;
	fcst0_month = ref_month + fcst_time_0;
        if (fcst0_month > 12) {
	    i = (fcst0_month - 1) / 12;
	    fcst0_year += i;
	    fcst0_month -= (i*12);
	}
	fcst0_day = 1;
	fcst0_hour = save->hour0;
	fcst0_minute = 0;
	fcst0_second= 0;
   }
   else {			// start at current month 
	fcst0_year = ref_year;
	fcst0_month = ref_month;
	fcst0_day = ref_day;
	fcst0_hour = ref_hour;
	fcst0_minute = ref_minute;
	fcst0_second= ref_second;
	if (save->dt == 24) {
	    i = save->hour0 - fcst0_hour;
	    if (i < 0) i += 24;
	    add_time(&fcst0_year, &fcst0_month, &fcst0_day, &fcst0_hour, &fcst0_minute, &fcst0_second, i, HOUR);
	}
    }

    sub_time(fcst0_year, fcst0_month, fcst0_day, fcst0_hour, fcst0_minute, fcst0_second, 
		ref_year, ref_month, ref_day, ref_hour, ref_minute, ref_second,
		&dtime, &unit);
    // set forecast time.
    if (dtime == 0) unit = MONTH;
    sec[4][17] = unit;
    int_char(dtime, sec[4]+18);

    if (fcst_time_1 == 0) fatal_error("fix-CFSv2_fcst: unexpected end_ft","");

    if (save->dt == 6) {		// ends at 18Z of last day of month
	fcst1_year = ref_year;
	fcst1_month = ref_month + fcst_time_1-1;
	if (fcst1_month > 12) {
            i = (fcst1_month - 1) / 12;
            fcst1_year += i;
            fcst1_month -= (i*12);
        }
        fcst1_day = num_days_in_month(fcst1_year, fcst1_month);
        fcst1_hour = 18;
        fcst1_minute = 0;
        fcst1_second= 0;

	// time increment = 6 hours
	sec[4][53] = (unsigned char) HOUR;
	int_char(6, sec[4]+54);
    }
    else {
	fcst1_year = ref_year;
	fcst1_month = ref_month + fcst_time_1-1;
	if (fcst1_month > 12) {
            i = (fcst1_month - 1) / 12;
            fcst1_year += i;
            fcst1_month -= (i*12);
        }
        fcst1_day = num_days_in_month(fcst1_year, fcst1_month);
        fcst1_hour = save->hour0;
        fcst1_minute = 0;
        fcst1_second= 0;

	// time increment = 24 hours
	sec[4][53] = (unsigned char)  HOUR;
	int_char(24, sec[4]+54);
    }

    // find stat processing time
    sub_time(fcst1_year, fcst1_month, fcst1_day, fcst1_hour, fcst1_minute, fcst1_second, 
		fcst0_year, fcst0_month, fcst0_day, fcst0_hour, fcst0_minute, fcst0_second, 
		&dtime, &unit);

    // change length of processing
    sec[4][48] = (unsigned char) unit;
    int_char(dtime, sec[4]+49);

// fprintf(stderr,"length of processing %d unit (%d)\n", dtime, unit);

    // save end-of-processing time
    save_time(fcst1_year, fcst1_month, fcst1_day, fcst1_hour, fcst1_minute, fcst1_second, sec[4]+34);

    // make sure that some basic info is correct
    sec[4][47] = 2;	// fcst_time++


    // now to add ensemble information
    for (i = 0; i < 34; i++) new_sec4[i] = sec[4][i];
    for (i = 34; i <= 57; i++) new_sec4[i+3] = sec[4][i];

    uint_char(61, new_sec4);			// length of new sec[4[
    new_sec4[7] = 0;				// pdt = 11
    new_sec4[8] = 11;

    // add perturbation info
    if (save->pert == 0) {			// pert == 0 means control forecast
        new_sec4[34] = 0;
        new_sec4[35] = 0;
    }
    else { 
        new_sec4[34] = 3;
        new_sec4[35] = save->pert;
    }
    new_sec4[35] = save->num_ensemble_members;
   
    sec[4] = new_sec4;
 
    save->fixed = save->fixed + 1;
    return 0;
}
Ejemplo n.º 10
0
/* THE MAIN */
int main (const int argc, const char * argv[]) {

  /* Socket data */
  soc_token soc = init_soc;
  soc_host lan;
  soc_port port;
  int soc_fd, fd;

  /* Socket message */
  msg_type msg;

  /* Times and timeouts */
  timeout_t start_time, end_time, current_time;
  timeout_t wait_timeout;
  double local_time, remote_time;

  /* Dynamic list of server infos */
  dlist list;
  info_type info;

  /* Utilities */
  boolean for_read;
  char buff[256];
  int res;
  char *index;

  /*********/
  /* Start */
  /*********/
  /* Save prog name */
  strcpy (prog, argv[0]);
  strcpy (prog, basename (prog));

  /*******************/
  /* Parse arguments */
  /*******************/
  /* Check args */
  if (argc != 2) {
    error ("Invalid argument");
  }
  /* Parse IPM address and port */
  strcpy (buff, argv[1]);
  index = strstr (buff, ":");
  if (index == NULL) {
     error ("Invalid argument");
  }
  *index = '\0';
  index++;
  if (soc_str2host (buff, &lan) != SOC_OK) {
    sprintf (buff, "Invalid ipm address %s", buff);
    error (buff);
  }
  if (soc_str2port (index, &port) != SOC_OK) {
    sprintf (buff, "Invalid port num %s", index);
    error (buff);
  }

  /**************/
  /* Initialize */
  /**************/
  /* Init dynamic list */
  dlist_init (& list, sizeof(info_type));
  /* Init socket */
  if (soc_open (&soc, udp_socket) != SOC_OK) {
    perror ("opening socket");
    error ("Socket initialization failed");
  }
  if (soc_set_dest_host_port (soc, &lan, port) != SOC_OK) {
    perror ("setting destination");
    error ("Socket initialization failed");
  }
  if (soc_link_port (soc, port) != SOC_OK) {
    perror ("linking to port");
    error ("Socket initialization failed");
  }
  if (soc_get_dest_host (soc, &lan) != SOC_OK) {
    perror ("getting dest lan");
    error ("Socket initialization failed");
  }
  if (soc_get_dest_port (soc, &port) != SOC_OK) {
    perror ("getting dest port");
    error ("Socket initialization failed");
  }
  /* Add socket to waiting point */
  if (soc_get_id(soc, &soc_fd) != SOC_OK) {
    perror ("getting socket id");
    error ("Socket initialization failed");

  }
  if (evt_add_fd(soc_fd, TRUE) != WAIT_OK) {
    perror("Adding fd");
    error ("Socket initialization failed");
  }
  /* Activate signal catching */
  activate_signal_handling();
  /* Report starting */
  buff[0]='\0';
  addr_image (&lan, buff);
  printf ("%s mcasting at address %s on port %d.\n", prog, buff, (int) port);
  /* Init times */
  get_time (&start_time);
  current_time = start_time;
  end_time = start_time;
  incr_time (&end_time, DELAY_CLIENT_MS);
  /* Send initial ping request */
  msg.ping = TRUE;
  msg.time = start_time;
  if (soc_send (soc, (soc_message) &msg, sizeof(msg)) != SOC_OK) {
    perror ("sending ping");
    error ("Sending ping request failed");
  }

  /*************/
  /* Main loop */
  /*************/
  for (;;) {
    /* First step is to loop until timeout */
    if (wait_timeout.tv_sec != -1) {
      wait_timeout = end_time;
      res = sub_time (&wait_timeout, &current_time);
      if (res <= 0) {
        break;
      }
    }
    if (evt_wait (&fd, &for_read, &wait_timeout) != WAIT_OK) {
      perror ("waiting for event");
      error ("Waiting for events failed");
    }
    if (! for_read) {
      error ("Write event received");
    }
    /* Termination signal */
    if (fd == SIG_EVENT) {
      if (get_signal () == SIG_TERMINATE) {
        break;
      }
    } else if (fd == NO_EVENT) {
      /* Timeout: first step ends with a dump of servers */
      if (dlist_length(&list) != 0) {
        dlist_rewind (&list, TRUE);
        for (;;) {
          dlist_read (&list, &info);
          /* Get host name if possible, else dump address */
          res = soc_host_name_of (&info.host, buff, sizeof(buff));
          if (res != SOC_OK) {
            buff[0]='\0';
            addr_image (&info.host, buff);
          }
          /* Compute (Start_time + Reception_time) / 2 */
          local_time = (time_to_double (&start_time)
                        + time_to_double (&info.reception_time) ) / 2.0;
          remote_time = time_to_double (&info.server_time);
          printf ("Host %s is shifted by %4.03fs\n", buff, remote_time - local_time);

          /* Done when last record has been put */
          if (dlist_get_pos (&list, FALSE) == 1) {
            break;
          }
          dlist_move (&list, TRUE);
        }
      }
      /* Now entering second step: infinite timeout */
      wait_timeout.tv_sec = -1;
      wait_timeout.tv_usec = -1;
      printf ("%s ready.\n", prog);
    } else if (fd != soc_fd) {
      sprintf (buff, "Invalid fd %d received", fd);
      error (buff);
    } else {
      /* Now this is the socket, read message */
      res = soc_receive (soc, (soc_message) &msg, sizeof(msg), TRUE);
      if (res < 0) {
        perror ("reading from socket");
        error ("Reading message failed");
      } else if (res != sizeof(msg)) {
        sprintf (buff, "Invalid size received, expected %d, got %d",
                       (int)sizeof(msg), res);
        error (buff);
      }
      get_time (&current_time);
      /* Client and server different behaviours */
      if ((wait_timeout.tv_sec != -1) && !msg.ping) {
        /* First step: store the address and time of server, if pong */
        if (soc_get_dest_host (soc, &(info.host)) != SOC_OK) {
          perror ("getting dest host");
          error ("Getting server address failed");
        }
        info.server_time = msg.time;
        info.reception_time = current_time;
        dlist_insert (&list, &info, TRUE);

      } else if ( (wait_timeout.tv_sec == -1) && msg.ping) {
        /* Second step: reply pong and time to ping */
        msg.time = current_time;
        msg.ping = FALSE;
        if (soc_send (soc, (soc_message) &msg, sizeof(msg)) != SOC_OK) {
          perror ("sending pong");
          error ("Sending pong request failed");
        }
      }
    }
  } /* End of main loop */


  /* Clean - Close */
  dlist_delete_all (&list);
  (void) evt_del_fd (soc_fd, TRUE);
  (void) soc_close (&soc);
  printf ("Done.\n");
  exit (0);
}
Ejemplo n.º 11
0
	int DispatcherPrivate::wall_diff(int timeprev)
	{
		return sub_time(_wall.read_us(), timeprev);
	}