ocssw  1.0
/disk01/web/ocssw/build/src/l1agen_modis/write_eng_data.c (r8218/r3)
Go to the documentation of this file.
00001 #include "L1A_prototype.h"
00002 #include "EN_eng_data.h"
00003 #include "PGS_MODIS_35005.h"
00004 #include "PGS_SMF.h"
00005 #include "hdfi.h"
00006 #include "hdf.h"
00007 #include "hntdefs.h"
00008 
00009 
00010 PGSt_SMF_status  write_eng_data (EN_VDATA_TYPE_t  *eng_data)
00011 
00012 /*
00013 !C****************************************************************************
00014 
00015 !Description:  This function writes the eng data values for the current scan 
00016                to the L1A file .
00017 
00018 !Input Parameters:
00019                EN_VDATA_TYPE_t  *eng_data   ** The eng_data array structure **
00020 
00021 !Output Parameters:
00022                None
00023 
00024 Return Values:
00025                MODIS_S_SUCCESS              (PGS_MODIS_35005.h)
00026                MODIS_F_WRITE_ENG_DATA_FAIL  (PGS_MODIS_35005.h)
00027 
00028 Externally Defined:  
00029                EN_VDATA_TYPE_t               (EN_eng_data.h)
00030                EN_NUM_VDATAS                 (EN_eng_data.h)
00031                EN_MAX_FIELDS_PER_VDATA       (EN_eng_data.h)
00032                PGSt_SMF_status               (PGS_SMF.h)
00033                MODIS_E_WRITE_VDATA           (PGS_MODIS_35005.h)
00034                EN_SC_ANCILLARY_VDATA_START   (EN_eng_data.h)
00035                EN_SC_ANCILLARY_VDATA_STOP    (EN_eng_data.h)
00036                EN_ANCIL_VDATA_BUFFER_SIZE    (EN_eng_data.h)
00037                MODIS_E_NULL_POINTER          (PGS_MODIS_35005.h)
00038                MODIS_E_VDATA_BUFFER_OVERFLOW (PGS_MODIS_35005.h)
00039 
00040 Called By:
00041                write_scan
00042                handle_missing_scans
00043 
00044 Routines Called:
00045                write_Vdata
00046                log_fmt_msg
00047 
00048 !Revision History:
00049                Revision 2.0  1998/10/26  11:05  EST
00050                John Seaton/SAIC/GSC  (seaton@ltpmail.gsfc.nasa.gov)
00051                Code added to write the Current/Prior S/C Ancillary
00052                Vdatas, which contain multiple size and order data.
00053 
00054                Revision 1.1  1997/09/03  10:55
00055                Tom Johnson/GSC     (johnson@ltpmail.gsfc.nasa.gov)
00056                Incorporate walkthrough comments
00057 
00058                Revision 1.0  1997/07/14  15:58 EDT
00059                David Catozzi/SAIC/GSC (cato@ltpmail.gsfc.nasa.gov)
00060                Original design.
00061 
00062 !Team-unique Header:
00063                This software is developed by the MODIS Science
00064                Data Support Team (SDST) for the National Aeronautics
00065                and Space Administration (NASA), Goddard Space Flight
00066                Center (GSFC), under contract NAS5-32373.
00067 
00068 !References and Credits:
00069                None
00070 
00071 !Design Notes:
00072                None
00073 
00074 !END***************************************************************************
00075 */
00076 
00077 {
00078   /***************************************************************************/
00079   /*                                                                         */
00080   /*                      Declare Local Variables                            */
00081   /*                                                                         */
00082   /*     ---------------------------------------------------------------     */
00083   /*                                                                         */
00084   /*     Allocate record_values[EN_MAX_FIELDS_PER_VDATA] as temporary        */
00085   /*     space to hold the field values to be written for the current        */
00086   /*     Vdata (eng_data[i])                                                 */
00087   /*                                                                         */
00088   /***************************************************************************/
00089 
00090   int i, j, k; 
00091 
00092   char             *routine = "write_eng_data";
00093 
00094   char             msg[300];
00095 
00096   PGSt_SMF_status  returnStatus; 
00097 
00098   PGSt_SMF_status  tempStatus; 
00099 
00100   uint16 record_values[EN_MAX_FIELDS_PER_VDATA];
00101 
00102   unsigned char *record = (unsigned char *) record_values;  
00103 
00104   unsigned char buffer[EN_ANCIL_VDATA_BUFFER_SIZE]="";
00105 
00106   unsigned char *p;
00107 
00108   int cur_pos_in_buf=0;
00109 
00110 
00111 
00112   /***************************************************************************/
00113   /*                                                                         */
00114   /*  Set routine to "write_eng_data"  (done during declaration)             */
00115   /*                                                                         */
00116   /*  Set returnStatus to MODIS_S_SUCCESS                                    */
00117   /*                                                                         */
00118   /***************************************************************************/
00119  
00120   returnStatus = MODIS_S_SUCCESS;
00121 
00122   /***************************************************************************/
00123   /* if input parameters equal NULL                                          */
00124   /* THEN                                                                    */
00125   /*   CALL log_fmt_msg to record error in LogStatus file                    */
00126   /*     INPUTS: MODIS_E_NULL_POINTER, routine, " "                          */
00127   /*     OUTPUTS: None                                                       */
00128   /*     RETURNS: None                                                       */
00129   /*                                                                         */
00130   /*     return MODIS_F_WRITE_ENG_DATA_FAIL                                  */
00131   /* ENDIF                                                                   */
00132   /***************************************************************************/
00133 
00134   if (eng_data == NULL) {
00135     log_fmt_msg(MODIS_E_NULL_POINTER, routine, " ");
00136     return MODIS_F_WRITE_ENG_DATA_FAIL;
00137   }
00138 
00139 
00140   /***************************************************************************/
00141   /*                                                                         */
00142   /*  DO FOR ( i = 0 to EN_NUM_VDATAS-1 )                                    */
00143   /*                                                                         */
00144   /*    IF i = EN_SC_ANCILLARY_VDATA_START or i = EN_SC_ANCILLARY_VDATA_END  */
00145   /*    THEN                                                                 */
00146   /*      initialize unsigned char buffer and a pointer into that buffer     */
00147   /*                                                                         */
00148   /*      initialize cur_pos_in_buf equal to zero                            */
00149   /*                                                                         */
00150   /*      FOR each field in the Vdata (j)                                    */
00151   /*                                                                         */
00152   /*        FOR each order within the field (k)                              */
00153   /*                                                                         */
00154   /*          IF cur_pos_in_buf > EN_ANCIL_DATA_BUFFER_SIZE                  */
00155   /*          THEN                                                           */
00156   /*            CALL log_fmt_msg to record error inthe LogStatus file        */
00157   /*              INPUTS: MODIS_E_VDATA_BUFFER_OVERFLOW, routine, " "        */
00158   /*              OUTPUTS: None                                              */
00159   /*              RETURNS: None                                              */
00160   /*                                                                         */
00161   /*            return MODIS_F_WRITE_ENG_DATA_FAIL                           */
00162   /*          ENDIF                                                          */
00163   /*                                                                         */
00164   /*          SWITCH on eng_data[i].field[j].type                            */
00165   /*                                                                         */
00166   /*             CASE DFNT_INT8 : copy data from                             */
00167   /*                              eng_data[i].fields[j].union_value.i8type   */
00168   /*                              into the buffer                            */
00169   /*                                                                         */
00170   /*                     increment the pointer into the buffer by the size of*/ 
00171   /*                      int8 type                                          */
00172   /*                                                                         */
00173   /*                     increment cur_pos_in_buf by size of int8 type       */
00174   /*                                                                         */
00175   /*                     break CASE                                          */
00176   /*                                                                         */
00177   /*            CASE DFNT_UINT8 : copy data from                             */
00178   /*                              eng_data[i].fields[j].union_value.ui8type  */
00179   /*                              into the buffer                            */
00180   /*                                                                         */
00181   /*                     increment the pointer into the buffer by the size of*/ 
00182   /*                      uint8 type                                         */
00183   /*                                                                         */
00184   /*                     increment cur_pos_in_buf by size of uint8 type      */
00185   /*                                                                         */
00186   /*                     break CASE                                          */
00187   /*                                                                         */
00188   /*            CASE DFNT_INT16 : copy data from                             */
00189   /*                              eng_data[i].fields[j].union_value.i16type  */
00190   /*                              into the buffer                            */
00191   /*                                                                         */
00192   /*                     increment cur_pos_in_buf by size of int16 type      */
00193   /*                                                                         */
00194   /*                     increment the pointer into the buffer by the size of*/ 
00195   /*                      int16 type                                         */
00196   /*                                                                         */
00197   /*                     break CASE                                          */
00198   /*                                                                         */
00199   /*            CASE DFNT_UINT16 : copy data from                            */
00200   /*                               eng_data[i].fields[j].union_value.ui16type*/ 
00201   /*                               into the buffer                           */
00202   /*                                                                         */
00203   /*                     increment cur_pos_in_buf by size of uint16 type     */
00204   /*                                                                         */
00205   /*                     increment the pointer into the buffer by the size of*/ 
00206   /*                      uint16 type                                        */
00207   /*                                                                         */
00208   /*                     break CASE                                          */
00209   /*                                                                         */
00210   /*            CASE DFNT_INT32 : copy data from                             */
00211   /*                              eng_data[i].fields[j].union_value.i32type  */
00212   /*                              into the buffer                            */
00213   /*                                                                         */
00214   /*                     increment cur_pos_in_buf by size of int32 type      */
00215   /*                                                                         */
00216   /*                     increment the pointer into the buffer by the size of*/ 
00217   /*                      int32 type                                         */
00218   /*                                                                         */
00219   /*                     break CASE                                          */
00220   /*                                                                         */
00221   /*            CASE DFNT_UINT32 : copy data from                            */
00222   /*                               eng_data[i].fields[j].union_value.ui32type*/ 
00223   /*                               into the buffer                           */
00224   /*                                                                         */
00225   /*                     increment cur_pos_in_buf by size of uint32 type     */
00226   /*                                                                         */
00227   /*                     increment the pointer into the buffer by the size of*/ 
00228   /*                      uint32 type                                        */
00229   /*                                                                         */
00230   /*                     break CASE                                          */
00231   /*                                                                         */
00232   /*             CASE default : CALL log_fmt_msg to record error in LogStatus*/
00233   /*                             INPUTS: MODIS_E_INVALID_VDATA_TYPE, routine,*/
00234   /*                                     " "                                 */
00235   /*                             OUTPUTS: None                               */
00236   /*                             RETURNS: None                               */
00237   /*                                                                         */
00238   /*                      break CASE                                         */
00239   /*                                                                         */
00240   /*          END SWITCH                                                     */
00241   /*                                                                         */
00242   /*        END FOR (k)                                                      */
00243   /*                                                                         */
00244   /*      END FOR (j)                                                        */
00245   /***************************************************************************/
00246  
00247   for (i = 0; i < EN_NUM_VDATAS; i++)
00248     {
00249      if ((i == EN_SC_ANCILLARY_VDATA_START) || (i == EN_SC_ANCILLARY_VDATA_END)) {
00250        memset(buffer, 0, sizeof(buffer));
00251        p = buffer;
00252        cur_pos_in_buf = 0;
00253        for (j = 0; j < eng_data[i].num_fields; j++) {
00254          for (k = 0; k < eng_data[i].field[j].order; k++ ) {
00255            if (cur_pos_in_buf > EN_ANCIL_VDATA_BUFFER_SIZE) {
00256              sprintf(msg, "Current Position: %d Max Buffer Size: %d", 
00257                      cur_pos_in_buf, EN_ANCIL_VDATA_BUFFER_SIZE);
00258              log_fmt_msg(MODIS_E_VDATA_BUFFER_OVERFLOW, routine, msg);
00259              return MODIS_F_WRITE_ENG_DATA_FAIL;
00260            }
00261 
00262            switch (eng_data[i].field[j].type) {
00263              case DFNT_INT8   :
00264                        memcpy(p, (const void *)&eng_data[i].field[j].union_value[k].i8type,
00265                                   sizeof(eng_data[i].field[j].union_value[k].i8type));
00266                        p += sizeof(eng_data[i].field[j].union_value[k].i8type);
00267                        cur_pos_in_buf += sizeof(eng_data[i].field[j].union_value[k].i8type);
00268                        break;
00269              case DFNT_UINT8   :
00270                        memcpy(p, (const void *)&eng_data[i].field[j].union_value[k].ui8type,
00271                                   sizeof(eng_data[i].field[j].union_value[k].ui8type));
00272                        p += sizeof(eng_data[i].field[j].union_value[k].ui8type);
00273                        cur_pos_in_buf += sizeof(eng_data[i].field[j].union_value[k].ui8type);
00274                        break;
00275              case DFNT_INT16   :
00276                        memcpy(p, (const void *)&eng_data[i].field[j].union_value[k].i16type,
00277                                   sizeof(eng_data[i].field[j].union_value[k].i16type));
00278                        p += sizeof(eng_data[i].field[j].union_value[k].i16type);
00279                        cur_pos_in_buf += sizeof(eng_data[i].field[j].union_value[k].i16type);
00280                        break;
00281              case DFNT_UINT16    :
00282                        memcpy(p, (const void *)&eng_data[i].field[j].union_value[k].ui16type,
00283                                   sizeof(eng_data[i].field[j].union_value[k].ui16type));
00284                        p += sizeof(eng_data[i].field[j].union_value[k].ui16type);
00285                        cur_pos_in_buf += sizeof(eng_data[i].field[j].union_value[k].ui16type);
00286                        break;
00287              case DFNT_INT32    :
00288                        memcpy(p, (const void *)&eng_data[i].field[j].union_value[k].i32type,
00289                                   sizeof(eng_data[i].field[j].union_value[k].i32type));
00290                        p += sizeof(eng_data[i].field[j].union_value[k].i32type);
00291                        cur_pos_in_buf += sizeof(eng_data[i].field[j].union_value[k].i32type);
00292                        break;
00293              case DFNT_UINT32    :
00294                        memcpy(p, (const void *)&eng_data[i].field[j].union_value[k].ui32type,
00295                                   sizeof(eng_data[i].field[j].union_value[k].ui32type));
00296                        p += sizeof(eng_data[i].field[j].union_value[k].ui32type);
00297                        cur_pos_in_buf += sizeof(eng_data[i].field[j].union_value[k].ui32type);
00298                        break;
00299              default             :
00300                        sprintf(msg, "Engineering Data Field Type: %d Valid Range: %d to %d", 
00301                          eng_data[i].field[j].type, EN_MIN_VDATA_TYPE, EN_MAX_VDATA_TYPE);
00302                        log_fmt_msg(MODIS_E_INVALID_VDATA_TYPE, routine, msg);
00303                        break;
00304            }  /* switch */
00305 
00306          } /* end for k */
00307         
00308        } /* for j */
00309 
00310   /***************************************************************************/
00311   /*      CALL write_Vdata to update all fields (write a single consolidated */
00312   /*          record of different data types for the current Vdata to the    */   
00313   /*          file) for this scan                                            */
00314   /*        INPUTS:  eng_data[i].vdata_name, buffer, 1 (Number of Records)   */
00315   /*        OUTPUTS: None                                                    */
00316   /*        RETURN:  tempStatus                                              */
00317   /***************************************************************************/
00318 
00319        tempStatus = write_Vdata(eng_data[i].vdata_name, (unsigned char *)buffer, 1);
00320 
00321   /***************************************************************************/
00322   /*      IF ( tempStatus is FAIL )                                          */  
00323   /*      THEN                                                               */
00324   /*        set Status to MODIS_E_WRITE_VDATA                                */
00325   /*        CALL log_fmt_msg to report that the fields for a Vdata could not */
00326   /*           be written to a scan                                          */ 
00327   /*          INPUTS:  Status, routine, msg                                  */
00328   /*          OUTPUTS: None                                                  */
00329   /*          RETURN:  None                                                  */
00330   /*        set returnStatus to FAIL                                         */
00331   /*      ENDIF                                                              */  
00332   /***************************************************************************/
00333 
00334        if (tempStatus == FAIL)
00335          {
00336           sprintf(msg, "Vdata Name = %s", eng_data[i].vdata_name);
00337           log_fmt_msg(MODIS_E_WRITE_VDATA, routine, msg);
00338           returnStatus = MODIS_F_WRITE_ENG_DATA_FAIL;
00339          }
00340 
00341      }
00342 
00343   /***************************************************************************/
00344   /*   ELSE                                                                  */
00345   /*                                                                         */ 
00346   /*     set all elements of record_values to 0                              */
00347   /*                                                                         */
00348   /***************************************************************************/
00349 
00350      else {
00351        memset(record_values, 0, sizeof(record_values));
00352 
00353 
00354   /***************************************************************************/
00355   /*                                                                         */
00356   /*     DO FOR ( j = 0 to eng_data[i].num_fields-1 )                        */
00357   /*        set record_values[j] to eng_data[i].field[j].value               */
00358   /*     END DO                                                              */
00359   /*     (To consolidate a record (a set of field values) for writing.)      */
00360   /*                                                                         */
00361   /***************************************************************************/
00362 
00363        for (j = 0; j < eng_data[i].num_fields; j++)
00364           record_values[j] = eng_data[i].field[j].value;
00365 
00366 
00367   /***************************************************************************/
00368   /*                                                                         */
00369   /*     CALL write_Vdata to update all fields (write a single consolidated  */
00370   /*        record for the current Vdata to the file) for this scan          */
00371   /*       INPUTS:  eng_data[i].vdata_name, record_values, 1                 */
00372   /*       OUTPUTS: None                                                     */
00373   /*       RETURN:  tempStatus                                               */
00374   /*                                                                         */
00375   /*     IF (tempStatus is FAIL)                                             */
00376   /*     THEN                                                                */
00377   /*        set Status to MODIS_E_WRITE_VDATA                                */
00378   /*        CALL log_fmt_msg to report that the fields for a Vdata could     */
00379   /*           not be written to a scan                                      */
00380   /*          INPUTS:  Status, routine, msg                                  */
00381   /*          OUTPUTS: None                                                  */
00382   /*          RETURN:  None                                                  */
00383   /*        set returnStatus to MODIS_F_WRITE_ENG_DATA_FAIL                  */
00384   /*     ENDIF                                                               */
00385   /*                                                                         */
00386   /*  END DO                                                                 */
00387   /*                                                                         */
00388   /***************************************************************************/
00389 
00390        tempStatus = write_Vdata(eng_data[i].vdata_name, record, 1);
00391        if (tempStatus == FAIL)
00392          {
00393           sprintf(msg, "Vdata Name = %s", eng_data[i].vdata_name);
00394           log_fmt_msg(MODIS_E_WRITE_VDATA, routine, msg);
00395           returnStatus = MODIS_F_WRITE_ENG_DATA_FAIL;
00396          }
00397      }
00398  
00399     }
00400 
00401 
00402   /***************************************************************************/
00403   /*                                                                         */
00404   /*  RETURN returnStatus                                                    */
00405   /*                                                                         */
00406   /***************************************************************************/
00407 
00408   return (returnStatus);
00409 
00410 }
00411