| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <math.h> |
|---|
| 4 | |
|---|
| 5 | /***************************************************************************** |
|---|
| 6 | * |
|---|
| 7 | * This function "unthins" thinned grib grids. |
|---|
| 8 | * Todd Hutchinson |
|---|
| 9 | * 9/24/99 |
|---|
| 10 | * tahutchinson@tasc.com |
|---|
| 11 | * |
|---|
| 12 | * Interface: |
|---|
| 13 | * Input: |
|---|
| 14 | * *in - 1-d array holding input grib data (an irregular thinned |
|---|
| 15 | * grid) |
|---|
| 16 | * *rowsizes - an array holding the sizes of the thinned rows |
|---|
| 17 | * ysize - the number of rows |
|---|
| 18 | * |
|---|
| 19 | * Ouput: |
|---|
| 20 | * *out - 1-d array holding output grid (a rectangular unthinned grid) |
|---|
| 21 | * *xsize - the number of columns in the output array |
|---|
| 22 | * |
|---|
| 23 | * Return value |
|---|
| 24 | * 1 for success, <0 for failure |
|---|
| 25 | ******************************************************************************/ |
|---|
| 26 | |
|---|
| 27 | int grib_unthin(float *in,float *out,int *rowsizes, int ysize, int *xsize) |
|---|
| 28 | { |
|---|
| 29 | int in_index = 0; |
|---|
| 30 | int out_index = 0; |
|---|
| 31 | int inrow_index; |
|---|
| 32 | float a, b; |
|---|
| 33 | int i, j; |
|---|
| 34 | float weight; |
|---|
| 35 | |
|---|
| 36 | /* Find maximum value */ |
|---|
| 37 | *xsize = 0; |
|---|
| 38 | for (j = 0; j<ysize; j++) { |
|---|
| 39 | if (rowsizes[j] > *xsize) { |
|---|
| 40 | *xsize = rowsizes[j]; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | for (j=0; j<ysize; j++) { |
|---|
| 45 | inrow_index = 0; |
|---|
| 46 | for (i=0; i<*xsize; i++) { |
|---|
| 47 | if (rowsizes[j] == *xsize) { |
|---|
| 48 | out[out_index] = in[in_index]; |
|---|
| 49 | in_index++; |
|---|
| 50 | out_index++; |
|---|
| 51 | } else { |
|---|
| 52 | b = (((float)(*xsize)-1)/((float)rowsizes[j]-1))*(inrow_index+1); |
|---|
| 53 | if (i >= b) { |
|---|
| 54 | inrow_index++; |
|---|
| 55 | b = (((float)(*xsize)-1)/((float)rowsizes[j]-1))*(inrow_index+1); |
|---|
| 56 | in_index++; |
|---|
| 57 | } |
|---|
| 58 | a = (((float)(*xsize)-1)/((float)rowsizes[j]-1))*(inrow_index); |
|---|
| 59 | weight = (i - a)/(b-a); |
|---|
| 60 | if (weight == 0) out[out_index] = in[in_index]; |
|---|
| 61 | else |
|---|
| 62 | out[out_index] = in[in_index]+weight*(in[in_index+1]-in[in_index]); |
|---|
| 63 | out_index++; |
|---|
| 64 | /* Advance to next row */ |
|---|
| 65 | if (i == (*xsize - 1)) in_index++; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | return 1; |
|---|
| 71 | } |
|---|