28 if( (
l1rec->cld_dat = malloc(
sizeof( cld_struc ) ) ) ==
NULL )
30 printf(
"-E- %s, %d: Failure to allocate cloud albedo data structure\n",
34 if( ( (
l1rec->cld_dat->sfc_albedo_659 = (
float *)
35 malloc(
npix *
sizeof(
float ) ) ) ==
NULL ) ||
36 ( (
l1rec->cld_dat->sfc_albedo_858 = (
float *)
37 malloc(
npix *
sizeof(
float ) ) ) ==
NULL ) ||
38 ( (
l1rec->cld_dat->sfc_albedo_1240 = (
float *)
39 malloc(
npix *
sizeof(
float ) ) ) ==
NULL ) ||
40 ( (
l1rec->cld_dat->sfc_albedo_1640 = (
float *)
41 malloc(
npix *
sizeof(
float ) ) ) ==
NULL ) ||
42 ( (
l1rec->cld_dat->sfc_albedo_2130 = (
float *)
43 malloc(
npix *
sizeof(
float ) ) ) ==
NULL ) )
45 printf(
"-E- %s, %d: Failure to allocate cloud albedo data structures\n",
52 int read_albedo( int32_t d_np, int32_t d_nl, int32_t st_lin, int32_t ix_clim,
53 int ncid,
int *d_id,
unsigned char ***alb_dat )
81 int32_t iwav, i_np, i_nl;
84 unsigned char *xfr_arr, **alb_lcl;
88 if( alb_lcl[0] !=
NULL )
90 for( iwav = 0; iwav < 5; iwav++)
91 free( alb_lcl[iwav] );
94 if( ( xfr_arr = (
unsigned char *) malloc( d_np * d_nl *
95 sizeof(
unsigned char ) ) ) ==
NULL )
97 printf(
"-E- %s, %d: Error allocating albedo read storage\n",
106 for( iwav = 0; iwav < 5; iwav++)
108 if( ( alb_lcl[iwav] = (
unsigned char *)
109 malloc( i_np * i_nl *
sizeof(
unsigned char ) ) ) ==
NULL )
111 printf(
"-E- %s, %d: Error allocating albedo read storage\n",
112 __FILE__, __LINE__ );
134 if( nc_get_vara_uchar( ncid, d_id[iwav],
start,
count, xfr_arr ) !=
137 printf(
"-E- %s, %d: Error reading albedo\n",
138 __FILE__, __LINE__ );
143 for( ilat = 0; ilat < d_nl; ilat++ )
145 memcpy( ( alb_lcl[iwav] + ilat * i_np + 1 ),
146 ( xfr_arr + ilat * d_np ), d_np );
148 *( alb_lcl[iwav] + ilat * i_np ) =
149 *( xfr_arr + ilat * d_np + ( d_np - 1 ) );
152 memcpy( ( alb_lcl[iwav] + ( i_nl - 1 ) * i_np ),
153 ( alb_lcl[iwav] + ( i_nl - 2 ) * i_np ), i_np );
184 static int32_t firstcall = 0, is_clim;
185 static int32_t subset_read = 0, scan_npix;
186 static float bdy_siz = 5.;
187 static float scale[5];
188 static float lat_min, lon_min, res1, res2;
189 static double lat_res, lon_res, alb_st_lat, alb_lat_rng[2] = { 900., -900. };
190 static unsigned char **alb_dat, fillv[5];
191 static int32_t ix_clim = -1, sub_nl;
192 static size_t d_np, d_nl, d_nt, i_np;
193 int32_t iwav, ipx, sub_st, good_nav, ix_lat, ix_lon;
194 int32_t n_done, ip, il;
195 float dat_sub[2][2], fin_val, sum;
196 float lat_rng[2] = { 900., -900. };
198 double sec, frac_lat, frac_lon,
lat,
lon;
200 char tit_clim[] =
"MODIS/TERRA+Aqua BRDF/Albedo Gap-Filled Snow-Free 8-Day climatology";
201 char tit_daily[] =
"MODIS/TERRA+Aqua BRDF/Albedo Gap-Filled Snow-Free data";
202 char str_tit[FILENAME_MAX];
203 static int ncid, d_id[5], dim_id, dim_id2;
206 char *alb_wav_names[5] = {
"Albedo_Map_0.659",
"Albedo_Map_0.858",
207 "Albedo_Map_1.24",
"Albedo_Map_1.64",
"Albedo_Map_2.13" };
213 alb_file =
input->sfc_albedo;
215 if (nc_open(alb_file, 0, &ncid) != NC_NOERR) {
217 "-E- %s %d: file: %s is not netcdf, not acceptable albedo file\n",
218 __FILE__, __LINE__, alb_file);
222 nc_get_att_text(ncid, NC_GLOBAL,
"title", str_tit);
223 if( strncmp( str_tit, tit_daily, strlen(tit_daily) ) == 0 )
225 printf(
"%s, %d: TEMP: we found the daily file: %s\n",
226 __FILE__, __LINE__, str_tit );
229 else if( strncmp( str_tit, tit_clim, strlen(tit_clim) ) == 0 )
231 printf(
"%s, %d: TEMP: we found the clim file: %s\n", __FILE__,
236 ix_clim = (
doy - 1 ) / 8;
237 printf(
"-I- %s, %d: Using climate doy # %d, index: %d\n", __FILE__,
238 __LINE__,
doy, ix_clim );
242 printf(
"-E- %s, %d: Incorrect title in albedo dataset: %s\n",
243 __FILE__, __LINE__, alb_file);
248 if( ( nc_get_att_float(ncid, NC_GLOBAL,
"geospatial_lat_min", &lat_min )
250 ( nc_get_att_float(ncid, NC_GLOBAL,
"geospatial_lon_min", &lon_min )
253 printf(
"-E- %s, %d: Unable to read geolocation information for albedo file: %s\n",
254 __FILE__, __LINE__, alb_file);
260 nc_inq_atttype( ncid, NC_GLOBAL,
"geospatial_lat_resolution", &att_typ );
261 if( att_typ == NC_FLOAT ) {
262 nc1 = nc_get_att_float(ncid, NC_GLOBAL,
"geospatial_lat_resolution",
265 nc1 = nc_get_att(ncid, NC_GLOBAL,
"geospatial_lat_resolution",
271 nc_inq_atttype( ncid, NC_GLOBAL,
"geospatial_lon_resolution", &att_typ );
272 if( att_typ == NC_FLOAT ) {
273 nc2 = nc_get_att_float(ncid, NC_GLOBAL,
"geospatial_lon_resolution",
276 nc1 = nc_get_att(ncid, NC_GLOBAL,
"geospatial_lon_resolution",
281 if( ( nc1 != NC_NOERR ) || ( nc2 != NC_NOERR ) )
283 printf(
"-E- %s, %d: Unable to read geolocation information for albedo file: %s\n",
284 __FILE__, __LINE__, alb_file);
291 scan_npix =
l1rec->npix;
293 if ((nc_inq_dimid(ncid,
"lat", &dim_id) != NC_NOERR) ||
294 (nc_inq_dimid(ncid,
"lon", &dim_id2) != NC_NOERR) )
296 printf(
"-E- %s, %d: Error retrieving the lat/lon size\n",
297 __FILE__, __LINE__ );
300 if ( ( nc_inq_dimlen(ncid, dim_id, &d_nl ) != NC_NOERR) ||
301 ( nc_inq_dimlen(ncid, dim_id2, &d_np ) != NC_NOERR) )
303 printf(
"-E- %s, %d: Error retrieving the lat/lon size\n",
304 __FILE__, __LINE__ );
309 if (nc_inq_dimid(ncid,
"doy", &dim_id) != NC_NOERR)
311 printf(
"-E- %s, %d: Error retrieving the doy size\n",
312 __FILE__, __LINE__ );
315 if ( nc_inq_dimlen(ncid, dim_id, &d_nt ) != NC_NOERR)
317 printf(
"-E- %s, %d: Error retrieving the doy size\n",
318 __FILE__, __LINE__ );
324 alb_dat = (
unsigned char **) malloc( 5 *
sizeof(
unsigned char * ) );
330 if( (
long)d_np * (long)d_nl < 4000000 ) subset_read = 0;
333 for( iwav = 0; iwav < 5; iwav++ )
337 if( nc_inq_varid(ncid, alb_wav_names[iwav], ( d_id + iwav ) ) != NC_NOERR)
339 printf(
"-E- %s, %d: Error retrieving the band id\n",
340 __FILE__, __LINE__ );
344 if( ( nc_get_att_uchar( ncid, d_id[iwav],
"_FillValue",
345 ( fillv + iwav ) ) != NC_NOERR ) ||
346 ( nc_get_att_float( ncid, d_id[iwav],
"scale_factor",
347 (
scale + iwav ) ) != NC_NOERR ) )
349 printf(
"-E- %s, %d: Error retrieving the fill or scale value\n",
350 __FILE__, __LINE__ );
354 if( subset_read == 0 )
356 if(
read_albedo( d_np, d_nl, 0, ix_clim, ncid, d_id, &alb_dat ) != 0 )
374 if( subset_read == 1 )
377 for( ipx = 0; ipx < scan_npix; ipx++ )
380 if( (
lat >= -90. ) && (
lat <= 90. ) )
382 if(
lat < lat_rng[0] ) lat_rng[0] =
lat;
383 if(
lat > lat_rng[1] ) lat_rng[1] =
lat;
387 if( ( alb_lat_rng[0] > lat_rng[0] ) || ( alb_lat_rng[1] < lat_rng[1] ) )
390 alb_lat_rng[0] = lat_rng[0] - bdy_siz;
391 if( alb_lat_rng[0] < -90. )
392 alb_lat_rng[0] = -90.;
395 sub_st = ( alb_lat_rng[0] + 90. ) / lat_res;
396 alb_lat_rng[0] = ( sub_st * lat_res ) - 90.;
398 alb_lat_rng[1] = lat_rng[1] + bdy_siz;
399 if( alb_lat_rng[1] > 90. )
400 alb_lat_rng[1] = 90.;
402 sub_st = ( alb_lat_rng[1] + 90. ) / lat_res;
403 alb_lat_rng[1] = ( sub_st * lat_res ) - 90.;
405 alb_st_lat = alb_lat_rng[0];
409 printf(
"Latitude: %f - %f\n\n\n",
410 alb_lat_rng[0], alb_lat_rng[1] );
411 sub_nl = ( alb_lat_rng[1] - alb_lat_rng[0] + lat_res/2. ) / lat_res;
412 sub_st = ( alb_st_lat + 90. ) / lat_res;
413 if(
read_albedo( d_np, sub_nl, sub_st, ix_clim, ncid, d_id,
419 for( ipx = 0; ipx < scan_npix; ipx++ )
426 if( (
lat >= -90. ) && (
lat <= 90. )
427 && (
lon >= -180. ) && (
lon <= 180. ) )
430 frac_lat = (
lat - alb_st_lat ) / lat_res;
431 frac_lon = (
lon + 180 ) / lon_res;
432 ix_lat = (int32_t) frac_lat;
433 ix_lon = (int32_t) frac_lon;
434 frac_lat = frac_lat - (
float) ix_lat;
435 frac_lon = frac_lon - (
float) ix_lon;
438 for( iwav = 0; iwav < 5; iwav++ )
445 for( ip = 0; ip < 2; ip++ )
447 for( il = 0; il < 2; il++ )
449 if( ix_lat + il >= sub_nl )
450 dat_sub[ip][il] = fillv[iwav];
452 dat_sub[ip][il] = *( alb_dat[iwav] + ( ix_lon + ip ) +
453 i_np * ( ix_lat + il ) );
454 if( dat_sub[ip][il] != fillv[iwav] )
456 sum += dat_sub[ip][il] *
scale[iwav];
468 for( ip = 0; ip < 2; ip++ )
469 for( il = 0; il < 2; il++ )
471 if( dat_sub[ip][il] == fillv[iwav] )
472 dat_sub[ip][il] = sum;
474 dat_sub[ip][il] *=
scale[iwav];
477 fin_val = ( 1 - frac_lon ) * ( ( dat_sub[0][0] * ( 1 - frac_lat ) ) +
478 ( dat_sub[0][1] * frac_lat ) ) +
479 frac_lon * ( ( dat_sub[1][0] * ( 1 - frac_lat ) ) +
480 ( dat_sub[1][1] * frac_lat ) );
486 case 0:
l1rec->cld_dat->sfc_albedo_659[ipx] = fin_val;
488 case 1:
l1rec->cld_dat->sfc_albedo_858[ipx] = fin_val;
490 case 2:
l1rec->cld_dat->sfc_albedo_1240[ipx] = fin_val;
492 case 3:
l1rec->cld_dat->sfc_albedo_1640[ipx] = fin_val;
494 case 4:
l1rec->cld_dat->sfc_albedo_2130[ipx] = fin_val;