+static void put_start_time_in_radar_header(Radar *radar)
+{
+ /* Get the earliest ray time and store it in radar header.
+ * The search is necessary because rays are not always in chronological order.
+ * For example, we have received data in which rays were apparently sorted by
+ * azimuth in some upstream software. This results in the ray times being out
+ * of order, because a sweep rarely actually begins at zero degrees.
+ *
+ * Written by Bart Kelley, SSAI, June 19, 2013
+ */
+
+ int i = 0;
+ Sweep *sweep;
+ Ray *ray;
+
+ int prevdate, thisdate;
+ float prevtime, thistime;
+
+ /* Get first sweep of first available field. */
+ for (i=0; i < MAX_RADAR_VOLUMES; i++) {
+ if ((sweep = radar->v[i]->sweep[0]) != NULL) break;
+ }
+ /* This shouldn't happen. */
+ if (i >= MAX_RADAR_VOLUMES) {
+ printf("put_start_time_in_radar_header: No radar volumes contained "
+ "sweep at index 0.\n");
+ return;
+ }
+
+ /* Get first ray and its time. */
+ i = 0;
+ while (!sweep->ray[i] && i < sweep->h.nrays) i++;
+ ray = sweep->ray[i];
+ prevdate = ray->h.year * 10000 + ray->h.month * 100 + ray->h.day;
+ prevtime = ray->h.hour * 10000 + ray->h.minute * 100 + ray->h.sec;
+
+ /* Compare times of remaining rays for earliest time. */
+ for (i=0; i<sweep->h.nrays; i++) {
+ ray = sweep->ray[i];
+ thisdate = ray->h.year * 10000 + ray->h.month * 100 + ray->h.day;
+ thistime = ray->h.hour * 10000 + ray->h.minute * 100 + ray->h.sec;
+ if (thisdate == prevdate) {
+ if (thistime < prevtime) prevtime = thistime;
+ }
+ else if (thisdate < prevdate) {
+ prevdate = thisdate;
+ prevtime = thistime;
+ }
+ }
+
+ radar->h.year = prevdate / 10000;
+ radar->h.month = prevdate / 100 % 100;
+ radar->h.day = prevdate % 100;
+ radar->h.hour = (int) prevtime / 10000;
+ radar->h.minute = (int) prevtime / 100 % 100;
+ radar->h.sec = fmod(prevtime,100.);
+}