-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodismeta.py
More file actions
137 lines (124 loc) · 4.89 KB
/
modismeta.py
File metadata and controls
137 lines (124 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
class that reads the NASA hdfeos CoreMetadata.0 attribute
and retrieves the orbitnumber, equator crossing time,
image lat/lon corners, etc.
"""
import types
import numpy as N
import pyhdf.SD
class metaParse:
def __init__(self,metaDat,altrDat):
import re
self.metaDat=metaDat
self.altrDat=altrDat
#search for the string following the words "VALUE= "
self.stringObject=\
re.compile('.*VALUE\s+=\s"(?P<value>.*)"',re.DOTALL)
#search for a string that looks like 11:22:33
self.timeObject=\
re.compile('.*(?P<time>\d{2}\:\d{2}\:\d{2}).*',re.DOTALL)
#search for a string that looks like 2006-10-02
self.dateObject=\
re.compile('.*(?P<date>\d{4}-\d{2}-\d{2}).*',\
re.DOTALL)
#search for a string that looks like "(anything between parens)"
self.coordObject=re.compile('.*\((?P<coord>.*)\)',re.DOTALL)
#search for a string that looks like "1234"
self.orbitObject=\
re.compile('.*VALUE\s+=\s(?P<orbit>\d+)\n',re.DOTALL)
def getstring(self,theName):
theString=self.metaDat.split(theName)
#should break into three pieces, we want middle
if len(theString) ==3:
theString=theString[1]
else:
altString=self.altrDat.split(theName)
if len(altString) == 3:
theString=altString[1]
else:
raise "couldn't parse %s" % (theName,)
return theString
def __call__(self,theName):
if theName=='CORNERS':
import string
#look for the corner coordinates by searching for
#the GRINGPOINTLATITUDE and GRINGPOINTLONGITUDE keywords
#and then matching the values inside two round parenthesis
#using the coord regular expression
theString= self.getstring('GRINGPOINTLATITUDE')
theMatch=self.coordObject.match(theString)
thelats=theMatch.group('coord').split(',')
thelats=map(string.atof,thelats)
theString= self.getstring('GRINGPOINTLONGITUDE')
theMatch=self.coordObject.match(theString)
thelongs=theMatch.group('coord').split(',')
thelongs=map(string.atof,thelongs)
coordlist=[]
for i in range(len(thelongs)):
coordlist.append((thelongs[i],thelats[i]))
value=coordlist
#regular value
else:
theString= self.getstring(theName)
#orbitnumber doesn't have quotes
if theName=='ORBITNUMBER':
theMatch=self.orbitObject.match(theString)
if theMatch:
value=theMatch.group('orbit')
else:
raise "couldn't fine ORBITNUMBER"
#expect quotes around anything else:
else:
theMatch=self.stringObject.match(theString)
if theMatch:
value=theMatch.group('value')
theDate=self.dateObject.match(value)
if theDate:
value=theDate.group('date') + " UCT"
else:
theTime=self.timeObject.match(value)
if theTime:
value=theTime.group('time') + " UCT"
else:
raise "couldn't parse %s" % (theName,)
return value
def parseMeta(filename):
if type(filename) == types.StringType:
infile = pyhdf.SD.SD(filename)
elif isinstance(filename,pyhdf.SD.SD):
infile=filename
else:
raise IOError, "need an hdf file or HDFFile instance"
metaDat=infile.__getattr__('CoreMetadata.0')
altrDat=infile.__getattr__('ArchiveMetadata.0')
# level-2 files stores GRING data in here
infile.end()
parseIt=metaParse(metaDat,altrDat)
outDict={}
outDict['orbit']=parseIt('ORBITNUMBER')
outDict['filename']=parseIt('LOCALGRANULEID')
outDict['stopdate']=parseIt('RANGEENDINGDATE')
outDict['startdate']=parseIt('RANGEBEGINNINGDATE')
outDict['starttime']=parseIt('RANGEBEGINNINGTIME')
outDict['stoptime']=parseIt('RANGEENDINGTIME')
outDict['equatortime']=parseIt('EQUATORCROSSINGTIME')
outDict['equatordate']=parseIt('EQUATORCROSSINGDATE')
outDict['nasaProductionDate']=parseIt('PRODUCTIONDATETIME')
outDict['daynight']=parseIt('DAYNIGHTFLAG')
corners=parseIt('CORNERS')
cornerlats=[]
cornerlons=[]
for (lon,lat) in corners:
cornerlats.append(lat)
cornerlons.append(lon)
outDict['cornerlats']=N.array(cornerlats)
outDict['cornerlons']=N.array(cornerlons)
return outDict
def dorun(filename=None):
import sys
if not filename:
filename=\
'MOD021KM.A2006275.0440.005.2008107091833.hdf'
print parseMeta(filename)
if __name__=='__main__':
dorun()