308 | | The attributes <varname>flag_values</varname> and <varname>flag_meanings</varname> are intended to make variables that contain flag values self describing. The <varname>flag_values</varname> attribute is the same type as the variable to which it is attached, and contains a list of the possible flag values. The <varname>flag_meanings</varname> attribute is a string whose value is a blank separated list of descriptive words or phrases, one for each flag value. If multi-word phrases are used to describe the flag values, then the words within a phrase should be connected with underscores. |
---|
309 | | </para> |
---|
310 | | <example><title>A flag variable</title> |
---|
| 308 | The attributes <varname>flag_values</varname><emphasis role="newtext">, |
---|
| 309 | <varname>flag_masks</varname></emphasis> and <varname>flag_meanings</varname> |
---|
| 310 | are intended to make variables that contain flag values self describing. |
---|
| 311 | <emphasis role="newtext">Status codes and Boolean (binary) condition flags |
---|
| 312 | may be expressed with different combinations of <varname>flag_values</varname> |
---|
| 313 | and <varname>flag_masks</varname> attribute definitions.</emphasis> |
---|
| 314 | </para> |
---|
| 315 | <para> |
---|
| 316 | <emphasis role="newtext">The <varname>flag_values</varname> and <varname>flag_meanings</varname> |
---|
| 317 | attributes describe a status flag consisting of mutually exclusive coded values.</emphasis> |
---|
| 318 | The <varname>flag_values</varname> attribute is the same type as the variable to which |
---|
| 319 | it is attached, and contains a list of the possible flag values. |
---|
| 320 | The <varname>flag_meanings</varname> attribute is a string whose value is a blank |
---|
| 321 | separated list of descriptive words or phrases, one for each flag value. |
---|
| 322 | If multi-word phrases are used to describe the flag values, then the words within |
---|
| 323 | a phrase should be connected with underscores. <emphasis role="newtext">The following example illustrates |
---|
| 324 | the use of flag values to express a speed quality with an enumerated status code.</emphasis> |
---|
| 325 | </para> |
---|
| 326 | <example><title>A flag variable<emphasis role="newtext">, using <varname>flag_values</varname></emphasis></title> |
---|
| 338 | <para> |
---|
| 339 | <emphasis role="newtext">The <varname>flag_masks</varname> and <varname>flag_meanings</varname> |
---|
| 340 | attributes describe a number of independent Boolean conditions using bit field notation by setting |
---|
| 341 | unique bits in each <varname>flag_masks</varname> value. <varname>The flag_masks</varname> attribute |
---|
| 342 | is the same type as the variable to which it is attached, and contains a list of values matching unique |
---|
| 343 | bit fields. The <varname>flag_meanings</varname> attribute is defined as above, one for each |
---|
| 344 | <varname>flag_masks</varname> value. A flagged condition is identified by performing a bitwise AND |
---|
| 345 | of the variable value and each <varname>flag_masks</varname> value; a non-zero result indicates a |
---|
| 346 | <varname>true</varname> condition. Thus, any or all of the flagged conditions may be <varname>true</varname>, |
---|
| 347 | depending on the variable bit settings. The following example illustrates the use of <varname>flag_masks</varname> |
---|
| 348 | to express six sensor status conditions.</emphasis> |
---|
| 349 | </para> |
---|
| 350 | <example> |
---|
| 351 | <title><emphasis role="newtext">A flag variable, using <varname>flag_masks</varname></emphasis></title> |
---|
| 352 | <programlisting><emphasis role="newtext"> |
---|
| 353 | byte sensor_status_qc(time, depth, lat, lon) ; |
---|
| 354 | sensor_status_qc:long_name = "Sensor Status" ; |
---|
| 355 | sensor_status_qc:_FillValue = 0b ; |
---|
| 356 | sensor_status_qc:valid_range = 1b, 63b ; |
---|
| 357 | sensor_status_qc:flag_masks = 1b, 2b, 4b, 8b, 16b, 32b ; |
---|
| 358 | sensor_status_qc:flag_meanings = "low_battery processor_fault |
---|
| 359 | memory_fault disk_fault |
---|
| 360 | software_fault |
---|
| 361 | maintenance_required" ;</emphasis> |
---|
| 362 | </programlisting> |
---|
| 363 | </example> |
---|
| 364 | <para> |
---|
| 365 | <emphasis role="newtext">The <varname>flag_masks</varname>, <varname>flag_values</varname> and |
---|
| 366 | <varname>flag_meanings</varname> attributes, used together, describe a blend of independent Boolean |
---|
| 367 | conditions and enumerated status codes. The <varname>flag_masks</varname> and <varname>flag_values</varname> |
---|
| 368 | attributes are both the same type as the variable to which they are attached. A flagged condition |
---|
| 369 | is identified by a bitwise AND of the variable value and each <varname>flag_masks</varname> value; |
---|
| 370 | a result that matches the <varname>flag_values</varname> value indicates a <varname>true</varname> |
---|
| 371 | condition. Repeated <varname>flag_masks</varname> define a bit field mask that identifies a number |
---|
| 372 | of status conditions with different <varname>flag_values</varname>. The <varname>flag_meanings</varname> |
---|
| 373 | attribute is defined as above, one for each <varname>flag_masks</varname> bit field and |
---|
| 374 | <varname>flag_values</varname> definition. Each <varname>flag_values</varname> and |
---|
| 375 | <varname>flag_masks</varname> value must coincide with a <varname>flag_meanings</varname> value. |
---|
| 376 | The following example illustrates the use of <varname>flag_masks</varname> and <varname>flag_values</varname> |
---|
| 377 | to express two sensor status conditions and one enumerated status code.</emphasis> |
---|
| 378 | </para> |
---|
| 379 | <example> |
---|
| 380 | <title><emphasis role="newtext">A flag variable, using <varname>flag_masks</varname> and <varname>flag_values</varname></emphasis></title> |
---|
| 381 | <programlisting><emphasis role="newtext"> |
---|
| 382 | byte sensor_status_qc(time, depth, lat, lon) ; |
---|
| 383 | sensor_status_qc:long_name = "Sensor Status" ; |
---|
| 384 | sensor_status_qc:_FillValue = 0b ; |
---|
| 385 | sensor_status_qc:valid_range = 1b, 15b ; |
---|
| 386 | sensor_status_qc:flag_masks = 1b, 2b, 12b, 12b, 12b ; |
---|
| 387 | sensor_status_qc:flag_values = 1b, 2b, 4b, 8b, 12b ; |
---|
| 388 | sensor_status_qc:flag_meanings = |
---|
| 389 | "low_battery |
---|
| 390 | hardware_fault |
---|
| 391 | offline_mode calibration_mode maintenance_mode" ;</emphasis> |
---|
| 392 | </programlisting> |
---|
| 393 | </example> |
---|
| 394 | <para> |
---|
| 395 | <emphasis role="newtext">In this case, mutually exclusive values are blended with Boolean values |
---|
| 396 | to maximize use of the available bits in a flag value. The table below represents the four binary |
---|
| 397 | digits (bits) expressed by the <varname>sensor_status_qc</varname> variable in the previous |
---|
| 398 | example.</emphasis> |
---|
| 399 | </para> |
---|
| 400 | <para> |
---|
| 401 | <emphasis role="newtext">Bit 0 and Bit 1 are Boolean values indicating a low battery condition |
---|
| 402 | and a hardware fault, respectively. The next two bits (Bit 2 and Bit 3) express an enumeration |
---|
| 403 | indicating abnormal sensor operating modes. Thus, if Bit 0 is set, the battery is low and if |
---|
| 404 | Bit 1 is set, there is a hardware fault - independent of the current sensor operating mode.</emphasis> |
---|
| 405 | </para> |
---|
| 406 | <table frame="all"><title><emphasis role="newtext">Flag Variable Bits (from Example)</emphasis></title> |
---|
| 407 | <tgroup cols="4" align="left" colsep="1" rowsep="1"> |
---|
| 408 | <colspec colwidth="50pt"/> |
---|
| 409 | <colspec colwidth="50pt"/> |
---|
| 410 | <colspec colwidth="50pt"/> |
---|
| 411 | <colspec colwidth="50pt"/> |
---|
| 412 | <thead> |
---|
| 413 | <row> |
---|
| 414 | <entry><emphasis role="newtext">Bit 3 (MSB)</emphasis></entry> |
---|
| 415 | <entry><emphasis role="newtext">Bit 2</emphasis></entry> |
---|
| 416 | <entry><emphasis role="newtext">Bit 1</emphasis></entry> |
---|
| 417 | <entry><emphasis role="newtext">Bit 0 (LSB)</emphasis></entry> |
---|
| 418 | </row> |
---|
| 419 | </thead> |
---|
| 420 | <tbody> |
---|
| 421 | <row> |
---|
| 422 | <entry></entry> |
---|
| 423 | <entry></entry> |
---|
| 424 | <entry><emphasis role="newtext">H/W Fault</emphasis></entry> |
---|
| 425 | <entry><emphasis role="newtext">Low Batt</emphasis></entry> |
---|
| 426 | </row> |
---|
| 427 | </tbody> |
---|
| 428 | </tgroup> |
---|
| 429 | </table> |
---|
| 430 | <para> |
---|
| 431 | <emphasis role="newtext">The remaining bits (Bit 2 and Bit 3) are decoded as follows:</emphasis> |
---|
| 432 | </para> |
---|
| 433 | <table frame="all"><title><emphasis role="newtext">Flag Variable Bit 2 and Bit 3 (from Example)</emphasis></title> |
---|
| 434 | <tgroup cols="3" align="left" colsep="1" rowsep="1"> |
---|
| 435 | <colspec colwidth="50pt"/> |
---|
| 436 | <colspec colwidth="50pt"/> |
---|
| 437 | <colspec colwidth="100pt"/> |
---|
| 438 | <thead> |
---|
| 439 | <row> |
---|
| 440 | <entry><emphasis role="newtext">Bit 3</emphasis></entry> |
---|
| 441 | <entry><emphasis role="newtext">Bit 2</emphasis></entry> |
---|
| 442 | <entry><emphasis role="newtext">Mode</emphasis></entry> |
---|
| 443 | </row> |
---|
| 444 | </thead> |
---|
| 445 | <tbody> |
---|
| 446 | <row> |
---|
| 447 | <entry><emphasis role="newtext">0</emphasis></entry> |
---|
| 448 | <entry><emphasis role="newtext">1</emphasis></entry> |
---|
| 449 | <entry><emphasis role="newtext">offline_mode</emphasis></entry> |
---|
| 450 | </row> |
---|
| 451 | <row> |
---|
| 452 | <entry><emphasis role="newtext">1</emphasis></entry> |
---|
| 453 | <entry><emphasis role="newtext">0</emphasis></entry> |
---|
| 454 | <entry><emphasis role="newtext">calibration_mode</emphasis></entry> |
---|
| 455 | </row> |
---|
| 456 | <row> |
---|
| 457 | <entry><emphasis role="newtext">1</emphasis></entry> |
---|
| 458 | <entry><emphasis role="newtext">1</emphasis></entry> |
---|
| 459 | <entry><emphasis role="newtext">maintenance_mode</emphasis></entry> |
---|
| 460 | </row> |
---|
| 461 | </tbody> |
---|
| 462 | </tgroup> |
---|
| 463 | </table> |
---|
| 464 | <para> |
---|
| 465 | <emphasis role="newtext">The "12b" flag mask is repeated in the <varname>sensor_status_qc</varname> |
---|
| 466 | <varname>flag_masks</varname> definition to explicitly declare the recommended bit field masks to |
---|
| 467 | repeatedly AND with the variable value while searching for matching enumerated values. An application |
---|
| 468 | determines if any of the conditions declared in the <varname>flag_meanings</varname> list are |
---|
| 469 | <varname>true</varname> by simply iterating through each of the <varname>flag_masks</varname> and |
---|
| 470 | AND'ing them with the variable. When a result is equal to the corresponding <varname>flag_values</varname> |
---|
| 471 | element, that condition is <varname>true</varname>. The repeated <varname>flag_masks</varname> enable |
---|
| 472 | a simple mechanism for clients to detect all possible conditions.</emphasis> |
---|
| 473 | </para> |
---|