SCALE-RM
read_toshiba.c
Go to the documentation of this file.
1 /*---------------------------------------------------------------
2  06 Mar. 2013 Shin Satoh
3 FUNCTION: int read_toshiba
4  to read PAWR data in Toshiba original format
5 ----------------------------------------------------------------*/
6 
7 /*
8  modified by Shigenori Otsuka
9 */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <limits.h>
15 #ifdef __APPLE__
16 #include <machine/endian.h>
17 #else
18 #include <endian.h>
19 #endif
20 #include "read_toshiba.h"
21 
22 int16_t char2int16(void *input)
23 {
24  int16_t output;
25 
26 #if __BYTE_ORDER == __LITTLE_ENDIAN
27  output = *((int16_t*)input);
28 #elif __BYTE_ORDER == __BIG_ENDIAN
29  output = ((int16_t)(((unsigned char*)input)[1]) << 8) | (int16_t)(((unsigned char*)input)[0]);
30 #else
31  output = ((char*)input)[1] * 0x100 + ((unsigned char*)input)[0];
32 #endif
33  return output;
34 }
35 
36 uint16_t char2uint16(void *input)
37 {
38  uint16_t output;
39 
40 #if __BYTE_ORDER == __LITTLE_ENDIAN
41  output = *((uint16_t*)input);
42 #elif __BYTE_ORDER == __BIG_ENDIAN
43  output = ((uint16_t)(((unsigned char*)input)[1]) << 8) | (uint16_t)(((unsigned char*)input)[0]);
44 #else
45  output = ((unsigned char*)input)[1] * 0x100 + ((unsigned char*)input)[0];
46 #endif
47  return output;
48 }
49 
50 int32_t char2int32(void *input)
51 {
52  int32_t output;
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
54  output = *((int32_t*)input);
55 #elif __BYTE_ORDER == __BIG_ENDIAN
56  output = ((int32_t)(((unsigned char*)input)[3]) << 24) | ((int32_t)(((unsigned char*)input)[2]) << 16) | ((int32_t)(((unsigned char*)input)[1]) << 8) | (int32_t)(((unsigned char*)input)[0]);
57 #else
58  output = ((char*)input)[3] * 0x1000000 + ((unsigned char*)input)[2] * 0x10000 + ((unsigned char*)input)[1] * 0x100 + ((unsigned char*)input)[0];
59 #endif
60  return output;
61 }
62 
63 int read_toshiba(char *fname, pawr_header *hd,
64  float az[ELDIM][AZDIM], float el[ELDIM][AZDIM],
65  float rtdat[ELDIM][AZDIM][RDIM])
66 {
67  const size_t bufsize = 40 * 1024 * 1024; // fixed size
68  size_t bsize; // actual data size
69  int ierr;
70  unsigned char *buf;
71  FILE *fp;
72 
73  buf = malloc(bufsize);
74  if(buf == NULL){
75  printf("failed to allocate memory in read_toshiba");
76  return -99;
77  }
78  if((fp = fopen(fname, "r")) == NULL){
79  //printf("file not found: %s\n", fname);
80  return -9;
81  }
82  bsize = fread(buf, 1, bufsize, fp);
83  if(bsize == 0){
84  printf("file size is 0: %s\n", fname);
85  return -9;
86  }
87 
88  ierr = decode_toshiba(bsize, buf, hd, az, el, rtdat);
89  if(ierr != 0) return ierr;
90 
91  free(buf);
92 
93  return 0;
94 }
95 
96 
97 int decode_toshiba(size_t bufsize, unsigned char *buf, pawr_header *hd,
98  float az[ELDIM][AZDIM], float el[ELDIM][AZDIM],
99  float rtdat[ELDIM][AZDIM][RDIM])
100 {
101  const size_t len_bufchd = 96;
102  const size_t len_bufpcd = 320;
103  const size_t len_bufblk = 64;
104  unsigned char *bufchd, *bufpcd, *bufblk, *bufdat;
105  float *flp;
106  float s_az, e_az, s_el, e_el;
107  float tx_pilot,return_pilot;
108  int i, j, k, iss, i0, i1, i2, i3, rnum, aznum, elnum;
109  int r_byte, irb, irb1, irb2;
110  int ios_obs, ios_spec, ios_loc, ios_rt, ios_mesh;
111  int ideg, ibunshi, ibunbo;
112  int prf, hit_num, long_pulse;
113  const float ideg2deg = (180.0 / 8192.0);
114  size_t buf_offset;
115 
116 /*----- open RT file -----*/
117  buf_offset = 0;
118 
119 /*----- SCAN data (PPI) loop -----*/
120  elnum = ELDIM;
121  /* elnum=86; */
122  for(k = 0; k < elnum; k++) {
123 
124 /*----- read common header (96 byte)-----*/
125  if(buf_offset == bufsize) {
126  //printf("# find EOF at elnum=%d\n", k);
127  goto READEND;
128  } else if(buf_offset + len_bufchd > bufsize) {
129  printf("# abnormal record length in common header\n");
130  goto READERR;
131  }
132  bufchd = buf + buf_offset;
133  buf_offset += len_bufchd;
134 
135  hd->s_yr = 1000 * (bufchd[20] >> 4) + 100 * (bufchd[20] & 15) + 10 * (bufchd[21] >> 4) + (bufchd[21] & 15);
136  hd->s_mn = 10 * (bufchd[22] >> 4) + (bufchd[22] & 15);
137  hd->s_dy = 10 * (bufchd[23] >> 4) + (bufchd[23] & 15);
138  hd->s_hr = 10 * (bufchd[24] >> 4) + (bufchd[24] & 15);
139  hd->s_mi = 10 * (bufchd[25] >> 4) + (bufchd[25] & 15);
140  hd->s_sc = 10 * (bufchd[26] >> 4) + (bufchd[26] & 15);
141  hd->e_yr = 1000 * (bufchd[28] >> 4) + 100 * (bufchd[28] & 15) + 10 * (bufchd[29] >> 4) + (bufchd[29] & 15);
142  hd->e_mn = 10 * (bufchd[30] >> 4) + (bufchd[30] & 15);
143  hd->e_dy = 10 * (bufchd[31] >> 4) + (bufchd[31] & 15);
144  hd->e_hr = 10 * (bufchd[32] >> 4) + (bufchd[32] & 15);
145  hd->e_mi = 10 * (bufchd[33] >> 4) + (bufchd[33] & 15);
146  hd->e_sc = 10 * (bufchd[34] >> 4) + (bufchd[34] & 15);
147  hd->data_size = 0x10000 * bufchd[10] + 0x100 * bufchd[9] + bufchd[8]; // ensure little endian
148 
149 /*----- read polar-coords-data header (320 byte)-----*/
150  if(buf_offset + len_bufpcd > bufsize) {
151  printf("# abnormal record length in polar-coords-data header\n");
152  goto READERR;
153  }
154  bufpcd = buf + buf_offset;
155  buf_offset += len_bufpcd;
156 
157  ios_obs = 56; /* offset of obs info */
158  ios_spec = 88; /* offset of spec info */
159  ios_loc = 216; /* offset of location info */
160  ios_rt = 232; /* offset of RT coords info */
161  ios_mesh = 264; /* offset of mesh data info */
162 
163  hd->total_step_num = bufpcd[ios_obs + 4];
164  hd->el_num = bufpcd[ios_obs + 10];
165  hd->total_el_num = bufpcd[ios_obs + 11];
166 
167  // SPEC part
168  hd->tx_freq = 0.0001 * char2int32(bufpcd + ios_spec + 0);
169  hd->tx_power = 0.0001 * char2int32(bufpcd + ios_spec + 8);
170  hd->pulse_len_l = 0.0001 * char2int32(bufpcd + ios_spec + 16);
171  hd->pulse_len_s = 0.0001 * char2int32(bufpcd + ios_spec + 20);
172  hd->ant_gain = 0.0001 * char2int32(bufpcd + ios_spec + 32);
173  hd->beam_wid_h = 0.0001 * char2int32(bufpcd + ios_spec + 40);
174  hd->beam_wid_v = 0.0001 * char2int32(bufpcd + ios_spec + 48);
175  hd->tx_loss = 0.0001 * char2int32(bufpcd + ios_spec + 56);
176  hd->rx_loss = 0.0001 * char2int32(bufpcd + ios_spec + 64);
177  hd->smin_h = 0.0001 * char2int32(bufpcd + ios_spec + 72);
178  hd->smin_l = 0.0001 * char2int32(bufpcd + ios_spec + 76);
179  hd->prf_l = char2int32(bufpcd + ios_spec + 88);
180  hd->prf_h = char2int32(bufpcd + ios_spec + 92);
181  hd->zr_b = 0.0001 * char2int32(bufpcd + ios_spec + 96);
182  hd->zr_beta = 0.0001 * char2int32(bufpcd + ios_spec + 100);
183 
184  // LOC part
185  hd->latitude = 0.001 * char2int32(bufpcd + ios_loc + 0);
186  hd->longitude = 0.001 * char2int32(bufpcd + ios_loc + 4);
187  hd->altitude = 0.1 * char2int32(bufpcd + ios_loc + 8);
188 
189  // RT part
190  hd->start_angle = char2int16(bufpcd + ios_rt + 0) * ideg2deg;
191  hd->end_angle = char2int16(bufpcd + ios_rt + 2) * ideg2deg;
192  hd->hit_num = char2uint16(bufpcd + ios_rt + 4);
193  hd->sector_num = char2uint16(bufpcd + ios_rt + 8);
194  hd->range_num = char2uint16(bufpcd + ios_rt + 16);
195  hd->range_res = char2uint16(bufpcd + ios_rt + 20);
196 
197  // MESH part
198  hd->mesh_size = bufpcd[ios_mesh + 0];
199  ibunshi = char2int16(bufpcd + ios_mesh + 4);
200  ibunbo = char2int16(bufpcd + ios_mesh + 8);
201  hd->mesh_lsb = (float)ibunshi / (float)ibunbo;
202  ibunshi = char2int16(bufpcd + ios_mesh + 12);
203  ibunbo = char2int16(bufpcd + ios_mesh + 16);
204  hd->mesh_offset = (float)ibunshi / (float)ibunbo;
205 
206  /*--- print header info ---*/
207  /*
208  if(k == 0){
209  printf("## date & time : %d/%d/%d, %d:%d:%d -",
210  hd->s_yr, hd->s_mn, hd->s_dy, hd->s_hr, hd->s_mi, hd->s_sc);
211  printf(" %d/%d/%d, %d:%d:%d\n",
212  hd->e_yr, hd->e_mn, hd->e_dy, hd->e_hr, hd->e_mi, hd->e_sc);
213  printf("## data_size=%d, total step num=%d, el num=%d/%d,",
214  hd->data_size, hd->total_step_num, hd->el_num, hd->total_el_num);
215  printf(" lat=%8.3f, lon=%8.3f, alt=%8.1f\n",
216  hd->latitude, hd->longitude, hd->altitude);
217  printf("## freq=%6.1f, power=%5.3f, pulse_len(L/S)=%4.1f/%3.1f,",
218  hd->tx_freq, hd->tx_power, hd->pulse_len_l, hd->pulse_len_s);
219  printf(" ant_gain=%5.2f, beam_wid(H/V)=%4.2f/%4.2f, loss(Tx/Rx)=%4.2f/%4.2f\n",
220  hd->ant_gain, hd->beam_wid_h, hd->beam_wid_v, hd->tx_loss, hd->rx_loss);
221  printf("## smin(H/L)=%6.1f/%6.1f, PRI(L/H)=%6.4f/%6.4f, ZR(B/beta)=%6.2f/%6.4f,",
222  hd->smin_h, hd->smin_l, hd->prf_l, hd->prf_h, hd->zr_b, hd->zr_beta);
223  printf(" start angle=%5.1f, end angle=%5.1f\n",
224  hd->start_angle, hd->end_angle);
225  printf("## hit num=%d, sector num=%d, range num=%d,",
226  hd->hit_num, hd->sector_num, hd->range_num);
227  printf(" mesh size=%d, LSB=%4.2f, offset=%4.2f\n",
228  hd->mesh_size, hd->mesh_lsb, hd->mesh_offset);
229  }
230  */
231 
232  /*----- read data block -----*/
233  aznum = hd->sector_num;
234  rnum = hd->range_num;
235 
236  for(j = 0; j < aznum; j++) {
237 
238  /*--- read block header (64 byte) ---*/
239  if(buf_offset + len_bufblk > bufsize) {
240  printf("# abnormal record length in data block\n");
241  goto READERR;
242  }
243  bufblk = buf + buf_offset;
244  buf_offset += len_bufblk;
245 
246  s_el = char2int16(bufblk + 4) * ideg2deg;
247  s_az = char2int16(bufblk + 6) * ideg2deg;
248  e_el = char2int16(bufblk + 8) * ideg2deg;
249  e_az = char2int16(bufblk + 10) * ideg2deg;
250  prf = char2int16(bufblk + 14);
251  long_pulse = bufblk[34];
252  hit_num = char2int16(bufblk + 36);
253  /*
254  tx_pilot = -327.68+0.01*(65536*bufpcd[46]+256*bufpcd[45]+bufpcd[44]);
255  return_pilot = -327.68+0.01*(65536*bufpcd[50]+256*bufpcd[49]+bufpcd[48]);
256  */
257 
258  /* ------------------ <check later: hit num etc.>
259  if(j<2){
260  printf(" K=%d J=%d: EL=%5.1f-%5.1f, AZ=%5.1f-%5.1f,",
261  k,j,s_el,e_el,s_az,e_az);
262  printf(" PRF=%d, hit=%d, long_pulse=%d\n",
263  prf,hit_num,long_pulse);
264  }
265  -------------------- */
266 
267  /*--- read data block ---*/
268  irb1 = (hd->data_size / hd->sector_num) - 64;
269  irb2 = hd->range_num * (hd->mesh_size / 8);
270  irb = irb2;
271  bufdat = buf + buf_offset;
272  buf_offset += irb;
273 
274  /*--- store data ---*/
275  el[k][j] = s_el;
276  az[k][j] = s_az;
277  if(hd->mesh_size == 4){
278  } else if(hd->mesh_size == 8){
279  for(i = 0; i < rnum; i++) {
280  rtdat[k][j][i] = hd->mesh_offset + hd->mesh_lsb * (*((unsigned char*)(bufdat + i)));
281  }
282  } else if(hd->mesh_size == 16){
283  for(i = 0; i < rnum; i++) {
284  rtdat[k][j][i] = hd->mesh_offset + hd->mesh_lsb * (char2uint16(bufdat + i * 2));
285  }
286  }
287 
288  } /* end of j-loop */
289  } /* end of k-loop */
290 
291  /*--- print el & az info ---
292  for(k=0; k<elnum; k++) {
293  printf("EL No=%3d: EL=%5.2f %5.2f %5.2f | AZ=%5.1f %5.1f %5.1f %5.1f %5.1f --- ",
294  k+1,el[k][0],el[k][10],el[k][aznum-1],az[k][0],az[k][1],az[k][2],az[k][3],az[k][4]);
295  printf("%5.1f %5.1f %5.1f %5.1f %5.1f\n",
296  az[k][aznum-5],az[k][aznum-4],az[k][aznum-3],az[k][aznum-2],az[k][aznum-1]);
297  }
298  --- */
299 
300  goto READEND;
301 
302  READERR:
303  return -8;
304 
305  READEND:
306  return 0;
307 }
pawr_header::smin_h
float smin_h
Definition: read_toshiba.h:25
pawr_header::pulse_len_s
float pulse_len_s
Definition: read_toshiba.h:23
pawr_header::range_res
int range_res
Definition: read_toshiba.h:20
pawr_header::e_yr
int e_yr
Definition: read_toshiba.h:17
pawr_header::beam_wid_v
float beam_wid_v
Definition: read_toshiba.h:24
pawr_header::s_sc
int s_sc
Definition: read_toshiba.h:16
pawr_header::mesh_size
int mesh_size
Definition: read_toshiba.h:20
pawr_header::smin_l
float smin_l
Definition: read_toshiba.h:25
pawr_header::s_mn
int s_mn
Definition: read_toshiba.h:16
RDIM
#define RDIM
Definition: read_toshiba.h:6
pawr_header::total_step_num
int total_step_num
Definition: read_toshiba.h:19
pawr_header::hit_num
int hit_num
Definition: read_toshiba.h:20
pawr_header::prf_h
float prf_h
Definition: read_toshiba.h:26
pawr_header::prf_l
float prf_l
Definition: read_toshiba.h:26
read_toshiba
int read_toshiba(char *fname, pawr_header *hd, float az[ELDIM][AZDIM], float el[ELDIM][AZDIM], float rtdat[ELDIM][AZDIM][RDIM])
Definition: read_toshiba.c:63
char2uint16
uint16_t char2uint16(void *input)
Definition: read_toshiba.c:36
char2int16
int16_t char2int16(void *input)
Definition: read_toshiba.c:22
pawr_header::e_dy
int e_dy
Definition: read_toshiba.h:17
pawr_header
Definition: read_toshiba.h:15
float
typedef float(real32_t)
pawr_header::e_mi
int e_mi
Definition: read_toshiba.h:17
pawr_header::rx_loss
float rx_loss
Definition: read_toshiba.h:25
char2int32
int32_t char2int32(void *input)
Definition: read_toshiba.c:50
mod_atmos_vars::j
real(rp), allocatable, target, public j
Definition: mod_atmos_vars.F90:141
pawr_header::beam_wid_h
float beam_wid_h
Definition: read_toshiba.h:24
pawr_header::mesh_lsb
float mesh_lsb
Definition: read_toshiba.h:22
scale_tracer::k
real(rp), public k
Definition: scale_tracer.F90:45
pawr_header::pulse_len_l
float pulse_len_l
Definition: read_toshiba.h:23
scale_file::i
logical, public i
Definition: scale_file.F90:196
pawr_header::sector_num
int sector_num
Definition: read_toshiba.h:20
pawr_header::tx_freq
float tx_freq
Definition: read_toshiba.h:23
pawr_header::range_num
int range_num
Definition: read_toshiba.h:20
pawr_header::longitude
double longitude
Definition: read_toshiba.h:21
pawr_header::e_sc
int e_sc
Definition: read_toshiba.h:17
pawr_header::start_angle
float start_angle
Definition: read_toshiba.h:22
AZDIM
#define AZDIM
Definition: read_toshiba.h:7
pawr_header::s_mi
int s_mi
Definition: read_toshiba.h:16
pawr_header::zr_beta
float zr_beta
Definition: read_toshiba.h:26
pawr_header::zr_b
float zr_b
Definition: read_toshiba.h:26
pawr_header::el_num
int el_num
Definition: read_toshiba.h:19
pawr_header::altitude
double altitude
Definition: read_toshiba.h:21
pawr_header::e_hr
int e_hr
Definition: read_toshiba.h:17
pawr_header::end_angle
float end_angle
Definition: read_toshiba.h:22
pawr_header::tx_power
float tx_power
Definition: read_toshiba.h:23
decode_toshiba
int decode_toshiba(size_t bufsize, unsigned char *buf, pawr_header *hd, float az[ELDIM][AZDIM], float el[ELDIM][AZDIM], float rtdat[ELDIM][AZDIM][RDIM])
Definition: read_toshiba.c:97
pawr_header::mesh_offset
float mesh_offset
Definition: read_toshiba.h:22
pawr_header::e_mn
int e_mn
Definition: read_toshiba.h:17
pawr_header::total_el_num
int total_el_num
Definition: read_toshiba.h:19
pawr_header::ant_gain
float ant_gain
Definition: read_toshiba.h:24
read_toshiba.h
pawr_header::data_size
int data_size
Definition: read_toshiba.h:18
pawr_header::s_hr
int s_hr
Definition: read_toshiba.h:16
pawr_header::tx_loss
float tx_loss
Definition: read_toshiba.h:25
ELDIM
#define ELDIM
Definition: read_toshiba.h:8
pawr_header::latitude
double latitude
Definition: read_toshiba.h:21
pawr_header::s_dy
int s_dy
Definition: read_toshiba.h:16
pawr_header::s_yr
int s_yr
Definition: read_toshiba.h:16