ocssw  1.0
/disk01/web/ocssw/build/src/l2gen/msl2val_hdf.c (r8106/r7246)
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.fid = *sd_id;
00082     ds_id.fftype = FMT_L2HDF;
00083 
00084     PTB( SetChrGA(ds_id, "Title", title) );
00085     PTB( SetI32GA(ds_id, "sensorID\0",(int32)valstr.l2sensorID) );
00086     PTB( SetI32GA(ds_id, "L3 validation sensorID", valstr.l3sensorID) );
00087     PTB( SetChrGA(ds_id, "Input Parameters", valstr.input_parms));
00088     
00089     PTB( CreateSDS(
00090     *sd_id,                                          /* file id         */
00091     "fileID",                                        /* short name      */
00092     "ID of the File",                                /* long name       */
00093     NULL,                                            /* standard name   */
00094     "dimensionless",                                 /* units           */
00095     0, 0,                                            /* valid range     */
00096     0,0,                                             /* slope, offset   */
00097     DFNT_INT16,                                      /* HDF number type */
00098     1,                                               /* rank            */
00099     npixs, 1, 1,                                     /* dimension sizes */
00100     "Number of Pixels", NULL, NULL                   /* dimension names */
00101     ) );
00102 
00103     PTB( CreateSDS(
00104     *sd_id,                                          /* file id         */
00105     "year",                                          /* short name      */
00106     "Pixel year",                                    /* long name       */
00107     NULL,                                            /* standard name   */
00108     "years",                                         /* units           */
00109     1996, 2038,                                      /* valid range     */
00110     0,0,                                             /* slope, offset   */
00111     DFNT_INT16,                                      /* HDF number type */
00112     1,                                               /* rank            */
00113     npixs, 1, 1,                                     /* dimension sizes */
00114     "Number of Pixels", NULL, NULL                   /* dimension names */
00115     ) );
00116 
00117     PTB( CreateSDS(
00118     *sd_id,                                          /* file id         */
00119     "day",                                           /* short name      */
00120     "Pixel day of year",                             /* long name       */
00121     NULL,                                            /* standard name   */
00122     "days",                                          /* units           */
00123     0,366,                                           /* valid range     */
00124     0,0,                                             /* slope, offset   */
00125     DFNT_INT16,                                      /* HDF number type */
00126     1,                                               /* rank            */
00127     npixs, 1, 1,                                     /* dimension sizes */
00128     "Number of Pixels", NULL, NULL                   /* dimension names */
00129     ) );
00130 
00131     PTB( CreateSDS(
00132     *sd_id,                                          /* file id         */
00133     "msec",                                          /* short name      */
00134     "Pixel time, milliseconds of day",               /* long name       */
00135     NULL,                                            /* standard name   */
00136     "milliseconds",                                  /* units           */
00137     0,0,                                             /* valid range     */
00138     0,0,                                             /* slope, offset   */
00139     DFNT_INT32,                                      /* HDF number type */
00140     1,                                               /* rank            */
00141     npixs, 1, 1,                                     /* dimension sizes */
00142     "Number of Pixels", NULL, NULL                   /* dimension names */
00143     ) );
00144 
00145     PTB( CreateSDS(
00146     *sd_id,                                          /* file id         */
00147     "iscan",                                         /* short name      */
00148     "Scan-line number for the pixel",                /* long name       */
00149     NULL,                                            /* standard name   */
00150     "dimensionless",                                 /* units           */
00151     0,0,                                             /* valid range     */
00152     0,0,                                             /* slope, offset   */
00153     DFNT_INT16,                                      /* HDF number type */
00154     1,                                               /* rank            */
00155     npixs, 1, 1,                                     /* dimension sizes */
00156     "Number of Pixels", NULL, NULL                   /* dimension names */
00157     ) );
00158 
00159     PTB( CreateSDS(
00160     *sd_id,                                          /* file id         */
00161     "nscenes",                                       /* short name      */
00162     "Number of scenes in L3 matching bin",           /* long name       */
00163     NULL,                                            /* standard name   */
00164     "dimensionless",                                 /* units           */
00165     0,0,                                             /* valid range     */
00166     0,0,                                             /* slope, offset   */
00167     DFNT_INT16,                                      /* HDF number type */
00168     1,                                               /* rank            */
00169     npixs, 1, 1,                                     /* dimension sizes */
00170     "Number of Pixels", NULL, NULL                   /* dimension names */
00171     ) );
00172 
00173     PTB( CreateSDS(
00174     *sd_id,                                          /* file id         */
00175     "nobs",                                          /* short name      */
00176     "Number of observations in L3 matching bin",     /* long name       */
00177     NULL,                                            /* standard name   */
00178     "dimensionless",                                 /* units           */
00179     0,0,                                             /* valid range     */
00180     0,0,                                             /* slope, offset   */
00181     DFNT_INT16,                                      /* HDF number type */
00182     1,                                               /* rank            */
00183     npixs, 1, 1,                                     /* dimension sizes */
00184     "Number of Pixels", NULL, NULL                   /* dimension names */
00185     ) );
00186 
00187     PTB( CreateSDS(
00188     *sd_id,                                          /* file id         */
00189     "lon",                                           /* short name      */
00190     "Pixel longitude",                               /* long name       */
00191     NULL,                                            /* standard name   */
00192     "degrees",                                       /* units           */
00193     -180.0, 180.0,                                   /* valid range     */
00194     0,0,                                             /* slope, offset   */
00195     DFNT_FLOAT32,                                    /* HDF number type */
00196     1,                                               /* rank            */
00197     npixs, 1, 1,                                     /* dimension sizes */
00198     "Number of Pixels", NULL, NULL                   /* dimension names */
00199     ) );
00200 
00201     PTB( CreateSDS(
00202     *sd_id,                                          /* file id         */
00203     "lat",                                           /* short name      */
00204     "Pixel latitude",                                /* long name       */
00205     NULL,                                            /* standard name   */
00206     "degrees",                                       /* units           */
00207     -90.0, 90.0,                                     /* valid range     */
00208     0,0,                                             /* slope, offset   */
00209     DFNT_FLOAT32,                                    /* HDF number type */
00210     1,                                               /* rank            */
00211     npixs, 1, 1,                                     /* dimension sizes */
00212     "Number of Pixels", NULL, NULL                   /* dimension names */
00213     ) );
00214     
00215     
00216     /*                                                                  */
00217     /* Create the geophysical SDSes for the L3 vicarious products       */
00218     /* ---------------------------------------------------------------- */
00219     /*                                                                  */
00220     ptr = &p;
00221     for (i=0; i<valstr.nl3prods; i++) {
00222 
00223         if ((ch = strstr(valstr.l3prods[i], "_vic")) != NULL) {
00224         ch[0] = '\x0';
00225     }
00226     
00227         if ((ptr = get_l2prod_index(valstr.l3prods[i],valstr.l3sensorID,
00228                valstr.nl3bands,0,1,valstr.l3Lambda)) == NULL) {
00229             fprintf(stderr,
00230             "-E- %s line %d: product index failure.\n",
00231             __FILE__,__LINE__);
00232             return(FATAL_ERROR);
00233         };
00234     strcat(valstr.l3prods[i], "_vic\x0");
00235 
00236         PTB( CreateSDS(
00237         *sd_id,                                      /* file id         */
00238         valstr.l3prods[i],                           /* short name      */
00239         ptr->title,                                  /* long name       */
00240         ptr->standard_name,                          /* standard name   */
00241         ptr->units,                                  /* units           */
00242         ptr->min, ptr->max,                          /* valid range     */
00243         0.0, 0.0,                                    /* slope, offset   */
00244         DFNT_FLOAT32,                                /* HDF number type */
00245         1,                                           /* number of dims  */
00246         npixs, 1, 1,                                 /* dimension sizes */
00247         "Number of Pixels", NULL, NULL               /* dimension names */
00248         ) );
00249     
00250     }
00251     
00252     
00253     /*                                                                  */
00254     /* Create the geophysical SDSes for the L2 validation products      */
00255     /* ---------------------------------------------------------------- */
00256     /*                                                                  */
00257     for (i=0; i<valstr.nl2prods; i++) {
00258 
00259         if ((ptr = get_l2prod_index(valstr.l2prods[i],valstr.l2sensorID,
00260                valstr.nl2bands,0,1,valstr.l2Lambda)) == NULL) {
00261             fprintf(stderr,
00262             "-E- %s line %d: product index failure.\n",
00263             __FILE__,__LINE__);
00264             return(FATAL_ERROR);
00265         };
00266     
00267     if (strcmp(valstr.l2prods[i], "l2_flags") == 0) {
00268     
00269             PTB( CreateSDS(
00270             *sd_id,                                      /* file id         */
00271             valstr.l2prods[i],                           /* short name      */
00272             ptr->title,                                  /* long name       */
00273             ptr->standard_name,                          /* standard name   */
00274             ptr->units,                                  /* units           */
00275             ptr->min, ptr->max,                          /* valid range     */
00276             0.0, 0.0,                                    /* slope, offset   */
00277             DFNT_INT32,                                  /* HDF number type */
00278             1,                                           /* number of dims  */
00279             npixs, 1, 1,                                 /* dimension sizes */
00280             "Number of Pixels", NULL, NULL               /* dimension names */
00281             ) );
00282         
00283     } else
00284         if ((strcmp(valstr.l2prods[i], "mside") == 0) || (strcmp(valstr.l2prods[i], "detnum") == 0)) {
00285     
00286             PTB( CreateSDS(
00287             *sd_id,                                      /* file id         */
00288             valstr.l2prods[i],                           /* short name      */
00289             ptr->title,                                  /* long name       */
00290             ptr->standard_name,                          /* standard name   */
00291             ptr->units,                                  /* units           */
00292             ptr->min, ptr->max,                          /* valid range     */
00293             0.0, 0.0,                                    /* slope, offset   */
00294             DFNT_UINT8,                                  /* HDF number type */
00295             1,                                           /* number of dims  */
00296             npixs, 1, 1,                                 /* dimension sizes */
00297             "Number of Pixels", NULL, NULL               /* dimension names */
00298             ) );
00299         
00300     } else
00301         if (strcmp(valstr.l2prods[i], "pixnum") == 0) {
00302     
00303             PTB( CreateSDS(
00304             *sd_id,                                      /* file id         */
00305             valstr.l2prods[i],                           /* short name      */
00306             ptr->title,                                  /* long name       */
00307             ptr->standard_name,                          /* standard name   */
00308             ptr->units,                                  /* units           */
00309             ptr->min, ptr->max,                          /* valid range     */
00310             0.0, 0.0,                                    /* slope, offset   */
00311             DFNT_INT16,                                  /* HDF number type */
00312             1,                                           /* number of dims  */
00313             npixs, 1, 1,                                 /* dimension sizes */
00314             "Number of Pixels", NULL, NULL               /* dimension names */
00315             ) );
00316 
00317     } else {
00318 
00319             PTB( CreateSDS(
00320             *sd_id,                                      /* file id         */
00321             valstr.l2prods[i],                           /* short name      */
00322             ptr->title,                                  /* long name       */
00323             ptr->standard_name,                          /* standard name   */
00324             ptr->units,                                  /* units           */
00325             ptr->min, ptr->max,                          /* valid range     */
00326             0.0, 0.0,                                    /* slope, offset   */
00327             DFNT_FLOAT32,                                /* HDF number type */
00328             1,                                           /* number of dims  */
00329             npixs, 1, 1,                                 /* dimension sizes */
00330             "Number of Pixels", NULL, NULL               /* dimension names */
00331             ) );
00332     }
00333 
00334     }
00335 
00336     
00337     return(LIFE_IS_GOOD);
00338     
00339 }
00340 
00341 
00342 
00343 
00344 
00345 int msval_append(char *valfile, msvalstr valstr)
00346 {
00347 
00348     int32 sd_id_old, sd_id;
00349     char  command[2*FILENAME_MAX], name[FILENAME_MAX], name1[FILENAME_MAX], *tempname;
00350     int   status, exists;
00351     int32_t  spix_old, spix, total_old, i;
00352     msvalstr valstr_old;
00353     
00354 
00355     spix_old = -1;
00356     total_old = 0;
00357     valstr_old.data = NULL;
00358     if (msval_readblocks(valfile, &sd_id_old, &spix_old, &total_old, &valstr_old) != LIFE_IS_GOOD) {
00359         printf("-E- %s line %d: Cannot read already existing SDS data %s\n",__FILE__,__LINE__,valfile);
00360         return(HDF_FUNCTION_ERROR);
00361     }
00362 
00363     if (valstr_old.npixs == 0L) exists = 0; else {
00364         exists = 1;
00365         if (valstr_old.l2sensorID != valstr.l2sensorID) {
00366             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]);
00367         free_valstr(valstr_old, 1);
00368         status = SDend(sd_id_old);
00369             return(HDF_FUNCTION_ERROR);
00370         }
00371         if (valstr_old.l3sensorID != valstr.l3sensorID) {
00372             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]);
00373         free_valstr(valstr_old, 1);
00374         status = SDend(sd_id_old);
00375             return(HDF_FUNCTION_ERROR);
00376         }
00377     }
00378             
00379     
00380     if (exists) { 
00381    
00382     strcpy(name1, valfile);
00383     tempname = dirname(name1);
00384     strcpy(name, tempname);
00385     strcat(name, "/temp.hdf");
00386     
00387     if (msval_create(name, &sd_id, valstr, total_old+valstr.npixs) != LIFE_IS_GOOD) {
00388             printf("-E- %s line %d: Cannot read already existing SDS data %s\n",__FILE__,__LINE__,name);
00389         free_valstr(valstr_old, 1);
00390         status = SDend(sd_id);
00391         status = SDend(sd_id_old);
00392             return(HDF_FUNCTION_ERROR);
00393         }
00394     
00395     spix = 0L;
00396         
00397     do {
00398         
00399         if (msval_writeblocks(name, sd_id, &spix, valstr_old, 0L) != LIFE_IS_GOOD) {
00400                 printf("-E- %s line %d: Cannot write existing SDS data into the temp file %s\n",__FILE__,__LINE__,name);
00401             free_valstr(valstr_old, 1);
00402             status = SDend(sd_id);
00403             status = SDend(sd_id_old);
00404                 return(HDF_FUNCTION_ERROR);
00405         }
00406         
00407         if (msval_readblocks(valfile, &sd_id_old, &spix_old, &total_old, &valstr_old) != LIFE_IS_GOOD) {
00408                 printf("-E- %s line %d: Cannot read already existing SDS data %s\n",__FILE__,__LINE__,valfile);
00409             free_valstr(valstr_old, 1);
00410             status = SDend(sd_id);
00411                 return(HDF_FUNCTION_ERROR);
00412         }
00413 
00414         } while (valstr_old.npixs > 0L);
00415 
00416 
00417     for (i=0; i<valstr.npixs; i++) valstr.fileID[i] += (int16)valstr_old.nfiles;
00418     
00419     if (msval_writeblocks(name, sd_id, &spix, valstr, valstr_old.nfiles) != LIFE_IS_GOOD) {
00420             printf("-E- %s line %d: Cannot write new SDS data into the temp file %s\n",__FILE__,__LINE__,name);
00421         status = SDend(sd_id);
00422             return(HDF_FUNCTION_ERROR);
00423     }
00424         
00425         if (SDend(sd_id) == FAIL) {
00426             printf("-E- %s line %d: Could not close HDF file, %s.\n",__FILE__,__LINE__,name);
00427             return(HDF_FUNCTION_ERROR);
00428         }
00429     
00430     sprintf(command, "mv %s %s", name, valfile);
00431     system(command);
00432 
00433     } else {
00434     
00435         if (msval_create(valfile, &sd_id, valstr, valstr.npixs) != LIFE_IS_GOOD) {
00436             printf("-E- %s line %d: Could not open HDF file, %s .\n",__FILE__,__LINE__,valfile);
00437         status = SDend(sd_id);
00438             return(HDF_FUNCTION_ERROR);
00439         }
00440     
00441     spix = 0L;
00442     
00443     if (msval_writeblocks(valfile, sd_id, &spix, valstr, 0L) != LIFE_IS_GOOD) {
00444             printf("-E- %s line %d: Cannot write new SDS data into the file %s\n",__FILE__,__LINE__,valfile);
00445         status = SDend(sd_id);
00446             return(HDF_FUNCTION_ERROR);
00447     }
00448         
00449         if (SDend(sd_id) == FAIL) {
00450             printf("-E- %s line %d: Could not close HDF file, %s.\n",__FILE__,__LINE__,valfile);
00451             return(HDF_FUNCTION_ERROR);
00452         }
00453     }
00454     
00455     
00456     return(LIFE_IS_GOOD);
00457     
00458 }    
00459 
00460 
00461 
00462 /* ------------------------------------------------------         */
00463 /* msval_read() - reads validation data and               */
00464 /*          subsamples if needed                          */
00465 /*          calptr comprises the actual data for npixs pixels     */
00466 /*          memory is allocated depending on the npixs in the file*/
00467 /* ------------------------------------------------------         */
00468 
00469 int msval_read(char *valfile, int32_t subsmpl, msvalstr *valstr)
00470 {
00471 
00472     int32 sd_id, sds_id;
00473     int32 dim_sizes[H4_MAX_VAR_DIMS];
00474     int32 stride[2], n_datasets, n_file_attr, rank, num_type, attributes;
00475     intn  status;
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     status = 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             status = 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             status = 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         status = 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     intn  status;
00708     char  name[H4_MAX_NC_NAME];
00709     int32_t  totalpix=0, totalfiles=0;
00710     
00711     
00712     if (subsmpl <= 1) subsmpl = 1;    
00713     
00714     sd_id = SDstart(valfile, DFACC_RDONLY);
00715     if (sd_id == FAIL) {
00716         printf("-E- %s line %d: Could not open HDF file, %s .\n",__FILE__,__LINE__,valfile);
00717         return(HDF_FUNCTION_ERROR);
00718     }
00719     
00720     strcpy(name, "sensorID\0");
00721     read_glbl_attr(sd_id, name, (VOIDP)sensorID);    
00722     
00723     totalfiles = 0L;
00724     do {
00725     sprintf(name, "filename%d", totalfiles);
00726         
00727     if (SDfindattr(sd_id, name) == FAIL) break; else ++totalfiles;
00728     
00729     } while (1);
00730 
00731     strcpy(name, "fileID\0");
00732     if (getDims(sd_id, name, dim_sizes) != 0) {
00733         printf("-E- %s line %d: Could not read HDF file dimensions, %s .\n",__FILE__,__LINE__,valfile);
00734         status = SDend(sd_id);
00735         return(HDF_FUNCTION_ERROR);
00736     }
00737     if (dim_sizes[0] < 0){
00738         printf("-E- %s line %d: The rank of the requested parameter (%s) is incorrect\n",__FILE__,__LINE__,name);
00739         status = SDend(sd_id);
00740         return(-1);
00741     }
00742     totalpix = (int32_t)dim_sizes[0];
00743     
00744     if (subsmpl > 1) totalpix = (totalpix+subsmpl-1)/subsmpl;
00745        
00746     if (SDend(sd_id) == FAIL) {
00747        printf("-E- %s line %d: Could not close HDF file, %s .\n",__FILE__,__LINE__,valfile);
00748        return(HDF_FUNCTION_ERROR);
00749     }
00750     
00751     *npixs = totalpix;
00752     *ngranuls = totalfiles;
00753         
00754     return(LIFE_IS_GOOD);
00755     
00756 }    
00757 
00758 
00759 
00760 
00761 
00762 /* ------------------------------------------------------         */
00763 /* msval_readblocks() - reads consecutive validation              */              
00764 /*          blocks of data of the size of 400,000 pixels          */                
00765 /*          calptr comprises the actual data for the 400,0000     */
00766 /*          pixels, memory is allocated with the first use        */
00767 /* ------------------------------------------------------         */
00768 
00769 int msval_readblocks(char *valfile, int32 *sd_id, int32_t *spix, int32_t *totalpixs, msvalstr *valstr)
00770 {
00771 
00772     int32 dim_sizes[H4_MAX_VAR_DIMS], sds_id;
00773     intn  status;
00774     char  name[H4_MAX_NC_NAME];
00775     int32_t  nfiles=0, i, l;
00776     int32 n_datasets, n_file_attr, rank, num_type, attributes;
00777     char  input_parms[16384], *ch;
00778     
00779     
00780     
00781     if (*spix >= *totalpixs) {
00782         if (SDend(*sd_id) == FAIL) {
00783             printf("-E- %s line %d: Could not close HDF file, %s .\n",__FILE__,__LINE__,valfile);
00784             return(HDF_FUNCTION_ERROR);
00785         }
00786         free_valstr(*valstr, 1);
00787         valstr->npixs = 0L;
00788     return(LIFE_IS_GOOD);
00789     }
00790     
00791     
00792     if (*spix < 0L) {
00793     
00794         valstr->npixs = 0L;
00795     valstr->nfiles = 0L;
00796     
00797         *sd_id = SDstart(valfile, DFACC_RDONLY);
00798         if (*sd_id == FAIL) {
00799         return(LIFE_IS_GOOD);
00800         }
00801     
00802         strcpy(name, "sensorID\0");
00803         if (read_glbl_attr(*sd_id, name, (VOIDP) &(valstr->l2sensorID)) != LIFE_IS_GOOD) {
00804             printf("-E- %s line %d: Could not read HDF sensor attribute, %s .\n",__FILE__,__LINE__,valfile);
00805             status = SDend(*sd_id);
00806             return(HDF_FUNCTION_ERROR);
00807         }
00808     
00809         strcpy(name, "L3 validation sensorID\0");
00810         if (read_glbl_attr(*sd_id, name, (VOIDP) &(valstr->l3sensorID)) != LIFE_IS_GOOD) {
00811             printf("-E- %s line %d: Could not read HDF L3 sensor attribute, %s .\n",__FILE__,__LINE__,valfile);
00812             status = SDend(*sd_id);
00813             return(HDF_FUNCTION_ERROR);
00814         }
00815     
00816         strcpy(name, "Input Parameters\0");
00817         read_glbl_attr(*sd_id, name, (VOIDP) input_parms);
00818         l = strlen(input_parms);
00819         if ((valstr->input_parms = (char *)malloc((l+1)*sizeof(char))) == NULL) {
00820             printf("-E- %s line %d: Error allocating memory to MSl2validate input parameter text.\n",__FILE__,__LINE__);
00821             status = SDend(*sd_id);
00822             return(HDF_FUNCTION_ERROR);
00823         }
00824         strncpy(valstr->input_parms, input_parms, l);
00825         valstr->input_parms[l] = '\x0';
00826     
00827     
00828         nfiles = 0L;
00829         do {
00830             sprintf(name, "filename%d", nfiles);
00831         
00832         if (SDfindattr(*sd_id,name) == FAIL) break; else ++nfiles;
00833     
00834         } while (1);
00835 
00836     
00837         strcpy(name, "fileID\0");
00838         if (getDims(*sd_id, name, dim_sizes) != 0) {
00839             printf("-E- %s line %d: Could not read HDF file dimensions, %s .\n",__FILE__,__LINE__,valfile);
00840             status = SDend(*sd_id);
00841             return(HDF_FUNCTION_ERROR);
00842         }
00843         if (dim_sizes[0] < 0){
00844             printf("-E- %s line %d: The rank of the requested parameter (%s) is incorrect\n",__FILE__,__LINE__,name);
00845             status = SDend(*sd_id);
00846             return(HDF_FUNCTION_ERROR);
00847         }
00848         *totalpixs = (int32_t)dim_sizes[0];
00849     
00850         if (nfiles == 0L || *totalpixs == 0L) {
00851             printf("-E- %s line %d: There are no data in the file, %s .\n",__FILE__,__LINE__,name);
00852         status = SDend(*sd_id);
00853             return(LIFE_IS_GOOD);
00854         }
00855     
00856 
00857         valstr->nl2prods = 1000;
00858         if ((valstr->l2prods = (prname *)malloc(valstr->nl2prods*sizeof(prname))) == NULL) {
00859             printf("-E- %s line %d: Error allocating memory to l2 product names.\n",__FILE__,__LINE__);
00860             exit(FATAL_ERROR);
00861         }
00862         valstr->nl2prods = 0;
00863         valstr->nl3prods = 1000;
00864         if ((valstr->l3prods = (prname *)malloc(valstr->nl3prods*sizeof(prname))) == NULL) {
00865             printf("-E- %s line %d: Error allocating memory to L3 product names.\n",__FILE__,__LINE__);
00866             exit(FATAL_ERROR);
00867         }
00868         valstr->nl3prods = 0;
00869     
00870     
00871         SDfileinfo(*sd_id, &n_datasets, &n_file_attr);
00872     
00873     
00874         for (i=0; i<n_datasets; i++) {
00875     
00876             sds_id = SDselect(*sd_id, i);
00877         status = SDgetinfo(sds_id, name, &rank, dim_sizes, &num_type, &attributes);
00878     
00879         if (strcmp(name, "fileID") == 0);
00880         else 
00881         if (strcmp(name, "year") == 0) ;
00882         else 
00883         if (strcmp(name, "day") == 0) ;
00884         else 
00885         if (strcmp(name, "msec") == 0) ;
00886         else 
00887         if (strcmp(name, "iscan") == 0) ;
00888         else 
00889         if (strcmp(name, "nscenes") == 0) ;
00890         else 
00891         if (strcmp(name, "nobs") == 0) ;
00892         else 
00893         if (strcmp(name, "lon") == 0) ;
00894         else 
00895         if (strcmp(name, "lat") == 0) ;
00896         else 
00897             if ((ch = strstr(name, "_vic")) != NULL) {
00898         
00899             strcpy(valstr->l3prods[valstr->nl3prods], name);
00900             valstr->nl3prods++;
00901         }
00902         else {
00903         
00904             strcpy(valstr->l2prods[valstr->nl2prods], name);
00905             valstr->nl2prods++;
00906             } 
00907         }
00908     
00909         if ((valstr->l2prods = (prname *)realloc((void *)valstr->l2prods, valstr->nl2prods*sizeof(prname))) == NULL) {
00910             printf("-E- %s line %d: Error reallocating memory to l2 product names.\n",__FILE__,__LINE__);
00911             exit(FATAL_ERROR);
00912         }
00913         if ((valstr->l3prods = (prname *)realloc((void *)valstr->l3prods, valstr->nl3prods*sizeof(prname))) == NULL) {
00914             printf("-E- %s line %d: Error reallocating memory to L3 product names.\n",__FILE__,__LINE__);
00915             exit(FATAL_ERROR);
00916         }
00917         valstr->l2Lambda = NULL;
00918     valstr->l3Lambda = NULL;
00919    
00920     
00921         if (*totalpixs > 1000000) 
00922             l = alloc_valstr(nfiles, 1000000L, valstr);
00923     else
00924             l = alloc_valstr(nfiles, *totalpixs, valstr);
00925         
00926 
00927         for (i=0; i<nfiles; i++) {
00928             sprintf(name, "filename%d", i);
00929         
00930         if (read_glbl_attr(*sd_id, name, (VOIDP) valstr->filenames[i]) != SUCCESS) {
00931             printf("-E- %s line %d: Error reading filename attributes (%s)\n",__FILE__,__LINE__,name);
00932                 status = SDend(*sd_id);
00933             free_valstr(*valstr, 1);
00934             valstr->npixs = 0L;
00935         valstr->nfiles = 0L;
00936                 return(HDF_FUNCTION_ERROR);
00937         }
00938     
00939         }
00940     
00941     *spix = 0L;
00942     
00943     } else {
00944         
00945     if (*spix+valstr->npixs > *totalpixs) valstr->npixs = *totalpixs - *spix;
00946     }   
00947         
00948     PTB( rdSDS(*sd_id,"fileID\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->fileID) );
00949     PTB( rdSDS(*sd_id,"year\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->year) );
00950     PTB( rdSDS(*sd_id,"day\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->day) );
00951     PTB( rdSDS(*sd_id,"msec\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->msec) );
00952     PTB( rdSDS(*sd_id,"lon\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->lon) );
00953     PTB( rdSDS(*sd_id,"lat\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->lat) );
00954     PTB( rdSDS(*sd_id,"iscan\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->iscan) );
00955     PTB( rdSDS(*sd_id,"nscenes\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->nscenes) );
00956     PTB( rdSDS(*sd_id,"nobs\0",(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->nobs) );
00957     for (i=0; i<valstr->nl3prods; i++)
00958     PTB( rdSDS(*sd_id,valstr->l3prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)(valstr->data_vic+i*valstr->npixs)) );
00959        
00960     l = 0;
00961     for (i=0; i<valstr->nl2prods; i++) {
00962         if (strcmp(valstr->l2prods[i], "l2_flags\0") == 0) {
00963         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->l2_flags) );
00964         } else
00965         if (strcmp(valstr->l2prods[i], "mside\0") == 0) {
00966         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->mside) );
00967         } else
00968         if (strcmp(valstr->l2prods[i], "detnum\0") == 0) {
00969         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->detnum) );
00970         } else
00971         if (strcmp(valstr->l2prods[i], "pixnum\0") == 0) {
00972         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)valstr->pixnum) );
00973         } else {
00974         PTB( rdSDS(*sd_id,valstr->l2prods[i],(int32)*spix,0,valstr->npixs,0,(VOIDP)&(valstr->data_val[l*valstr->npixs])) );
00975         ++l;
00976     }
00977     }
00978 
00979     *spix += valstr->npixs;
00980         
00981     return(LIFE_IS_GOOD);
00982     
00983 }    
00984 
00985 
00986 
00987 
00988 
00989 /* ------------------------------------------------------         */
00990 /* msval_writeblocks() - writes consecutive validation            */              
00991 /*          blocks of data                                    */                
00992 /*          calptr comprises the actual data for the 1,000,0000   */
00993 /*          pixels, memory is allocated with the first use        */
00994 /* ------------------------------------------------------         */
00995 
00996 int msval_writeblocks(char *valfile, int32 sd_id, int32_t *spix, msvalstr valstr, int32_t nfiles)
00997 {
00998 
00999     char  name[H4_MAX_NC_NAME];
01000     int32_t  i, j, l;
01001 
01002     idDS ds_id;
01003     ds_id.fid = sd_id;
01004     ds_id.fftype = FMT_L2HDF;
01005     
01006     if (nfiles < 0) nfiles = 0L;    
01007     
01008     if (*spix <= 0 || nfiles > 0) {
01009     
01010         for (i=0; i<valstr.nfiles; i++) {
01011         
01012            j = i + nfiles;
01013            sprintf(name, "filename%d", j);
01014         
01015            PTB( SetChrGA(ds_id, name, (char *)valstr.filenames[i]) );
01016            
01017     }
01018         
01019     }
01020         
01021     PTB( sd_writedata(sd_id,"fileID\0",(VOIDP)valstr.fileID,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01022     PTB( sd_writedata(sd_id,"year\0",(VOIDP)valstr.year,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01023     PTB( sd_writedata(sd_id,"day\0",(VOIDP)valstr.day,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01024     PTB( sd_writedata(sd_id,"msec\0",(VOIDP)valstr.msec,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01025     PTB( sd_writedata(sd_id,"lon\0",(VOIDP)valstr.lon,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01026     PTB( sd_writedata(sd_id,"lat\0",(VOIDP)valstr.lat,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01027     PTB( sd_writedata(sd_id,"iscan\0",(VOIDP)valstr.iscan,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01028     PTB( sd_writedata(sd_id,"nscenes\0",(VOIDP)valstr.nscenes,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01029     PTB( sd_writedata(sd_id,"nobs\0",(VOIDP)valstr.nobs,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01030     for (i=0; i<valstr.nl3prods; i++)
01031     PTB( sd_writedata(sd_id,valstr.l3prods[i],(VOIDP)(valstr.data_vic+i*valstr.npixs),(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01032        
01033     l = 0;
01034     for (i=0; i<valstr.nl2prods; i++) {
01035         if (strcmp(valstr.l2prods[i], "l2_flags\0") == 0) {
01036         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.l2_flags,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01037         } else
01038         if (strcmp(valstr.l2prods[i], "mside\0") == 0) {
01039         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.mside,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01040         } else
01041         if (strcmp(valstr.l2prods[i], "detnum\0") == 0) {
01042         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.detnum,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01043         } else
01044         if (strcmp(valstr.l2prods[i], "pixnum\0") == 0) {
01045         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)valstr.pixnum,(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01046         } else {
01047         PTB( sd_writedata(sd_id,valstr.l2prods[i],(VOIDP)(valstr.data_val+l*valstr.npixs),(int32)*spix,0,0,(int32)valstr.npixs,1,1) );
01048         ++l;
01049     }
01050     }
01051 
01052     *spix += valstr.npixs;
01053     
01054     return(LIFE_IS_GOOD);
01055     
01056 }    
01057 
01058 
01059 
01060 /* ------------------------------------------------------  */
01061 /* rdstrideSDS() - reads complete SDS (scientific data set)*/
01062 /*           for a given stride and returns the data       */
01063 /*                                                         */
01064 /* ------------------------------------------------------  */
01065 int rdstrideSDS(int32 fileID, char *sdsname, int32 stride[2], VOIDP array_data)
01066 {
01067    int32 sds_id, status, numtype;
01068    int32 sds_index, rank, dims[H4_MAX_VAR_DIMS], nattrs;
01069    int32 start[2], edges[2];
01070    char  tmp_sdsname[H4_MAX_NC_NAME];
01071 
01072 
01073    /* Get the SDS index */
01074    sds_index = SDnametoindex(fileID,sdsname);
01075 
01076    if (sds_index < 0) {
01077      printf("-E- %s: SDS \"%s\" not found.\n", "rdSDS",sdsname);
01078      status = SDend(fileID);
01079      return(HDF_FUNCTION_ERROR);
01080    }
01081 
01082 
01083    /* Select the SDS */
01084    if ((sds_id = SDselect(fileID, sds_index)) == FAIL) {
01085      printf("-E- %s line %d: Could not read parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01086      status = SDend(fileID);
01087      return(HDF_FUNCTION_ERROR);
01088    }
01089 
01090    /* Get the rank and number type of the array */
01091    if ((SDgetinfo(sds_id, tmp_sdsname, &rank, dims, &numtype, &nattrs)) == FAIL) {
01092      printf("-E- %s line %d: Could not get info about parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01093      status = SDend(fileID);
01094      return(HDF_FUNCTION_ERROR);
01095    }
01096 
01097 
01098    /* Define the location, pattern and size of the data to read */
01099    /* set 1st dimension */
01100    start[0] = 0;
01101    start[1] = 0;
01102    edges[0] = dims[0];
01103    /* if rank > 1, set 2nd dimension */
01104    if (rank > 1) edges[1] = dims[1];
01105 
01106    /* Based on number type, call the corresponding wrapper
01107    for the HDF SDreaddata function */ 
01108    if ((SDreaddata(sds_id, start, stride, edges, array_data)) == FAIL) {
01109      printf("-E- %s line %d: Could not read data for parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01110      status = SDend(fileID);
01111      return(HDF_FUNCTION_ERROR);
01112    }
01113     
01114    /* Terminate access to the array */
01115    if ((SDendaccess(sds_id)) == FAIL) {
01116      printf("-E- %s line %d: Could not end access for parameter %s in HDF file.\n",__FILE__,__LINE__,sdsname);
01117      status = SDend(fileID);
01118      return(HDF_FUNCTION_ERROR);
01119    }
01120 
01121    return(LIFE_IS_GOOD);
01122    
01123 }
01124 
01125 
01126 int read_glbl_attr(int32 sd_id, char *name, VOIDP ptr) 
01127 {                                           
01128 
01129   int32 index;
01130   
01131   if ((index = SDfindattr(sd_id,name)) == FAIL) return(HDF_FUNCTION_ERROR);
01132 
01133   if (SDreadattr(sd_id,index,(VOIDP)ptr)){ 
01134     printf("-E- %s line %d: Could not get global attribute, %s.\n", __FILE__,__LINE__,(name));
01135     return(HDF_FUNCTION_ERROR);
01136   }
01137 
01138   return(LIFE_IS_GOOD);
01139                                                          
01140 }   
01141 
01142 
01143 
01144 
01145 
01146 
01147 
01148 
01149 int32_t alloc_valstr(int32_t nfiles, int32_t npixs, msvalstr *valstr)
01150 {
01151     
01152     int32_t  len, i;
01153     unsigned char  *p;
01154     
01155     
01156     len = nfiles*sizeof(stname) + 6*npixs*sizeof(int16) + npixs*sizeof(int32) + 2*npixs*sizeof(float32);
01157     
01158     for (i=0; i<valstr->nl3prods; i++) len += npixs*sizeof(float32);
01159     
01160     for (i=0; i<valstr->nl2prods; i++) {
01161     
01162         if (strcmp(valstr->l2prods[i], "l2_flags") == 0) len += npixs*sizeof(int32);
01163     else
01164         if (strcmp(valstr->l2prods[i], "mside") == 0) len += npixs*sizeof(uint8);
01165         else
01166         if (strcmp(valstr->l2prods[i], "detnum") == 0) len += npixs*sizeof(uint8);
01167         else
01168         if (strcmp(valstr->l2prods[i], "pixnum") == 0) len += npixs*sizeof(int16);
01169         else
01170     len += npixs*sizeof(float32);
01171     }
01172     
01173         
01174     if ((p = (unsigned char *) malloc(len)) == NULL) {
01175     printf("%s -Error: Cannot allocate memory to validation data\n",__FILE__);
01176     exit(FATAL_ERROR);
01177     }
01178     valstr->nfiles = nfiles;
01179     valstr->npixs = npixs;
01180     valstr->data = p;
01181     valstr->filenames = (stname *)  p; p += nfiles*sizeof(stname);
01182     valstr->fileID =    (int16  *)  p; p += npixs*sizeof(int16);
01183     valstr->year =  (int16  *)  p; p += npixs*sizeof(int16);
01184     valstr->day =   (int16  *)  p; p += npixs*sizeof(int16);
01185     valstr->msec =  (int32  *)  p; p += npixs*sizeof(int32);
01186     valstr->lon =   (float32 *) p; p += npixs*sizeof(float32);
01187     valstr->lat =   (float32 *) p; p += npixs*sizeof(float32);
01188     valstr->iscan =     (int16   *) p; p += npixs*sizeof(int16);
01189     valstr->nscenes =   (int16   *) p; p += npixs*sizeof(int16);
01190     valstr->nobs =  (int16   *) p; p += npixs*sizeof(int16);
01191     valstr->data_vic =  (float32 *) p; p += npixs*valstr->nl3prods*sizeof(float32);
01192     for (i=0; i<valstr->nl2prods; i++) {
01193         if (strcmp(valstr->l2prods[i], "l2_flags") == 0) {
01194             valstr->l2_flags =  (int32  *) p; p += npixs*sizeof(int32);
01195         } else
01196         if (strcmp(valstr->l2prods[i], "mside") == 0) {
01197         valstr->mside =     (uint8  *) p; p += npixs*sizeof(uint8);
01198         } else
01199         if (strcmp(valstr->l2prods[i], "detnum") == 0) {
01200         valstr->detnum =    (uint8  *) p; p += npixs*sizeof(uint8);
01201         } else
01202         if (strcmp(valstr->l2prods[i], "pixnum") == 0) {
01203         valstr->pixnum =    (int16  *) p; p += npixs*sizeof(int16);
01204         }
01205     }
01206     valstr->data_val =  (float32 *) p;
01207     
01208     return(len);
01209     
01210 }
01211    
01212 
01213 
01214 
01215 
01216 
01217 
01218 
01219 void free_valstr(msvalstr valstr, int all)
01220 {
01221      if (valstr.data != NULL) free(valstr.data);
01222      valstr.data = NULL;
01223      
01224      if (all) {
01225         if (valstr.l2Lambda != NULL) free(valstr.l2Lambda); valstr.l2Lambda = NULL;
01226     if (valstr.l2prods != NULL) free(valstr.l2prods); valstr.l2prods = NULL;
01227     if (valstr.l3Lambda != NULL) free(valstr.l3Lambda); valstr.l3Lambda = NULL;
01228     if (valstr.l3prods != NULL) free(valstr.l3prods); valstr.l3prods = NULL;
01229     if (valstr.input_parms != NULL) free(valstr.input_parms); valstr.input_parms = NULL;
01230      }
01231      
01232 }
01233 
01234