MISR Toolkit  1.5.1
MtkGCTPCreateLatLon.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkGCTPCreateLatLon =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2008, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrMapQuery.h"
18 #include "MisrUtil.h"
19 #include "MisrCoordQuery.h" /* definitino of inv_init */
20 #include <stdlib.h>
21 #include <math.h>
22 
37  const MTKt_GenericMapInfo *Map_info,
38  const MTKt_GCTPProjInfo *Proj_info,
39  MTKt_DataBuffer *Latitude,
40  MTKt_DataBuffer *Longitude
41 )
42 {
43  MTKt_status status; /* Return status */
44  MTKt_status status_code; /* Return status of this function */
46  /* Latitude values in degrees */
47  MTKt_DataBuffer longitude_tmp = MTKT_DATABUFFER_INIT;
48  /* Longitude values in degrees */
49  int iflg; /* Status flag returned by GCTP library. */
50  int (*inv_trans[1000])(); /* Array of transform functions returned by GCTP. */
51  int iline; /* Loop iterator. */
52  int isample; /* Loop iterator. */
53  double rad2deg = 180.0 / acos(-1);
54 
55  /* ------------------------------------------------------------------ */
56  /* Argument check: Map_info == NULL */
57  /* Map_info->size_x < 1 */
58  /* Map_info->size_y < 1 */
59  /* ------------------------------------------------------------------ */
60 
61  if (Map_info == NULL) {
62  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Map_info == NULL");
63  }
64  if (Map_info->size_line < 1) {
65  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Map_info->size_line < 1");
66  }
67  if (Map_info->size_sample < 1) {
68  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Map_info->size_sample < 1");
69  }
70 
71  /* ------------------------------------------------------------------ */
72  /* Argument check: Proj_info == NULL */
73  /* ------------------------------------------------------------------ */
74 
75  if (Proj_info == NULL) {
76  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Proj_info == NULL");
77  }
78 
79  /* ------------------------------------------------------------------ */
80  /* Argument check: Latitude == NULL */
81  /* ------------------------------------------------------------------ */
82 
83  if (Latitude == NULL) {
84  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Latitude == NULL");
85  }
86 
87  /* ------------------------------------------------------------------ */
88  /* Argument check: Longitude == NULL */
89  /* ------------------------------------------------------------------ */
90 
91  if (Longitude == NULL) {
92  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Longitude == NULL");
93  }
94 
95  /* ------------------------------------------------------------------ */
96  /* Allocate memory for latitude and longitude. */
97  /* ------------------------------------------------------------------ */
98 
99  status = MtkDataBufferAllocate(Map_info->size_line, Map_info->size_sample,
100  MTKe_double, &latitude_tmp);
101  MTK_ERR_COND_JUMP(status);
102 
103  status = MtkDataBufferAllocate(Map_info->size_line, Map_info->size_sample,
104  MTKe_double, &longitude_tmp);
105  MTK_ERR_COND_JUMP(status);
106 
107  /* ------------------------------------------------------------------ */
108  /* Initialize GCTP library. */
109  /* ------------------------------------------------------------------ */
110 
111  inv_init(Proj_info->proj_code, Proj_info->zone_code, Proj_info->proj_param,
112  Proj_info->sphere_code, 0, 0, &iflg, inv_trans);
113  if (iflg) {
114  printf("iflg = %d\n",iflg);
116  }
117 
118  /* ------------------------------------------------------------------ */
119  /* For each pixel... */
120  /* ------------------------------------------------------------------ */
121 
122  for (iline = 0 ; iline < Map_info->size_line ; iline++) {
123  for (isample = 0 ; isample < Map_info->size_sample ; isample++) {
124  double x,y; /* Map coordinates */
125  double lat,lon; /* Lat/lon coordinates */
126  double line, sample; /* Pixel coordinates. */
127 
128  /* ------------------------------------------------------------------ */
129  /* Calculate map coordinate for this pixel. */
130  /* ------------------------------------------------------------------ */
131 
132  line = (((iline - Map_info->tline[2]) * Map_info->tline[3]) -
133  Map_info->tline[1]) / Map_info->tline[0];
134  sample = (((isample - Map_info->tsample[2]) * Map_info->tsample[3]) -
135  Map_info->tsample[1]) / Map_info->tsample[0];
136 
137  /* ------------------------------------------------------------------ */
138  /* Calculate lat/lon values at this map coordinate. */
139  /* Mapping from X, Y to line, sample depends on the origin code as */
140  /* follows: */
141  /* */
142  /* origin_code line sample */
143  /* UL Y X */
144  /* UR X Y */
145  /* LL X Y */
146  /* LR Y X */
147  /* */
148  /* Argument check: Origin_code not recognized */
149  /* ------------------------------------------------------------------ */
150 
151  switch(Map_info->origin_code) {
152  case MTKe_ORIGIN_UL:
153  case MTKe_ORIGIN_LR:
154  x = sample;
155  y = line;
156  break;
157  case MTKe_ORIGIN_UR:
158  case MTKe_ORIGIN_LL:
159  x = line;
160  y = sample;
161  break;
162  default:
163  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Origin_code not recognized");
164  }
165 
166  inv_trans[Proj_info->proj_code](x, y, &lon, &lat);
167 
168  /* ------------------------------------------------------------------ */
169  /* Set lat/lon for this pixel. */
170  /* ------------------------------------------------------------------ */
171 
172  latitude_tmp.data.d[iline][isample] = lat * rad2deg;
173  longitude_tmp.data.d[iline][isample] = lon * rad2deg;
174 
175  /* ------------------------------------------------------------------ */
176  /* End loop for each pixel. */
177  /* ------------------------------------------------------------------ */
178 
179  }
180  }
181 
182  /* ------------------------------------------------------------------ */
183  /* Return. */
184  /* ------------------------------------------------------------------ */
185 
186  *Latitude = latitude_tmp;
187  *Longitude = longitude_tmp;
188  return MTK_SUCCESS;
189 
190 ERROR_HANDLE:
191  MtkDataBufferFree(&latitude_tmp);
192  MtkDataBufferFree(&longitude_tmp);
193  return status_code;
194 }
195 
196 
MTKt_double ** d
Definition: MisrUtil.h:94
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
MTKt_OriginCode origin_code
Definition: MisrMapQuery.h:115
Generic map information.
Definition: MisrMapQuery.h:98
MTKt_DataBufferType data
Definition: MisrUtil.h:104
GCTP projection information.
Definition: MisrMapQuery.h:123
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_status MtkGCTPCreateLatLon(const MTKt_GenericMapInfo *Map_info, const MTKt_GCTPProjInfo *Proj_info, MTKt_DataBuffer *Latitude, MTKt_DataBuffer *Longitude)
Create an array of latitude and longitude values corresponding to each pixel in the given map...
2-dimensional Data Buffer
Definition: MisrUtil.h:98
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
double proj_param[15]
Definition: MisrMapQuery.h:127
#define MTK_ERR_CODE_MSG_JUMP(code, msg)
Definition: MisrError.h:181
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
int inv_init(int insys, int inzone, const double *inparm, int indatum, char *fn27, char *fn83, int *iflg, int(*inv_trans[])(double, double, double *, double *))

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51