ocssw  1.0
/disk01/web/ocssw/build/src/l2gen/msl2val_hdf.c (r10038/r9763)
Go to the documentation of this file.
00001 /* ========================================================================
00002  *  Procedures create, read, and write HDF msvalidation pixel data
00003  *
00004  * int msval_append(char *valfile, msvalstr valstr) - creates HDF, if
00005  *          it does not exist already, and appends data
00006  * int msval_read(char *valfile, int32_t subsmpl, msvalstr *valstr) - 
00007  *          reads subsampled pixel validation data and
00008  *          allocates memory and data to the valstr structure
00009  * int msval_add(char *valfile, int32_t subsmpl, msvalstr *valstr, int32_t *npixs, int32_t *ngranuls) 
00010  *                      reads validation data from an hdf file,
00011  *                  subsamples if needed, and adds pixel and granule
00012  *                  data to an already existing valstr structure
00013  *
00014  *     Programmer     Organization      Date       Description of change
00015  *   --------------   ------------    --------     ---------------------
00016  *   Ewa Kwiatkowska   SAIC       13 January 2004    Original development
00017  *   Ewa Kwiatkowska   SAIC       20 October 2004    Introduced user defined list
00018  *                           of L2 and L3 products
00019  *                           All L2 and L3 products are float32
00020  *                           except for L2 l2_flags, mside, detnum, pixnum
00021  *   Ewa Kwiatkowska   SAIC       7 November 2007    Added nscenes, nobs product
00022  *   Joel Gales        Futurtech  3 November 2012    Add ds_id.fftype
00023  *
00024  * ======================================================================== */
00025 
00026 
00027 #include <stdio.h>
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #include <stdlib.h>
00031 #include <fcntl.h>
00032 #include <string.h>
00033 #include <errno.h>
00034 #include <unistd.h>
00035 #include <libgen.h>
00036 #include <math.h>
00037 
00038 #include "l12_proto.h"
00039 #include "passthebuck.h"
00040 #include "l2prod_struc.h"
00041 #include "filehandle.h"
00042 #include "hdf_utils.h"
00043 #include "mfhdf.h"
00044 #include "msl2val_struc.h"
00045 
00046 
00047 int rdstrideSDS(int32 fileID, char *sdsname, int32 stride[2], VOIDP array_data);
00048 int read_glbl_attr(int32 sd_id, char *name, VOIDP ptr);
00049 int msval_read(char *valfile, int32_t subsmpl, msvalstr *valstr);
00050 int msval_npixs(char *valfile, int32_t subsmpl, int32_t *sensorID, int32_t *npixs, int32_t *ngranuls);
00051 int msval_create(char *valfile, int32 *sd_id, msvalstr valstr, int32_t npixs);
00052 int msval_readblocks(char *valfile, int32 *sd_id, int32_t *spix, int32_t *totalpixs, msvalstr *valstr);
00053 int msval_writeblocks(char *valfile, int32 sd_id, int32_t *spix, msvalstr valstr, int32_t nfiles);
00054 
00055 
00056 
00057 
00058 int msval_create(char *valfile, int32 *sd_id, msvalstr valstr, int32_t npixs)
00059 {
00060 
00061     char title[255], *ch;
00062     l2prodstr p, *ptr;
00063     int i;
00064     
00065     
00066 
00067     sprintf(title, "%s Level-2 validation pixels", sensorName[valstr.l2sensorID]);
00068 
00069     
00070     *sd_id = SDstart(valfile, DFACC_CREATE);
00071     if (*sd_id == FAIL) {
00072        printf("-E- %s line %d: Could not create HDF file, %s .\n",__FILE__,__LINE__,valfile);
00073        return(HDF_FUNCTION_ERROR);
00074     }   
00075     
00076     /*                                                                  */
00077     /* Create the pixel SDSes                                           */
00078     /* ---------------------------------------------------------------- */
00079     /*                                                                  */
00080     idDS ds_id;
00081     ds_id.deflate = 0;
00082     ds_id.fid = *sd_id;
00083     ds_id.fftype = DS_HDF;
00084 
00085     PTB( SetChrGA(ds_id, "title", title) );
00086     PTB( SetI32GA(ds_id, "sensorID\0",(int32)valstr.l2sensorID) );
00087     PTB( SetI32GA(ds_id, "L3 validation sensorID", valstr.l3sensorID) );
00088     PTB( SetChrGA(ds_id, "Input Parameters", valstr.input_parms));
00089     
00090     PTB( CreateSDS(
00091     *sd_id,                                          /* file id         */
00092     "fileID",                                        /* short name      */
00093     "ID of the File",                                /* long name       */
00094     NULL,                                            /* standard name   */
00095     "dimensionless",                                 /* units           */
00096     0, 0,                                            /* valid range     */
00097     0,0,                                             /* slope, offset   */
00098     DFNT_INT16,                                      /* HDF number type */
00099     1,                                               /* rank            */
00100     npixs, 1, 1,                                     /* dimension sizes */
00101     "Number of Pixels", NULL, NULL                   /* dimension names */
00102     ) );
00103 
00104     PTB( CreateSDS(
00105     *sd_id,                                          /* file id         */
00106     "year",                                          /* short name      */
00107     "Pixel year",                                    /* long name       */
00108     NULL,                                            /* standard name   */
00109     "years",                                         /* units           */
00110     1996, 2038,                                      /* valid range     */
00111     0,0,                                             /* slope, offset   */
00112     DFNT_INT16,                                      /* HDF number type */
00113     1,                                               /* rank            */
00114     npixs, 1, 1,                                     /* dimension sizes */
00115     "Number of Pixels", NULL, NULL                   /* dimension names */
00116     ) );
00117 
00118     PTB( CreateSDS(
00119     *sd_id,                                          /* file id         */
00120     "day",                                           /* short name      */
00121     "Pixel day of year",                             /* long name       */
00122     NULL,                                            /* standard name   */
00123     "days",                                          /* units           */
00124     0,366,                                           /* valid range     */
00125     0,0,                                             /* slope, offset   */
00126     DFNT_INT16,                                      /* HDF number type */
00127     1,                                               /* rank            */
00128     npixs, 1, 1,                                     /* dimension sizes */
00129     "Number of Pixels", NULL, NULL                   /* dimension names */
00130     ) );
00131 
00132     PTB( CreateSDS(
00133     *sd_id,                                          /* file id         */
00134     "msec",                                          /* short name      */
00135     "Pixel time, milliseconds of day",               /* long name       */
00136     NULL,                                            /* standard name   */
00137     "milliseconds",                                  /* units           */
00138     0,0,                                             /* valid range     */
00139     0,0,                                             /* slope, offset   */
00140     DFNT_INT32,                                      /* HDF number type */
00141     1,                                               /* rank            */
00142     npixs, 1, 1,                                     /* dimension sizes */
00143     "Number of Pixels", NULL, NULL                   /* dimension names */
00144     ) );
00145 
00146     PTB( CreateSDS(
00147     *sd_id,                                          /* file id         */
00148     "iscan",                                         /* short name      */
00149     "Scan-line number for the pixel",                /* long name       */
00150     NULL,                                            /* standard name   */
00151     "dimensionless",                                 /* units           */
00152     0,0,                                             /* valid range     */
00153     0,0,                                             /* slope, offset   */
00154     DFNT_INT16,                                      /* HDF number type */
00155     1,                                               /* rank            */
00156     npixs, 1, 1,                                     /* dimension sizes */
00157     "Number of Pixels", NULL, NULL                   /* dimension names */
00158     ) );
00159 
00160     PTB( CreateSDS(
00161     *sd_id,                                          /* file id         */
00162     "nscenes",                                       /* short name      */
00163     "Number of scenes in L3 matching bin",           /* long name       */
00164     NULL,                                            /* standard name   */
00165     "dimensionless",                                 /* units           */
00166     0,0,                                             /* valid range     */
00167     0,0,                                             /* slope, offset   */
00168     DFNT_INT16,                                      /* HDF number type */
00169     1,                                               /* rank            */
00170     npixs, 1, 1,                                     /* dimension sizes */
00171     "Number of Pixels", NULL, NULL                   /* dimension names */
00172     ) );
00173 
00174     PTB( CreateSDS(
00175     *sd_id,                                          /* file id         */
00176     "nobs",                                          /* short name      */
00177     "Number of observations in L3 matching bin",     /* long name       */
00178     NULL,                                            /* standard name   */
00179     "dimensionless",                                 /* units           */
00180     0,0,                                             /* valid range     */
00181     0,0,                                             /* slope, offset   */
00182     DFNT_INT16,                                      /* HDF number type */
00183     1,                                               /* rank            */
00184     npixs, 1, 1,                                     /* dimension sizes */
00185     "Number of Pixels", NULL, NULL                   /* dimension names */
00186     ) );
00187 
00188     PTB( CreateSDS(
00189     *sd_id,                                          /* file id         */
00190     "lon",                                           /* short name      */
00191     "Pixel longitude",                               /* long name       */
00192     NULL,                                            /* standard name   */
00193     "degrees",                                       /* units           */
00194     -180.0, 180.0,                                   /* valid range     */
00195     0,0,                                             /* slope, offset   */
00196     DFNT_FLOAT32,                                    /* HDF number type */
00197     1,                                               /* rank            */
00198     npixs, 1, 1,                                     /* dimension sizes */
00199     "Number of Pixels", NULL, NULL                   /* dimension names */
00200     ) );
00201 
00202     PTB( CreateSDS(
00203     *sd_id,                                          /* file id         */
00204     "lat",                                           /* short name      */
00205     "Pixel latitude",                                /* long name       */
00206     NULL,                                            /* standard name   */
00207     "degrees",                                       /* units           */
00208     -90.0, 90.0,                                     /* valid range     */
00209     0,0,                                             /* slope, offset   */
00210     DFNT_FLOAT32,                                    /* HDF number type */
00211     1,                                               /* rank            */
00212     npixs, 1, 1,                                     /* dimension sizes */
00213     "Number of Pixels", NULL, NULL                   /* dimension names */
00214     ) );
00215     
00216     
00217     /*                                                                  */
00218     /* Create the geophysical SDSes for the L3 vicarious products       */
00219     /* ---------------------------------------------------------------- */
00220     /*                                                                  */
00221     ptr = &p;
00222     for (i=0; i<valstr.nl3prods; i++) {
00223 
00224         if ((ch = strstr(valstr.l3prods[i], "_vic")) != NULL) {
00225         ch[0] = '\x0';
00226     }
00227     
00228         if ((ptr = get_l2prod_index(valstr.l3prods[i],valstr.l3sensorID,
00229                valstr.nl3bands,0,1,valstr.l3Lambda)) == NULL) {
00230             fprintf(stderr,
00231             "-E- %s line %d: product index failure.\n",
00232             __FILE__,__LINE__);
00233             return(FATAL_ERROR);
00234         };
00235     strcat(valstr.l3prods[i], "_vic\x0");
00236 
00237         PTB( CreateSDS(
00238         *sd_id,                                      /* file id         */
00239         valstr.l3prods[i],                           /* short name      */
00240         ptr->title,                                  /* long name       */
00241         ptr->standard_name,                          /* standard name   */
00242         ptr->units,                                  /* units           */
00243         ptr->min, ptr->max,                          /* valid range     */
00244         0.0, 0.0,                                    /* slope, offset   */
00245         DFNT_FLOAT32,                                /* HDF number type */
00246         1,                                           /* number of dims  */
00247         npixs, 1, 1,                                 /* dimension sizes */
00248         "Number of Pixels", NULL, NULL               /* dimension names */
00249         ) );
00250     
00251     }
00252     
00253     
00254     /*                                                                  */
00255     /* Create the geophysical SDSes for the L2 validation products      */
00256     /* ---------------------------------------------------------------- */
00257     /*                                                                  */
00258     for (i=0; i<valstr.nl2prods; i++) {
00259 
00260         if ((ptr = get_l2prod_index(valstr.l2prods[i],valstr.l2sensorID,
00261                valstr.nl2bands,0,1,valstr.l2Lambda)) == NULL) {
00262             fprintf(stderr,
00263             "-E- %s line %d: product index failure.\n",
00264             __FILE__,__LINE__);
00265             return(FATAL_ERROR);
00266         };
00267     
00268     if (strcmp(valstr.l2prods[i], "l2_flags") == 0) {
00269     
00270             PTB( CreateSDS(
00271             *sd_id,                                      /* file id         */
00272             valstr.l2prods[i],                           /* short name      */
00273             ptr->title,                                  /* long name       */
00274             ptr->standard_name,                          /* standard name   */
00275             ptr->units,                                  /* units           */
00276             ptr->min, ptr->max,                          /* valid range     */
00277             0.0, 0.0,                                    /* slope, offset   */
00278             DFNT_INT32,                                  /* HDF number type */
00279             1,                                           /* number of dims  */
00280             npixs, 1, 1,                                 /* dimension sizes */
00281             "Number of Pixels", NULL, NULL               /* dimension names */
00282             ) );
00283         
00284     } else
00285         if ((strcmp(valstr.l2prods[i], "mside") == 0) || (strcmp(valstr.l2prods[i], "detnum") == 0)) {
00286     
00287             PTB( CreateSDS(
00288             *sd_id,                                      /* file id         */
00289             valstr.l2prods[i],                           /* short name      */
00290             ptr->title,                                  /* long name       */
00291             ptr->standard_name,                          /* standard name   */
00292             ptr->units,                                  /* units           */
00293             ptr->min, ptr->max,                          /* valid range     */
00294             0.0, 0.0,                                    /* slope, offset   */
00295             DFNT_UINT8,                                  /* HDF number type */
00296             1,                                           /* number of dims  */
00297             npixs, 1, 1,                                 /* dimension sizes */
00298             "Number of Pixels", NULL, NULL               /* dimension names */
00299             ) );
00300         
00301     } else
00302         if (strcmp(valstr.l2prods[i], "pixnum") == 0) {
00303     
00304             PTB( CreateSDS(
00305             *sd_id,                                      /* file id         */
00306             valstr.l2prods[i],                           /* short name      */
00307             ptr->title,                                  /* long name       */
00308             ptr->standard_name,                          /* standard name   */
00309             ptr->units,                                  /* units           */
00310             ptr->min, ptr->max,                          /* valid range     */
00311             0.0, 0.0,                                    /* slope, offset   */
00312             DFNT_INT16,                                  /* HDF number type */
00313             1,                                           /* number of dims  */
00314             npixs, 1, 1,                                 /* dimension sizes */
00315             "Number of Pixels", NULL, NULL               /* dimension names */
00316             ) );
00317 
00318     } else {
00319 
00320             PTB( CreateSDS(
00321             *sd_id,                                      /* file id         */
00322             valstr.l2prods[i],                           /* short name      */
00323             ptr->title,                                  /* long name       */
00324             ptr->standard_name,                          /* standard name   */
00325             ptr->units,                                  /* units           */
00326             ptr->min, ptr->max,                          /* valid range     */
00327             0.0, 0.0,                                    /* slope, offset   */
00328             DFNT_FLOAT32,                                /* HDF number type */
00329             1,                                           /* number of dims  */
00330             npixs, 1, 1,                                 /* dimension sizes */
00331             "Number of Pixels", NULL, NULL               /* dimension names */
00332             ) );
00333     }
00334 
00335     }
00336 
00337     
00338     return(LIFE_IS_GOOD);
00339     
00340 }
00341 
00342 
00343 
00344 
00345 
00346 int msval_append(char *valfile, msvalstr valstr)
00347 {
00348 
00349     int32 sd_id_old, sd_id;
00350     char  command[2*FILENAME_MAX], name[FILENAME_MAX], name1[FILENAME_MAX], *tempname;
00351     int   exists;
00352     int32_t  spix_old, spix, total_old, i;
00353     msvalstr valstr_old;
00354     
00355 
00356     spix_old = -1;
00357     total_old = 0;
00358     valstr_old.data = NULL;
00359     if (msval_readblocks(valfile, &sd_id_old, &spix_old, &total_old, &valstr_old) != LIFE_IS_GOOD) {
00360         printf("-E- %s line %d: Cannot read already existing SDS data %s\n",__FILE__,__LINE__,valfile);
00361         return(HDF_FUNCTION_ERROR);
00362     }
00363 
00364     if (valstr_old.npixs == 0L) exists = 0; else {
00365         exists = 1;
00366         if (valstr_old.l2sensorID != valstr.l2sensorID) {
00367             printf("-E- %s line %d: Attempt to write into a file whose data come from a different sensor %s\n",__FILE__,__LINE__,sensorName[valstr_old.l2sensorID]);
00368         free_valstr(valstr_old, 1);
00369         SDend(sd_id_old);
00370             return(HDF_FUNCTION_ERROR);
00371         }
00372         if (valstr_old.l3sensorID != valstr.l3sensorID) {
00373             printf("-E- %s line %d: Attempt to write into a file whose data come from different validations sensors %s\n",__FILE__,__LINE__,sensorName[valstr_old.l3sensorID]);
00374         free_valstr(valstr_old, 1);
00375         SDend(sd_id_old);
00376             return(HDF_FUNCTION_ERROR);
00377         }
00378     }
00379             
00380     
00381     if (exists) { 
00382    
00383     strcpy(name1, valfile);
00384     tempname = dirname(name1);
00385     strcpy(name, tempname);
00386     strcat(name, "/temp.hdf");
00387     
00388     if (msval_create(name, &sd_id, valstr, total_old+valstr.npixs) != LIFE_IS_GOOD) {
00389             printf("-E- %s line %d: Cannot read already existing SDS data %s\n",__FILE__,__LINE__,name);
00390         free_valstr(valstr_old, 1);
00391         SDend(sd_id);
00392         SDend(sd_id_old);
00393             return(HDF_FUNCTION_ERROR);
00394         }
00395     
00396     spix = 0L;
00397         
00398     do {
00399         
00400         if (msval_writeblocks(name, sd_id, &spix, valstr_old, 0L) != LIFE_IS_GOOD) {
00401                 printf("-E- %s line %d: Cannot write existing SDS data into the temp file %s\n",__FILE__,__LINE__,name);
00402             free_valstr(valstr_old, 1);
00403             SDend(sd_id);
00404             SDend(sd_id_old);
00405                 return(HDF_FUNCTION_ERROR);
00406         }
00407         
00408         if (msval_readblocks(valfile, &sd_id_old, &spix_old, &total_old, &valstr_old) != LIFE_IS_GOOD) {
00409                 printf("-E- %s line %d: Cannot read already existing SDS data %s\n",__FILE__,__LINE__,valfile);
00410             free_valstr(valstr_old, 1);
00411             SDend(sd_id);
00412                 return(HDF_FUNCTION_ERROR);
00413         }
00414 
00415         } while (valstr_old.npixs > 0L);
00416 
00417 
00418     for (i=0; i<valstr.npixs; i++) valstr.fileID[i] += (int16)valstr_old.nfiles;
00419     
00420     if (msval_writeblocks(name, sd_id, &spix, valstr, valstr_old.nfiles) != LIFE_IS_GOOD) {
00421             printf("-E- %s line %d: Cannot write new SDS data into the temp file %s\n",__FILE__,__LINE__,name);
00422         SDend(sd_id);
00423             return(HDF_FUNCTION_ERROR);
00424     }
00425         
00426         if (SDend(sd_id) == FAIL) {
00427             printf("-E- %s line %d: Could not close HDF file, %s.\n",__FILE__,__LINE__,name);
00428             return(HDF_FUNCTION_ERROR);
00429         }
00430     
00431     sprintf(command, "mv %s %s", name, valfile);
00432     system(command);
00433 
00434     } else {
00435     
00436         if (msval_create(valfile, &sd_id, valstr, valstr.npixs) != LIFE_IS_GOOD) {
00437             printf("-E- %s line %d: Could not open HDF file, %s .\n",__FILE__,__LINE__,valfile);
00438         SDend(sd_id);
00439             return(HDF_FUNCTION_ERROR);
00440         }
00441     
00442     spix = 0L;
00443     
00444     if (msval_writeblocks(valfile, sd_id, &spix, valstr, 0L) != LIFE_IS_GOOD) {
00445             printf("-E- %s line %d: Cannot write new SDS data into the file %s\n",__FILE__,__LINE__,valfile);
00446         SDend(sd_id);
00447             return(HDF_FUNCTION_ERROR);
00448     }
00449         
00450         if (SDend(sd_id) == FAIL) {
00451             printf("-E- %s line %d: Could not close HDF file, %s.\n",__FILE__,__LINE__,valfile);
00452             return(HDF_FUNCTION_ERROR);
00453         }
00454     }
00455     
00456     
00457     return(LIFE_IS_GOOD);
00458     
00459 }    
00460 
00461 
00462 
00463 /* ------------------------------------------------------         */
00464 /* msval_read() - reads validation data and               */
00465 /*          subsamples if needed                          */
00466 /*          calptr comprises the actual data for npixs pixels     */
00467 /*          memory is allocated depending on the npixs in the file*/
00468 /* ------------------------------------------------------         */
00469 
00470 int msval_read(char *valfile, int32_t subsmpl, msvalstr *valstr)
00471 {
00472 
00473     int32 sd_id, sds_id;
00474     int32 dim_sizes[H4_MAX_VAR_DIMS];
00475     int32 stride[2], n_datasets, n_file_attr, rank, num_type, attributes;
00476     char  name[H4_MAX_NC_NAME];
00477     int32_t  nfiles=0, totalpix=0, i, l;
00478     char  input_parms[16384], *ch;
00479     
00480     
00481     if (subsmpl <= 1) subsmpl = 1;
00482     
00483     sd_id = SDstart(valfile, DFACC_RDONLY);
00484     if (sd_id == FAIL) {
00485         printf("-E- %s line %d: Could not open HDF file, %s .\n",__FILE__,__LINE__,valfile);
00486         return(HDF_FUNCTION_ERROR);
00487     }
00488     
00489     strcpy(name, "sensorID\0");
00490     read_glbl_attr(sd_id, name, (VOIDP) &(valstr->l2sensorID));
00491     strcpy(name, "L3 validation sensorID\0");
00492     read_glbl_attr(sd_id, name, (VOIDP) &(valstr->l3sensorID));
00493     strcpy(name, "Input Parameters\0");
00494     read_glbl_attr(sd_id, name, (VOIDP) input_parms);
00495     l = strlen(input_parms);
00496     if ((valstr->input_parms = (char *)malloc((l+1)*sizeof(char))) == NULL) {
00497         printf("-E- %s line %d: Error allocating memory to MSl2validate input parameter text.\n",__FILE__,__LINE__);
00498         return(HDF_FUNCTION_ERROR);
00499     }
00500     strncpy(valstr->input_parms, input_parms, l);
00501     valstr->input_parms[l] = '\x0';
00502     
00503 
00504     valstr->nl2prods = 1000;
00505     if ((valstr->l2prods = (prname *)malloc(valstr->nl2prods*sizeof(prname))) == NULL) {
00506         printf("-E- %s line %d: Error allocating memory to l2 product names.\n",__FILE__,__LINE__);
00507         return(HDF_FUNCTION_ERROR);
00508     }
00509     valstr->nl2prods = 0;
00510     valstr->nl3prods = 1000;
00511     if ((valstr->l3prods = (prname *)malloc(valstr->nl3prods*sizeof(prname))) == NULL) {
00512         printf("-E- %s line %d: Error allocating memory to L3 product names.\n",__FILE__,__LINE__);
00513         return(HDF_FUNCTION_ERROR);
00514     }
00515     valstr->nl3prods = 0;
00516    
00517     
00518     nfiles = 0L;
00519     do {
00520         sprintf(name, "filename%d", nfiles);
00521         
00522     if (SDfindattr(sd_id,name) == FAIL) break; else ++nfiles;
00523     
00524     } while (1);
00525 
00526     
00527     if (nfiles == 0L) {
00528         printf("-E- %s line %d: Could not find filename attributes, %s .\n",__FILE__,__LINE__,name);
00529         return(HDF_FUNCTION_ERROR);
00530     }
00531     
00532     
00533     SDfileinfo(sd_id, &n_datasets, &n_file_attr);
00534     
00535     
00536     for (i=0; i<n_datasets; i++) {
00537     
00538         sds_id = SDselect(sd_id, i);
00539     SDgetinfo(sds_id, name, &rank, dim_sizes, &num_type, &attributes);
00540     
00541     if (strcmp(name, "fileID") == 0) {
00542             if (getDims(sd_id, name, dim_sizes) != 0) {
00543             printf("-E- %s line %d: Could not read HDF file dimensions, %s .\n",__FILE__,__LINE__,valfile);
00544             SDend(sd_id);
00545             return(HDF_FUNCTION_ERROR);
00546             }
00547             if (dim_sizes[0] < 0) {
00548             printf("-E- %s line %d: The rank of the requested parameter (%s) is incorrect\n",__FILE__,__LINE__,name);
00549             SDend(sd_id);
00550             return(HDF_FUNCTION_ERROR);
00551             }
00552             if (dim_sizes[0] == SD_UNLIMITED) printf("  Dimension SD_UNLIMITED"); else totalpix = (int32_t)dim_sizes[0];
00553     }
00554     else 
00555     if (strcmp(name, "year") == 0) ;
00556     else 
00557     if (strcmp(name, "day") == 0) ;
00558     else 
00559     if (strcmp(name, "msec") == 0) ;
00560     else 
00561     if (strcmp(name, "iscan") == 0) ;
00562     else 
00563     if (strcmp(name, "nscenes") == 0) ;
00564     else 
00565     if (strcmp(name, "nobs") == 0) ;
00566     else 
00567     if (strcmp(name, "lon") == 0) ;
00568     else 
00569     if (strcmp(name, "lat") == 0) ;
00570     else 
00571         if ((ch = strstr(name, "_vic")) != NULL) {
00572         
00573         strcpy(valstr->l3prods[valstr->nl3prods], name);
00574         valstr->nl3prods++;
00575     }
00576     else {
00577         
00578         strcpy(valstr->l2prods[valstr->nl2prods], name);
00579         valstr->nl2prods++;
00580         } 
00581     }
00582     
00583     if ((valstr->l2prods = (prname *)realloc((void *)valstr->l2prods, valstr->nl2prods*sizeof(prname))) == NULL) {
00584         printf("-E- %s line %d: Error reallocating memory to l2 product names.\n",__FILE__,__LINE__);
00585         return(HDF_FUNCTION_ERROR);
00586     }
00587     if ((valstr->l3prods = (prname *)realloc((void *)valstr->l3prods, valstr->nl3prods*sizeof(prname))) == NULL) {
00588         printf("-E- %s line %d: Error reallocating memory to L3 product names.\n",__FILE__,__LINE__);
00589         return(HDF_FUNCTION_ERROR);
00590     }
00591     valstr->l2Lambda = NULL;
00592     valstr->l3Lambda = NULL;
00593     
00594     
00595     
00596     totalpix = (totalpix+subsmpl-1)/subsmpl;
00597     
00598     l = alloc_valstr(nfiles, totalpix, valstr);
00599         
00600 
00601     for (i=0; i<nfiles; i++) {
00602     sprintf(name, "filename%d", i);
00603         
00604     if (read_glbl_attr(sd_id, name, (VOIDP) valstr->filenames[i]) != SUCCESS) {
00605         printf("-E- %s line %d: Error reading filename attributes (%s)\n",__FILE__,__LINE__,name);
00606             free_valstr(*valstr, 1);
00607         SDend(sd_id);
00608             return(HDF_FUNCTION_ERROR);
00609     }
00610     
00611     }
00612             
00613         
00614     if (subsmpl <= 1) {
00615         PTB( rdSDS(sd_id,"fileID\0",0,0,0,0,(VOIDP)valstr->fileID) );
00616         PTB( rdSDS(sd_id,"year\0",0,0,0,0,(VOIDP)valstr->year) );
00617         PTB( rdSDS(sd_id,"day\0",0,0,0,0,(VOIDP)valstr->day) );
00618         PTB( rdSDS(sd_id,"msec\0",0,0,0,0,(VOIDP)valstr->msec) );
00619         PTB( rdSDS(sd_id,"lon\0",0,0,0,0,(VOIDP)valstr->lon) );
00620         PTB( rdSDS(sd_id,"lat\0",0,0,0,0,(VOIDP)valstr->lat) );
00621         PTB( rdSDS(sd_id,"iscan\0",0,0,0,0,(VOIDP)valstr->iscan) );
00622         PTB( rdSDS(sd_id,"nscenes\0",0,0,0,0,(VOIDP)valstr->nscenes) );
00623         PTB( rdSDS(sd_id,"nobs\0",0,0,0,0,(VOIDP)valstr->nobs) );
00624     for (i=0; i<valstr->nl3prods; i++)
00625        PTB( rdSDS(sd_id,valstr->l3prods[i],0,0,0,0,(VOIDP)&(valstr->data_vic[i*totalpix])) );
00626        
00627     l = 0;
00628     for (i=0; i<valstr->nl2prods; i++) {
00629             if (strcmp(valstr->l2prods[i], "l2_flags") == 0) {
00630         PTB( rdSDS(sd_id,valstr->l2prods[i],0,0,0,0,(VOIDP)valstr->l2_flags) );
00631             } else
00632             if (strcmp(valstr->l2prods[i], "mside") == 0) {
00633         PTB( rdSDS(sd_id,valstr->l2prods[i],0,0,0,0,(VOIDP)valstr->mside) );
00634             } else
00635             if (strcmp(valstr->l2prods[i], "detnum") == 0) {
00636         PTB( rdSDS(sd_id,valstr->l2prods[i],0,0,0,0,(VOIDP)valstr->detnum) );
00637             } else
00638             if (strcmp(valstr->l2prods[i], "pixnum") == 0) {
00639         PTB( rdSDS(sd_id,valstr->l2prods[i],0,0,0,0,(VOIDP)valstr->pixnum) );
00640             } else {
00641             PTB( rdSDS(sd_id,valstr->l2prods[i],0,0,0,0,(VOIDP)&(valstr->data_val[l*totalpix])) );
00642         ++l;
00643         }
00644     }
00645     
00646     } else {
00647     
00648     stride[0] = subsmpl;
00649     stride[1] = 1;
00650     PTB( rdstrideSDS(sd_id,"fileID\0",stride,(VOIDP)valstr->fileID) );
00651     PTB( rdstrideSDS(sd_id,"year\0",stride,(VOIDP)valstr->year) );
00652         PTB( rdstrideSDS(sd_id,"day\0",stride,(VOIDP)valstr->day) );
00653         PTB( rdstrideSDS(sd_id,"msec\0",stride,(VOIDP)valstr->msec) );
00654         PTB( rdstrideSDS(sd_id,"lon\0",stride,(VOIDP)valstr->lon) );
00655         PTB( rdstrideSDS(sd_id,"lat\0",stride,(VOIDP)valstr->lat) );
00656         PTB( rdstrideSDS(sd_id,"iscan\0",stride,(VOIDP)valstr->iscan) );
00657         PTB( rdstrideSDS(sd_id,"nscenes\0",stride,(VOIDP)valstr->nscenes) );
00658         PTB( rdstrideSDS(sd_id,"nobs\0",stride,(VOIDP)valstr->nobs) );
00659     for (i=0; i<valstr->nl3prods; i++)
00660        PTB( rdstrideSDS(sd_id,valstr->l3prods[i],stride,(VOIDP)(valstr->data_vic+i*totalpix)) );
00661        
00662     l = 0;
00663     for (i=0; i<valstr->nl2prods; i++) {
00664             if (strcmp(valstr->l2prods[i], "l2_flags\0") == 0) {
00665         PTB( rdstrideSDS(sd_id,valstr->l2prods[i],stride,(VOIDP)valstr->l2_flags) );
00666             } else
00667             if (strcmp(valstr->l2prods[i], "mside\0") == 0) {
00668         PTB( rdstrideSDS(sd_id,valstr->l2prods[i],stride,(VOIDP)valstr->mside) );
00669             } else
00670             if (strcmp(valstr->l2prods[i], "detnum\0") == 0) {
00671         PTB( rdstrideSDS(sd_id,valstr->l2prods[i],stride,(VOIDP)valstr->detnum) );
00672             } else
00673             if (strcmp(valstr->l2prods[i], "pixnum\0") == 0) {
00674         PTB( rdstrideSDS(sd_id,valstr->l2prods[i],stride,(VOIDP)valstr->pixnum) );
00675             } else {
00676         PTB( rdstrideSDS(sd_id,valstr->l2prods[i],stride,(VOIDP)&(valstr->data_val[l*totalpix])) );
00677         ++l;
00678         }
00679     }
00680        
00681     }
00682     
00683     if (SDend(sd_id) == FAIL) {
00684        printf("-E- %s line %d: Could not close HDF file, %s .\n",__FILE__,__LINE__,valfile);
00685        return(HDF_FUNCTION_ERROR);
00686     }
00687     
00688     return(LIFE_IS_GOOD);
00689     
00690 }    
00691 
00692 
00693 
00694 
00695 
00696 
00697 /* ------------------------------------------------------         */
00698 /* msval_npixs() - checks number of pixels contained in the       */
00699 /*      validation hdf file                               */
00700 /* ------------------------------------------------------         */
00701 
00702 int msval_npixs(char *valfile, int32_t subsmpl, int32_t *sensorID, int32_t *npixs, int32_t *ngranuls)
00703 {
00704 
00705     int32 sd_id;
00706     int32 dim_sizes[H4_MAX_VAR_DIMS];
00707     char  name[H4_MAX_NC_NAME];
00708     int32_t  totalpix=0, totalfiles=0;
00709     
00710     
00711     if (subsmpl <= 1) subsmpl = 1;    
00712     
00713     sd_id = SDstart(valfile, DFACC_RDONLY);
00714     if (sd_id == FAIL) {
00715         printf("-E- %s line %d: Could not open HDF file, %s .\n",__FILE__,__LINE__,valfile);
00716         return(HDF_FUNCTION_ERROR);
00717     }
00718     
00719     strcpy(name, "sensorID\0");
00720     read_glbl_attr(sd_id, name, (VOIDP)sensorID);    
00721     
00722     totalfiles = 0L;
00723     do {
00724     sprintf(name, "filename%d", totalfiles);
00725         
00726     if (SDfindattr(sd_id, name) == FAIL) break; else ++totalfiles;
00727     
00728     } while (1);
00729 
00730     strcpy(name, "fileID\0");
00731     if (getDims(sd_id, name, dim_sizes) != 0) {
00732         printf("-E- %s line %d: Could not read HDF file dimensions, %s .\n",__FILE__,__LINE__,valfile);
00733         SDend(sd_id);
00734         return(HDF_FUNCTION_ERROR);
00735     }
00736     if (dim_sizes[0] < 0){
00737         printf("-E- %s line %d: The rank of the requested parameter (%s) is incorrect\n",__FILE__,__LINE__,name);
00738         SDend(sd_id);
00739         return(-1);
00740     }
00741     totalpix = (int32_t)dim_sizes[0];
00742     
00743     if (subsmpl > 1) totalpix = (totalpix+subsmpl-1)/subsmpl;
00744        
00745     if (SDend(sd_id) == FAIL) {
00746        printf("-E- %s line %d: Could not close HDF file, %s .\n",__FILE__,__LINE__,valfile);
00747        return(HDF_FUNCTION_ERROR);
00748     }
00749     
00750     *npixs = totalpix;
00751     *ngranuls = totalfiles;
00752         
00753     return(LIFE_IS_GOOD);
00754     
00755 }    
00756 
00757 
00758 
00759 
00760 
00761 /* ------------------------------------------------------         */
00762 /* msval_readblocks() - reads consecutive validation              */              
00763 /*          blocks of data of the size of 400,000 pixels          */                
00764 /*          calptr comprises the actual data for the 400,0000     */
00765 /*          pixels, memory is allocated with the first use        */
00766 /* ------------------------------------------------------         */
00767 
00768 int msval_readblocks(char *valfile, int32 *sd_id, int32_t *spix, int32_t *totalpixs, msvalstr *valstr)
00769 {
00770 
00771     int32 dim_sizes[H4_MAX_VAR_DIMS], sds_id;
00772     char  name[H4_MAX_NC_NAME];
00773     int32_t  nfiles=0, i, l;
00774     int32 n_datasets, n_file_attr, rank, num_type, attributes;
00775     char  input_parms[16384], *ch;
00776     
00777     
00778     
00779     if (*spix >= *totalpixs) {
00780         if (SDend(*sd_id) == FAIL) {
00781             printf("-E- %s line %d: Could not close HDF file, %s .\n",__FILE__,__LINE__,valfile);
00782             return(HDF_FUNCTION_ERROR);
00783         }
00784         free_valstr(*valstr, 1);
00785         valstr->npixs = 0L;
00786     return(LIFE_IS_GOOD);
00787     }
00788     
00789     
00790     if (*spix < 0L) {
00791     
00792         valstr->npixs = 0L;
00793     valstr->nfiles = 0L;
00794     
00795         *sd_id = SDstart(valfile, DFACC_RDONLY);
00796         if (*sd_id == FAIL) {
00797         return(LIFE_IS_GOOD);
00798         }
00799     
00800         strcpy(name, "sensorID\0");
00801         if (read_glbl_attr(*sd_id, name, (VOIDP) &(valstr->l2sensorID)) != LIFE_IS_GOOD) {
00802             printf("-E- %s line %d: Could not read HDF sensor attribute, %s .\n",__FILE__,__LINE__,valfile);
00803             SDend(*sd_id);
00804             return(HDF_FUNCTION_ERROR);
00805         }
00806     
00807         strcpy(name, "L3 validation sensorID\0");
00808         if (read_glbl_attr(*sd_id, name, (VOIDP) &(valstr->l3sensorID)) != LIFE_IS_GOOD) {
00809             printf("-E- %s line %d: Could not read HDF L3 sensor attribute, %s .\n",__FILE__,__LINE__,valfile);
00810             SDend(*sd_id);
00811             return(HDF_FUNCTION_ERROR);
00812         }
00813     
00814         strcpy(name, "Input Parameters\0");
00815         read_glbl_attr(*sd_id, name, (VOIDP) input_parms);
00816         l = strlen(input_parms);
00817         if ((valstr->input_parms = (char *)malloc((l+1)*sizeof(char))) == NULL) {
00818             printf("-E- %s line %d: Error allocating memory to MSl2validate input parameter text.\n",__FILE__,__LINE__);
00819             SDend(*sd_id);
00820             return(HDF_FUNCTION_ERROR);
00821         }
00822         strncpy(valstr->input_parms, input_parms, l);
00823         valstr->input_parms[l] = '\x0';
00824     
00825     
00826         nfiles = 0L;
00827         do {
00828             sprintf(name, "filename%d", nfiles);
00829         
00830         if (SDfindattr(*sd_id,name) == FAIL) break; else ++nfiles;
00831     
00832         } while (1);
00833 
00834     
00835         strcpy(name, "fileID\0");
00836         if (getDims(*sd_id, name, dim_sizes) != 0) {
00837             printf("-E- %s line %d: Could not read HDF file dimensions, %s .\n",__FILE__,__LINE__,valfile);
00838             SDend(*sd_id);
00839             return(HDF_FUNCTION_ERROR);
00840         }
00841         if (dim_sizes[0] < 0){
00842             printf("-E- %s line %d: The rank of the requested parameter (%s) is incorrect\n",__FILE__,__LINE__,name);
00843             SDend(*sd_id);
00844             return(HDF_FUNCTION_ERROR);
00845         }
00846         *totalpixs = (int32_t)dim_sizes[0];
00847     
00848         if (nfiles == 0L || *totalpixs == 0L) {
00849             printf("-E- %s line %d: There are no data in the file, %s .\n",__FILE__,__LINE__,name);
00850         SDend(*sd_id);
00851             return(LIFE_IS_GOOD);
00852         }
00853     
00854 
00855         valstr->nl2prods = 1000;
00856         if ((valstr->l2prods = (prname *)malloc(valstr->nl2prods*sizeof(prname))) == NULL) {
00857             printf("-E- %s line %d: Error allocating memory to l2 product names.\n",__FILE__,__LINE__);
00858             exit(FATAL_ERROR);
00859         }
00860         valstr->nl2prods = 0;
00861         valstr->nl3prods = 1000;
00862         if ((valstr->l3prods = (prname *)malloc(valstr->nl3prods*sizeof(prname))) == NULL) {
00863             printf("-E- %s line %d: Error allocating memory to L3 product names.\n",__FILE__,__LINE__);
00864             exit(FATAL_ERROR);
00865         }
00866         valstr->nl3prods = 0;
00867     
00868     
00869         SDfileinfo(*sd_id, &n_datasets, &n_file_attr);
00870     
00871     
00872         for (i=0; i<n_datasets; i++) {
00873     
00874             sds_id = SDselect(*sd_id, i);
00875         SDgetinfo(sds_id, name, &rank, dim_sizes, &num_type, &attributes);
00876     
00877         if (strcmp(name, "fileID") == 0);
00878         else 
00879         if (strcmp(name, "year") == 0) ;
00880         else 
00881         if (strcmp(name, "day") == 0) ;
00882         else 
00883         if (strcmp(name, "msec") == 0) ;
00884         else 
00885         if (strcmp(name, "iscan") == 0) ;
00886         else 
00887         if (strcmp(name, "nscenes") == 0) ;
00888         else 
00889         if (strcmp(name, "nobs") == 0) ;
00890         else 
00891         if (strcmp(name, "lon") == 0) ;
00892         else 
00893         if (strcmp(name, "lat") == 0) ;
00894         else 
00895             if ((ch = strstr(name, "_vic")) != NULL) {
00896         
00897             strcpy(valstr->l3prods[valstr->nl3prods], name);
00898             valstr->nl3prods++;
00899         }
00900         else {
00901         
00902             strcpy(valstr->l2prods[valstr->nl2prods], name);
00903             valstr->nl2prods++;
00904             } 
00905         }
00906     
00907         if ((valstr->l2prods = (prname *)realloc((void *)valstr->l2prods, valstr->nl2prods*sizeof(prname))) == NULL) {
00908             printf("-E- %s line %d: Error reallocating memory to l2 product names.\n",__FILE__,__LINE__);
00909             exit(FATAL_ERROR);
00910         }
00911         if ((valstr->l3prods = (prname *)realloc((void *)valstr->l3prods, valstr->nl3prods*sizeof(prname))) == NULL) {
00912             printf("-E- %s line %d: Error reallocating memory to L3 product names.\n",__FILE__,__LINE__);
00913             exit(FATAL_ERROR);
00914         }
00915         valstr->l2Lambda = NULL;
00916     valstr->l3Lambda = NULL;
00917    
00918     
00919         if (*totalpixs > 1000000) 
00920             l = alloc_valstr(nfiles, 1000000L, valstr);
00921     else
00922             l = alloc_valstr(nfiles, *totalpixs, valstr);
00923         
00924 
00925         for (i=0; i<nfiles; i++) {
00926             sprintf(name, "filename%d", i);
00927         
00928         if (read_glbl_attr(*sd_id, name, (VOIDP) valstr->filenames[i]) != SUCCESS) {
00929             printf("-E- %s line %d: Error reading filename attributes (%s)\n",__FILE__,__LINE__,name);
00930                 SDend(*sd_id);
00931             free_valstr(*valstr, 1);
00932             valstr->npixs = 0L;
00933         valstr->nfiles = 0L;
00934                 return(HDF_FUNCTION_ERROR);
00935         }
00936     
00937         }
00938     
00939     *spix = 0L;
00940     
00941     } else {
00942         
00943     if (*spix+valstr->npixs > *totalpixs) valstr->npixs = *totalpixs - *spix;
00944     }   
00945         
00946     PTB( rdSDS(*sd_id,"fileID\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->fileID) );
00947     PTB( rdSDS(*sd_id,"year\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->year) );
00948     PTB( rdSDS(*sd_id,"day\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->day) );
00949     PTB( rdSDS(*sd_id,"msec\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->msec) );
00950     PTB( rdSDS(*sd_id,"lon\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->lon) );
00951     PTB( rdSDS(*sd_id,"lat\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->lat) );
00952     PTB( rdSDS(*sd_id,"iscan\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->iscan) );
00953     PTB( rdSDS(*sd_id,"nscenes\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->nscenes) );
00954     PTB( rdSDS(*sd_id,"nobs\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->nobs) );
00955     for (i=0; i<valstr->nl3prods; i++)
00956     PTB( rdSDS(*sd_id,valstr->l3prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)(valstr->data_vic+i*valstr->npixs)) );
00957        
00958     l = 0;
00959     for (i=0; i<valstr->nl2prods; i++) {
00960         if (strcmp(valstr->l2prods[i], "l2_flags\0") == 0) {
00961         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->l2_flags) );
00962         } else
00963         if (strcmp(valstr->l2prods[i], "mside\0") == 0) {
00964         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->mside) );
00965         } else
00966         if (strcmp(valstr->l2prods[i], "detnum\0") == 0) {
00967         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->detnum) );
00968         } else
00969         if (strcmp(valstr->l2prods[i], "pixnum\0") == 0) {
00970         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->pixnum) );
00971         } else {
00972         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)&(valstr->data_val[l*valstr->npixs])) );
00973         ++l;
00974     }
00975     }
00976 
00977     *spix += valstr->npixs;
00978         
00979     return(LIFE_IS_GOOD);
00980     
00981 }    
00982 
00983 
00984 
00985 
00986 
00987 /* ------------------------------------------------------         */
00988 /* msval_writeblocks() - writes consecutive validation            */              
00989 /*          blocks of data                                    */                
00990 /*          calptr comprises the actual data for the 1,000,0000   */
00991 /*          pixels, memory is allocated with the first use        */
00992 /* ------------------------------------------------------         */
00993 
00994 int msval_writeblocks(char *valfile, int32 sd_id, int32_t *spix, msvalstr valstr, int32_t nfiles)
00995 {
00996 
00997     char  name[H4_MAX_NC_NAME];
00998     int32_t  i, j, l;
00999 
01000     idDS ds_id;
01001     ds_id.deflate = 0;
01002     ds_id.fid = sd_id;
01003     ds_id.fftype = DS_HDF;
01004     
01005     if (nfiles < 0) nfiles = 0L;    
01006     
01007     if (*spix <= 0 || nfiles > 0) {
01008     
01009         for (i=0; i<valstr.nfiles; i++) {
01010         
01011            j = i + nfiles;
01012            sprintf(name, "filename%d", j);
01013         
01014            PTB( SetChrGA(ds_id, name, (char *)valstr.filenames[i]) );
01015            
01016     }
01017         
01018     }
01019         
01020     PTB( sd_writedata(sd_id,"fileID\0",(VOIDP)valstr.fileID,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01021     PTB( sd_writedata(sd_id,"year\0",(VOIDP)valstr.year,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01022     PTB( sd_writedata(sd_id,"day\0",(VOIDP)valstr.day,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01023     PTB( sd_writedata(sd_id,"msec\0",(VOIDP)valstr.msec,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01024     PTB( sd_writedata(sd_id,"lon\0",(VOIDP)valstr.lon,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01025     PTB( sd_writedata(sd_id,"lat\0",(VOIDP)valstr.lat,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01026     PTB( sd_writedata(sd_id,"iscan\0",(VOIDP)valstr.iscan,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01027     PTB( sd_writedata(sd_id,"nscenes\0",(VOIDP)valstr.nscenes,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01028     PTB( sd_writedata(sd_id,"nobs\0",(VOIDP)valstr.nobs,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01029     for (i=0; i<valstr.nl3prods; i++)
01030     PTB( sd_writedata(sd_id,valstr.l3prods[i],(VOIDP)(valstr.data_vic+i*valstr.npixs),(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01031        
01032     l = 0;
01033     for (i=0; i<valstr.nl2prods; i++) {
01034         if (strcmp(valstr.l2prods[i], "l2_flags\0") == 0) {
01035         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.l2_flags,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01036         } else
01037         if (strcmp(valstr.l2prods[i], "mside\0") == 0) {
01038         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.mside,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01039         } else
01040         if (strcmp(valstr.l2prods[i], "detnum\0") == 0) {
01041         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.detnum,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01042         } else
01043         if (strcmp(valstr.l2prods[i], "pixnum\0") == 0) {
01044         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.pixnum,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01045         } else {
01046         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)(valstr.data_val+l*valstr.npixs),(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01047         ++l;
01048     }
01049     }
01050 
01051     *spix += valstr.npixs;
01052     
01053     return(LIFE_IS_GOOD);
01054     
01055 }    
01056 
01057 
01058 
01059 /* ------------------------------------------------------  */
01060 /* rdstrideSDS() - reads complete SDS (scientific data set)*/
01061 /*           for a given stride and returns the data       */
01062 /*                                                         */
01063 /* ------------------------------------------------------  */
01064 int rdstrideSDS(int32 fileID, char *sdsname, int32 stride[2], VOIDP array_data)
01065 {
01066    int32 sds_id, numtype;
01067    int32 sds_index, rank, dims[H4_MAX_VAR_DIMS], nattrs;
01068    int32 start[2], edges[2];
01069    char  tmp_sdsname[H4_MAX_NC_NAME];
01070 
01071 
01072    /* Get the SDS index */
01073    sds_index = SDnametoindex(fileID,sdsname);
01074 
01075    if (sds_index < 0) {
01076      printf("-E- %s: SDS \"%s\" not found.\n", "rdSDS",sdsname);
01077      SDend(fileID);
01078      return(HDF_FUNCTION_ERROR);
01079    }
01080 
01081 
01082    /* Select the SDS */
01083    if ((sds_id = SDselect(fileID, sds_index)) == FAIL) {
01084      printf("-E- %s line %d: Could not read parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01085      SDend(fileID);
01086      return(HDF_FUNCTION_ERROR);
01087    }
01088 
01089    /* Get the rank and number type of the array */
01090    if ((SDgetinfo(sds_id, tmp_sdsname, &rank, dims, &numtype, &nattrs)) == FAIL) {
01091      printf("-E- %s line %d: Could not get info about parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01092      SDend(fileID);
01093      return(HDF_FUNCTION_ERROR);
01094    }
01095 
01096 
01097    /* Define the location, pattern and size of the data to read */
01098    /* set 1st dimension */
01099    start[0] = 0;
01100    start[1] = 0;
01101    edges[0] = dims[0];
01102    /* if rank > 1, set 2nd dimension */
01103    if (rank > 1) edges[1] = dims[1];
01104 
01105    /* Based on number type, call the corresponding wrapper
01106    for the HDF SDreaddata function */ 
01107    if ((SDreaddata(sds_id, start, stride, edges, array_data)) == FAIL) {
01108      printf("-E- %s line %d: Could not read data for parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01109      SDend(fileID);
01110      return(HDF_FUNCTION_ERROR);
01111    }
01112     
01113    /* Terminate access to the array */
01114    if ((SDendaccess(sds_id)) == FAIL) {
01115      printf("-E- %s line %d: Could not end access for parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01116      SDend(fileID);
01117      return(HDF_FUNCTION_ERROR);
01118    }
01119 
01120    return(LIFE_IS_GOOD);
01121    
01122 }
01123 
01124 
01125 int read_glbl_attr(int32 sd_id, char *name, VOIDP ptr) 
01126 {                                           
01127 
01128   int32 index;
01129   
01130   if ((index = SDfindattr(sd_id,name)) == FAIL) return(HDF_FUNCTION_ERROR);
01131 
01132   if (SDreadattr(sd_id,index,(VOIDP)ptr)){ 
01133     printf("-E- %s line %d: Could not get global attribute, %s.\n", __FILE__,__LINE__,(name));
01134     return(HDF_FUNCTION_ERROR);
01135   }
01136 
01137   return(LIFE_IS_GOOD);
01138                                                          
01139 }   
01140 
01141 
01142 
01143 
01144 
01145 
01146 
01147 
01148 int32_t alloc_valstr(int32_t nfiles, int32_t npixs, msvalstr *valstr)
01149 {
01150     
01151     int32_t  len, i;
01152     unsigned char  *p;
01153     
01154     
01155     len = nfiles*sizeof(stname) + 6*npixs*sizeof(int16) + npixs*sizeof(int32) + 2*npixs*sizeof(float32);
01156     
01157     for (i=0; i<valstr->nl3prods; i++) len += npixs*sizeof(float32);
01158     
01159     for (i=0; i<valstr->nl2prods; i++) {
01160     
01161         if (strcmp(valstr->l2prods[i], "l2_flags") == 0) len += npixs*sizeof(int32);
01162     else
01163         if (strcmp(valstr->l2prods[i], "mside") == 0) len += npixs*sizeof(uint8);
01164         else
01165         if (strcmp(valstr->l2prods[i], "detnum") == 0) len += npixs*sizeof(uint8);
01166         else
01167         if (strcmp(valstr->l2prods[i], "pixnum") == 0) len += npixs*sizeof(int16);
01168         else
01169     len += npixs*sizeof(float32);
01170     }
01171     
01172         
01173     if ((p = (unsigned char *) malloc(len)) == NULL) {
01174     printf("%s -Error: Cannot allocate memory to validation data\n",__FILE__);
01175     exit(FATAL_ERROR);
01176     }
01177     valstr->nfiles = nfiles;
01178     valstr->npixs = npixs;
01179     valstr->data = p;
01180     valstr->filenames = (stname *)  p; p += nfiles*sizeof(stname);
01181     valstr->fileID =    (int16  *)  p; p += npixs*sizeof(int16);
01182     valstr->year =  (int16  *)  p; p += npixs*sizeof(int16);
01183     valstr->day =   (int16  *)  p; p += npixs*sizeof(int16);
01184     valstr->msec =  (int32  *)  p; p += npixs*sizeof(int32);
01185     valstr->lon =   (float32 *) p; p += npixs*sizeof(float32);
01186     valstr->lat =   (float32 *) p; p += npixs*sizeof(float32);
01187     valstr->iscan =     (int16   *) p; p += npixs*sizeof(int16);
01188     valstr->nscenes =   (int16   *) p; p += npixs*sizeof(int16);
01189     valstr->nobs =  (int16   *) p; p += npixs*sizeof(int16);
01190     valstr->data_vic =  (float32 *) p; p += npixs*valstr->nl3prods*sizeof(float32);
01191     for (i=0; i<valstr->nl2prods; i++) {
01192         if (strcmp(valstr->l2prods[i], "l2_flags") == 0) {
01193             valstr->l2_flags =  (int32  *) p; p += npixs*sizeof(int32);
01194         } else
01195         if (strcmp(valstr->l2prods[i], "mside") == 0) {
01196         valstr->mside =     (uint8  *) p; p += npixs*sizeof(uint8);
01197         } else
01198         if (strcmp(valstr->l2prods[i], "detnum") == 0) {
01199         valstr->detnum =    (uint8  *) p; p += npixs*sizeof(uint8);
01200         } else
01201         if (strcmp(valstr->l2prods[i], "pixnum") == 0) {
01202         valstr->pixnum =    (int16  *) p; p += npixs*sizeof(int16);
01203         }
01204     }
01205     valstr->data_val =  (float32 *) p;
01206     
01207     return(len);
01208     
01209 }
01210    
01211 
01212 
01213 
01214 
01215 
01216 
01217 
01218 void free_valstr(msvalstr valstr, int all)
01219 {
01220      if (valstr.data != NULL) free(valstr.data);
01221      valstr.data = NULL;
01222      
01223      if (all) {
01224         if (valstr.l2Lambda != NULL) free(valstr.l2Lambda); valstr.l2Lambda = NULL;
01225     if (valstr.l2prods != NULL) free(valstr.l2prods); valstr.l2prods = NULL;
01226     if (valstr.l3Lambda != NULL) free(valstr.l3Lambda); valstr.l3Lambda = NULL;
01227     if (valstr.l3prods != NULL) free(valstr.l3prods); valstr.l3prods = NULL;
01228     if (valstr.input_parms != NULL) free(valstr.input_parms); valstr.input_parms = NULL;
01229      }
01230      
01231 }
01232 
01233