#########################################################################################
# HDF-EOS V2.1 MODIS L1B Coarse Prodcut 
# Original V2.0
# Original V2.1
#	Modified on 10/15/99 by S. Devadiga
#	Includes rescaling of SDS and inclusion L1B band QA SDSs
# Modified V2.1.1
#	Modified on 03/20/00 by S. Devadiga
# 	Fixed the scale/offset problem while computing original radiance/reflectance value
#	from L1B SDSs and then rescaling while computing calibrated value for Coarse L1B SDSs.	
# Modified V2.1.2
#	Modified April 2000 by S. Devadiga
#	Added the -sub option to produce coarse product by subsampling.
#	Fixed metadata production esdt shortname, date time, ESDT shortname, input pointer
#	and local granule id
#	Reworked the scale and range to accomodate possible -ve radiance/reflectance values
#	in the input L1B.
# 
# Product ID: MOD02Cxx.Ayyyyddd.hhmm.vvv.yyyydddhhmmss.hdf
# 	where
#	
#	MOD02Cxx        -> ESDT short name for MODISL1B coarse product 
#			   xx: RS (coarse by average) SS (coarse by subsample)
#	A	        -> EOS-AM1 satellite
#	hhmm	        -> granule beginning time hour and minute
#	yyyyddd	        -> four digit year followed by julian day number within the year
#	vvv	        -> three digit version number for the product
#	yyyydddhhmmss	-> day and time the granule was processed
#	hdf	        -> an hdf file
#
# Description:
#	MOD02Cxx is a Coarse Resolution L1B granule at 5KM resolution created by 
#	averaging or subsampling the 1km resolution data in the L1B 1km data product 
#	(MOD021KM). 
#
#	Fill values and out of range values are excluded from average. Seven land
#	bands and the 1KM reflectance bands are rescaled using the original 
#	reflectance scale and offset, and the emissive bands are rescaled using the
#	radiance scale and offset values. Offset for all coarse L1B SDS are set to
#	0. Scale may differ from band to band so that the input range is properly
#	accomodated within the output range of -5000 and 32767. The coarse resolution 
#	granules contain:
#		all 36 MODIS bands in case of day and mixed granules, whereas
#		    only 16 emissive bands in case of night granules.
#       	aggregated geolocation and viewing geometry
#		3 L1B qa SDSs:
#		    land bands qa (day and mixed)
#		    1km reflectance bands qa (day and mixed)
#		    1km emissive bands qa (all granules) 
#	QA sds are bit fields indicating the presence of at least one out of range or 
#       fill value within the averaging area. When the input granule size is not an
#       integral multiple of 5km, an extra row or column is created by averaging the 
#	residual rows/columns.  
#
#	In case of subsampling no QA SDS are produced. Every 3rd line and sample is
#	considered for output.
#
#	The product will contain the same metadata as in the input 1km L1B product. 
#	However the metadata SHORTNAME, LOCALGRANULEID, PRODUCTIONDATETIME and 
#	INPUTPOINTER are updated. The product will not contain any structural metadata.
#
#	One run of the core executable MOD_PR02CRS.exe can create coarse resolution 
#	product of one or more L1B granules either by subsampling or by averaging.
#	In the MODAPS production system this core executable is run by front-end 
#	perl script. The perl script runs the core executable twice to create
#	coarse product by subsample and average separately.
#
#	L1B reflectance/radiance values from MOD02 SDSs are retrieved by using the formulae:
#		reflectance/radiance = (sds value - offset)*scale 
#	L1B reflectance/radiance values from MOD02CRS are to be retrieved using formulae:
#		reflectance/radiance = (sds value - offset)*scale 
#
#
# MOD02Cxx Fill values:
#	Fill values in the original L1B in the range (65500 - 65535) are
#	shifted in the coarse product to the range (-5000 - -5035). In case
#	of averaging SDS will have only one fill value i.e. -5035 used when
#	no valid observations are available for averaging. In case of
#	subsampling one or more of the following fill values may be present
#	in the coarse SDS.
#		SDS Fill value                                    -5035
#		L1A DN is missing within a scan                   -5034
#		Detector is saturated                             -5033
#		Cannot compute zero point DN                      -5032
#		Detector is dead                                  -5031
#		RSB dn* below bottom end of range in input L1B    -5030
#		(unused)                                          -5029
#		Aggregation algorithm failure                     -5028
#		Rotation of Earth-view sector from nominal
#	         	science collection position               -5027
#		Moon in SV port for TEB band                      -5026
#		(reserved for future use)                         -5001 - -5025
#		NAD colsed upper limit                            -5000
#
#
# MODIS Science Team Member
#       Chris O. Justice
#       MODIS Land Science Team
#       University of Virginia
#       NASA's GSFC Code 923
#       Greenbelt, MD20771
#       phone: 301-286-7372
#       justice@kratmos.gsfc.nasa.gov
#
# Point of Contact
#	Jacques Descloitres
#	MODIS Land Science Team - LDOPE
#	University of Maryland
#       NASA's GSFC Code 922
#       Greenbelt, MD 20771
#       phone: 301-614-5456
#       jack@ltpmail.gsfc.nasa.gov
#
#       Sadashiva Devadiga
#       MODIS Land Science Team - LDOPE
#       Science Systems and Applications Inc.
#       NASA's GSFC Code 922
#       Greenbelt, MD 20771
#       phone: 301-614-5449
#       devadiga@ltpmail.gsfc.nasa.gov
#
# Developer:
#       Sadashiva Devadiga
#       MODIS Land Science Team - LDOPE
#       Science Systems and Applications Inc.
#       NASA's GSFC Code 922
#       Greenbelt, MD 20771
#       phone: 301-614-5449
#       devadiga@ltpmail.gsfc.nasa.gov
#
########################################################################################

