/* sync2.h
   ======= */

/* Sessai Special additional functions to sync.c in support.lib */

/* functions to synchronize the radar */

//#include <stdio.h>
//#include <stdlib.h>
//#include <sys/types.h>
//#include "radops.h"
//#include "support.h"

/* global declaration of the time so we don't need to keep calling 
 *  read_clock() 
 */
 
//extern int yr,mon,day,hr,min,sec;
//extern int msec,usec;

/* This function waits for a two minute boundary */

/* additional function by Sessai */

void wait_boundary2(int bnd,int bnd_sec) {
   long int sleep_time;

   if (bnd*60+bnd_sec <=0) return;	// added by Sessai on Nov.28,1997

   // sleep_time=(bnd*60L*1000L)-
   sleep_time=( (bnd*60L+bnd_sec)*1000L ) -
               ( ( ((min*60+sec)*1000L)+msec ) % ( (bnd*60L+bnd_sec)*1000L) );
   sleep_time-=20L;
   if (sleep_time<0) sleep_time=0L;
   sleep((unsigned int) (sleep_time/1000));
   delay((unsigned int) (sleep_time % 1000));

}

/* additional function by Sessai on June 12, 1997 */

void wait_boundary3(int bnd_min,int bnd_sec,
                int bnd_start_hr,int bnd_start_min,int bnd_start_sec) {
   long int sleep_time;
   long int tmpL;

   if (bnd_min*60+bnd_sec <=0) return;	// added by Sessai on Nov.28,1997

   tmpL= (((long)(          hr*60+          min)*60L
                +(long)sec          )*1000L+(long)msec)
        -(((long)(bnd_start_hr*60+bnd_start_min)*60L
                +(long)bnd_start_sec)*1000L);
   if(tmpL < 0L) tmpL+=86400000L;       // 24*60*60*1000
   sleep_time=( (bnd_min*60L+bnd_sec)*1000L ) -
               ( tmpL % ( (bnd_min*60L+bnd_sec)*1000L) );
   sleep_time-=20L;
   if (sleep_time<0) sleep_time=0L;
   sleep((unsigned int) (sleep_time/1000));
   delay((unsigned int) (sleep_time % 1000));

}


/* This function calculates how many beams we should skip */

/* additional function by Sessai */

int calc_skip2(int bnd,int bnd_sec) {

        /* modified by Sessai!!!!!!!!! */
        int rs;
        if (intt <=0) return 0;

	if (bnd*60+bnd_sec <=0) return 0; // added by Sessai on Nov.28,1997

        // rs=(min % bnd)*60 + sec;
        rs=(min*60+sec)%(bnd*60+bnd_sec);
        if (rs > intt/4 + 1) return (rs + (intt + 1)/2)/intt + 2;

  /* if (sec !=0) return ((min % bnd)*60 + sec + (intt + 1)/2)/intt + 2; */
  return 0;
}

/* additional function in incorporated with wait_boundary_3
                                        by Sessai on June 12, 1997 */

int calc_skip3(int bnd_min,int bnd_sec,
                int bnd_start_hr,int bnd_start_min,int bnd_start_sec) {

        int rs;
        long int tmpL;

        if (intt <=0) return 0;

	if (bnd_min*60+bnd_sec <=0) return 0; // added by Sessai on Nov.28,1997

        tmpL= ((long)(          hr*60+          min)*60L+(long)sec          )
             -((long)(bnd_start_hr*60+bnd_start_min)*60L+(long)bnd_start_sec);
        if(tmpL < 0L) tmpL+=86400L;     // 24*60*60
        rs=(int)(tmpL%(bnd_min*60+bnd_sec));
        if (rs > intt/4 + 1) return (rs + (intt + 1)/2)/intt + 2;

  return 0;
}


/* check for two hour file boundary */

/* determine day or night */