ocssw  1.0
/disk01/web/ocssw/build/src/l2gen/water.c (r8099/r2592)
Go to the documentation of this file.
00001 /* ============================================================================================== */
00002 /* module water.c - functions to read and return absorption and backscatter of pure sea water     */
00003 /*                                                                                                */
00004 /* B. Franz, NASA/GSFC Ocean Color Discipline Processing Group, Sep. 2004                         */
00005 /*                                                                                                */
00006 /* ============================================================================================== */
00007 
00008 #include "l12_proto.h"
00009 
00010 #define MINAWTAB 200
00011 #define MAXAWTAB 2249
00012 #define INTAWTAB 1
00013 #define NAWTAB  ((MAXAWTAB-MINAWTAB)/INTAWTAB + 1)
00014 
00015 static double awtab [NAWTAB];
00016 static double bbwtab[NAWTAB];
00017 static int32_t   ntab   = NAWTAB;
00018 static int    min_wl = MINAWTAB;
00019 static int    del_wl = INTAWTAB;
00020 
00021 
00022 /* ---------------------------------------------------------------------------------------------- */
00023 /* read_water_spectra() - called once to load look-up table static arrays                         */
00024 /* ---------------------------------------------------------------------------------------------- */
00025 void read_water_spectra(void) 
00026 {
00027     static int firstCall = 1;
00028 
00029     FILE *fp;
00030     char *filedir;
00031     char  filename[FILENAME_MAX];
00032     char  line [80];
00033     int32_t  i, imin, imax;
00034     int32_t  j;
00035     float wl,aw,bw;
00036 
00037     if (!firstCall) return;
00038 
00039     if ((filedir = getenv("OCDATAROOT")) == NULL) {
00040         printf("-E- %s: OCDATAROOT env variable undefined.\n", __FILE__);
00041         exit(1);
00042     }
00043     strcpy(filename, filedir);
00044     strcat(filename, "/common/water_spectra.dat");
00045 
00046     if ( (fp = fopen(filename,"r")) == NULL ) {
00047         fprintf(stderr,
00048                 "-E- %s line %d: unable to open %s for reading\n",__FILE__,__LINE__,filename);
00049         exit(1);
00050     }
00051 
00052     i=0;
00053     while (i < ntab) {
00054         if ( fgets( line, 80, fp ) == NULL ) {
00055             fprintf(stderr,"-E- %s line %d: error reading %s at line %d\n",__FILE__,__LINE__,filename,i);
00056             exit(1);
00057         }
00058         if (line[0] != '/' && line[0] !='!') {
00059             sscanf(line,"%f %f %f",&wl,&aw,&bw);
00060             awtab [i] = aw;
00061             bbwtab[i] = bw/2.0;
00062         i++;
00063     }
00064     }
00065 
00066     firstCall=0;
00067 }
00068 
00069 
00070 /* ---------------------------------------------------------------------------------------------- */
00071 /* aw_spectra() - returns water absorption at wavelength, wl, averaged over bandwidth, width      */
00072 /* ---------------------------------------------------------------------------------------------- */
00073 float aw_spectra(int32_t wl, int32_t width)
00074 {
00075     static int firstCall = 1;
00076 
00077     int32_t  itab = (wl - min_wl)/del_wl;
00078     int32_t  imin = MAX(itab - width/2/del_wl,    0);
00079     int32_t  imax = MIN(itab + width/2/del_wl, ntab);    
00080     float aw   = 0;
00081     int32_t  i;
00082 
00083     if (firstCall) {
00084         read_water_spectra();
00085     firstCall = 0;
00086     }
00087 
00088     if (itab < 0) {
00089         fprintf(stderr,
00090                 "-W- %s line %d: wavelength of %d outside aw table range.\n",__FILE__,__LINE__,wl);
00091         itab=0;
00092     } else if (itab > ntab-1) {
00093         fprintf(stderr,
00094                 "-W- %s line %d: wavelength of %d outside aw table range.\n",__FILE__,__LINE__,wl);
00095         itab=ntab-1;
00096     }
00097 
00098     for (i=imin; i<=imax; i++)
00099         aw += (float) awtab[i];
00100 
00101     aw /= (imax-imin+1);
00102 
00103     return(aw);
00104 }
00105     
00106 
00107 /* ---------------------------------------------------------------------------------------------- */
00108 /* bbw_spectra() - returns water backscatter at wavelength, wl, averaged over bandwidth, width    */
00109 /* ---------------------------------------------------------------------------------------------- */
00110 float bbw_spectra(int32_t wl, int32_t width)
00111 {
00112     static int firstCall = 1;
00113 
00114     int32_t  itab = (wl - min_wl)/del_wl;
00115     int32_t  imin = MAX(itab - width/2/del_wl,    0);
00116     int32_t  imax = MIN(itab + width/2/del_wl, ntab);    
00117     float bbw  = 0;
00118     int32_t  i;
00119 
00120     if (firstCall) {
00121         read_water_spectra();
00122     firstCall = 0;
00123     }
00124 
00125     if (itab < 0) {
00126         fprintf(stderr,
00127                 "-W- %s line %d: wavelength of %d outside bbw table range.\n",__FILE__,__LINE__,wl);
00128         itab=0;
00129     } else if (itab > ntab-1) {
00130         fprintf(stderr,
00131                 "-W- %s line %d: wavelength of %d outside bbw table range.\n",__FILE__,__LINE__,wl);
00132         itab=ntab-1;
00133     }
00134 
00135     for (i=imin; i<=imax; i++)
00136         bbw += (float) bbwtab[i];
00137 
00138     bbw /= (imax-imin+1);
00139 
00140     return(bbw);
00141 }
00142     
00143 
00144 /* ---------------------------------------------------------------------------------------------- */
00145 /* returns aw and bbw at specified "sensor" wavelengths, appropriate to the derived nLw           */
00146 /* ---------------------------------------------------------------------------------------------- */
00147 void get_aw_bbw(l2str *l2rec,float wave[],int nwave,float *aw,float *bbw)
00148 {
00149     int ib, iw;
00150 
00151     if (l2rec->input->outband_opt >= 2) {
00152         for (ib=0; ib<nwave; ib++) {
00153             aw [ib] = aw_spectra (wave[ib],BANDW);
00154             bbw[ib] = bbw_spectra(wave[ib],BANDW);
00155         }
00156     } else {
00157         float *senaw;
00158         float *senbbw;
00159         rdsensorinfo(l2rec->sensorID,l2rec->input->evalmask,"aw", (void **) &senaw );
00160         rdsensorinfo(l2rec->sensorID,l2rec->input->evalmask,"bbw",(void **) &senbbw);
00161         for (ib=0; ib<nwave; ib++) {
00162         iw = bindex_get(wave[ib]);
00163             aw [ib] = senaw [iw];
00164             bbw[ib] = senbbw[iw];
00165     }
00166     }
00167 }