00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define UNDEFINED 9.999e20 // STD Ncep Grib undefined value
00019
00020 #define UINT2(a,b) ((int) ((a << 8) + (b)))
00021 #define UINT4(a,b,c,d) ((int) ((a << 24) + (b << 16) + (c << 8) + (d)))
00022 #define UINT3(a,b,c) ((int) ((a << 16) + (b << 8) + (c)))
00023 #define INT2(a,b) ((1-(int) ((unsigned) (a & 0x80) >> 6)) * (int) (((a & 0x7f) << 8) + b))
00024 #define INT3(a,b,c) ((1-(int) ((unsigned) (a & 0x80) >> 6)) * (int) (((a & 127) << 16)+(b<<8)+c))
00025
00026 #define PDS_Len1(pds) (pds[0])
00027 #define PDS_Len2(pds) (pds[1])
00028 #define PDS_Len3(pds) (pds[2])
00029 #define PDS_LEN(pds) ((int) ((pds[0]<<16)+(pds[1]<<8)+pds[2]))
00030 #define PDS_Vsn(pds) (pds[3])
00031 #define PDS_Center(pds) (pds[4])
00032 #define PDS_Model(pds) (pds[5])
00033 #define PDS_Grid(pds) (pds[6])
00034 #define PDS_HAS_GDS(pds) ((pds[7] & 128) != 0)
00035 #define PDS_HAS_BMS(pds) ((pds[7] & 64) != 0)
00036 #define PDS_PARAM(pds) (pds[8])
00037 #define PDS_L_TYPE(pds) (pds[9])
00038 #define PDS_LEVEL1(pds) (pds[10])
00039 #define PDS_LEVEL2(pds) (pds[11])
00040 #define PDS_KPDS5(pds) (pds[8])
00041 #define PDS_KPDS6(pds) (pds[9])
00042 #define PDS_KPDS7(pds) ((int) ((pds[10]<<8) + pds[11]))
00043 #define PDS_Field(pds) ((pds[8]<<24)+(pds[9]<<16) + (pds[10]<<8)+pds[11])
00044 #define PDS_Year(pds) (pds[12])
00045 #define PDS_Month(pds) (pds[13])
00046 #define PDS_Day(pds) (pds[14])
00047 #define PDS_Hour(pds) (pds[15])
00048 #define PDS_Minute(pds) (pds[16])
00049 #define PDS_ForecastTimeUnit(pds) (pds[17])
00050 #define PDS_P1(pds) (pds[18])
00051 #define PDS_P2(pds) (pds[19])
00052 #define PDS_P1P2(pds) UINT2((pds[18]),(pds[19]))
00053 #define PDS_TimeRange(pds) (pds[20])
00054 #define PDS_NumAve(pds) ((int) ((pds[21]<<8)+pds[22]))
00055 #define PDS_NumMissing(pds) (pds[23])
00056 #define PDS_Century(pds) (pds[24])
00057 #define PDS_Subcenter(pds) (pds[25])
00058 #define PDS_DecimalScale(pds) INT2(pds[26],pds[27])
00059 #define PDS_Year4(pds) (pds[12] + 100*(pds[24] - 1))
00060
00061
00062
00063 #define PDS_EcLocalId(pds) (PDS_LEN(pds) >= 41 ? (pds[40]) : 0)
00064 #define PDS_EcClass(pds) (PDS_LEN(pds) >= 42 ? (pds[41]) : 0)
00065 #define PDS_EcType(pds) (PDS_LEN(pds) >= 43 ? (pds[42]) : 0)
00066 #define PDS_EcStream(pds) (PDS_LEN(pds) >= 45 ? (INT2(pds[43], pds[44])) : 0)
00067 #define PDS_EcENS(pds) (PDS_LEN(pds) >= 52 && pds[40] == 1 && pds[43] * 256 + pds[44] == 1035 && pds[50] != 0)
00068 #define PDS_EcFcstNo(pds) (pds[50])
00069 #define PDS_EcNoFcst(pds) (pds[51])
00070
00071
00072
00073 #define PDS_NcepENS(pds) (PDS_LEN(pds) >= 44 && pds[25] == 2 && pds[40] == 1)
00074 #define PDS_NcepFcstType(pds) (pds[41])
00075 #define PDS_NcepFcstNo(pds) (pds[42])
00076 #define PDS_NcepFcstProd(pds) (pds[43])
00077
00078 #define GDS_Len1(gds) (gds[0])
00079 #define GDS_Len2(gds) (gds[1])
00080 #define GDS_Len3(gds) (gds[2])
00081 #define GDS_LEN(gds) ((int) ((gds[0]<<16)+(gds[1]<<8)+gds[2]))
00082 #define GDS_NV(gds) (gds[3])
00083 #define GDS_DataType(gds) (gds[5])
00084 #define GDS_LatLon(gds) (gds[5] == 0)
00085 #define GDS_Mercator(gds) (gds[5] == 1)
00086 #define GDS_Gnomonic(gds) (gds[5] == 2)
00087 #define GDS_Lambert(gds) (gds[5] == 3)
00088 #define GDS_Gaussian(gds) (gds[5] == 4)
00089 #define GDS_Polar(gds) (gds[5] == 5)
00090 #define GDS_RotLL(gds) (gds[5] == 10)
00091 #define GDS_Harmonic(gds) (gds[5] == 50)
00092 #define GDS_Triangular(gds) (gds[5] == 192)
00093 #define GDS_ssEgrid(gds) (gds[5] == 201) // semi-staggered E grid
00094 #define GDS_fEgrid(gds) (gds[5] == 202) // filled E grid
00095 #define GDS_ss2dEgrid(gds) (gds[5] == 203) // semi-staggered E grid 2d
00096 #define GDS_has_dy(mode) ((mode) & 128)
00097 #define GDS_LatLon_nx(gds) ((int) ((gds[6] << 8) + gds[7]))
00098 #define GDS_LatLon_ny(gds) ((int) ((gds[8] << 8) + gds[9]))
00099 #define GDS_LatLon_La1(gds) INT3(gds[10],gds[11],gds[12])
00100 #define GDS_LatLon_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00101 #define GDS_LatLon_mode(gds) (gds[16])
00102 #define GDS_LatLon_La2(gds) INT3(gds[17],gds[18],gds[19])
00103 #define GDS_LatLon_Lo2(gds) INT3(gds[20],gds[21],gds[22])
00104 #define GDS_LatLon_dx(gds) (gds[16] & 128 ? INT2(gds[23],gds[24]) : 0)
00105 #define GDS_LatLon_dy(gds) (gds[16] & 128 ? INT2(gds[25],gds[26]) : 0)
00106 #define GDS_Gaussian_nlat(gds) ((gds[25]<<8)+gds[26])
00107 #define GDS_LatLon_scan(gds) (gds[27])
00108 #define GDS_Polar_nx(gds) ((gds[6] << 8) + gds[7])
00109 #define GDS_Polar_ny(gds) ((gds[8] << 8) + gds[9])
00110 #define GDS_Polar_La1(gds) INT3(gds[10],gds[11],gds[12])
00111 #define GDS_Polar_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00112 #define GDS_Polar_mode(gds) (gds[16])
00113 #define GDS_Polar_Lov(gds) INT3(gds[17],gds[18],gds[19])
00114 #define GDS_Polar_scan(gds) (gds[27])
00115 #define GDS_Polar_Dx(gds) INT3(gds[20], gds[21], gds[22])
00116 #define GDS_Polar_Dy(gds) INT3(gds[23], gds[24], gds[25])
00117 #define GDS_Polar_pole(gds) ((gds[26] & 128) == 128)
00118 #define GDS_Lambert_nx(gds) ((gds[6] << 8) + gds[7])
00119 #define GDS_Lambert_ny(gds) ((gds[8] << 8) + gds[9])
00120 #define GDS_Lambert_La1(gds) INT3(gds[10],gds[11],gds[12])
00121 #define GDS_Lambert_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00122 #define GDS_Lambert_mode(gds) (gds[16])
00123 #define GDS_Lambert_Lov(gds) INT3(gds[17],gds[18],gds[19])
00124 #define GDS_Lambert_dx(gds) INT3(gds[20],gds[21],gds[22])
00125 #define GDS_Lambert_dy(gds) INT3(gds[23],gds[24],gds[25])
00126 #define GDS_Lambert_NP(gds) ((gds[26] & 128) == 0)
00127 #define GDS_Lambert_scan(gds) (gds[27])
00128 #define GDS_Lambert_Latin1(gds) INT3(gds[28],gds[29],gds[30])
00129 #define GDS_Lambert_Latin2(gds) INT3(gds[31],gds[32],gds[33])
00130 #define GDS_Lambert_LatSP(gds) INT3(gds[34],gds[35],gds[36])
00131 #define GDS_Lambert_LonSP(gds) INT3(gds[37],gds[37],gds[37])
00132 #define GDS_ssEgrid_n(gds) UINT2(gds[6],gds[7])
00133 #define GDS_ssEgrid_n_dum(gds) UINT2(gds[8],gds[9])
00134 #define GDS_ssEgrid_La1(gds) INT3(gds[10],gds[11],gds[12])
00135 #define GDS_ssEgrid_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00136 #define GDS_ssEgrid_mode(gds) (gds[16])
00137 #define GDS_ssEgrid_La2(gds) UINT3(gds[17],gds[18],gds[19])
00138 #define GDS_ssEgrid_Lo2(gds) UINT3(gds[20],gds[21],gds[22])
00139 #define GDS_ssEgrid_di(gds) (gds[16] & 128 ? INT2(gds[23],gds[24]) : 0)
00140 #define GDS_ssEgrid_dj(gds) (gds[16] & 128 ? INT2(gds[25],gds[26]) : 0)
00141 #define GDS_ssEgrid_scan(gds) (gds[27])
00142 #define GDS_fEgrid_n(gds) UINT2(gds[6],gds[7])
00143 #define GDS_fEgrid_n_dum(gds) UINT2(gds[8],gds[9])
00144 #define GDS_fEgrid_La1(gds) INT3(gds[10],gds[11],gds[12])
00145 #define GDS_fEgrid_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00146 #define GDS_fEgrid_mode(gds) (gds[16])
00147 #define GDS_fEgrid_La2(gds) UINT3(gds[17],gds[18],gds[19])
00148 #define GDS_fEgrid_Lo2(gds) UINT3(gds[20],gds[21],gds[22])
00149 #define GDS_fEgrid_di(gds) (gds[16] & 128 ? INT2(gds[23],gds[24]) : 0)
00150 #define GDS_fEgrid_dj(gds) (gds[16] & 128 ? INT2(gds[25],gds[26]) : 0)
00151 #define GDS_fEgrid_scan(gds) (gds[27])
00152 #define GDS_ss2dEgrid_nx(gds) UINT2(gds[6],gds[7])
00153 #define GDS_ss2dEgrid_ny(gds) UINT2(gds[8],gds[9])
00154 #define GDS_ss2dEgrid_La1(gds) INT3(gds[10],gds[11],gds[12])
00155 #define GDS_ss2dEgrid_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00156 #define GDS_ss2dEgrid_mode(gds) (gds[16])
00157 #define GDS_ss2dEgrid_La2(gds) INT3(gds[17],gds[18],gds[19])
00158 #define GDS_ss2dEgrid_Lo2(gds) INT3(gds[20],gds[21],gds[22])
00159 #define GDS_ss2dEgrid_di(gds) (gds[16] & 128 ? INT2(gds[23],gds[24]) : 0)
00160 #define GDS_ss2dEgrid_dj(gds) (gds[16] & 128 ? INT2(gds[25],gds[26]) : 0)
00161 #define GDS_ss2dEgrid_scan(gds) (gds[27])
00162 #define GDS_Merc_nx(gds) UINT2(gds[6],gds[7])
00163 #define GDS_Merc_ny(gds) UINT2(gds[8],gds[9])
00164 #define GDS_Merc_La1(gds) INT3(gds[10],gds[11],gds[12])
00165 #define GDS_Merc_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00166 #define GDS_Merc_mode(gds) (gds[16])
00167 #define GDS_Merc_La2(gds) INT3(gds[17],gds[18],gds[19])
00168 #define GDS_Merc_Lo2(gds) INT3(gds[20],gds[21],gds[22])
00169 #define GDS_Merc_Latin(gds) INT3(gds[23],gds[24],gds[25])
00170 #define GDS_Merc_scan(gds) (gds[27])
00171 #define GDS_Merc_dx(gds) (gds[16]&128 ? INT3(gds[28],gds[29],gds[30]):0)
00172 #define GDS_Merc_dy(gds) (gds[16]&128 ? INT3(gds[31],gds[32],gds[33]):0)
00173 #define GDS_RotLL_nx(gds) UINT2(gds[6],gds[7])
00174 #define GDS_RotLL_ny(gds) UINT2(gds[8],gds[9])
00175 #define GDS_RotLL_La1(gds) INT3(gds[10],gds[11],gds[12])
00176 #define GDS_RotLL_Lo1(gds) INT3(gds[13],gds[14],gds[15])
00177 #define GDS_RotLL_mode(gds) (gds[16])
00178 #define GDS_RotLL_La2(gds) INT3(gds[17],gds[18],gds[19])
00179 #define GDS_RotLL_Lo2(gds) INT3(gds[20],gds[21],gds[22])
00180 #define GDS_RotLL_dx(gds) (gds[16] & 128 ? INT2(gds[23],gds[24]) : 0)
00181 #define GDS_RotLL_dy(gds) (gds[16] & 128 ? INT2(gds[25],gds[26]) : 0)
00182 #define GDS_RotLL_scan(gds) (gds[27])
00183 #define GDS_RotLL_LaSP(gds) INT3(gds[32],gds[33],gds[34])
00184 #define GDS_RotLL_LoSP(gds) INT3(gds[35],gds[36],gds[37])
00185 #define GDS_RotLL_RotAng(gds) ibm2flt(&(gds[38]))
00186 #define GDS_Triangular_ni2(gds) INT2(gds[6],gds[7])
00187 #define GDS_Triangular_ni3(gds) INT2(gds[8],gds[9])
00188 #define GDS_Triangular_ni(gds) INT3(gds[13],gds[14],gds[15])
00189 #define GDS_Triangular_nd(gds) INT3(gds[10],gds[11],gds[12])
00190 #define GDS_Harmonic_nj(gds) ((int) ((gds[6] << 8) + gds[7]))
00191 #define GDS_Harmonic_nk(gds) ((int) ((gds[8] << 8) + gds[9]))
00192 #define GDS_Harmonic_nm(gds) ((int) ((gds[10] << 8) + gds[11]))
00193 #define GDS_Harmonic_type(gds) (gds[12])
00194 #define GDS_Harmonic_mode(gds) (gds[13])
00195 #define GDS_Spaceview_lap(gds) INT3(gds[10],gds[11],gds[12])
00196 #define GDS_Spaceview_lop(gds) INT3(gds[13],gds[14],gds[15])
00197 #define GDS_Spaceview_dx(gds) INT3(gds[17],gds[18],gds[19])
00198 #define GDS_Spaceview_dy(gds) INT3(gds[20],gds[21],gds[22])
00199 #define GDS_Spaceview_Xp(gds) INT2(gds[23],gds[24])
00200 #define GDS_Spaceview_Yp(gds) INT2(gds[25],gds[26])
00201 #define GDS_Spaceview_or(gds) INT3(gds[25],gds[29],gds[30])
00202 #define GDS_Spaceview_nr(gds) INT3(gds[31],gds[32],gds[33])
00203 #define GDS_PV(gds) ((gds[3] == 0) ? -1 : (int) gds[4] - 1)
00204 #define GDS_PL(gds) ((gds[4] == 255) ? -1 : (int) gds[3] * 4 + (int) gds[4] - 1)
00205
00206 #define BDS_LEN(bds) ((int) ((bds[0]<<16)+(bds[1]<<8)+bds[2]))
00207 #define BDS_Flag(bds) (bds[3])
00208 #define BDS_Grid(bds) ((bds[3] & 128) == 0)
00209 #define BDS_Harmonic(bds) (bds[3] & 128)
00210 #define BDS_Packing(bds) ((bds[3] & 64) != 0)
00211 #define BDS_SimplePacking(bds) ((bds[3] & 64) == 0)
00212 #define BDS_ComplexPacking(bds) ((bds[3] & 64) != 0)
00213 #define BDS_OriginalType(bds) ((bds[3] & 32) != 0)
00214 #define BDS_OriginalFloat(bds) ((bds[3] & 32) == 0)
00215 #define BDS_OriginalInt(bds) ((bds[3] & 32) != 0)
00216 #define BDS_MoreFlags(bds) ((bds[3] & 16) != 0)
00217 #define BDS_UnusedBits(bds) ((int) (bds[3] & 15))
00218 #define BDS_BinScale(bds) INT2(bds[4],bds[5])
00219 #define BDS_RefValue(bds) (ibm2flt(bds+6))
00220 #define BDS_NumBits(bds) ((int) bds[10])
00221 #define BDS_Harmonic_RefValue(bds) (ibm2flt(bds+11))
00222 #define BDS_DataStart(bds) ((int) (11 + BDS_MoreFlags(bds)*3))
00223 #define BDS_NValues(bds) (((BDS_LEN(bds) - BDS_DataStart(bds))*8 - BDS_UnusedBits(bds)) / BDS_NumBits(bds))
00224
00225 #define BMS_LEN(bms) ((bms) == NULL ? 0 : (bms[0]<<16)+(bms[1]<<8)+bms[2])
00226 #define BMS_UnusedBits(bms)((bms) == NULL ? 0 : bms[3])
00227 #define BMS_StdMap(bms) ((bms) == NULL ? 0 : ((bms[4]<<8) + bms[5]))
00228 #define BMS_bitmap(bms) ((bms) == NULL ? NULL : (bms)+6)
00229 #define BMS_nxny(bms) ((((bms) == NULL) || BMS_StdMap(bms)) ? 0 : (BMS_LEN(bms)*8 - 48 - BMS_UnusedBits(bms)))
00230
00232 typedef enum {
00233 WGRIB_ENCODE_END,
00234 WGRIB_ENCODE_INIT,
00235 WGRIB_ENCODE_BYTE,
00236 WGRIB_ENCODE_2BYTES,
00237 WGRIB_ENCODE_S2BYTES,
00238 WGRIB_ENCODE_3BYTES,
00239 WGRIB_ENCODE_S3BYTES,
00240 WGRIB_ENCODE_BIT,
00241 WGRIB_ENCODE_AND,
00242 WGRIB_ENCODE_OR
00243 } WGRIB_ENCODE_OPERATION;
00244
00246 unsigned char *mk_PDS(unsigned char *pds, ...);
00248 unsigned char *mk_GDS(unsigned char *gds, ...);
00250 unsigned char *mk_BMS(unsigned char *pds, float *bindata, int *n,
00251 float undef_low, float undef_hi);
00253 unsigned char *mk_BDS(unsigned char *pds, float *bindata, int n);
00254
00256 int GDS_grid(unsigned char *gds, unsigned char *bds,
00257 int *nx, int *ny, long int *nxny);
00259 void BDS_unpack(float *flt, unsigned char *bds, unsigned char *bitmap,
00260 int n_bits, int n, double ref, double scale);
00261
00263 struct ParmTable {
00264 char *name;
00265 char *comment;
00266 } ;
00267
00269 struct ParmTable *Parm_Table(int center, int subcenter,
00270 int ptable, int process);
00271
00273 double int_power(double x, int y);
00275 double ibm2flt(unsigned char *ibm);
00276