9 #define MSG_FLAG 0x80000001
12 #define IN_TILE_DEGREES_LON 180.0
13 #define IN_TILE_DEGREES_LAT 180.0
14 #define IN_TILE_PTS_X 1250
15 #define IN_TILE_PTS_Y 1250
16 #define OUT_TILE_DEGREES_LON 180.0
17 #define OUT_TILE_DEGREES_LAT 180.0
18 #define OUT_TILE_PTS_X 1250
19 #define OUT_TILE_PTS_Y 1250
25 int **** data_cache
= NULL
;
26 char ** fname_cache
= NULL
;
29 int *** supertile
= NULL
;
30 int supertile_min_x
= 999999;
31 int supertile_min_y
= 999999;
33 void free_newtile(int *** x
)
37 for(i
=0; i
<IN_TILE_PTS_X
; i
++)
39 for(j
=0; j
<IN_TILE_PTS_Y
; j
++)
48 int *** read_tile(char * fname
)
52 unsigned char buf
[IN_TILE_PTS_X
*IN_TILE_PTS_Y
*ZDIM
*N_BYTES
];
55 retval
= (int ***)malloc(sizeof(int **)*IN_TILE_PTS_X
);
56 for(i
=0; i
<IN_TILE_PTS_X
; i
++)
58 retval
[i
] = (int **)malloc(sizeof(int *)*IN_TILE_PTS_Y
);
59 for(j
=0; j
<IN_TILE_PTS_Y
; j
++)
61 retval
[i
][j
] = (int *)malloc(sizeof(int)*ZDIM
);
65 if ((fd
= open(fname
,O_RDONLY
)) == -1)
67 fprintf(stderr
,"Error opening source file %s\n", fname
);
71 read(fd
, (void *)&buf
, IN_TILE_PTS_X
*IN_TILE_PTS_Y
*ZDIM
*N_BYTES
);
75 /* Put buf into retval */
76 for(i
=0; i
<IN_TILE_PTS_X
; i
++)
78 for(j
=0; j
<IN_TILE_PTS_Y
; j
++)
83 for(b
=0; b
<N_BYTES
; b
++)
85 retval
[i
][j
][k
] |= buf
[k
*N_BYTES
*IN_TILE_PTS_X
*IN_TILE_PTS_Y
+j
*N_BYTES
*IN_TILE_PTS_X
+i
*N_BYTES
+b
] << 8*(N_BYTES
-b
-1);
94 int *** get_tile_from_cache(int i
, int j
)
96 int ii
, jj
, kk
, k
, least
, least_idx
;
97 int *** retval
, *** localptr
;
100 fname
= (char *)malloc(256);
102 i
= (i
/IN_TILE_PTS_X
)*IN_TILE_PTS_X
+1;
103 j
= (j
/IN_TILE_PTS_Y
)*IN_TILE_PTS_Y
+1;
105 snprintf(fname
,256,"%5.5i-%5.5i.%5.5i-%5.5i",i
,i
+IN_TILE_PTS_X
-1,j
,j
+IN_TILE_PTS_Y
-1);
107 /* Find out whether tile containing (i,j) is in cache */
108 if (data_cache
!= NULL
)
110 for(k
=0; k
<CACHESIZE
; k
++)
112 if (fname_cache
[k
] != NULL
)
114 if (strncmp(fname_cache
[k
],fname
,256) == 0)
117 retval
= data_cache
[k
];
124 /* If not, read from file */
125 localptr
= read_tile(fname
);
127 /* Also store tile in the cache */
128 if (data_cache
== NULL
)
130 data_cache
= (int ****)malloc(sizeof(int ***)*CACHESIZE
);
131 fname_cache
= (char **)malloc(sizeof(char *)*CACHESIZE
);
132 lru
= (int *)malloc(sizeof(int)*CACHESIZE
);
133 for(k
=0; k
<CACHESIZE
; k
++)
135 data_cache
[k
] = NULL
;
136 fname_cache
[k
] = NULL
;
143 for(k
=0; k
<CACHESIZE
; k
++)
153 if (data_cache
[least_idx
] == NULL
)
155 data_cache
[least_idx
] = localptr
;
156 fname_cache
[least_idx
] = fname
;
161 free_newtile(data_cache
[least_idx
]);
162 data_cache
[least_idx
] = localptr
;
163 free(fname_cache
[least_idx
]);
164 fname_cache
[least_idx
] = fname
;
172 void build_supertile(int i
, int j
)
179 i
= i
+ IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
);
180 if (i
>= IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
))
181 i
= i
- IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
);
183 j
= j
+ IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
);
184 if (j
>= IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
))
185 j
= j
- IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
);
187 if (supertile
== NULL
)
189 supertile
= (int ***)malloc(sizeof(int **)*3*IN_TILE_PTS_X
);
190 for(ii
=0; ii
<3*IN_TILE_PTS_X
; ii
++)
192 supertile
[ii
] = (int **)malloc(sizeof(int *)*3*IN_TILE_PTS_Y
);
193 for(jj
=0; jj
<3*IN_TILE_PTS_Y
; jj
++)
195 supertile
[ii
][jj
] = (int *)malloc(sizeof(int)*ZDIM
);
200 supertile_min_x
= (i
/ IN_TILE_PTS_X
)*IN_TILE_PTS_X
;
201 supertile_min_y
= (j
/ IN_TILE_PTS_Y
)*IN_TILE_PTS_Y
;
203 /* Get tile containing (i,j) from cache*/
204 /* Get surrounding tiles from cache */
207 ii
= i
- IN_TILE_PTS_X
;
208 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
210 jj
= j
- IN_TILE_PTS_Y
;
215 ii
-= (int)(180./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
216 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
218 newtile
= get_tile_from_cache(ii
, jj
);
221 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
223 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
225 for(kk
=0; kk
<ZDIM
; kk
++)
226 supertile
[ii
][jj
][kk
] = newtile
[ii
][IN_TILE_PTS_Y
-1-jj
][kk
];
232 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
234 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
236 for(kk
=0; kk
<ZDIM
; kk
++)
237 supertile
[ii
][jj
][kk
] = newtile
[ii
][jj
][kk
];
243 ii
= i
- IN_TILE_PTS_X
;
244 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
246 newtile
= get_tile_from_cache(ii
, jj
);
247 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
249 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
251 for(kk
=0; kk
<ZDIM
; kk
++)
252 supertile
[ii
][jj
+IN_TILE_PTS_Y
][kk
] = newtile
[ii
][jj
][kk
];
257 ii
= i
- IN_TILE_PTS_X
;
258 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
260 jj
= j
+ IN_TILE_PTS_Y
;
261 if (jj
>= (int)(180./IN_TILE_DEGREES_LAT
)*IN_TILE_PTS_Y
)
265 ii
-= (int)(180./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
266 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
268 newtile
= get_tile_from_cache(ii
, jj
);
271 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
273 for(jj
=0; jj
<IN_TILE_PTS_X
; jj
++)
275 for(kk
=0; kk
<ZDIM
; kk
++)
276 supertile
[ii
][jj
+2*IN_TILE_PTS_Y
][kk
] = newtile
[ii
][IN_TILE_PTS_Y
-1-jj
][kk
];
282 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
284 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
286 for(kk
=0; kk
<ZDIM
; kk
++)
287 supertile
[ii
][jj
+2*IN_TILE_PTS_Y
][kk
] = newtile
[ii
][jj
][kk
];
295 jj
= j
- IN_TILE_PTS_Y
;
300 ii
-= (int)(180./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
301 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
303 newtile
= get_tile_from_cache(ii
, jj
);
306 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
308 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
310 for(kk
=0; kk
<ZDIM
; kk
++)
311 supertile
[ii
+IN_TILE_PTS_X
][jj
][kk
] = newtile
[ii
][IN_TILE_PTS_Y
-1-jj
][kk
];
317 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
319 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
321 for(kk
=0; kk
<ZDIM
; kk
++)
322 supertile
[ii
+IN_TILE_PTS_X
][jj
][kk
] = newtile
[ii
][jj
][kk
];
328 newtile
= get_tile_from_cache(i
, j
);
329 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
331 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
333 for(kk
=0; kk
<ZDIM
; kk
++)
334 supertile
[ii
+IN_TILE_PTS_X
][jj
+IN_TILE_PTS_Y
][kk
] = newtile
[ii
][jj
][kk
];
341 jj
= j
+ IN_TILE_PTS_Y
;
342 if (jj
>= (int)(180./IN_TILE_DEGREES_LAT
)*IN_TILE_PTS_Y
)
346 ii
-= (int)(180./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
347 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
349 newtile
= get_tile_from_cache(ii
, jj
);
352 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
354 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
356 for(kk
=0; kk
<ZDIM
; kk
++)
357 supertile
[ii
+IN_TILE_PTS_X
][jj
+2*IN_TILE_PTS_Y
][kk
] = newtile
[ii
][IN_TILE_PTS_Y
-1-jj
][kk
];
363 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
365 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
367 for(kk
=0; kk
<ZDIM
; kk
++)
368 supertile
[ii
+IN_TILE_PTS_X
][jj
+2*IN_TILE_PTS_Y
][kk
] = newtile
[ii
][jj
][kk
];
374 ii
= i
+ IN_TILE_PTS_X
;
375 if (ii
>= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
) ii
-= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
377 jj
= j
- IN_TILE_PTS_Y
;
382 ii
-= (int)(180./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
383 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
385 newtile
= get_tile_from_cache(ii
, jj
);
388 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
390 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
392 for(kk
=0; kk
<ZDIM
; kk
++)
393 supertile
[ii
+2*IN_TILE_PTS_X
][jj
][kk
] = newtile
[ii
][IN_TILE_PTS_Y
-1-jj
][kk
];
399 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
401 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
403 for(kk
=0; kk
<ZDIM
; kk
++)
404 supertile
[ii
+2*IN_TILE_PTS_X
][jj
][kk
] = newtile
[ii
][jj
][kk
];
410 ii
= i
+ IN_TILE_PTS_X
;
411 if (ii
>= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
) ii
-= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
413 newtile
= get_tile_from_cache(ii
, jj
);
414 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
416 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
418 for(kk
=0; kk
<ZDIM
; kk
++)
419 supertile
[ii
+2*IN_TILE_PTS_X
][jj
+IN_TILE_PTS_Y
][kk
] = newtile
[ii
][jj
][kk
];
424 ii
= i
+ IN_TILE_PTS_X
;
425 if (ii
>= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
) ii
-= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
427 jj
= j
+ IN_TILE_PTS_Y
;
428 if (jj
>= (int)(180./IN_TILE_DEGREES_LAT
)*IN_TILE_PTS_Y
)
432 ii
-= (int)(180./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
433 if (ii
< 0) ii
+= (int)(360./IN_TILE_DEGREES_LON
)*IN_TILE_PTS_X
;
435 newtile
= get_tile_from_cache(ii
, jj
);
438 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
440 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
442 for(kk
=0; kk
<ZDIM
; kk
++)
443 supertile
[ii
+2*IN_TILE_PTS_X
][jj
+2*IN_TILE_PTS_Y
][kk
] = newtile
[ii
][IN_TILE_PTS_Y
-1-jj
][kk
];
449 for(ii
=0; ii
<IN_TILE_PTS_X
; ii
++)
451 for(jj
=0; jj
<IN_TILE_PTS_Y
; jj
++)
453 for(kk
=0; kk
<ZDIM
; kk
++)
454 supertile
[ii
+2*IN_TILE_PTS_X
][jj
+2*IN_TILE_PTS_Y
][kk
] = newtile
[ii
][jj
][kk
];
460 int get_value(int i
, int j
, int k
, int irad
)
468 i
= i
+ IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
);
469 if (i
>= IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
))
470 i
= i
- IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
);
472 j
= j
+ IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
);
473 if (j
>= IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
))
474 j
= j
- IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
);
476 i_src
= i
% IN_TILE_PTS_X
+ IN_TILE_PTS_X
;
477 j_src
= j
% IN_TILE_PTS_Y
+ IN_TILE_PTS_Y
;
479 /* Interpolate values from supertile */
482 for(ii
=i_src
; ii
<=i_src
+irad
; ii
++)
484 for(jj
=j_src
; jj
<=j_src
+irad
; jj
++)
486 sum
+= supertile
[ii
][jj
][k
];
491 if (n
> 0) return sum
/n
;
495 int is_in_supertile(int i
, int j
)
498 i
= i
+ IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
);
499 if (i
>= IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
))
500 i
= i
- IN_TILE_PTS_X
*(int)(360./IN_TILE_DEGREES_LON
);
502 j
= j
+ IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
);
503 if (j
>= IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
))
504 j
= j
- IN_TILE_PTS_Y
*(int)(180./IN_TILE_DEGREES_LAT
);
506 /* Check whether (i,j) is in the interior of supertile */
507 if ((i
>= supertile_min_x
) && (i
< supertile_min_x
+IN_TILE_PTS_X
) &&
508 (j
>= supertile_min_y
) && (j
< supertile_min_y
+IN_TILE_PTS_Y
))
514 int main(int argc
, char ** argv
)
516 int tile_x
, tile_y
, input_x
, input_y
, temp_x
, temp_y
;
517 int i
, j
, k
, z
, ii
, jj
;
523 unsigned char * outdata
;
524 char out_filename
[256];
526 r_rel
= (float)IN_TILE_PTS_X
/(float)OUT_TILE_PTS_X
*OUT_TILE_DEGREES_LON
/IN_TILE_DEGREES_LON
;
527 ir_rel
= (int)rint(r_rel
);
529 /* Allocate memory to hold a single output tile */
530 intdata
= (int ***)malloc(sizeof(int **)*(OUT_TILE_PTS_X
+2*HALO_WIDTH
));
531 for(i
=0; i
<(OUT_TILE_PTS_X
+2*HALO_WIDTH
); i
++)
533 intdata
[i
] = (int **)malloc(sizeof(int *)*(OUT_TILE_PTS_Y
+2*HALO_WIDTH
));
534 for(j
=0; j
<(OUT_TILE_PTS_Y
+2*HALO_WIDTH
); j
++)
536 intdata
[i
][j
] = (int *)malloc(sizeof(int)*ZDIM
);
540 /* Allocate output buffer */
541 outdata
= (unsigned char *)malloc((OUT_TILE_PTS_X
+2*HALO_WIDTH
)*(OUT_TILE_PTS_Y
+2*HALO_WIDTH
)*ZDIM
*N_BYTES
);
543 for(tile_x
=0; tile_x
<OUT_TILE_PTS_X
*(int)(360.0/OUT_TILE_DEGREES_LON
); tile_x
+=OUT_TILE_PTS_X
)
545 for(tile_y
=0; tile_y
<OUT_TILE_PTS_Y
*(int)(180.0/OUT_TILE_DEGREES_LAT
); tile_y
+=OUT_TILE_PTS_Y
)
547 /* Build name of output file for current tile_x and tile_y */
548 sprintf(out_filename
,"retiled/%5.5i-%5.5i.%5.5i-%5.5i",tile_x
+1,tile_x
+OUT_TILE_PTS_X
,tile_y
+1,tile_y
+OUT_TILE_PTS_Y
);
550 /* Initialize the output data for current tile */
551 for(i
=0; i
<(OUT_TILE_PTS_X
+2*HALO_WIDTH
); i
++)
553 for(j
=0; j
<(OUT_TILE_PTS_Y
+2*HALO_WIDTH
); j
++)
555 intdata
[i
][j
][0] = MSG_FLAG
;
559 /* Attempt to open output file */
560 if ((out_fd
= open(out_filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) == -1)
562 fprintf(stderr
, "Error: Could not create or open file %s\n", out_filename
);
566 /* Fill tile with data */
567 for(i
=-1*HALO_WIDTH
; i
<OUT_TILE_PTS_X
+HALO_WIDTH
; i
++)
569 for(j
=-1*HALO_WIDTH
; j
<OUT_TILE_PTS_Y
+HALO_WIDTH
; j
++)
571 if (intdata
[i
+HALO_WIDTH
][j
+HALO_WIDTH
][0] == MSG_FLAG
)
573 i_src
= ir_rel
*(tile_x
+i
);
574 j_src
= ir_rel
*(tile_y
+j
);
576 build_supertile(i_src
,j_src
);
578 for(ii
=-1*HALO_WIDTH
; ii
<OUT_TILE_PTS_X
+HALO_WIDTH
; ii
++)
580 for(jj
=-1*HALO_WIDTH
; jj
<OUT_TILE_PTS_Y
+HALO_WIDTH
; jj
++)
582 i_src
= ir_rel
*(tile_x
+ii
);
583 j_src
= ir_rel
*(tile_y
+jj
);
584 if (is_in_supertile(i_src
,j_src
))
586 for(k
=0; k
<ZDIM
; k
++)
587 intdata
[ii
+HALO_WIDTH
][jj
+HALO_WIDTH
][k
] = get_value(i_src
,j_src
,k
,ir_rel
-1);
596 /* Write out the data */
597 for(i
=0; i
<(OUT_TILE_PTS_X
+2*HALO_WIDTH
); i
++)
599 for(j
=0; j
<(OUT_TILE_PTS_Y
+2*HALO_WIDTH
); j
++)
601 for(z
=0; z
<ZDIM
; z
++)
603 for(k
=0; k
<N_BYTES
; k
++)
605 outdata
[z
*N_BYTES
*(OUT_TILE_PTS_Y
+2*HALO_WIDTH
)*(OUT_TILE_PTS_X
+2*HALO_WIDTH
)+j
*N_BYTES
*(OUT_TILE_PTS_X
+2*HALO_WIDTH
)+i
*N_BYTES
+k
] =
606 (intdata
[i
][j
][z
] & (0xff << 8*(N_BYTES
-k
-1))) >> (8*(N_BYTES
-k
-1));
611 write(out_fd
,(void *)outdata
,(OUT_TILE_PTS_X
+2*HALO_WIDTH
)*(OUT_TILE_PTS_Y
+2*HALO_WIDTH
)*ZDIM
*N_BYTES
);
613 printf("Wrote file %s\n",out_filename
);
617 /* Deallocate memory */
618 for(i
=0; i
<(OUT_TILE_PTS_X
+2*HALO_WIDTH
); i
++)
620 for(j
=0; j
<(OUT_TILE_PTS_Y
+2*HALO_WIDTH
); j
++)