Generic Constructs

ECS Metadata Strings:

CoreMetadata.0
	Identical to the CoreMetadata.0 in the input MOD021KM. Following changes are
	made as required.
        	SHORTNAME: MOD02CRS (for average) MOD02CSS (for subset)
		INPUTPOINTER: will contain input MOD021KM granule
		LOCALGRANULEID: updated to reflect change in ESDT short name and
				processing date time.
		PRODUCTIONDATETIME: updated to reflect the processing date time.

ArchiveMetadata.0
	Identical to the ArchiveMetadata.0 in the input MOD021KM.


========================================================================================

StructuralMetadata.0
    Will not contain any Structural Metadata

========================================================================================

Description of SDS Data Fields
-------------------------------

Data Field 1 and 2: (Day and Mixed granules only)
------------------
        Description: EV_250_Avg5km_RefSB_Band* (* -> 1 or 2)
	        Aggregated L1B reflectance count computed by averaging the L1B SDS 
		EV_250_Aggr1km_RefSB in MOD021KM. Values in the valid range are
		used in averaging. 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING      59      "EV_250_Avg5km_RefSB_Band* by averaging EV_250_Aggr1km_RefSB"
	unit	                  STRING      11      "none" 
	valid_range		  INT16       2        -4999, 32767
	_FillValue		  INT16       1        -5000 
	scale_factor	          UINT16      1        (variable) 
	offset	    		  UINT16      1        0 

Data Field (3-7): (Day and Mixed granules only)
-----------------
        Description: EV_500_Aggr5km_RefSB_Band* (* -> 3 - 7) 
	        Aggregated L1B reflectance count computed by averaging the L1B SDS 
		EV_500_Aggr1km_RefSB in MOD021KM. Values in the valid range are 
		used in averaging. 
        Type: HDF-INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING      59      "EV_500_Avg5km_RefSB_Band* by averaging EV_500_Aggr1km_RefSB"
	unit	                  STRING      11      "none" 
	valid_range		  INT16        2       -4999, 32767
	_FillValue		  INT16        1       -5000
	scale_factor	          UINT16       1       (variable) 
	offset	    		  UINT16       1       0 

