33 static int32_t cur_ctht_scan = -1;
36 static int32_t firstin = 0;
38 static int32_t *outint;
39 int32_t cur_scan = l2rec->l1rec->iscan;
40 int32_t
npix = l2rec->l1rec->npix;
41 int32_t clin, ipix, ntyp = 2, phase_v;
44 outint = (int32_t *)outbuf;
49 if( ( outbuf = malloc(
npix * ntyp *
sizeof(
float ) ) ) ==
NULL )
51 printf(
"%s, %d: E - Unable to allocate the ctht output buffer\n",
58 if( cur_ctht_scan != cur_scan )
60 cur_ctht_scan = cur_scan;
63 printf(
"%s, %d: E - CTH computation failure\n", __FILE__, __LINE__ );
72 memcpy( outbuf,
ctht_lins.ct[clin]->ctp,
npix *
sizeof(
float ) );
76 memcpy( outbuf,
ctht_lins.ct[clin]->ctt,
npix *
sizeof(
float ) );
80 memcpy( outbuf,
ctht_lins.ct[clin]->cth,
npix *
sizeof(
float ) );
84 for( ipix = 0; ipix <
npix; ipix++ )
86 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
88 ctht_lins.ct[clin]->cth_cod_all[ phase_v + ntyp * ipix ];
93 memcpy( outbuf,
ctht_lins.ct[clin]->cth_cod_all,
94 npix * ntyp *
sizeof(
float ) );
98 memcpy( outbuf,
ctht_lins.ct[clin]->cth_alb_all,
99 npix * ntyp *
sizeof(
float ) );
103 for( ipix = 0; ipix <
npix; ipix++ )
105 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
107 ctht_lins.ct[clin]->cth_alb_all[ phase_v + ntyp * ipix ];
112 for( ipix = 0; ipix <
npix; ipix++ )
114 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
116 ctht_lins.ct[clin]->dcod[ phase_v + ntyp * ipix ];
121 for( ipix = 0; ipix <
npix; ipix++ )
123 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
125 ctht_lins.ct[clin]->dlcod[ phase_v + ntyp * ipix ];
130 for( ipix = 0; ipix <
npix; ipix++ )
132 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
134 ctht_lins.ct[clin]->dcth[ phase_v + ntyp * ipix ];
139 for( ipix = 0; ipix <
npix; ipix++ )
141 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
143 ctht_lins.ct[clin]->dctp[ phase_v + ntyp * ipix ];
148 for( ipix = 0; ipix <
npix; ipix++ )
150 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
152 ctht_lins.ct[clin]->dctt[ phase_v + ntyp * ipix ];
157 for( ipix = 0; ipix <
npix; ipix++ )
159 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
161 ctht_lins.ct[clin]->dalb[ phase_v + ntyp * ipix ];
166 memcpy( outbuf,
ctht_lins.ct[clin]->ct_phase,
167 npix *
sizeof(
unsigned char ) );
171 memcpy( outbuf,
ctht_lins.ct[clin]->cth_lcod_all,
172 npix * ntyp *
sizeof(
float ) );
176 memcpy( outbuf,
ctht_lins.ct[clin]->cth_all,
177 npix * ntyp *
sizeof(
float ) );
181 memcpy( outbuf,
ctht_lins.ct[clin]->ctp_all,
182 npix * ntyp *
sizeof(
float ) );
186 memcpy( outbuf,
ctht_lins.ct[clin]->cost_ss,
187 npix * ntyp *
sizeof(
float ) );
191 memcpy( outbuf,
ctht_lins.ct[clin]->acost,
192 npix * ntyp *
sizeof(
float ) );
196 memcpy( outint,
ctht_lins.ct[clin]->nitr,
197 npix * ntyp *
sizeof( int32_t) );
201 memcpy( outbuf,
ctht_lins.ct[clin]->cth_raw,
202 npix * ntyp *
sizeof(
float ) );
206 memcpy( outbuf,
ctht_lins.ct[clin]->ctt_all,
207 npix * ntyp *
sizeof(
float ) );
212 for( ipix = 0; ipix <
npix; ipix++ )
214 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
216 ctht_lins.ct[clin]->cth_lcod_all[ phase_v + ntyp * ipix ];
221 for( ipix = 0; ipix <
npix; ipix++ )
223 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
225 ctht_lins.ct[clin]->cost_ss[ phase_v + ntyp * ipix ];
230 for( ipix = 0; ipix <
npix; ipix++ )
232 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
234 ctht_lins.ct[clin]->acost[ phase_v + ntyp * ipix ];
239 for( ipix = 0; ipix <
npix; ipix++ )
241 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
243 ctht_lins.ct[clin]->nitr[ phase_v + ntyp * ipix ];
248 for( ipix = 0; ipix <
npix; ipix++ )
250 phase_v =
ctht_lins.ct[clin]->ct_phase[ipix];
252 ctht_lins.ct[clin]->cth_raw[ phase_v + ntyp * ipix ];
257 memcpy( outbuf,
ctht_lins.ct[clin]->dcod,
258 npix * ntyp *
sizeof(
float ) );
262 memcpy( outbuf,
ctht_lins.ct[clin]->dlcod,
263 npix * ntyp *
sizeof(
float ) );
267 memcpy( outbuf,
ctht_lins.ct[clin]->dcth,
268 npix * ntyp *
sizeof(
float ) );
272 memcpy( outbuf,
ctht_lins.ct[clin]->dctp,
273 npix * ntyp *
sizeof(
float ) );
277 memcpy( outbuf,
ctht_lins.ct[clin]->dctt,
278 npix * ntyp *
sizeof(
float ) );
282 memcpy( outbuf,
ctht_lins.ct[clin]->dalb,
283 npix * ntyp *
sizeof(
float ) );
290 printf(
"%s, %d: E - CTH computation could not find the catalog # %d\n",
291 __FILE__, __LINE__, prodnum );
310 int32_t ctht_nrec = 3;
312 static int32_t *ctht_stat;
313 static int32_t ctht_init = 0;
314 int32_t ilin, il2, lin_in_que, scn_in_que, ntyp = 2;
316 static ctht_lin_str *ctht_sav;
317 int32_t
npix = l2rec->l1rec->npix;
319 if(
input->proc_cloud != 1 ) {
320 printf(
"E, %s, %d: For cloud processing, proc_cloud must be set to 1\n",
321 __FILE__, __LINE__ );
325 if( l2rec->l1rec->anc_add ==
NULL ) {
326 printf(
"E, %s, %d: For cloud processing, anc_profile_1, _2, _3 must be specified\n", __FILE__, __LINE__ );
331 if( strstr(
input->met1,
"climatology" ) !=
NULL ) {
332 printf(
"E, %s, %d: Ancillary meteorological data from a climatology file cannot be used for cloud processing\n", __FILE__, __LINE__ );
333 printf(
"Use daily files in MET1, 2, 3 l2gen inputs\n" );
342 if( ( (
ctht_lins.ct = (ctht_lin_str **)malloc(
343 ctht_nrec *
sizeof( ctht_lin_str * ) ) ) ==
NULL ) ||
344 ( ( ctht_stat = (int32_t *)malloc(
345 ctht_nrec *
sizeof( int32_t ) ) ) ==
NULL ) )
347 printf(
"%s - %d: Allocation of ctht record space failed\n",
348 __FILE__, __LINE__ );
351 for( ilin = 0; ilin < ctht_nrec; ilin++ )
354 (ctht_lin_str *) malloc(
sizeof( ctht_lin_str) ) ) ==
NULL )
356 printf(
"%s - %d: Allocation of ctht record space failed\n",
357 __FILE__, __LINE__ );
363 (
float *) malloc(
npix *
sizeof(
float) ) ) ==
NULL ) ||
365 (
float *) malloc(
npix *
sizeof(
float) ) ) ==
NULL ) ||
367 (
float *) malloc(
npix *
sizeof(
float) ) ) ==
NULL ) ||
369 (
unsigned char *) malloc(
npix *
sizeof(
unsigned char) ) ) ==
NULL )
371 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
373 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
375 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
377 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
379 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
381 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
383 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
385 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
387 (int32_t *) malloc(
npix * ntyp *
sizeof(int32_t) ) ) ==
NULL ) )
389 printf(
"%s - %d: Allocation of ctht record space failed\n",
390 __FILE__, __LINE__ );
393 if( ( (
ctht_lins.ct[ilin]->cth_cod_all =
394 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
396 (
float *) malloc(
npix * 3 * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
398 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
400 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
402 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
404 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
406 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) ||
408 (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL ) )
410 printf(
"%s - %d: Allocation of ctht record space failed\n",
411 __FILE__, __LINE__ );
421 for( ilin = 0; ilin < ctht_nrec; ilin++ )
425 for( ilin = 0; ilin < ctht_nrec; ilin++ )
427 lin_in_que = ilin +
l1que.nq / 2 - ctht_nrec / 2;
429 scn_in_que =
l1que.r[lin_in_que].iscan;
431 for( il2 = 0; il2 < ctht_nrec; il2++ )
433 if(
ctht_lins.ct[il2]->iscan == scn_in_que )
445 for( ilin = 0; ilin < ctht_nrec; ilin++ )
447 if( ctht_stat[ilin] == 0 )
449 lin_in_que = ilin +
l1que.nq / 2 - ctht_nrec / 2;
450 scn_in_que =
l1que.r[lin_in_que].iscan;
454 printf(
"%s - %d: Problem found running comp_ctht_lin\n",
455 __FILE__, __LINE__ );
479 int32_t n_ctht_bnd = 8, n_prd, ny;
480 static int32_t ctht_init = 0;
481 static int32_t l1_bnd_ix[8];
482 static double *sy, *tmp_lut;
483 static ctht_lut_str full_lut_wtr, full_lut_ice;
484 static ctht_unc_str unc;
485 static oe_info_str oe_info;
486 static ctht_lut_str *lut;
487 static float *lcl_prof_t, *lcl_prof_h, *lcl_p_set;
488 static int32_t lcl_nlvl, lcl_lvl_ptr, fill_lvl;
489 float solz,
senz, relaz, windsp, glint_ref;
490 int32_t ipix, ityp,
npix, nbnd_ref, do_const;
491 int32_t ntyp = 2, foff, stype;
493 float err1, err2, ev1, ev2;
494 double rhot[8], xa[3];
495 float *sxax[4], p_log_lo, p_log_hi;
498 int32_t ibnd, ibnd2, icod, icth, ialb, icor, ib1, ib2, ilev;
502 int32_t ctht_wav[] = { 754, 757, 759, 762, 764, 767, 769, 772 };
503 int32_t ctht_all_bnd = 0, close_ind;
504 int64_t ar_off[16], tmp_off, tmp_off2, off_cost;
505 float ar_wt[16], orig_wt[4];
506 float dp, found_hgt, lo_cost, spress_out;
509 double min_cost, yres[8], xres[8];
510 double fg_cost_t1, fg_cost_t2;
511 double int_pls, int_neg, int_ctr,
coord, pctr;
512 int32_t min_cost_loc[3];
513 static double *fg_cost;
514 static gsl_matrix *gm_sa, *gm_sy, *gm_sy_inv, *gm_sa_inv;
515 static gsl_vector *gv_yres, *gv_sy_inv_yres, *gv_xres, *gv_sa_inv_xres;
520 static float *ret_lcod;
522 float merra_p_set[] = { 1000., 975., 950., 925., 900., 875., 850., 825.,
523 800., 775., 750., 725., 700., 650., 600., 550., 500., 450., 400.,
524 350., 300., 250., 200., 150., 100., 70., 50., 40., 30., 20., 10.,
525 7., 5., 4., 3., 2., 1., 0.7, 0.5, 0.4, 0.3, 0.1 };
526 int32_t nlvl_merra2 = 42, found_phase;
529 unc.noise_err = 0.005;
531 float lut_dalb = 0.1;
551 for( ibnd = 0; ibnd < n_ctht_bnd; ibnd++ )
553 l1_bnd_ix[ibnd] =
bindex_get( ctht_wav[ibnd] );
554 if( l1_bnd_ix[ibnd] < 0 )
557 printf(
"%s, %d, I: CTHT wave: %d not found\n", __FILE__, __LINE__,
562 printf(
"%s, %d, I: Got CTHT band wavelength %d as index %d\n",
563 __FILE__, __LINE__, ctht_wav[ibnd], l1_bnd_ix[ibnd] );
566 if( ctht_all_bnd == 0 )
568 printf(
"%s, %d, E: Not all bands available for CTHT work\n",
569 __FILE__, __LINE__ );
575 printf(
"%s, %d, E: Failure reading unc or lut\n", __FILE__, __LINE__ );
580 if( ( full_lut_wtr.lut_nwave != full_lut_ice.lut_nwave ) ||
581 ( full_lut_wtr.lut_nwave != ny ) )
583 printf(
"%s, %d, E: expected # bands is not in the table\n",
587 if( ( full_lut_wtr.lut_nsolz != full_lut_ice.lut_nsolz ) ||
588 ( full_lut_wtr.lut_nsenz != full_lut_ice.lut_nsenz ) ||
589 ( full_lut_wtr.lut_nrelaz != full_lut_ice.lut_nrelaz ) ||
590 ( full_lut_wtr.lut_nlev != full_lut_ice.lut_nlev ) ||
591 ( full_lut_wtr.lut_npress != full_lut_ice.lut_npress ) ||
592 ( full_lut_wtr.lut_nalb != full_lut_ice.lut_nalb ) ||
593 ( full_lut_wtr.lut_ncod != full_lut_ice.lut_ncod ) ||
594 ( full_lut_wtr.lut_naod != full_lut_ice.lut_naod ) ||
595 ( full_lut_wtr.lut_ncth != full_lut_ice.lut_ncth ) )
597 printf(
"%s, %d, E: LUTs (ice and water) have different array sizes\n",
602 sy = (
double *)malloc( n_ctht_bnd * n_ctht_bnd *
sizeof(
double) );
604 if( ( fg_cost = (
double *)
605 malloc( full_lut_wtr.lut_ncod * full_lut_wtr.lut_ncth *
606 full_lut_wtr.lut_nalb *
sizeof(
double) ) ) ==
NULL )
608 printf(
"%s, %d, E: unable to allocate the cost array\n",
609 __FILE__, __LINE__ );
614 if( ( tmp_lut = (
double *) malloc( lut->lut_nwave * lut->lut_ncod *
615 lut->lut_ncth * lut->lut_nalb *
sizeof(
double) ) ) ==
NULL )
617 printf(
"%s - %d: E: Failure to allocate the pixel-specific LUT\n",
618 __FILE__, __LINE__ );
622 gm_sa = gsl_matrix_alloc( n_prd, n_prd );
623 gm_sy = gsl_matrix_alloc( ny, ny );
624 gv_yres = gsl_vector_alloc( ny );
625 gv_sy_inv_yres = gsl_vector_alloc( ny );
626 gv_xres = gsl_vector_alloc(n_prd);
627 gv_sa_inv_xres = gsl_vector_alloc(n_prd);
631 if( ( ret_lcod = (
float *) malloc(
npix * ntyp *
sizeof(
float) ) ) ==
NULL )
634 "%s, %d, E: ctht was unable to allocate all the storage needed\n",
635 __FILE__, __LINE__ );
639 if( ( ( lcl_prof_t = (
float *) malloc( ( nlvl_merra2 + 1 ) *
sizeof(
float) ) ) ==
NULL )
640 || ( ( lcl_prof_h = (
float *) malloc( ( nlvl_merra2 + 1 ) *
sizeof(
float) ) ) ==
NULL )
641 || ( ( lcl_p_set = (
float *) malloc( ( nlvl_merra2 + 1 ) *
sizeof(
float) ) ) ==
NULL ) )
643 printf(
"%s, %d, E: Unable to allocate local profile storage\n",
644 __FILE__, __LINE__ );
650 nbnd_ref =
l1rec->l1file->nbands;
654 for( ipix = 0; ipix <
npix; ipix++ )
661 if(
l1rec->cloud[ipix] == 1 )
667 relaz =
l1rec->delphi[ipix];
668 windsp =
l1rec->ws[ipix];
671 relaz = fmod(
fabs( relaz ), 360. );
672 relaz = ( relaz <= 180. ) ? relaz : 360. - relaz;
675 for( ibnd = 0; ibnd < n_ctht_bnd; ibnd++ )
677 foff = l1_bnd_ix[ibnd] + nbnd_ref * ipix;
681 PI / 180. ) *
l1rec->Fo[l1_bnd_ix[ibnd]] );
686 if(
l1rec->land[ipix] ) stype = 1;
687 if(
l1rec->ice[ipix] ) stype = 2;
693 lcl_prof_t[0] =
l1rec->sfct[ipix];
694 lcl_prof_h[0] =
l1rec->height[ipix];
695 lcl_p_set[0] =
l1rec->sfcp[ipix];
700 for(
int ilvl = 0; ilvl < nlvl_merra2; ilvl ++ )
704 if( lcl_lvl_ptr == 0 )
706 if( (
l1rec->anc_add->prof_height[ ipix * nlvl_merra2 + ilvl ]
708 (
l1rec->anc_add->prof_temp[ ipix * nlvl_merra2 + ilvl ]
710 ( merra_p_set[ilvl] <
l1rec->sfcp[ipix] ) &&
711 (
l1rec->anc_add->prof_height[ ipix * nlvl_merra2 + ilvl ]
712 >
l1rec->height[ipix] ) )
721 lcl_prof_t[lcl_lvl_ptr] =
l1rec->anc_add->prof_temp[ ipix * nlvl_merra2 + ilvl ];
722 lcl_prof_h[lcl_lvl_ptr] =
l1rec->anc_add->prof_height[ ipix * nlvl_merra2 + ilvl ];
723 lcl_p_set[lcl_lvl_ptr] = merra_p_set[ilvl];
727 lcl_nlvl = lcl_lvl_ptr;
732 for( ityp = 0; ityp < ntyp; ityp++ )
738 for( ib1= 0; ib1 < n_ctht_bnd; ib1++ )
740 for( ib2 = 0; ib2 < n_ctht_bnd; ib2++ )
742 *( sy + ib1 + n_ctht_bnd * ib2 ) = 0.;
746 *( sy + ib1 + n_ctht_bnd * ib2 ) += unc.noise_err *
747 unc.noise_err * rhot[ib1] * rhot[ib2];
749 *( sy + ib1 + n_ctht_bnd * ib2 ) += unc.cal_err * unc.cal_err *
750 rhot[ib1] * rhot[ib2];
752 ev1 = unc.icpts[ ityp + ntyp * ( stype + unc.nsurf * ib1 ) ] +
753 unc.grads[ ityp + ntyp * ( stype + unc.nsurf * ib1 ) ] *
755 ev2 = unc.fit_rms[ ityp + ntyp * ( stype + unc.nsurf * ib1 ) ];
756 err1 = ( ev1 > ev2 ) ? ev1 : ev2;
758 ev1 = unc.icpts[ ityp + ntyp * ( stype + unc.nsurf * ib2 ) ] +
759 unc.grads[ ityp + ntyp * ( stype + unc.nsurf * ib2 ) ] *
761 ev2 = unc.fit_rms[ ityp + ntyp * ( stype + unc.nsurf * ib2 ) ];
762 err2 = ( ev1 > ev2 ) ? ev1 : ev2;
764 *( sy + ib1 + n_ctht_bnd * ib2 ) += err1 * err2 *
765 unc.err_corrs_category[ityp + ntyp *
766 ( stype + unc.nsurf * ( ib1 + n_ctht_bnd * ib2 ) ) ];
776 double max_glint_for_table;
777 max_glint_for_table = lut->lut_alb[lut->lut_nalb-1];
778 xa[0] = 3.; xa[1] = 3.;
781 xa[2] = ( 0.02 + glint_ref ) / lut_dalb;
783 else if( stype == 1 )
785 xa[2] =
l1rec->cld_dat->cth_alb_init[ipix] / lut_dalb;
789 xa[2] = 0.9 / lut_dalb;
791 if( xa[2] >= max_glint_for_table / lut_dalb )
797 xa[2] = max_glint_for_table / lut_dalb;
809 gsl_matrix_set_zero( gm_sa );
811 gsl_matrix_set( gm_sa, 0, 0, pow( 100., 2 ) );
813 gsl_matrix_set( gm_sa, 1, 1, pow( 100., 2 ) );
819 gsl_matrix_set( gm_sa, 2, 2,
820 pow( ( 0.01 + 0.2 * glint_ref ) / lut_dalb, 2. ) );
822 else if( stype == 1 )
824 gsl_matrix_set( gm_sa, 2, 2,
825 pow( (
l1rec->cld_dat->cth_alb_unc_init[ipix] / lut_dalb ), 2. ) );
830 gsl_matrix_set( gm_sa, 2, 2, pow( 0.05 / lut_dalb, 2. ) );
838 lut = ( ityp == 0 ) ? &full_lut_wtr : &full_lut_ice;
840 sxax[0] = lut->lut_press;
841 snax[0] = lut->lut_npress;
842 sxax[1] = lut->lut_solz;
843 snax[1] = lut->lut_nsolz;
844 sxax[2] = lut->lut_senz;
845 snax[2] = lut->lut_nsenz;
846 sxax[3] = lut->lut_relaz;
847 snax[3] = lut->lut_nrelaz;
848 n_block = lut->lut_ncod * lut->lut_ncth * lut->lut_nalb *
858 spress_out =
l1rec->sfcp[ipix];
859 float xpt[4] = { spress_out,
solz,
senz, relaz };
862 xpt[3] =
fabs( fmod( xpt[3], 180. ) );
865 xpt[0] = ( xpt[0] > 1050. ) ? 1050. : xpt[0];
866 xpt[0] = ( xpt[0] < 600. ) ? 600. : xpt[0];
872 if(
int_4d( sxax, snax, n_block, xpt, ar_off, ar_wt, orig_wt ) != 0 )
883 for( ibnd = 0; ibnd < lut->lut_nwave; ibnd++ )
885 for( icod = 0; icod < lut->lut_ncod; icod++ )
887 for( icth = 0; icth < lut->lut_ncth; icth++ )
889 for( ialb = 0; ialb < lut->lut_nalb; ialb++ )
891 tmp_off = icod + lut->lut_ncod * ( icth + lut->lut_ncth *
892 ( ialb + lut->lut_nalb * ibnd ) );
893 tmp_off2 = ialb + lut->lut_nalb * ( icth + lut->lut_ncth *
894 ( icod + lut->lut_ncod * ibnd ) );
895 *( tmp_lut + tmp_off ) = 0;
896 for( icor = 0; icor < 16; icor++ )
898 *( tmp_lut + tmp_off ) += ar_wt[icor] *
899 *(lut->lut + ar_off[icor] + tmp_off2 );
909 for( icod = 0; icod < lut->lut_ncod; icod++ )
911 for( icth = 0; icth < lut->lut_ncth; icth++ )
914 for( ialb = 0; ialb < lut->lut_nalb; ialb++ )
918 off_cost = icod + lut->lut_ncod *
919 ( icth + lut->lut_ncth * ialb );
920 *( fg_cost + off_cost ) = 1.e10;
922 ialb = (int32_t) ( xa[2] + .5 );
926 off_cost = icod + lut->lut_ncod * ( icth + lut->lut_ncth * ialb );
927 for( ibnd = 0; ibnd < lut->lut_nwave; ibnd++ )
929 tmp_off = icod + lut->lut_ncod * ( icth + lut->lut_ncth *
930 ( ialb + lut->lut_nalb * ibnd ) );
931 yres[ibnd] = *( tmp_lut + tmp_off ) - rhot[ibnd];
934 xres[0] = icod - xa[0];
935 xres[1] = icth - xa[1];
936 xres[2] = ialb - xa[2];
942 for( ibnd = 0; ibnd < lut->lut_nwave; ibnd++ )
944 gsl_vector_set( gv_yres, ibnd, yres[ibnd] );
945 for( ibnd2= 0; ibnd2 < lut->lut_nwave; ibnd2++ )
947 gsl_matrix_set( gm_sy, ibnd, ibnd2,
948 *( sy + ibnd + 8 * ibnd2 ) );
954 gsl_blas_dgemv( CblasNoTrans, 1., gm_sy_inv, gv_yres, 0., gv_sy_inv_yres );
955 gsl_matrix_free( gm_sy_inv );
956 gsl_blas_ddot( gv_sy_inv_yres, gv_yres, &fg_cost_t1 );
958 for( ibnd = 0; ibnd < 3; ibnd++ )
960 gsl_vector_set( gv_xres, ibnd, xres[ibnd] );
965 gsl_blas_dgemv( CblasNoTrans, 1., gm_sa_inv, gv_xres, 0.,
967 gsl_matrix_free( gm_sa_inv );
968 gsl_blas_ddot( gv_sa_inv_xres, gv_xres, &fg_cost_t2 );
971 *( fg_cost + off_cost ) = fg_cost_t1 + fg_cost_t2;
977 if( *( fg_cost + off_cost ) < min_cost )
979 min_cost = *( fg_cost + off_cost );
980 min_cost_loc[0] = icod;
981 min_cost_loc[1] = icth;
982 min_cost_loc[2] = ialb;
990 if( min_cost >= 1.e10 )
992 printf(
"%s, %d, I: the minimum cost is 1.e10. or higher\n",
993 __FILE__, __LINE__ );
1001 lut_dims[0] = lut->lut_ncod;
1002 lut_dims[1] = lut->lut_ncth;
1003 lut_dims[2] = lut->lut_nalb;
1004 lut_dims[3] = lut->lut_nwave;
1006 lut_dims, min_cost_loc, &oe_info );
1019 ctht_lin->cth_lcod_all[ ityp + ntyp * ipix ] =
1020 axis_interp( lut->lut_lcod, lut->lut_ncod, oe_info.x_prd_state[0] );
1022 ctht_lin->cth_cod_all[ ityp + ntyp * ipix ] =
1023 pow( 10., ctht_lin->cth_lcod_all[ ityp + ntyp * ipix ] );
1026 ctht_lin->cth_all[ ityp + ntyp * ipix ] =
1027 axis_interp( lut->lut_cth, lut->lut_ncth, oe_info.x_prd_state[1] )
1028 +
l1rec->height[ipix] / 1000.;
1030 ctht_lin->ctp_all[ ityp + ntyp * ipix ] =
1031 axis_interp( lut->lut_pp0, lut->lut_ncth, oe_info.x_prd_state[1] ) *
1034 ctht_lin->cth_alb_all[ ityp + ntyp * ipix ] =
1035 axis_interp( lut->lut_alb, lut->lut_nalb, oe_info.x_prd_state[2] );
1038 ctht_lin->cost_ss[ ityp + ntyp * ipix ] = oe_info.cost;
1040 ctht_lin->acost[ ityp + ntyp * ipix ] = oe_info.acost;
1042 ctht_lin->nitr[ ityp + ntyp * ipix ] = oe_info.nitr;
1047 for(
int xx = 0; xx < nxx; xx++ )
1048 ctht_lin->oe_akdiag[ xx + nxx * ( ityp + ntyp * ipix ) ] =
1049 gsl_matrix_get( oe_info.gm_ak, xx, xx );
1067 coord = oe_info.x_prd_state[0] +
1068 sqrt( gsl_matrix_get( oe_info.gm_sx, 0, 0 ) );
1069 if( lut->lut_ncod - 1 <
coord )
coord = lut->lut_ncod - 1;
1070 int_ctr =
axis_interp( lut->lut_lcod, lut->lut_ncod,
1071 oe_info.x_prd_state[0] );
1074 coord = oe_info.x_prd_state[0] -
1075 sqrt( gsl_matrix_get( oe_info.gm_sx, 0, 0 ) );
1079 ctht_lin->dlcod[ ityp + ntyp * ipix ] =
1080 fmax(
fabs( int_pls - int_ctr ),
fabs( int_neg -int_ctr ) );
1082 pctr = pow( 10., int_ctr );
1083 int_pls = pow( 10., int_pls );
1084 int_neg = pow( 10., int_neg );
1085 ctht_lin->dcod[ ityp + ntyp * ipix ] =
1086 fmax(
fabs( int_pls - pctr ),
fabs( int_neg - pctr ) );
1089 coord = oe_info.x_prd_state[1] +
1090 sqrt( gsl_matrix_get( oe_info.gm_sx, 1, 1 ) );
1091 if( lut->lut_ncth - 1 <
coord )
coord = lut->lut_ncth - 1;
1092 int_ctr =
axis_interp( lut->lut_cth, lut->lut_ncth,
1093 oe_info.x_prd_state[1] );
1096 coord = oe_info.x_prd_state[1] -
1097 sqrt( gsl_matrix_get( oe_info.gm_sx, 1, 1 ) );
1101 ctht_lin->dcth[ ityp + ntyp * ipix ] =
1102 fmax(
fabs( int_pls - int_ctr ),
fabs( int_neg - int_ctr ) );
1108 ctht_lin->cth_raw[ ityp + ntyp * ipix ] =
1109 ctht_lin->cth_all[ ityp + ntyp * ipix ];
1112 for( ilev = 0; ilev < lcl_nlvl; ilev++ )
1114 if( lcl_p_set[ilev] < ctht_lin->ctp_all[ ityp + ntyp * ipix ] )
1116 close_ind = ( ilev == 0 ) ? 0 : ilev -1;
1120 if( ( close_ind == -1 ) || ( close_ind == lcl_nlvl-1 ) )
1122 p_log_lo = log10( lcl_p_set[lcl_nlvl-2] );
1123 p_log_hi = log10( lcl_p_set[lcl_nlvl-1] );
1127 p_log_lo = log10( lcl_p_set[close_ind] );
1128 p_log_hi = log10( lcl_p_set[ close_ind + 1 ] );
1130 dp = ( log10( ctht_lin->ctp_all[ ityp + ntyp * ipix ] ) - p_log_lo )
1131 / ( p_log_hi - p_log_lo );
1135 found_hgt =
axis_interp( lcl_prof_h, lcl_nlvl, close_ind +
dp );
1137 if( found_hgt < l1rec->height[ipix] ) found_hgt =
1138 l1rec->height[ipix] + 10.;
1139 ctht_lin->cth_all[ ityp + ntyp * ipix ] = found_hgt / 1000.;
1142 ctht_lin->ctt_all[ ityp + ntyp * ipix ] =
C_TO_K +
1145 if( ctht_lin->cth_all[ ityp + ntyp * ipix ] > 100. )
1147 printf(
"%s, %d, W: crazy CTH, profile issue, will use non MERRA\n",
1148 __FILE__, __LINE__ );
1149 ctht_lin->cth_all[ ityp + ntyp * ipix ] =
1150 ctht_lin->cth_raw[ ityp + ntyp * ipix ];
1153 if( ctht_lin->ctt_all[ ityp + ntyp * ipix ] >= 350. )
1154 ctht_lin->ctt_all[ ityp + ntyp * ipix ] =
1161 coord = oe_info.x_prd_state[1] +
1162 sqrt( gsl_matrix_get( oe_info.gm_sx, 1, 1 ) );
1163 if( lut->lut_ncth - 1 <
coord )
coord = lut->lut_ncth - 1;
1164 int_ctr =
axis_interp( lut->lut_pp0, lut->lut_ncth,
1165 oe_info.x_prd_state[1] ) * spress_out;
1169 coord = oe_info.x_prd_state[1] -
1170 sqrt( gsl_matrix_get( oe_info.gm_sx, 1, 1 ) );
1175 ctht_lin->dctp[ ityp + ntyp * ipix ] =
1176 fmax(
fabs( int_pls - int_ctr ),
fabs( int_neg - int_ctr ) );
1182 ctht_lin->dctt[ ityp + ntyp * ipix ] =
1183 6. * ctht_lin->dcth[ ityp + ntyp * ipix ];
1186 coord = oe_info.x_prd_state[2] +
1187 sqrt( gsl_matrix_get( oe_info.gm_sx, 2, 2 ) );
1188 if( lut->lut_nalb - 1 <
coord )
coord = lut->lut_nalb - 1;
1189 int_ctr =
axis_interp( lut->lut_alb, lut->lut_nalb,
1190 oe_info.x_prd_state[2] );
1193 coord = oe_info.x_prd_state[2] -
1194 sqrt( gsl_matrix_get( oe_info.gm_sx, 2, 2 ) );
1197 ctht_lin->dalb[ ityp + ntyp * ipix ] =
1198 fmax(
fabs( int_pls - int_ctr ),
fabs( int_neg - int_ctr ) );
1205 for( ityp = 0; ityp < ntyp; ityp++ )
1207 if( ctht_lin->cost_ss[ ityp + ntyp * ipix ] ==
BAD_FLT )
1214 if( ctht_lin->cost_ss[ ityp + ntyp * ipix ] < lo_cost )
1217 lo_cost = ctht_lin->cost_ss[ ityp + ntyp * ipix ];
1221 ctht_lin->ct_phase[ipix] = found_phase;
1225 ctht_lin->cth[ipix] = ctht_lin->cth_all[ found_phase + ntyp * ipix ];
1226 ctht_lin->ctp[ipix] = ctht_lin->ctp_all[ found_phase + ntyp * ipix ];
1227 ctht_lin->ctt[ipix] = ctht_lin->ctt_all[ found_phase + ntyp * ipix ];
1231 ctht_lin->cth[ipix] =
BAD_FLT;
1232 ctht_lin->ctp[ipix] =
BAD_FLT;
1233 ctht_lin->ctt[ipix] =
BAD_FLT;
1242 ctht_lin->iscan =
l1rec->iscan;
1243 ctht_lin->npix =
l1rec->npix;
1259 ctht_lin->cth[ipix] =
BAD_FLT;
1260 ctht_lin->ctp[ipix] =
BAD_FLT;
1261 ctht_lin->ctt[ipix] =
BAD_FLT;
1266 for(
int i = 0;
i < ntyp;
i++ )
1268 ctht_lin->cth_all[
i + ntyp * ipix ] =
BAD_FLT;
1269 ctht_lin->ctp_all[
i + ntyp * ipix ] =
BAD_FLT;
1270 ctht_lin->ctt_all[
i + ntyp * ipix ] =
BAD_FLT;
1271 ctht_lin->cth_cod_all[
i + ntyp * ipix ] =
BAD_FLT;
1272 ctht_lin->cth_lcod_all[
i + ntyp * ipix ] =
BAD_FLT;
1273 ctht_lin->cth_raw[
i + ntyp * ipix ] =
BAD_FLT;
1274 ctht_lin->cth_alb_all[
i + ntyp * ipix ] =
BAD_FLT;
1275 ctht_lin->cost_ss[
i + ntyp * ipix ] =
BAD_FLT;
1276 ctht_lin->acost[
i + ntyp * ipix ] =
BAD_FLT;
1277 ctht_lin->nitr[
i + ntyp * ipix ] =
BAD_INT;
1278 for(
int inx = 0; inx < 3; inx++ )
1279 ctht_lin->oe_akdiag[ inx * (
i + ntyp * ipix ) ] =
BAD_FLT;
1280 ctht_lin->dlcod[
i + ntyp * ipix ] =
BAD_FLT;
1281 ctht_lin->dcod[
i + ntyp * ipix ] =
BAD_FLT;
1282 ctht_lin->dcth[
i + ntyp * ipix ] =
BAD_FLT;
1283 ctht_lin->dctp[
i + ntyp * ipix ] =
BAD_FLT;
1284 ctht_lin->dctt[
i + ntyp * ipix ] =
BAD_FLT;
1285 ctht_lin->dalb[
i + ntyp * ipix ] =
BAD_FLT;
1308 float cth, ctp, ctt;
1317 else if( proc_flg== 1 )
1319 switch (
l1rec->iscan % 3 )
1338 for( ipix = 0; ipix <
npix; ipix++ )
1340 ctht_lin->cth[ipix] = cth;
1341 ctht_lin->ctp[ipix] = ctp;
1342 ctht_lin->ctt[ipix] = ctt;
1344 ctht_lin->iscan =
l1rec->iscan;
1345 ctht_lin->npix =
l1rec->npix;
1350 ctht_lut_str *full_lut_wtr, ctht_lut_str *full_lut_ice )
1362 int ncid, dim_loc, varid;
1364 char unc_file[FILENAME_MAX], lut_wtr_file[FILENAME_MAX], lut_ice_file[FILENAME_MAX];
1365 char tit_unc[] = {
"Forward model uncertainty coefficients for cloud top pressure retrieval from oci" };
1366 char tit_lut[] = {
"LUT for cloud top pressure retrieval from PACE OCI" };
1367 ctht_lut_str *lut_ptr;
1369 int32_t ilut, ilev, ihgt, close_ind;
1370 float pstep, frac_ind;
1373 if ((env_arr = getenv(
"OCDATAROOT")) ==
NULL) {
1374 printf(
"-E- %s, %d: Error looking up environmental variable OCDATAROOT\n", __FILE__, __LINE__ );
1377 strcpy( unc_file, env_arr );
1378 strcat( unc_file,
"/cloud/" );
1380 strcat( unc_file,
"/" );
1381 strcpy( lut_wtr_file, unc_file );
1382 strcpy( lut_ice_file, unc_file );
1384 strcat( unc_file,
"oci_fm_err_fits_lut_v17_sim_v5.nc" );
1385 strcat( lut_wtr_file,
"oci_o2_cth_lut_v17_water.nc" );
1386 strcat( lut_ice_file,
"oci_o2_cth_lut_v17_column_8elements.nc" );
1389 printf(
"%s, %d: I: Reading cloud top uncertainty file: %s\n",
1390 __FILE__, __LINE__, unc_file );
1391 DPTB( nc_open( unc_file, 0, &ncid ) );
1394 size_t attlen = 0, dim_len;
1395 DPTB( nc_inq_attlen(ncid, NC_GLOBAL,
"description", &attlen) );
1396 char **string_attr = (
char**)malloc(attlen *
sizeof(
char*));
1397 memset(string_attr, 0, attlen *
sizeof(
char*));
1398 DPTB( nc_get_att_string(ncid, NC_GLOBAL,
"description", string_attr) );
1400 if( strncmp( string_attr[0], tit_unc, strlen(tit_unc) ) != 0 )
1402 printf(
"%s, %d: E - Description of ctht uncertainty file is bad\n",
1403 __FILE__, __LINE__ );
1407 DPTB( nc_inq_dimid( ncid,
"nphase", &dim_loc ) );
1408 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1409 unc->nphase = dim_len;
1411 DPTB( nc_inq_dimid( ncid,
"nsurf", &dim_loc ) );
1412 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1413 unc->nsurf = dim_len;
1415 DPTB( nc_inq_dimid( ncid,
"nbands", &dim_loc ) );
1416 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1417 unc->nbands = dim_len;
1422 DPTB( nc_inq_varid( ncid,
"icpts", &varid ) );
1423 unc->icpts = (
float *)malloc( unc->nphase * unc->nsurf * unc->nbands *
1425 DPTB( nc_get_var_float( ncid, varid, unc->icpts ) );
1427 DPTB( nc_inq_varid( ncid,
"grads", &varid ) );
1428 unc->grads = (
float *)malloc( unc->nphase * unc->nsurf * unc->nbands *
1430 DPTB( nc_get_var_float( ncid, varid, unc->grads ) );
1432 DPTB( nc_inq_varid( ncid,
"fit_rms", &varid ) );
1433 unc->fit_rms = (
float *)malloc( unc->nphase * unc->nsurf * unc->nbands *
1435 DPTB( nc_get_var_float( ncid, varid, unc->fit_rms ) );
1437 DPTB( nc_inq_varid( ncid,
"err_corrs_category", &varid ) );
1438 unc->err_corrs_category = (
float *)malloc( unc->nphase * unc->nsurf *
1439 unc->nbands * unc->nbands *
sizeof(
float) );
1440 DPTB( nc_get_var_float( ncid, varid, unc->err_corrs_category ) );
1441 DPTB( nc_close( ncid ) );
1445 for( ilut = 0; ilut < 2; ilut++ )
1447 lut_ptr = ( ilut == 0 ) ? full_lut_wtr : full_lut_ice;
1448 lut_fil = ( ilut == 0 ) ? lut_wtr_file : lut_ice_file;
1450 printf(
"%s, %d: I: Reading cloud top phase LUT file: %s\n",
1451 __FILE__, __LINE__, lut_fil );
1452 DPTB( nc_open( lut_fil, 0, &ncid ) );
1454 size_t attlen = 0, dim_len;
1455 DPTB( nc_inq_attlen(ncid, NC_GLOBAL,
"description", &attlen) );
1456 char **string_attr = (
char**)malloc(attlen *
sizeof(
char*));
1457 memset(string_attr, 0, attlen *
sizeof(
char*));
1458 DPTB( nc_get_att_string(ncid, NC_GLOBAL,
"description", string_attr) );
1460 if( strncmp( string_attr[0], tit_lut, strlen(tit_lut) ) != 0 )
1462 printf(
"%s, %d: E - Description of ctht lut file is bad\n",
1463 __FILE__, __LINE__ );
1464 printf(
"For file: %s\n", lut_fil );
1468 DPTB( nc_inq_dimid( ncid,
"nsza", &dim_loc ) );
1469 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1470 lut_ptr->lut_nsolz = dim_len;
1472 DPTB( nc_inq_dimid( ncid,
"nvza", &dim_loc ) );
1473 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1474 lut_ptr->lut_nsenz = dim_len;
1476 DPTB( nc_inq_dimid( ncid,
"nazi", &dim_loc ) );
1477 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1478 lut_ptr->lut_nrelaz = dim_len;
1480 DPTB( nc_inq_dimid( ncid,
"nlevs", &dim_loc ) );
1481 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1482 lut_ptr->lut_nlev = dim_len;
1484 DPTB( nc_inq_dimid( ncid,
"npress", &dim_loc ) );
1485 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1486 lut_ptr->lut_npress = dim_len;
1488 DPTB( nc_inq_dimid( ncid,
"nalb", &dim_loc ) );
1489 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1490 lut_ptr->lut_nalb = dim_len;
1492 DPTB( nc_inq_dimid( ncid,
"ncod", &dim_loc ) );
1493 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1494 lut_ptr->lut_ncod = dim_len;
1496 DPTB( nc_inq_dimid( ncid,
"naod", &dim_loc ) );
1497 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1498 lut_ptr->lut_naod = dim_len;
1500 DPTB( nc_inq_dimid( ncid,
"nbands", &dim_loc ) );
1501 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1502 lut_ptr->lut_nwave = dim_len;
1504 DPTB( nc_inq_dimid( ncid,
"ncth", &dim_loc ) );
1505 DPTB( nc_inq_dimlen( ncid, dim_loc, &dim_len ) );
1506 lut_ptr->lut_ncth = dim_len;
1512 if( lut_ptr->lut_nwave !=
nband )
1514 printf(
"%s, %d: E: CTHT LUT # bands != expected #: %d\n", __FILE__, __LINE__,
nband );
1518 lut_ptr->lut_solz = (
float *) malloc( lut_ptr->lut_nsolz *
sizeof(
float) );
1519 lut_ptr->lut_senz = (
float *) malloc( lut_ptr->lut_nsenz *
sizeof(
float) );
1520 lut_ptr->lut_relaz = (
float *) malloc( lut_ptr->lut_nrelaz *
sizeof(
float) );
1521 if( ( lut_ptr->lut_solz ==
NULL ) || ( lut_ptr->lut_senz ==
NULL ) || ( lut_ptr->lut_relaz ==
NULL ) )
1523 printf(
"%s, %d: E: unable to allocate lut_solz, lut_senz, or lut_relaz\n", __FILE__, __LINE__ );
1526 DPTB( nc_inq_varid( ncid,
"sza", &varid ) );
1527 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_solz ) );
1529 DPTB( nc_inq_varid( ncid,
"vza", &varid ) );
1530 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_senz) );
1532 DPTB( nc_inq_varid( ncid,
"azi", &varid ) );
1533 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_relaz ) );
1535 lut_ptr->lut_press = (
float *) malloc( lut_ptr->lut_npress *
sizeof(
float) );
1536 lut_ptr->lut_alb = (
float *) malloc( lut_ptr->lut_nalb *
sizeof(
float) );
1537 lut_ptr->lut_cod = (
float *) malloc( lut_ptr->lut_ncod *
sizeof(
float) );
1538 lut_ptr->lut_lcod = (
float *) malloc( lut_ptr->lut_ncod *
sizeof(
float) );
1539 if( ( lut_ptr->lut_press ==
NULL ) || ( lut_ptr->lut_alb ==
NULL ) || ( lut_ptr->lut_cod ==
NULL ) )
1541 printf(
"%s, %d: E: unable to allocate lut_press, lut_alb, or lut_cod\n", __FILE__, __LINE__ );
1544 DPTB( nc_inq_varid( ncid,
"spress", &varid ) );
1545 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_press ) );
1547 DPTB( nc_inq_varid( ncid,
"cod", &varid ) );
1548 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_cod ) );
1550 DPTB( nc_inq_varid( ncid,
"lcod", &varid ) );
1551 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_lcod ) );
1553 DPTB( nc_inq_varid( ncid,
"salb", &varid ) );
1554 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_alb ) );
1556 lut_ptr->lut_aod = (
float *) malloc( lut_ptr->lut_naod *
sizeof(
float) );
1557 lut_ptr->lut_wave = (
float *) malloc( lut_ptr->lut_nwave *
sizeof(
float) );
1558 lut_ptr->lut_cth = (
float *) malloc( lut_ptr->lut_ncth *
sizeof(
float) );
1559 if( ( lut_ptr->lut_aod ==
NULL ) || ( lut_ptr->lut_wave ==
NULL ) || ( lut_ptr->lut_cth ==
NULL ) )
1561 printf(
"%s, %d: E: unable to allocate lut_aod, lut_wave, or lut_cth\n", __FILE__, __LINE__ );
1564 DPTB( nc_inq_varid( ncid,
"aod", &varid ) );
1565 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_aod ) );
1567 DPTB( nc_inq_varid( ncid,
"band_wavelengths", &varid ) );
1568 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_wave ) );
1570 DPTB( nc_inq_varid( ncid,
"cth", &varid ) );
1571 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut_cth ) );
1575 lut_ptr->z_and_pp0 = (
float *) malloc( lut_ptr->lut_nlev * 2 *
sizeof(
float) );
1576 lut_ptr->lut_pp0 = (
float *) malloc( lut_ptr->lut_ncth *
sizeof(
float ) );
1577 lut_ptr->lut = (
float *) malloc( lut_ptr->lut_nrelaz * lut_ptr->lut_nsenz
1578 * lut_ptr->lut_nsolz * lut_ptr->lut_npress * lut_ptr->lut_nwave *
1579 lut_ptr->lut_ncod * lut_ptr->lut_ncth * lut_ptr->lut_nalb *
1581 if( ( lut_ptr->z_and_pp0 ==
NULL ) || ( lut_ptr->lut ==
NULL ) ||
1582 ( lut_ptr->lut_pp0 ==
NULL ) )
1584 printf(
"%s, %d: E: unable to allocate lut_z_and_pp0, lut_pp0, or lut\n",
1585 __FILE__, __LINE__ );
1588 DPTB( nc_inq_varid( ncid,
"z_and_pp0", &varid ) );
1589 DPTB( nc_get_var_float( ncid, varid, lut_ptr->z_and_pp0 ) );
1591 DPTB( nc_inq_varid( ncid,
"lut", &varid ) );
1592 DPTB( nc_get_var_float( ncid, varid, lut_ptr->lut ) );
1594 DPTB( nc_close( ncid ) );
1598 float *and_pp0 = (
float *) malloc( lut_ptr->lut_nlev *
sizeof(
float) );
1599 for( ihgt = 0; ihgt < lut_ptr->lut_nlev; ihgt++ )
1600 and_pp0[ihgt] = lut_ptr->z_and_pp0[ 1 + 2 * ihgt ];
1601 for( ilev = 0; ilev < lut_ptr->lut_ncth; ilev++ )
1604 lut_ptr->lut_pp0[ilev] =
BAD_FLT;
1606 for( ihgt = 0; ihgt < lut_ptr->lut_nlev; ihgt++ )
1608 if( lut_ptr->z_and_pp0[ ihgt * 2 ] < lut_ptr->lut_cth[ilev] )
1610 close_ind = ( ihgt == 0 ) ? 0 : ihgt - 1;
1614 if( close_ind < lut_ptr->lut_nlev - 1 )
1615 pstep = lut_ptr->z_and_pp0[ ( close_ind + 1 ) * 2 ] -
1616 lut_ptr->z_and_pp0[ close_ind * 2 ];
1618 pstep = lut_ptr->z_and_pp0[ close_ind * 2 ] -
1619 lut_ptr->z_and_pp0[ ( close_ind - 1 ) * 2 ];
1620 frac_ind = close_ind + ( lut_ptr->lut_cth[ilev] -
1621 lut_ptr->z_and_pp0[ close_ind * 2 ] ) / pstep;
1622 lut_ptr->lut_pp0[ilev] =
axis_interp( and_pp0, lut_ptr->lut_nlev,
1633 #define DTOR PI / 180.
1653 double cos_two_omega, cos_beta;
1654 double a1,
b1, c1, d1, rgl, wprime, ref_coef;
1656 double zxprime, zyprime, sigx, sigy, w;
1657 double zeta, eta, p_gram, zx, zy;
1658 double dsenz, dsolz, drelaz;
1660 int32_t do_gordon = 1;
1664 drelaz = ( do_gordon ) ? ( 180. - (
double)relaz ) *
DTOR :
1665 (
double)relaz *
DTOR;
1675 zx = ( -1.0 * sin(dsenz) * sin(drelaz) ) / ( cos(dsolz) + cos(dsenz) );
1676 zy = ( sin(dsolz) + sin(dsenz) * cos( drelaz ) ) /
1677 ( cos(dsolz) + cos(dsenz) );
1685 sigx = sqrt( 0.003 + 0.00192 * windsp );
1686 sigy = sqrt( 0.00316 * windsp );
1688 zeta = zxprime / sigx;
1689 eta = zyprime / sigy;
1691 p_gram = ( 1. / ( 2. *
PI * sigx * sigy ) ) *
1692 exp( -0.5 * ( pow( zeta, 2. ) + pow( eta, 2. ) ) );
1698 cos_two_omega = cos(dsenz) * cos(dsolz) +
1699 sin(dsenz) * sin( dsolz ) * cos(drelaz);
1700 cos_beta = ( cos(dsolz) + cos(dsenz) ) / ( sqrt( 2 + 2 * cos_two_omega ) );
1701 w = ( cos_two_omega >= 1. ) ? 0. : 0.5 * acos( cos_two_omega );
1704 wprime = asin( 1.00029 * sin(w) / nr );
1705 a1 = sin( w - wprime );
1706 b1 = sin( w + wprime );
1707 c1 = tan( w - wprime );
1708 d1 = tan( w + wprime );
1710 if( (
b1 == 0 ) || ( d1 == 0 ) )
1713 ref_coef = 0.5 * ( ( a1 * a1 ) / (
b1 *
b1 ) + ( c1 * c1 ) / ( d1 * d1 ) );
1715 rgl =
PI * p_gram * ref_coef / ( 4. * cos( dsolz ) * cos( dsenz ) *
1716 ( pow( cos_beta, 4. ) ) );
1730 gsl_permutation *
p = gsl_permutation_alloc(
size);
1731 gsl_matrix *gm_work;
1735 gm_work = gsl_matrix_alloc(
size,
size );
1736 gsl_matrix_memcpy( gm_work,
matrix );
1739 gsl_linalg_LU_decomp(gm_work,
p, &
s);
1742 gsl_matrix *inv = gsl_matrix_alloc(
size,
size);
1743 gsl_linalg_LU_invert(gm_work,
p, inv);
1745 gsl_permutation_free(
p);
1746 gsl_matrix_free( gm_work );
1759 printf(
"%d rows, %d columns\n", nrow, ncol );
1760 for (
i = 0;
i < nrow; ++
i) {
1761 for (
j = 0;
j < ncol; ++
j) {
1762 element = gsl_matrix_get(
matrix,
i,
j);
1763 printf(
"%f ", element);