MISR Toolkit  1.5.1
MtkWriteEnviFile.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkWriteEnviFile =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2005, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrWriteData.h"
18 #include "MisrError.h"
19 #include "MisrUtil.h"
20 #include "MisrUnitConv.h"
21 #include <string.h>
22 #include <stdio.h>
23 #include <math.h>
24 
32  const char *envifilename,
34  MTKt_MapInfo mapinfo,
35  const char *misrfilename,
36  const char *misrgridname,
37  const char *misrfieldname )
38 {
39  FILE *fp; /* File pointer */
40  char hdrfname[300]; /* ENVI Filename plus extension */
41  char imgfname[300]; /* ENVI Filename plus extension */
42  int idl_datatype[MTKd_NDATATYPE] = { 0, 1, 1, 1, 1, 2, 12, 3, 13, 14, 15, 4, 5 };
43  /* IDL/MTK Datatype mapping -
44  1=8 bit byte;
45  2=16-bit signed integer;
46  3=32-bit signed long integer;
47  4=32-bit floating point;
48  5=64-bit double precision floating point;
49  6=2x32-bit complex, real-imaginary pair of double precision;
50  9=2x64-bit double precision complex, real-imaginary pair of double precision;
51  12=16-bit unsigned integer;
52  13=32-bit unsigned long integer;
53  14=64-bit signed long integer; and
54  15=64-bit unsigned long integer. */
55  double a, b, e2; /* Ellipsoid semi-major axis, semi-minor axis,
56  and eccentricity squared */
57  double IncAng, AscLong; /* Inclination angle and Longitude of ascending node (dd) */
58  double x0, y0; /* False Easting and False Northing (dd) */
59  double PSRev, LRat; /* Satellite revolution and Landsat Ratio (dd) */
60  int endian = 1; /* Endian test */
61  char *endian_ptr = (char *)&endian;
62 
63  if (envifilename == NULL)
64  return MTK_NULLPTR;
65 
66  /* Write binary file */
67 
68  strcpy(imgfname, envifilename);
69  strcat(imgfname, ".img");
70  if ((fp = fopen(imgfname, "wb")) == NULL) {
71  MTK_ERR_MSG_JUMP("Error opening binfile");
72  }
73  fwrite(buf.dataptr, buf.datasize, buf.nline * buf.nsample, fp);
74  fclose(fp);
75 
76 
77  /* Write hdr file */
78 
79  strcpy(hdrfname, envifilename);
80  strcat(hdrfname, ".hdr");
81  if ((fp = fopen(hdrfname, "wb")) == NULL) {
82  MTK_ERR_MSG_JUMP("Error opening hdrfile");
83  }
84 
85  fprintf(fp, "ENVI\n");
86  fprintf(fp, "description = {%s}\n", misrfilename);
87  fprintf(fp, "samples = %d\n", buf.nsample);
88  fprintf(fp, "lines = %d\n", buf.nline);
89  fprintf(fp, "bands = 1\n");
90  fprintf(fp, "header offset = 0\n");
91  fprintf(fp, "file type = ENVI Standard\n");
92  fprintf(fp, "data type = %d\n", idl_datatype[buf.datatype]);
93  fprintf(fp, "interleave = bsq\n");
94  fprintf(fp, "sensor type = MISR\n");
95  if (endian_ptr[0] == 1)
96  fprintf(fp,"byte order = 0\n");
97  else
98  fprintf(fp,"byte order = 1\n");
99 
100  fprintf(fp, "x start = 0.0\n");
101  fprintf(fp, "y start = 0.0\n");
102 
103  fprintf(fp, "map info = {");
104  fprintf(fp, "Space Oblique Mercator A (MISR Path %d), ", mapinfo.path);
105  fprintf(fp, "1.0, ");
106  fprintf(fp, "1.0, ");
107  fprintf(fp, "%f, ", mapinfo.som.ulc.x);
108  fprintf(fp, "%f, ", mapinfo.som.ulc.y);
109  fprintf(fp, "%f, ", (float)mapinfo.resolution);
110  fprintf(fp, "%f, ", (float)mapinfo.resolution);
111  fprintf(fp, "WGS-84, ");
112  fprintf(fp, "units=Meters, ");
113  fprintf(fp, "rotation=90.0");
114  fprintf(fp, "}\n");
115 
116  /* Convert eccentricity fo the ellipsoid to b parameter */
117  a = mapinfo.pp.projparam[0];
118  e2 = mapinfo.pp.projparam[1];
119  e2 = fabs(e2);
120  b = sqrt(pow(a,2) - e2 * pow(a,2));
121  /* Convert from packed DMS to decimal degrees */
122  MtkDmsToDd(mapinfo.pp.projparam[3], &IncAng);
123  MtkDmsToDd(mapinfo.pp.projparam[4], &AscLong);
124  PSRev = mapinfo.pp.projparam[8];
125  MtkDmsToDd(mapinfo.pp.projparam[9], &LRat);
126  /* No false easting or false northing */
127  x0 = 0.0;
128  y0 = 0.0;
129  /* Apparently Envi uses 37 for generic SOM A - see ENVI's map_proj.txt */
130 
131  fprintf(fp, "projection info = {");
132  fprintf(fp, "37, ");
133  fprintf(fp, "%f, ", a);
134  fprintf(fp, "%f, ", b);
135  fprintf(fp, "%f, ", IncAng);
136  fprintf(fp, "%f, ", AscLong);
137  fprintf(fp, "%f, ", x0);
138  fprintf(fp, "%f, ", y0);
139  fprintf(fp, "%f, ", PSRev);
140  fprintf(fp, "%f, ", LRat);
141  fprintf(fp, "WGS-84, ");
142  fprintf(fp, "Space Oblique Mercator A (MISR Path %d), ", mapinfo.path);
143  fprintf(fp, "units=Meters");
144  fprintf(fp, "}\n");
145 
146  fprintf(fp, "pixel size = {");
147  fprintf(fp, "%f, ", (float)mapinfo.resolution);
148  fprintf(fp, "%f, ", (float)mapinfo.resolution);
149  fprintf(fp, "units=Meters");
150  fprintf(fp, "}\n");
151 
152  fprintf(fp, "band names = {");
153  fprintf(fp, "%s:%s", misrgridname, misrfieldname);
154  fprintf(fp, "}\n");
155 
156  fclose(fp);
157 
158  return MTK_SUCCESS;
159 ERROR_HANDLE:
160  return MTK_FAILURE;
161 }
double projparam[15]
Map Information.
Definition: MisrMapQuery.h:65
2-dimensional Data Buffer
Definition: MisrUtil.h:98
long b
Definition: jpegint.h:371
MTKt_MisrProjParam pp
Definition: MisrMapQuery.h:76
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDmsToDd(double dms, double *dd)
Convert packed degrees, minutes, seconds to decimal degrees.
Definition: MtkDmsToDd.c:33
MTKt_DataType datatype
Definition: MisrUtil.h:102
#define MTKd_NDATATYPE
Definition: MisrUtil.h:30
MTKt_status MtkWriteEnviFile(const char *envifilename, MTKt_DataBuffer buf, MTKt_MapInfo mapinfo, const char *misrfilename, const char *misrgridname, const char *misrfieldname)
Write ENVI file.
#define MTK_ERR_MSG_JUMP(msg)
Definition: MisrError.h:169
MTKt_status
Definition: MisrError.h:11
MTKt_SomRegion som
Definition: MisrMapQuery.h:74
HDFFCLIBAPI intf * buf
MTKt_SomCoord ulc
Definition: MisrMapQuery.h:56

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