Data Field (8-22): (Day or mixed granules only)
-----------------
        Description: EV_1KM_Aggr5km_RefSB_Band* (* -> 8-12, 13lo, 13hi, 14lo, 14hi, 15-19, 26) 
	        Aggregated L1B reflectance count computed by averaging the L1B SDS 
		EV_1KM_RefSB in MOD021KM. Values in the valid range are used in averaging. 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
	HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING   variable   "EV_1KM_Avg5km_RefSB_Band* by averaging EV_1KM_RefSB"
	unit	                  STRING      11      "none" 
	valid_range		  INT16        2       -4999, 32767
	_FillValue		  INT16        1       -5000
	scale_factor	          UINT16       1       (variable) 
	offset	    		  UINT16       1       0 

Data Field (23-38): 
-------------------
        Description: EV_1KM_Avg5km_Emissive_Band* (* -> 20-25, 27-36) 
        Type: HDF_INT6
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
	HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING      58      "EV_1KM_Avg5km_Emissive_Band* by averaging EV_1KM_Emissive"
	unit	                  STRING      11      "Watts/m^2/micrometer/steradian" 
	valid_range		  INT16        2       -4999, 32767
	_FillValue		  INT16        1       -5000
	scale_factor	          UINT16       1       (variable) 
	offset	    		  UINT16       1       0 

Data Field 39: (Day and Mixed granules only: not created if subsampling is used)
-------------
	Description: QA_L1B_Avg_Land_Bands
		QA SDS for the 7 seven land bands. Least significant 7 bits are used. 
	Bit Description:
		1:	At least one observation within the averaging window is
		        out of range and/or fill value.
		0:	All obserations are considered good and used in averaging. 
		bit position		bit values
		    0			  band 1 (0 or 1)
		    1			  band 2 (0 or 1)
		    2			  band 3 (0 or 1)
		    3			  band 4 (0 or 1)
		    4			  band 5 (0 or 1)
		    5			  band 6 (0 or 1)
		    6			  band 7 (0 or 1)
		    7			  not used (set to 0) 
	Type: HDF_UINT8
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING      37      "Quality of Aggregated L1B: Land Bands"
	unit			  STRING       9      "bit field"

Data Field 40: (Day and Mixed granules only: not created if subsampling is used)
--------------
	Descciption: QA_L1B_Avg_1KM_Reflectance_Bands
		QA SDS for the 15 1km reflectance bands. Least significant 15 bits are used. 
	Bit Description:
		1:	At least one observation within the averaging window is
		        out of range and/or fill value.
		0:	All obserations are considered good and used in averaging. 
		bit position		bit values
		    0			  band 8 (0 or 1)
		    1			  band 9 (0 or 1)
		    2			  band 10 (0 or 1)
		    3			  band 11 (0 or 1)
		    4			  band 12 (0 or 1)
		    5			  band 13lo (0 or 1)
		    6			  band 13hi (0 or 1)
		    7			  band 14lo (0 or 1)
		    8			  band 14hi (0 or 1)
		    9			  band 15 (0 or 1)
		    10			  band 16 (0 or 1)
		    11			  band 17 (0 or 1)
		    12			  band 18 (0 or 1)
		    13			  band 19 (0 or 1)
		    14			  band 26 (0 or 1)
		    15			  not used (set to 0) 
	Type: HDF-UINT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING      26      "Quality of Aggregated L1B: 1km Reflectance Bands"
	unit			  STRING       9      "bit field"

