OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ias_geo_convert_deg2dms.c
Go to the documentation of this file.
1 /****************************************************************************
2 NAME: ias_geo_convert_deg2dms
3 
4 PURPOSE: To convert total degrees to packed degrees, minutes, seconds.
5 
6 RETURN VALUE: Type = int
7 Value Description
8 ----- -----------
9 SUCCESS Successful completion
10 ERROR Operation failed
11 
12 ALGORITHM DESCRIPTION:
13  Receive an angle degrees
14  Convert it to DMS.
15  The angle is then checked to be sure it is within the limits
16  of its use (LAT, LON, or DEGREES).
17 
18 *****************************************************************************/
19 #include <stdlib.h>
20 #include <string.h>
21 #include "ias_logging.h"
22 #include "ias_geo.h"
23 
25 (
26  double deg, /* I: Angle in seconds, minutes, or degrees */
27  double *dms, /* O: Angle converted to DMS */
28  const char *check /* I: Angle usage type (LAT, LON, or DEGREES) */
29 )
30 {
31  double tsec; /* Total seconds in the input angle */
32  double maxdms; /* Upper bound of the angle value for its use */
33  double mindms; /* Lower bound of the angle value for its use */
34 
35  int tdeg; /* Degrees in the input angle */
36  int tmin; /* Minutes in the input angle */
37  int sign; /* Sign of the input angle */
38 
39  /* Find the upper and lower bound limits for the angle based on its
40  usage type (LAT, LON, DEGREES). */
41  if (strcmp (check,"LAT") == 0)
42  {
43  maxdms = 90000000;
44  mindms = -90000000;
45  }
46  else if (strcmp (check,"LON") == 0)
47  {
48  maxdms = 180000000;
49  mindms = -180000000;
50  }
51  else
52  { /* DEGREES */
53  maxdms = 360000000;
54  mindms = 0;
55  }
56 
57  /* Convert the angle to DMS based on the coform in degrees. */
58 
59  /* Extract the deg, min, and sec portions of the angle. */
60  ias_geo_find_deg(deg, &tdeg);
61  ias_geo_find_min(deg, &tmin);
62  ias_geo_find_sec(deg, &tsec);
63 
64  /* Find the sign of the angle based on the degrees. */
65  sign = 1;
66  if (tdeg < 0)
67  sign = -1;
68 
69  /* Calculate DMS from the degree, minutes, seconds values. */
70  tdeg = abs (tdeg);
71  *dms = ((tdeg * 1000000) + (tmin * 1000) + tsec) * sign;
72 
73  /* Check to be sure the coordinate is within the bounds. */
74  if ((*dms > maxdms) || (*dms < mindms))
75  {
76  IAS_LOG_ERROR("Calculated DMS value of %f outside bounds of %f to %f",
77  *dms, mindms, maxdms);
78  return ERROR;
79  }
80 
81  return SUCCESS;
82 }
#define SUCCESS
Definition: ObpgReadGrid.h:15
#define IAS_LOG_ERROR(format,...)
Definition: ias_logging.h:96
int sign(double x)
Definition: proj_cproj.c:340
void ias_geo_find_deg(double angle, int *degree)
void ias_geo_find_min(double angle, int *minute)
void ias_geo_find_sec(double angle, double *second)
int ias_geo_convert_deg2dms(double deg, double *dms, const char *check)
#define abs(a)
Definition: misc.h:90
#define ERROR
Definition: ancil.h:24