File: mcidas.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (225 lines) | stat: -rw-r--r-- 4,521 bytes parent folder | download | duplicates (4)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* mcidas.c:
 *
 * McIDAS areafile support.  contributed by Glenn P. Davis
 * (davis@unidata.ucar.edu).
 */

#include "xloadimage.h"
#include "mcidas.h"

char *mc_sensor();

static struct {
  int   days;
  char *name;
} month_info[13] = {
  { 0,  "Pad" },
  { 31, "Jan" },
  { 29, "Feb" }, /* longest */
  { 31, "Mar" },
  { 30, "Apr" },
  { 31, "May" },
  { 30, "Jun" },
  { 31, "Jul" },
  { 31, "Aug" },
  { 30, "Sep" },
  { 31, "Oct" },
  { 30, "Nov" },
  { 31, "Dec" }
};

/* convert numeric dates to human-readable
 */
static char *convert_date(time, date)
     int time, date;
{ static char buf[30];
  int hour;
  int minute;
  int second;
  int year;
  int month;
  int day;

  year = date / 1000;
  if (year > 50) /* gives us a range from 1950 - 2050, probably good enough */
    year += 1900;
  else
    year += 2000;
  day = date % 1000;

  /* adjust day for non-leap-years if necessary
   */
  if ((day > 31 + 28) && ((year % 4) || (year == 2000)))
    day++;

  /* find month in year that this day falls in
   */
  for (month= 1; day > month_info[month].days; day -= month_info[month].days)
    month++;

  /* break time-of-day up
   */
  hour = time / 10000;
  minute = (time % 10000) / 100;
  second = (time % 100);

  snprintf(buf, 29, "%d:%2.2d:%2.2d %s %d, %d (day %d)",
	  hour, minute, second, month_info[month].name, day, year,
	  (date % 1000));
  return(buf);
}

/*
 * convert from little endian to big endian four byte object
 */
static unsigned long
vhtonl(lend)
unsigned long 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) ;
}

static void babble(name, dir)
     char *name;
     struct area_dir *dir;
{
  printf("%s is a %ldx%ld McIDAS areafile from %s at %s (%ld, %ld) (%ld, %ld)\n",
	 name,
	 dir->esiz, dir->lsiz,
	 mc_sensor(dir->satid),
	 convert_date(dir->itime, dir->idate),
	 dir->lcor,
	 dir->ecor,
	 dir->lres,
	 dir->eres) ;
}

static void swap_bytes(dir)
     struct area_dir *dir;
{
  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) ;
}

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

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

  case sizeof(struct area_dir):
    if (memToValLSB((char *)&dir.type, 4) != 4) {
      r= 0;
      break;
    }
    if (dir.type != 4)
      swap_bytes(&dir);
    babble(name, &dir);
    r= 1;
    break;

  default:
    r= 0;
    break;
  }
  zclose(zf);
  return(r);
}


Image *mcidasLoad(fullname, name, verbose)
     char         *fullname, *name;
     unsigned int  verbose;
{ ZFILE          *zf;
  struct area_dir  dir;
  struct navigation  nav;
  Image          *image;
  unsigned int    y;

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

  case sizeof(struct area_dir):
    if (memToValLSB((char *)&dir.type, 4) != 4) {
      zclose(zf);
      return(NULL) ;
    }
    if (dir.type != 4)
      swap_bytes(&dir);
    break;

  default:
    zclose(zf);
    return(NULL);
  }

  if (verbose)
    babble(name, &dir);

  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, dir.zsiz * 8);

  /* set up the colormap, linear grey scale
   */
  for (y= 0; y < image->rgb.size; y++) {
      *(image->rgb.red + y)= 
	  *(image->rgb.green + y)=
	      *(image->rgb.blue + y)= y * (65536 / image->rgb.size) ;
  }
  image->rgb.used= image->rgb.size ;

  /* read the first band from the image and warn if there are other bands
   * we can't read.
   */
  zread(zf, image->data, dir.esiz * dir.lsiz * dir.zsiz) ;
  if (dir.bands > 1)
      printf("Warning: Only showing first of %ld bands\n", dir.bands);

  zclose(zf);
  image->title= dupString(name);
  return(image);
}