File: mcidas.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (189 lines) | stat: -rw-r--r-- 3,773 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* mcidas.c:
 *
 * McIDAS areafile support.  contributed by Glenn P. Davis
 * (davis@unidata.ucar.edu).
 */

#include "xli.h"
#include "imagetypes.h"
#include "mcidas.h"

char *mc_sensor(int sscode);

/*
 * convert from little endian to big endian four byte object
 */
static unsigned long
vhtonl(long unsigned int lend)
{
	unsigned long bend ;
	unsigned char *lp, *bp ;

	lp = ((unsigned char *)&lend) + 3 ;
	bp = (unsigned char *) &bend ;

	*bp++ = *lp-- ;
	*bp++ = *lp-- ;
	*bp++ = *lp-- ;
	*bp = *lp ;

	return(bend) ;
}


/* ARGSUSED */
int mcidasIdent(char *fullname, char *name)
{ ZFILE          *zf;
  struct area_dir dir ;
  int             r;
  int doswap = 0 ;

  if (! (zf= zopen(fullname))) {
    perror("mcidasIdent");
    return(0);
  }
  switch (zread(zf, (byte *)&dir, sizeof(struct area_dir))) {

  case sizeof(struct area_dir):
    if (dir.type == 4) {
      r= 1;
      break;
    }
    else if (dir.type == 67108864) {
      r= 1;
      doswap = 1 ;
    } else {
      r= 0;
      break;
    }

  case -1:
    perror("mcidasIdent");
  default:
    r= 0;
    break;
  }
  zclose(zf);
  if(r && doswap) {
    unsigned long *begin ; 
    unsigned long *ulp ;
    begin = (unsigned long *)&dir ;
    for(ulp = begin ; ulp < &begin[AREA_COMMENTS] ; ulp++)
       *ulp = vhtonl(*ulp) ;
    for(ulp = &begin[AREA_CALKEY] ; ulp < &begin[AREA_STYPE] ; ulp++)
       *ulp = vhtonl(*ulp) ;
  }

  /* a simple sanity check */
  if (dir.idate > 99999 || dir.itime > 999999)
    r = 0;
	
  if (r) {
   (void)printf("%s %ld %ld (%ld, %ld) (%ld, %ld)\n",
		 mc_sensor(dir.satid),
		 dir.idate,
		 dir.itime,
		 dir.lcor,
		 dir.ecor,
		 dir.lres,
		 dir.eres) ;
  }
  return(r);
}

Image *mcidasLoad(char *fullname, ImageOptions *image_ops, boolean verbose)
{ ZFILE        *zf;
  char         *name = image_ops->name;
  struct area_dir  dir;
  struct navigation  nav;
  Image          *image;
  unsigned int    y;
  int doswap = 0 ;

  if (! (zf= zopen(fullname))) {
    perror("mcidasLoad");
    return(NULL);
  }
  switch (zread(zf, (byte *)&dir, sizeof(struct area_dir))) {

  case sizeof(struct area_dir):
    if (dir.type != 4) {
      if(dir.type != 67108864) {
        zclose(zf);
        return(NULL) ;
      } else {
	doswap = 1 ;
      }
    }
    break;

  case -1:
    perror("mcidasLoad");
  default:
    zclose(zf);
    return(NULL);
  }

  if(doswap) {
    unsigned long *begin ; 
    unsigned long *ulp ;
    begin = (unsigned long *)&dir ;
    for(ulp = begin ; ulp < &begin[AREA_COMMENTS] ; ulp++)
       *ulp = vhtonl(*ulp) ;
     for(ulp = &begin[AREA_CALKEY] ; ulp < &begin[AREA_STYPE] ; ulp++)
        *ulp = vhtonl(*ulp) ;
   }

  /* a simple sanity check */
  if (dir.idate > 99999 || dir.itime > 999999) {
    zclose(zf);
    return(NULL);
  }
	
  if (verbose) {
    printf("%s %ld %ld (%ld, %ld) (%ld, %ld)\n",
		 mc_sensor(dir.satid),
		 dir.idate,
		 dir.itime,
		 dir.lcor,
		 dir.ecor,
		 dir.lres,
		 dir.eres);
  }

  znocache(zf);
  /* skip the nav */
  if( zread(zf, (byte *)&nav, sizeof(struct navigation)) !=
     sizeof(struct navigation)) {
      zclose(zf);
      return(NULL) ;
  }

  /* get an image to put the data in
   */

   image= newRGBImage(dir.esiz,
		       dir.lsiz,
		       8 * dir.bands);
  image->title= dupString(name);

  /* set up the colormap, linear grey scale
   */

    for (y= 0; y < 255; y++) {
      *(image->rgb.red + y)= 
       *(image->rgb.green + y)=
        *(image->rgb.blue + y)= y * 257 ;
    }
    image->rgb.used= 255 ;

  if (zread(zf, image->data, dir.esiz * dir.lsiz) != dir.esiz * dir.lsiz) {
    fprintf(stderr,"mcidasLoad: %s - Short read within image data\n",name);
    zclose(zf);
    return(image);
  }

  zclose(zf);
  read_trail_opt(image_ops,zf,image,verbose);
  return(image);
}