Example #1
0
main()
{
      int year,month,day;
      double lon, lat;
      double daylen, civlen, nautlen, astrlen;
      double rise, set, civ_start, civ_end, naut_start, naut_end,
             astr_start, astr_end;
      double daylen_int,daylen_frac;  /* CWR */
      int    rs, civ, naut, astr;
      int daylen_hours,daylen_min, gmtoff_hr; /* cwr */
      double rise_int,rise_frac,set_int,set_frac; 
      int rise_hours,rise_min,set_hours,set_min;

      time_t the_time; /* only need so can get time zone of local machine... */
      struct tm *mylocal_time;
      char buf[80];
      
/* get time and offset from GMT */ 
      time(&the_time);
      mylocal_time = localtime(&the_time);
      gmtoff_hr = mylocal_time->tm_gmtoff/3600;

      printf("Enter longitude and latitude in decimal degrees, i.e. -97.75 30.23\n");
      printf("Hint: divide minutes by 60 to get decimal portion\n");
      printf( "Longitude (+ is east) and latitude (+ is north) : " );
      fgets(buf, 80, stdin);
      sscanf(buf, "%lf %lf", &lon, &lat );

      for(;;)
      {
            printf( "Input date ( yyyy mm dd ) (ctrl-C exits): " );
            fgets(buf, 80, stdin);
            sscanf(buf, "%d %d %d", &year, &month, &day );

            daylen  = day_length(year,month,day,lon,lat);
            civlen  = day_civil_twilight_length(year,month,day,lon,lat);
            nautlen = day_nautical_twilight_length(year,month,day,lon,lat);
            astrlen = day_astronomical_twilight_length(year,month,day,
                  lon,lat);
                  
            daylen_frac = modf(daylen,&daylen_int); /* daylen into int and fraction */
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "Day length:                 %5.2f hours", daylen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf(civlen,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "With civil twilight         %5.2f hours", civlen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf(nautlen,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "With nautical twilight      %5.2f hours", nautlen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf(astrlen,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "With astronomical twilight  %5.2f hours", astrlen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf((civlen-daylen)/2.0,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "Length of twilight: civil   %5.2f hours",
                  (civlen-daylen)/2.0);
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf((nautlen-daylen)/2.0,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "                  nautical  %5.2f hours",
                  (nautlen-daylen)/2.0);
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf((astrlen-daylen)/2.0,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "              astronomical  %5.2f hours",
                  (astrlen-daylen)/2.0);
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );

            rs   = sun_rise_set         ( year, month, day, lon, lat,
                                          &rise, &set );
            civ  = civil_twilight       ( year, month, day, lon, lat,
                                          &civ_start, &civ_end );
            naut = nautical_twilight    ( year, month, day, lon, lat,
                                          &naut_start, &naut_end );
            astr = astronomical_twilight( year, month, day, lon, lat,
                                          &astr_start, &astr_end );

            daylen_frac = modf(((rise+set)/2.0)+gmtoff_hr ,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);

            printf( "Sun at south %5.2fh UT,%20d:%02d local\n", (rise+set)/2.0,daylen_hours,daylen_min);

            switch( rs )
            {
                case 0:
                    /* add minutes and local. CWR*/
                    rise_frac = modf(rise+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(set+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Sun rises %5.2fh UT, sets %5.2fh UT;\n    rises %02d:%02d,     sets %02d:%02d local\n",
                             rise, set, rise_hours,rise_min,set_hours,set_min);
                    break;
                case +1:
                    printf( "Sun above horizon\n" );
                    break;
                case -1:
                    printf( "Sun below horizon\n" );
                    break;
            }

            switch( civ )
            {
                case 0:
                    rise_frac = modf(civ_start+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(civ_end+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Civil twilight starts %5.2fh, "
                            "ends %5.2fh UT;\n               starts %02d:%02d,  ends %02d:%02d local\n", 
                            civ_start, civ_end, 
                            rise_hours, rise_min, set_hours, set_min);
                    break;
                case +1:
                    printf( "Never darker than civil twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as civil twilight\n" );
                    break;
            }

            switch( naut )
            {
                case 0:
                    rise_frac = modf(naut_start+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(naut_end+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Nautical twilight starts %5.2fh, "
                            "ends %5.2fh UT;\n                  starts %02d:%02d,  ends %02d:%02d local\n", 
                            naut_start, naut_end, 
                            rise_hours, rise_min, set_hours, set_min );
                    break;
                case +1:
                    printf( "Never darker than nautical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as nautical twilight\n" );
                    break;
            }

            switch( astr )
            {
                case 0:
                    rise_frac = modf(astr_start+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(astr_end+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Astronomical twilight starts %5.2fh, "
                            "ends %5.2fh UT;\n                      starts %02d:%02d,  ends %02d:%02d local\n", 
                            astr_start, astr_end, 
                            rise_hours, rise_min, set_hours, set_min );
                    break;
                case +1:
                    printf( "Never darker than astronomical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as astronomical twilight\n" );
                    break;
            }
      return 0;
      }
}
Example #2
0
main()
{
      int year,month,day;
      double lon, lat;
      double daylen, civlen, nautlen, astrlen;
      double rise, set, civ_start, civ_end, naut_start, naut_end,
             astr_start, astr_end;
      int    rs, civ, naut, astr;
      char buf[80];

      printf( "Longitude (+ is east) and latitude (+ is north) : " );
      fgets(buf, 80, stdin);
      sscanf(buf, "%lf %lf", &lon, &lat );

      for(;;)
      {
            printf( "Input date ( yyyy mm dd ) (ctrl-C exits): " );
            fgets(buf, 80, stdin);
            sscanf(buf, "%d %d %d", &year, &month, &day );

            daylen  = day_length(year,month,day,lon,lat);
            civlen  = day_civil_twilight_length(year,month,day,lon,lat);
            nautlen = day_nautical_twilight_length(year,month,day,lon,lat);
            astrlen = day_astronomical_twilight_length(year,month,day,
                  lon,lat);

            printf( "Day length:                 %5.2f hours\n", daylen );
            printf( "With civil twilight         %5.2f hours\n", civlen );
            printf( "With nautical twilight      %5.2f hours\n", nautlen );
            printf( "With astronomical twilight  %5.2f hours\n", astrlen );
            printf( "Length of twilight: civil   %5.2f hours\n",
                  (civlen-daylen)/2.0);
            printf( "                  nautical  %5.2f hours\n",
                  (nautlen-daylen)/2.0);
            printf( "              astronomical  %5.2f hours\n",
                  (astrlen-daylen)/2.0);

            rs   = sun_rise_set         ( year, month, day, lon, lat,
                                          &rise, &set );
            civ  = civil_twilight       ( year, month, day, lon, lat,
                                          &civ_start, &civ_end );
            naut = nautical_twilight    ( year, month, day, lon, lat,
                                          &naut_start, &naut_end );
            astr = astronomical_twilight( year, month, day, lon, lat,
                                          &astr_start, &astr_end );

            printf( "Sun at south %5.2fh UT\n", (rise+set)/2.0 );

            switch( rs )
            {
                case 0:
                    printf( "Sun rises %5.2fh UT, sets %5.2fh UT\n",
                             rise, set );
                    break;
                case +1:
                    printf( "Sun above horizon\n" );
                    break;
                case -1:
                    printf( "Sun below horizon\n" );
                    break;
            }

            switch( civ )
            {
                case 0:
                    printf( "Civil twilight starts %5.2fh, "
                            "ends %5.2fh UT\n", civ_start, civ_end );
                    break;
                case +1:
                    printf( "Never darker than civil twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as civil twilight\n" );
                    break;
            }

            switch( naut )
            {
                case 0:
                    printf( "Nautical twilight starts %5.2fh, "
                            "ends %5.2fh UT\n", naut_start, naut_end );
                    break;
                case +1:
                    printf( "Never darker than nautical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as nautical twilight\n" );
                    break;
            }

            switch( astr )
            {
                case 0:
                    printf( "Astronomical twilight starts %5.2fh, "
                            "ends %5.2fh UT\n", astr_start, astr_end );
                    break;
                case +1:
                    printf( "Never darker than astronomical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as astronomical twilight\n" );
                    break;
            }
      return 0;
      }
}