You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

313 regels
6.6 KiB

  1. /*
  2. * Atpdec
  3. * Copyright (c) 2004 by Thierry Leconte (F4DWV)
  4. *
  5. * $Id$
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sndfile.h>
  25. #include <math.h>
  26. #include "offsets.h"
  27. #define REGORDER 3
  28. typedef struct {
  29. double cf[REGORDER + 1];
  30. } rgparam;
  31. static void rgcomp(double x[16], rgparam * rgpr)
  32. {
  33. /*{ 0.106,0.215,0.324,0.433,0.542,0.652,0.78,0.87 ,0.0 }; */
  34. const double y[9] =
  35. { 31.07, 63.02, 94.96, 126.9, 158.86, 191.1, 228.62, 255.0, 0.0 };
  36. extern void polyreg(const int m, const int n, const double x[],
  37. const double y[], double c[]);
  38. polyreg(REGORDER, 9, x, y, rgpr->cf);
  39. }
  40. static double rgcal(float x, rgparam * rgpr)
  41. {
  42. double y, p;
  43. int i;
  44. for (i = 0, y = 0.0, p = 1.0; i < REGORDER + 1; i++) {
  45. y += rgpr->cf[i] * p;
  46. p = p * x;
  47. }
  48. return (y);
  49. }
  50. static double tele[16];
  51. static double Cs;
  52. static int nbtele;
  53. int Calibrate(float **prow, int nrow, int offset)
  54. {
  55. double teleline[3000];
  56. double wedge[16];
  57. rgparam regr[30];
  58. int n;
  59. int k;
  60. int mtelestart, telestart;
  61. int channel = -1;
  62. float max;
  63. printf("Calibration ... ");
  64. fflush(stdout);
  65. /* build telemetry values lines */
  66. for (n = 0; n < nrow; n++) {
  67. int i;
  68. teleline[n] = 0.0;
  69. for (i = 3; i < 43; i++) {
  70. teleline[n] += prow[n][i + offset + CH_WIDTH];
  71. }
  72. teleline[n] /= 40.0;
  73. }
  74. if (nrow < 192) {
  75. fprintf(stderr, " impossible, not enought row\n");
  76. return (0);
  77. }
  78. /* find telemetry start in the 2nd third */
  79. max = 0.0;
  80. mtelestart = 0;
  81. for (n = nrow / 3 - 64; n < 2 * nrow / 3 - 64; n++) {
  82. float df;
  83. df = (teleline[n - 4] + teleline[n - 3] + teleline[n - 2] +
  84. teleline[n - 1]) / (teleline[n] + teleline[n + 1] +
  85. teleline[n + 2] + teleline[n + 3]);
  86. if (df > max) {
  87. mtelestart = n;
  88. max = df;
  89. }
  90. }
  91. mtelestart -= 64;
  92. telestart = mtelestart % 128;
  93. if (mtelestart < 0 || nrow < telestart + 128) {
  94. fprintf(stderr, " impossible, not enought row\n");
  95. return (0);
  96. }
  97. /* compute wedges and regression */
  98. for (n = telestart, k = 0; n < nrow - 128; n += 128, k++) {
  99. int j;
  100. for (j = 0; j < 16; j++) {
  101. int i;
  102. wedge[j] = 0.0;
  103. for (i = 1; i < 7; i++)
  104. wedge[j] += teleline[n + j * 8 + i];
  105. wedge[j] /= 6;
  106. }
  107. rgcomp(wedge, &(regr[k]));
  108. if (k == nrow / 256) {
  109. int i, l;
  110. /* telemetry calibration */
  111. for (j = 0; j < 16; j++) {
  112. tele[j] = rgcal(wedge[j], &(regr[k]));
  113. }
  114. /* channel ID */
  115. for (j = 0, max = 10000.0, channel = -1; j < 6; j++) {
  116. float df;
  117. df = wedge[15] - wedge[j];
  118. df = df * df;
  119. if (df < max) {
  120. channel = j;
  121. max = df;
  122. }
  123. }
  124. /* Cs computation */
  125. for (Cs = 0.0, i = 0, j = n; j < n + 128; j++) {
  126. double csline;
  127. for (csline = 0.0, l = 3; l < 43; l++)
  128. csline += prow[n][l + offset -SPC_WIDTH];
  129. csline /= 40.0;
  130. if (csline > 50.0) {
  131. Cs += csline;
  132. i++;
  133. }
  134. }
  135. Cs /= i;
  136. Cs = rgcal(Cs, &(regr[k]));
  137. }
  138. }
  139. nbtele = k;
  140. /* calibrate */
  141. for (n = 0; n < nrow; n++) {
  142. float *pixelv;
  143. int i;
  144. pixelv = prow[n];
  145. for (i = 0; i < CH_WIDTH; i++) {
  146. float pv;
  147. int k, kof;
  148. pv = pixelv[i + offset];
  149. k = (n - telestart) / 128;
  150. if (k >= nbtele)
  151. k = nbtele - 1;
  152. kof = (n - telestart) % 128;
  153. if (kof < 64) {
  154. if (k < 1) {
  155. pv = rgcal(pv, &(regr[k]));
  156. } else {
  157. pv = rgcal(pv, &(regr[k])) * (64 + kof) / 128.0 +
  158. rgcal(pv, &(regr[k - 1])) * (64 - kof) / 128.0;
  159. }
  160. } else {
  161. if ((k + 1) >= nbtele) {
  162. pv = rgcal(pv, &(regr[k]));
  163. } else {
  164. pv = rgcal(pv, &(regr[k])) * (192 - kof) / 128.0 +
  165. rgcal(pv, &(regr[k + 1])) * (kof - 64) / 128.0;
  166. }
  167. }
  168. if (pv > 255.0)
  169. pv = 255.0;
  170. if (pv < 0.0)
  171. pv = 0.0;
  172. pixelv[i + offset] = pv;
  173. }
  174. }
  175. printf("Done\n");
  176. return (channel+1);
  177. }
  178. /* ------------------------------temperature calibration -----------------------*/
  179. extern int satnum;
  180. #include "satcal.h"
  181. typedef struct {
  182. double Nbb;
  183. double Cs;
  184. double Cb;
  185. int ch;
  186. } tempparam;
  187. /* temperature compensation for IR channel */
  188. static void tempcomp(double t[16], int ch, tempparam * tpr)
  189. {
  190. double Tbb, T[4];
  191. double C;
  192. int n;
  193. tpr->ch = ch - 4;
  194. /* compute equivalent T black body */
  195. for (n = 0; n < 4; n++) {
  196. float d0, d1, d2;
  197. C = t[9 + n] * 4.0;
  198. d0 = satcal[satnum].d[n][0];
  199. d1 = satcal[satnum].d[n][1];
  200. d2 = satcal[satnum].d[n][2];
  201. T[n] = d0;
  202. T[n] += d1 * C;
  203. C = C * C;
  204. T[n] += d2 * C;
  205. }
  206. Tbb = (T[0] + T[1] + T[2] + T[3]) / 4.0;
  207. Tbb =
  208. satcal[satnum].rad[tpr->ch].A +
  209. satcal[satnum].rad[tpr->ch].B * Tbb;
  210. /* compute radiance Black body */
  211. C = satcal[satnum].rad[tpr->ch].vc;
  212. tpr->Nbb = c1 * C * C * C / (exp(c2 * C / Tbb) - 1.0);
  213. /* store Count Blackbody and space */
  214. tpr->Cs = Cs * 4.0;
  215. tpr->Cb = t[14] * 4.0;
  216. }
  217. static double tempcal(float Ce, tempparam * rgpr)
  218. {
  219. double Nl, Nc, Ns, Ne;
  220. double T, vc;
  221. Ns = satcal[satnum].cor[rgpr->ch].Ns;
  222. Nl = Ns + (rgpr->Nbb - Ns) * (rgpr->Cs - Ce * 4.0) / (rgpr->Cs -
  223. rgpr->Cb);
  224. Nc = satcal[satnum].cor[rgpr->ch].b[0] +
  225. satcal[satnum].cor[rgpr->ch].b[1] * Nl +
  226. satcal[satnum].cor[rgpr->ch].b[2] * Nl * Nl;
  227. Ne = Nl + Nc;
  228. vc = satcal[satnum].rad[rgpr->ch].vc;
  229. T = c2 * vc / log(c1 * vc * vc * vc / Ne + 1.0);
  230. T = (T - satcal[satnum].rad[rgpr->ch].A) / satcal[satnum].rad[rgpr->ch].B;
  231. /* rescale to range 0-255 for -60 +40 °C */
  232. T = (T - 273.15 + 60.0) / 100.0 * 256.0;
  233. return (T);
  234. }
  235. void Temperature(float **prow, int nrow, int channel, int offset)
  236. {
  237. tempparam temp;
  238. int n;
  239. printf("Temperature ... ");
  240. fflush(stdout);
  241. tempcomp(tele, channel, &temp);
  242. for (n = 0; n < nrow; n++) {
  243. float *pixelv;
  244. int i;
  245. pixelv = prow[n];
  246. for (i = 0; i < CH_WIDTH; i++) {
  247. float pv;
  248. pv = tempcal(pixelv[i + offset], &temp);
  249. if (pv > 255.0)
  250. pv = 255.0;
  251. if (pv < 0.0)
  252. pv = 0.0;
  253. pixelv[i + offset] = pv;
  254. }
  255. }
  256. printf("Done\n");
  257. }