OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ias_geo_convert_dms2deg.c
Go to the documentation of this file.
1 /****************************************************************************
2 NAME: ias_geo_convert_dms2deg
3 
4 PURPOSE: Convert angles to total degrees.
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 in DMS
14  Convert the angle to total degrees (in place).
15  The angle is then checked to be sure it is within the limits of
16  its use (LAT, LON, or DEGREES).
17 
18 *****************************************************************************/
19 #include <string.h>
20 #include "ias_const.h"
21 #include "ias_logging.h"
22 #include "ias_geo.h"
23 
25 (
26  double angle_dms, /* I: Angle in DMS (DDDMMMSSS) format */
27  double *angle_degrees,/* O: Angle in decimal degrees */
28  const char *type /* I: Angle usage type (LAT, LON, or DEGREES) */
29 )
30 {
31  float second; /* Second of DMS angle value */
32  float upper; /* Upper bound of the angle value for its use */
33  float lower; /* Lower bound of the angle value for its use */
34 
35  int degree; /* Degree of DMS angle value */
36  int minute; /* Minute of DMS angle value */
37  short sign; /* Sign of the angle */
38 
39  /* Find the upper and lower bound limits for the angle based on its
40  usage type. */
41  if (strncmp (type,"LAT",3) == 0)
42  { /* LAT */
43  upper = 90.0;
44  lower = -90.0;
45  } /* LAT */
46  else if (strncmp (type,"LON",3) == 0)
47  { /* LON */
48  upper = 180.0;
49  lower = -180.0;
50  } /* LON */
51  else
52  { /* DEGREES */
53  upper = 360.0;
54  lower = 0.0;
55  }
56 
57  /* Convert the angle to total degrees based on DMS. */
58  if (angle_dms < 0)
59  {
60  sign = -1;
61  angle_dms = angle_dms * -1;
62  }
63  else
64  sign = 1;
65 
66  degree = (int) (angle_dms / 1000000);
67  angle_dms = angle_dms - (degree * 1000000);
68  minute = (int) (angle_dms / 1000);
69  second = angle_dms - (minute * 1000);
70  *angle_degrees = sign * (degree + (minute/60.0) + (second/3600.0));
71 
72  /* Check to make sure the angle is within the limits of its use (LAT, LON,
73  or DEGREES) */
74  if ((*angle_degrees > upper) || (*angle_degrees < lower))
75  {
76  IAS_LOG_ERROR("Illegal coordinate value in decdeg");
77  IAS_LOG_ERROR("Calculated angle of %f outside bounds of %f to %f",
78  *angle_degrees, lower, upper);
79  return ERROR;
80  }
81 
82  return SUCCESS;
83 }
#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
int ias_geo_convert_dms2deg(double angle_dms, double *angle_degrees, const char *type)
#define ERROR
Definition: ancil.h:24