Data Field 41: (not created if subsampling is used)
--------------
	Descciption: QA_L1B_Avg_1KM_Emissive_Bands 
		QA SDS for the 16 1km emissive bands.
	Bit Description:
		1:	At least one observation within the averaging window is
		        out of range and/or fill value.
		0:	All obserations are considered good and used in averaging. 
		bit position		bit values
		    0			  band 20 (0 or 1)
		    1			  band 21 (0 or 1)
		    2			  band 22 (0 or 1)
		    3			  band 23 (0 or 1)
		    4			  band 24 (0 or 1)
		    5			  band 25 (0 or 1)
		    6			  band 27 (0 or 1)
		    7			  band 28 (0 or 1)
		    8			  band 29 (0 or 1)
		    9			  band 30 (0 or 1)
		    10			  band 31 (0 or 1)
		    11			  band 32 (0 or 1)
		    12			  band 33 (0 or 1)
		    13			  band 34 (0 or 1)
		    14			  band 35 (0 or 1)
		    15			  band 36 (0 or 1)
	Type: HDF_UINT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                  	  Number of
                                  	  Values or
        Attribute		  Data	  Max. String    Value
                         	  Type      Length

        long_name       	  STRING      45      "Quality of Aggregated L1B: 1km Emissive Bands"
	unit			  STRING       9      "bit field"

Data Field 42: 
-------------
        Description: Latitude 
        Type: HDF_FLOAT32
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name		STRING      8  		"Latitude"
	units			STRING	    7		"degrees"
	valid_range		FLOAT	    2		-90.f, 90.f
	_FillValue		FLOAT       1		999.f

Data Field 43: 
--------------
        Description: Longitude 
        Type: HDF_FLOAT32
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      9           "Longitude"
        units                   STRING      7           "degrees"
        valid_range             FLOAT       2           -180.f, 180.f
        _FillValue              FLOAT       1           999.f

Data Field 44: 
--------------
        Description: Height 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      6           "Height"
        units                   STRING      6           "meters"
        valid_range             INT16       2           -400s, 10000s 
        _FillValue              INT16       1           -32767s 

Data Field 45: 
--------------
        Description: SensorZenith 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      12          "SensorZenith"
        units                   STRING      7           "degrees"
        valid_range             INT16       2           0s, 18000s 
	scale_factor		INT16	    1		0.01	        
        _FillValue              INT16       1           -32767s

Data Field 46: 
--------------
        Description: SensorAzimuth 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      13          "SensorAzimuth"
        units                   STRING      7           "degrees"
        valid_range             INT16       2           -18000s, 18000s 
        scale_factor            INT16       1           0.01 
        _FillValue              INT16       1           -32767s

Data Field 47: 
--------------
        Description: Range 
        Type: HDF-INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      12          "Range"
        units                   STRING      6           "meters"
        valid_range             INT16       2           27000s, -1s 
        scale_factor            INT16       1           25 
        _FillValue              INT16       1           0s 

Data Field 48: 
--------------
        Description: SolarZenith 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      11          "SolarZenith"
        units                   STRING      7           "degrees"
        valid_range             INT16       2           0s, 18000s 
        scale_factor            INT16       1           0.01 
        _FillValue              INT16       1           -32767s 

Data Field 49: 
--------------
        Description: SolarAzimuth 
        Type: HDF_INT16
        Dimensions:
                Dimension Name          Value
		XDim			406
		YDim			271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      12          "SolarAzimuth"
        units                   STRING      7           "degrees"
        valid_range             INT16       2           -18000s, 18000s 
        scale_factor            INT16       1           0.01
        _FillValue              INT16       1           -32767s

Data Field 50: 
--------------
	Description: gflags
	Type: HDF_BYTE
	Dimensions:
                Dimension Name          Value
                XDim                    406
                YDim                    271
        HDF Attributes:
                                        Number of
                                        Values or
        Attribute               Data    Max. String      Value
                                Type      Length

        long_name               STRING      6           "gflags"
	_FillValue		UINT8       1		 -1
        Bit 7(MSB)		STRING      23          "1 = invalid input data"
        Bit 6			STRING      30          "1 = no ellipsoid intersection" 
        Bit 5			STRING      26          "1 = no valid terrain data" 
        Bit 4			STRING      39          "1 = DEM missing or of inferior quality" 
        Bit 3			STRING      25          "1 = invalid sensor range" 
========================================================================================