MISR Toolkit  1.5.1
MtkGenericMapInfoRead.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkGenericMapInfoRead =
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 #include "MisrToolkit.h"
17 #include <stdlib.h>
18 #include <math.h>
19 #ifndef _MSC_VER
20 #include <strings.h> /* for strncasecmp */
21 #endif
22 
23 #define MAX_LENGTH 1000
24 
79  const char *Filename,
80  MTKt_GenericMapInfo *Map_info
81 )
82 {
83  MTKt_status status; /* Return status */
84  MTKt_status status_code; /* Return status of this function */
86  /* Map information */
87  double min_corner_x = 0.0;
88  double min_corner_y = 0.0;
89  double resolution_x = 0.0;
90  double resolution_y = 0.0;
91  int number_pixel_x = 0;
92  int number_pixel_y = 0;
93  MTKt_OriginCode origin_code = -1;
94  MTKt_PixRegCode pix_reg_code = -1;
95  FILE *fp = NULL;
96  char line[MAX_LENGTH]; /* Line buffer for input file. */
97  char origin_code_string[3] = {0,0,0};
98  /* Buffer for origin_code string. */
99  char pix_reg_code_string[7] = {0,0,0,0,0,0,0};
100  /* Buffer for pix_reg_code string. */
101  int min_corner_x_found = 0;
102  int min_corner_y_found = 0;
103  int resolution_x_found = 0;
104  int resolution_y_found = 0;
105  int number_pixel_x_found = 0;
106  int number_pixel_y_found = 0;
107  int origin_code_found = 0;
108  int pix_reg_code_found = 0;
109 
110  /* ------------------------------------------------------------------ */
111  /* Argument check: Filename == NULL */
112  /* ------------------------------------------------------------------ */
113 
114  if (Filename == NULL) {
115  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Filename == NULL");
116  }
117 
118  /* ------------------------------------------------------------------ */
119  /* Argument check: Map_info == NULL */
120  /* ------------------------------------------------------------------ */
121 
122  if (Map_info == NULL) {
123  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Map_info == NULL");
124  }
125 
126  /* ------------------------------------------------------------------ */
127  /* Open file. */
128  /* ------------------------------------------------------------------ */
129 
130  fp = fopen(Filename,"r");
131  if (fp == NULL) {
133  }
134 
135  /* ------------------------------------------------------------------ */
136  /* Scan input file for map parameters. */
137  /* ------------------------------------------------------------------ */
138 
139  while (NULL != fgets(line,MAX_LENGTH,fp)) {
140  if (line[0] == '#') continue;
141 
142  if (1 == sscanf(line, "min_corner_x = %lf",&min_corner_x)) {
143  min_corner_x_found = 1;
144  } else if (1 == sscanf(line, "min_corner_y = %lf",&min_corner_y)) {
145  min_corner_y_found = 1;
146  } else if (1 == sscanf(line, "resolution_x = %lf",&resolution_x)) {
147  resolution_x_found = 1;
148  } else if (1 == sscanf(line, "resolution_y = %lf",&resolution_y)) {
149  resolution_y_found = 1;
150  } else if (1 == sscanf(line, "number_pixel_x = %d",&number_pixel_x)) {
151  number_pixel_x_found = 1;
152  } else if (1 == sscanf(line, "number_pixel_y = %d",&number_pixel_y)) {
153  number_pixel_y_found = 1;
154  } else if (1 == sscanf(line, "origin_code = %2s",origin_code_string)) {
155  origin_code_found = 1;
156  } else if (1 == sscanf(line, "pix_reg_code = %6s",pix_reg_code_string)) {
157  pix_reg_code_found = 1;
158  } else {
159  /* Skip unrecognized input */
160  }
161  }
162 
163  /* ------------------------------------------------------------------ */
164  /* Close file. */
165  /* ------------------------------------------------------------------ */
166 
167  fclose(fp);
168  fp = NULL;
169 
170  /* ------------------------------------------------------------------ */
171  /* Check that all required paramters were found. */
172  /* Argument check: min_corner_x not found */
173  /* min_corner_y not found */
174  /* resolution_x not found */
175  /* resolution_y not found */
176  /* number_pixel_x not found */
177  /* number_pixel_y not found */
178  /* origin_code not found */
179  /* pix_reg_code not found */
180  /* ------------------------------------------------------------------ */
181 
182  if (!min_corner_x_found) {
183  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"min_corner_x not found");
184  }
185  if (!min_corner_y_found) {
186  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"min_corner_y not found");
187  }
188  if (!resolution_x_found) {
189  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"resolution_x not found");
190  }
191  if (!resolution_y_found) {
192  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"resolution_y not found");
193  }
194  if (!number_pixel_x_found) {
195  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"number_pixel_x not found");
196  }
197  if (!number_pixel_y_found) {
198  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"number_pixel_y not found");
199  }
200  if (!origin_code_found) {
201  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"origin_code not found");
202  }
203  if (!pix_reg_code_found) {
204  MTK_ERR_CODE_MSG_JUMP(MTK_NOT_FOUND,"pix_reg_code not found");
205  }
206 
207  /* ------------------------------------------------------------------ */
208  /* Parse origin_code */
209  /* ------------------------------------------------------------------ */
210 
211  if (0 == strncasecmp("UL",origin_code_string,2)) {
212  origin_code = MTKe_ORIGIN_UL;
213  } else if (0 == strncasecmp("UR",origin_code_string,2)) {
214  origin_code = MTKe_ORIGIN_UR;
215  } else if (0 == strncasecmp("LL",origin_code_string,2)) {
216  origin_code = MTKe_ORIGIN_LL;
217  } else if (0 == strncasecmp("LR",origin_code_string,2)) {
218  origin_code = MTKe_ORIGIN_LR;
219  } else {
220  fprintf(stderr,"Unrecognized origin_code: %2s\n",origin_code_string);
222  }
223 
224  /* ------------------------------------------------------------------ */
225  /* Parse pix_reg_code */
226  /* ------------------------------------------------------------------ */
227 
228  if (0 == strncasecmp("CENTER",pix_reg_code_string,6)) {
229  pix_reg_code = MTKe_PIX_REG_CENTER;
230  } else if (0 == strncasecmp("CORNER",pix_reg_code_string,6)) {
231  pix_reg_code = MTKe_PIX_REG_CORNER;
232  } else {
233  fprintf(stderr,"Unrecognized pix_reg_code: %6s\n",pix_reg_code_string);
235  }
236 
237  /* ------------------------------------------------------------------ */
238  /* Initialize map information. */
239  /* ------------------------------------------------------------------ */
240 
241  status = MtkGenericMapInfo(min_corner_x, min_corner_y, resolution_x, resolution_y,
242  number_pixel_x, number_pixel_y, origin_code,
243  pix_reg_code, &map_info_tmp);
244  MTK_ERR_COND_JUMP(status);
245 
246  /* ------------------------------------------------------------------ */
247  /* Return. */
248  /* ------------------------------------------------------------------ */
249 
250  *Map_info = map_info_tmp;
251  return MTK_SUCCESS;
252 
253 ERROR_HANDLE:
254  if (fp != NULL) {
255  fclose(fp);
256  }
257  return status_code;
258 }
259 
260 
MTKt_OriginCode
Origin code.
Definition: MisrMapQuery.h:84
Generic map information.
Definition: MisrMapQuery.h:98
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_PixRegCode
Pixel registration code.
Definition: MisrMapQuery.h:92
MTKt_status MtkGenericMapInfoRead(const char *Filename, MTKt_GenericMapInfo *Map_info)
Initialize a MTKt_GenericMapInfo structure using data from an external file.
#define MAX_LENGTH
#define MTK_ERR_CODE_MSG_JUMP(code, msg)
Definition: MisrError.h:181
int strncasecmp(const char *s1, const char *s2, size_t n)
Definition: strncasecmp.c:18
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
#define MTKT_GENERICMAPINFO_INIT
Definition: MisrMapQuery.h:120
MTKt_status MtkGenericMapInfo(double Min_x, double Min_y, double Resolution_x, double Resolution_y, int Number_pixel_x, int Number_pixel_y, MTKt_OriginCode Origin_code, MTKt_PixRegCode Pix_reg_code, MTKt_GenericMapInfo *Map_info)
Initialize a MTKt_GenericMapInfo structure.

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