Changeset 1686
- Timestamp:
- 09/11/06 14:26:32 (14 years ago)
- Location:
- TI02-CSML/branches/CSML2
- Files:
-
- 5 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TI02-CSML/branches/CSML2/API/ops_GridSeriesFeature.py
r1594 r1686 13 13 14 14 def getAllowedSubsettings(self): 15 return ['subsetToGridSeries'] 15 return ['subsetToGridSeries'] #other operations 16 16 17 def getDomainReference(self): 18 #Inspects a time position list for the domain reference. 19 #TODO, does not handle a file extract in place of a list. 20 if isinstance(self.domain.domainReference,csml.parser.TimePositionList): 21 time = {} 22 time['t'] = self.domain.domainReference.timePositions 23 domainref = time 24 return domainref 17 def getDomain(self) 18 #need some sort of domain 19 25 20 26 def getDomainComplement(self): 27 #This will return a list containing one or more ordinates: 28 #currently in form [Name, values] 29 #assumes ordinate.axisValues is a file extract 30 #TODO axisValues may be inline -see also domainReference for similar problem 31 domaincomp ={} 32 dc = self.domain.domainComplement 33 #dc should be a grid! 34 if isinstance(dc, csml.parser.Grid): 35 for ordinate in dc.ordinates: 36 if isinstance(ordinate.axisValues,csml.parser.AbstractFileExtract): 37 domaincomp[ordinate.definesAxis]=ordinate.axisValues.getData() 38 else: 39 #build domain complement dictionary: 40 valList=[] 41 for val in ordinate.axisValues.split(','): 42 valList.append(val) 43 domaincomp[ordinate.definesAxis]=valList 44 45 return domaincomp 46 47 def getDomain(self): 48 #returns both the domain reference axes and domain compliment axes in a single domain dictionary 49 #axes are in no particular order 50 domain = {} 51 dr=getDomainReference(self) 52 dc=getDomainComplement(self) 53 for key in dc.keys(): 54 domain[key]=dc[key] 55 for key in dr.keys(): 56 domain[key]=dr[key] 57 return domain 58 59 60 61 62 def subsetToGridSeries(self, timeSubset, csmlpath=None, ncpath=None,**kwargs): 21 def subsetToGridSeries(self, csmlpath=None, ncpath=None,**kwargs): 63 22 #MUST be supplied with a CSMLContainer object to store the subsetted feature in 64 23 65 #pathToSubsetCSML = container.csmlpath66 if ncpath is not None:67 pathToSubsetNetCDF=ncpath68 else:69 pathToSubsetNetCDF='temp.nc'70 71 #deal with longitude requests72 #if the request is in -ve,+ve eg (-30,30) but the data is in (0,360) need to handle this by changing the args.73 dc = self.getDomainComplement()74 for key in dc.keys():75 if key == 'longitude': #how do we test if it is longitude properly?76 for val in dc[key]:77 if val < 0:78 pass79 else:80 if kwargs[key][0] < 0:81 kwargs[key]=(kwargs[key][0]+360,kwargs[key][1])82 if kwargs[key][1] < 0:83 kwargs[key]=(kwargs[key][0],kwargs[key][1]+360)84 85 domainref = getDomainReference(self)86 self.times=timeSubset87 self.files=[]88 strTimes=''89 fulldata=[]90 if len(self.times) == 2:91 try:92 tone=ops_AbstractFeature.__getCDtime(self.times[0])93 except:94 tone=self.times[0]95 try:96 ttwo=ops_AbstractFeature.__getCDtime(self.times[1])97 except:98 ttwo=self.times[1]99 dr=getDomainReference(self)100 self.times=[]101 for time in dr['t'].split():102 timeok=csml.API.ops_AbstractFeature.__compareTimes(tone,time,ttwo)103 if timeok ==1:104 self.times.append(time)105 106 #to keep track of files that have already been fetched. eg. if multiple times are in a single file only need to get data from that file once...107 numFiles=len(self.rangeSet.aggregatedArray.component[0].fileName.split())108 timeToFileRatio=len(domainref['t'].split())/numFiles109 filesFetched=[]110 #get data:111 for time in self.times:112 listPosition=domainref['t'].split().index(time)113 strTimes= strTimes + ' ' + time114 timedim=self.rangeSet.aggregatedArray.component[0].variableName115 for comp in self.rangeSet.aggregatedArray.component:116 filePos=(listPosition)/timeToFileRatio117 if filePos in filesFetched:118 continue #already got data from this file, try next time119 data=comp.getData(fileposition=filePos, times=self.times, **kwargs)120 self.files.append(comp.fileName.split()[filePos])121 if fulldata ==[]:122 fulldata = data.tolist()123 else:124 for item in data.tolist():125 fulldata.append(item)126 filesFetched.append(filePos)127 axisorder = data.getAxisIds() #will need later!128 try:129 caltype=self.domain.domainReference.frame.split(':',1)[0]130 calunits=self.domain.domainReference.frame.split(':',1)[1]131 csml.csmllibs.csmltime.setcdtimeCalendar(caltype)132 except:133 csml.csmllibs.csmltime.setcdtimeCalendar(csml.csmllibs.csmltime.cdtime.DefaultCalendar)134 ### define domain and rangeSet to use for feature in csml document####135 domain=csml.parser.GridSeriesDomain()136 domain.domainReference=csml.parser.TimePositionList(timePositions=strTimes)137 grid=csml.parser.Grid()138 #dc = self.getDomainComplement()139 ordinates= []140 i=0141 valueStore=[] # use the values again later to generate netcdf142 arraySize=0143 totalArraySize=1144 for key in dc.keys():145 arraySize=0146 i=i+1147 god=csml.parser.GridOrdinateDescription()148 god.gridAxesSpanned='dim%s'%i149 god.sequenceRule='+x+y+z'150 god.definesAxis=key151 straxisValues=''152 #now deal with each argument:153 154 if key in kwargs:155 if kwargs[key][0] < kwargs[key][1]:156 for val in dc[key]:157 if val is not '':158 if float(val) >= kwargs[key][0]:159 if float(val) <= kwargs[key] [1]:160 arraySize=arraySize+1161 straxisValues=straxisValues+ str(val) + ', '162 else:#this if deals with selections such as longitude (330,30) where the lower limit is 'greater' than the upper limit in a mathematical sense.163 for val in dc[key]:164 if val is not '':165 if val >= kwargs[key][0]:166 arraySize=arraySize+1167 straxisValues=straxisValues+ str(val) + ', '168 for val in dc[key]:169 if val is not '':170 if val <= kwargs[key] [1]:171 arraySize=arraySize+1172 straxisValues=straxisValues+ str(val) + ', '173 else: # this dimension has not been subsetted at all174 for val in dc[key]:175 if val is not '':176 arraySize=arraySize+1177 straxisValues=straxisValues+ str(val) + ', '178 totalArraySize=totalArraySize*arraySize179 god.axisValues=straxisValues[:-2]180 ordinates.append(god)181 totalArraySize=totalArraySize*len(self.times)182 grid.ordinates=ordinates183 domain.domainComplement=grid184 rangeSet=csml.parser.RangeSet()185 rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=pathToSubsetNetCDF,variableName=self.id,arraySize=[arraySize])186 187 csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()188 subsettedFeature=csmlWrap.createGridSeriesFeature(domain,rangeSet,datasetID="A",featureID="B",description="C")189 #container.appendFeature(subsettedFeature)190 191 ### write netcdf using NCWriter class (wraps cdms) ###192 nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)193 floatTimes=[]194 for time in self.times:195 time=csml.API.ops_AbstractFeature.__getCDtime(time).torel(calunits)196 floatTimes.append(time.value)197 nc.addAxis('t',floatTimes,isTime=1,units=calunits,calendar=caltype)198 for ordinate in ordinates:199 lon,lat=None,None200 if ordinate.definesAxis=='longitude':201 lon=1202 if ordinate.definesAxis=='latitude':203 lat=1204 #convert to list205 vals=[]206 for val in ordinate.axisValues.split(','):207 vals.append(float(val))208 nc.addAxis(ordinate.definesAxis,vals,isLon=lon,isLat=lat,units='')#to do, units attribute for CF compliance209 if len(ordinates)==3:210 axes=['t',axisorder[1],axisorder[2],axisorder[3]]211 elif len(grid.ordinates)==2:212 axes=['t',axisorder[1],axisorder[2]]213 nc.addVariable(fulldata,self.id, axes,units='') #to do, units attribute for CF compliance214 nc.closeFinishedFile()215 24 return subsettedFeature, pathToSubsetNetCDF 216 25 #container.attachNetCDFFile(nc) -
TI02-CSML/branches/CSML2/API/ops_TrajectoryFeature.py
r1025 r1686 1 1 ''' ops_TrajectoryFeature contains operations for TrajectoryFeature''' 2 3 #or movingpintseries - whatever it is called 2 4 3 5 def testmethod(self): -
TI02-CSML/branches/CSML2/parser.py
r1623 r1686 4 4 import sys 5 5 from UserString import MutableString 6 7 #CSML Version 2 NOTES 8 # I have removed obviously not needed classes from CSML V 1 but have left in 9 # many classes eg. AbstractGML SRSReferenceGroup etc that will probably be needed 10 # if they aren't we can remove them later 11 # Added stubs for some new classes (the feature types) 6 12 7 13 # NOTES … … 2009 2015 return csmlFrag 2010 2016 2011 class AbstractDomain(object):2012 """AbstractDomain2013 +domainReference[0..1]:_DomainReference2014 +domainComplement [0...1]:_DomainComplement2015 """2016 def __init__(self,domainReference=None,domainComplement=None, *args,**kwargs):2017 if domainReference:2018 self.domainReference=domainReference2019 if domainComplement:2020 self.domainComplement=domainComplement2021 def fromXML(self,csmlFrag):2022 for frag in csmlFrag.getchildren():2023 if frag.tag == CSML('domainReference'):2024 subfrag=frag.getchildren()[0]2025 if subfrag.tag==CSML('Position'):2026 self.domainReference=Position()2027 elif subfrag.tag == CSML('Trajectory'):2028 self.domainReference=Trajectory()2029 elif subfrag.tag==CSML('OrientedPosition'):2030 self.domainReference=OrientedPosition()2031 elif subfrag.tag == CSML('OrientedTrajectory'):2032 self.domainReference=OrientedTrajectory()2033 elif subfrag.tag == GML('TimeInstant'):2034 self.domainReference=TimeInstant()2035 elif subfrag.tag == GML('TimePositionList'):2036 self.domainReference=TimePositionList()2037 self.domainReference.fromXML(subfrag)2038 2039 if frag.tag == CSML('domainComplement'):2040 subfrag=frag.getchildren()[0]2041 if subfrag.tag==CSML('DirectPositionList'):2042 self.domainComplement=DirectPositionList()2043 self.domainComplement.fromXML(subfrag)2044 elif subfrag.tag==CSML('Grid'):2045 self.domainComplement=Grid()2046 self.domainComplement.fromXML(subfrag)2047 def toXML(self,csmlFrag):2048 if hasattr(self,'domainReference'):2049 frag = Element(CSML('domainReference'))2050 #Note self.domainReference contains a Position or a Trajectory or a OrientedPosition etc... object2051 #self.domainReference.toXML(frag)2052 if isinstance(self.domainReference,OrientedPosition):2053 subFrag=Element(CSML('OrientedPosition'))2054 self.domainReference.toXML(subFrag)2055 frag.append(subFrag)2056 csmlFrag.append(frag)2057 elif isinstance(self.domainReference,OrientedTrajectory):2058 subFrag=Element(CSML('OrientedTrajectory'))2059 self.domainReference.toXML(subFrag)2060 frag.append(subFrag)2061 csmlFrag.append(frag)2062 elif isinstance(self.domainReference,Position):2063 subFrag=Element(CSML('Position'))2064 self.domainReference.toXML(subFrag)2065 frag.append(subFrag)2066 csmlFrag.append(frag)2067 elif isinstance(self.domainReference,Trajectory):2068 subFrag=Element(CSML('Trajectory'))2069 self.domainReference.toXML(subFrag)2070 frag.append(subFrag)2071 csmlFrag.append(frag)2072 elif isinstance(self.domainReference,TimeInstant):2073 subFrag=Element(GML('TimeInstant'))2074 self.domainReference.toXML(subFrag)2075 frag.append(subFrag)2076 csmlFrag.append(frag)2077 elif isinstance(self.domainReference,TimePositionList):2078 subFrag=Element(GML('TimePositionList'))2079 self.domainReference.toXML(subFrag)2080 frag.append(subFrag)2081 csmlFrag.append(frag)2082 if hasattr(self,'domainComplement'):2083 frag = Element(CSML('domainComplement'))2084 if isinstance(self.domainComplement,DirectPositionList):2085 subFrag=Element(CSML('DirectPositionList'))2086 self.domainComplement.toXML(subFrag)2087 frag.append(subFrag)2088 csmlFrag.append(frag)2089 elif isinstance(self.domainComplement,Grid):2090 subFrag=Element(CSML('Grid'))2091 self.domainComplement.toXML(subFrag)2092 frag.append(subFrag)2093 csmlFrag.append(frag)2094 return csmlFrag2095 2017 2096 2018 … … 2509 2431 return csmlFrag 2510 2432 2511 2512 class Grid(GmlGrid):2513 """#Class GmlGrid representing a gml:Grid2514 Grid(GmlGrid)2515 +srsName[0..1]:string2516 +srsdimension[0..1]:string2517 +dimension[0..1]:string2518 +ordinates [0..1]:GridOrdinateDescription2519 """2520 2521 def __init__(self,srsName=None,srsDimension=None,dimension=None,ordinates=None):2522 GmlGrid.__init__(self)2523 if srsName:2524 self.srsName=srsName2525 if srsDimension:2526 self.srsDimension=srsDimension2527 if dimension:2528 self.dimension=dimension2529 if ordinates:2530 self.ordinates=ordinates2531 def fromXML(self, csmlFrag):2532 GmlGrid.fromXML(self,csmlFrag)2533 if csmlFrag.attrib.has_key('srsName'):2534 self.srsName = csmlFrag.attrib['srsName']2535 if csmlFrag.attrib.has_key('srsDimension'):2536 self.srsDimension = csmlFrag.attrib['srsDimension']2537 if csmlFrag.attrib.has_key('dimension'):2538 self.dimension = csmlFrag.attrib['dimension']2539 for frag in csmlFrag:2540 if frag.tag==CSML('ordinate'):2541 checkArrayAttrib(self,'ordinates')2542 ordinate=GridOrdinateDescription()2543 GridOrdinateDescription.fromXML(ordinate, frag)2544 self.ordinates.append(ordinate)2545 def toXML(self,csmlFrag):2546 GmlGrid.toXML(self,csmlFrag)2547 if hasattr(self, 'srsName'):2548 csmlFrag.attrib['srsName'] = self.srsName2549 if hasattr(self, 'srsDimension'):2550 csmlFrag.attrib['srsDimension'] = self.srsDimension2551 if hasattr(self, 'dimension'):2552 csmlFrag.attrib['dimension'] = self.dimension2553 2554 if hasattr(self, 'ordinates'):2555 for ordinate in self.ordinates:2556 frag=Element(CSML('ordinate'))2557 GridOrdinateDescription.toXML(ordinate, frag)2558 csmlFrag.append(frag)2559 return csmlFrag2560 2433 2561 2434 class TimeInstant(object): … … 2919 2792 2920 2793 class RangeSet(AssociationAttributeGroup): 2921 """class GML RangeSet 2922 2923 RangeSet() 2924 +quantityList[0..1]:MeasureOrNullList 2925 +dataBlock[0..1]:DataBlock 2926 +arrayDescriptor[0..1]:arrayDescriptor 2927 +aggregatedArray[0..1]:AggregatedArray 2928 """ 2929 2930 def __init__(self,quantityList=None, dataBlock=None, arrayDescriptor=None, aggregatedArray=None,*args,**kwargs): 2931 if quantityList: 2932 self.quantityList=quantityList 2933 if dataBlock: 2934 self.dataBlock=dataBlock 2935 if arrayDescriptor: 2936 self.arrayDescriptor=arrayDescriptor 2937 if aggregatedArray: 2938 self.aggregatedArray=aggregatedArray 2939 AssociationAttributeGroup.__init__(self,*args,**kwargs) 2794 2795 def __init__(self,*args,**kwargs): 2796 AssociationAttributeGroup.__init__(self,*args,**kwargs) 2940 2797 2941 2798 def fromXML(self,csmlFrag): 2942 for frag in csmlFrag.getchildren():2943 if frag.tag==GML('QuantityList'):2944 self.quantityList=MeasureOrNullList()2945 MeasureOrNullList.fromXML(self.quantityList,frag)2946 elif frag.tag==GML('DataBlock'):2947 self.dataBlock=DataBlock()2948 DataBlock.fromXML(self.dataBlock, frag)2949 elif frag.tag == CSML('NetCDFExtract'):2950 self.arrayDescriptor=NetCDFExtract()2951 NetCDFExtract.fromXML(self.arrayDescriptor,frag)2952 elif frag.tag == CSML('NASAAmesExtract'):2953 self.arrayDescriptor=NASAAmesExtract()2954 NASAAmesExtract.fromXML(self.arrayDescriptor,frag)2955 elif frag.tag == CSML('GRIBExtract'):2956 self.arrayDescriptor=GRIBExtract()2957 GRIBExtract.fromXML(self.ArrayDescriptor,frag)2958 elif frag.tag == CSML('PPExtract'):2959 self.arrayDescriptor=PPExtract()2960 PPExtract.fromXML(self.ArrayDescriptor,frag)2961 elif frag.tag == CSML('AggregatedArray'):2962 self.aggregatedArray=AggregatedArray()2963 AggregatedArray.fromXML(self.aggregatedArray,frag)2964 2799 AssociationAttributeGroup.fromXML(self,csmlFrag) 2965 2800 2966 2801 def toXML(self, csmlFrag): 2967 if hasattr(self,'quantityList'): 2968 frag = Element(GML('QuantityList')) 2969 MeasureOrNullList.toXML(self.quantityList,frag) 2970 csmlFrag.append(frag) 2971 if hasattr(self,'dataBlock'): 2972 frag=Element(GML('DataBlock')) 2973 DataBlock.toXML(self.dataBlock,frag) 2974 csmlFrag.append(frag) 2975 if hasattr(self,'aggregatedArray'): 2976 frag=Element(CSML('AggregatedArray')) 2977 AggregatedArray.toXML(self.aggregatedArray,frag) 2978 csmlFrag.append(frag) 2979 if hasattr(self,'arrayDescriptor'): 2980 if isinstance(self.arrayDescriptor,NetCDFExtract): 2981 frag=Element(CSML('NetCDFExtract')) 2982 NetCDFExtract.toXML(self.arrayDescriptor,frag) 2983 csmlFrag.append(frag) 2984 elif isinstance(self.arrayDescriptor,NASAAmesExtract): 2985 frag=Element(CSML('NASAAmesExtract')) 2986 NASAAmesExtract.toXML(self.arrayDescriptor,frag) 2987 csmlFrag.append(frag) 2988 elif isinstance(self.arrayDescriptor,GRIBExtract): 2989 frag=Element(CSML('GRIBExtract')) 2990 GRIBExtract.toXML(self.arrayDescriptor,frag) 2991 csmlFrag.append(frag) 2992 elif isinstance(self.arrayDescriptor,PPExtract): 2993 frag=Element(CSML('PPExtract')) 2994 PPAmesExtract.toXML(self.arrayDescriptor,frag) 2995 csmlFrag.append(frag) 2996 csmlFrag=AssociationAttributeGroup.toXML(self,csmlFrag) 2997 return csmlFrag 2998 2999 class PointDomain(AbstractDomain): 3000 """ PointDomain(AbstractDomain) 3001 """ 3002 def __init__(self,*args,**kwargs): 3003 AbstractDomain.__init__(self,*args,**kwargs) 3004 def fromXML(self,csmlFrag): 3005 AbstractDomain.fromXML(self,csmlFrag) 3006 def toXML(self,csmlFrag): 3007 AbstractDomain.toXML(self,csmlFrag) 3008 return csmlFrag 3009 3010 class ProfileDomain(AbstractDomain): 3011 """ ProfileDomain(AbstractDomain) 3012 """ 3013 def __init__(self,*args,**kwargs): 3014 AbstractDomain.__init__(self,*args,**kwargs) 3015 def fromXML(self,csmlFrag): 3016 AbstractDomain.fromXML(self,csmlFrag) 3017 def toXML(self,csmlFrag): 3018 AbstractDomain.toXML(self,csmlFrag) 3019 return csmlFrag 3020 3021 class GridDomain(AbstractDomain): 3022 """ GridDomain(AbstractDomain) 3023 """ 3024 def __init__(self,*args,**kwargs): 3025 AbstractDomain.__init__(self,*args,**kwargs) 3026 def fromXML(self,csmlFrag): 3027 AbstractDomain.fromXML(self,csmlFrag) 3028 def toXML(self,csmlFrag): 3029 AbstractDomain.toXML(self,csmlFrag) 3030 return csmlFrag 3031 3032 class PointSeriesDomain(AbstractDomain): 3033 """ PointSeriesDomain(AbstractDomain) 3034 """ 3035 def __init__(self,*args,**kwargs): 3036 AbstractDomain.__init__(self,*args,**kwargs) 3037 def fromXML(self,csmlFrag): 3038 AbstractDomain.fromXML(self,csmlFrag) 3039 def toXML(self,csmlFrag): 3040 AbstractDomain.toXML(self,csmlFrag) 3041 return csmlFrag 3042 3043 class ProfileSeriesDomain(AbstractDomain): 3044 """ ProfileSeriesDomain(AbstractDomain) 3045 """ 3046 def __init__(self,*args,**kwargs): 3047 AbstractDomain.__init__(self,*args,**kwargs) 3048 def fromXML(self,csmlFrag): 3049 AbstractDomain.fromXML(self,csmlFrag) 3050 def toXML(self,csmlFrag): 3051 AbstractDomain.toXML(self,csmlFrag) 3052 return csmlFrag 3053 3054 class GridSeriesDomain(AbstractDomain): 3055 """ GridSeriesDomain(AbstractDomain) 3056 """ 3057 def __init__(self,*args,**kwargs): 3058 AbstractDomain.__init__(self,*args,**kwargs) 3059 def fromXML(self,csmlFrag): 3060 AbstractDomain.fromXML(self,csmlFrag) 3061 def toXML(self,csmlFrag): 3062 AbstractDomain.toXML(self,csmlFrag) 2802 AssociationAttributeGroup.toXML(self,csmlFrag) 3063 2803 return csmlFrag 3064 2804 3065 2805 class PointFeature(AbstractStaticCoverageFeature): 3066 2806 """PointFeature(AbstractStaticCoverageFeature) 3067 +domain[0..1]:PointDomain 3068 """ 3069 def __init__(self, domain=None,*args,**kwargs): 2807 """ 2808 def __init__(self, *args,**kwargs): 3070 2809 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 3071 if domain:3072 self.domain=domain3073 def __str__(self):3074 return 'PointFeature'3075 2810 def fromXML(self,csmlFrag): 3076 2811 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 3077 for frag in csmlFrag.getchildren():3078 if frag.tag == CSML('PointDomain'):3079 self.domain = PointDomain()3080 self.domain.fromXML(frag)3081 2812 def toXML(self,csmlFrag): 3082 2813 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 3083 if hasattr(self,'domain'): 3084 frag = Element(CSML('PointDomain')) 3085 PointDomain.toXML(self.domain,frag) 3086 # if hasattr(self,'href'): 3087 # frag.attrib[XLINK('href')] = self.href 3088 csmlFrag.append(frag) 3089 return csmlFrag 3090 3091 3092 class PointSeriesFeature(AbstractSeriesCoverageFeature): 3093 """PointFeature(AbstractStaticCoverageFeature) 3094 +domain[0..1]:PointSeriesDomain 3095 """ 3096 3097 def __init__(self,domain=None,*args,**kwargs): 3098 AbstractSeriesCoverageFeature.__init__(self,*args,**kwargs) 3099 if domain: 3100 self.domain=domain 3101 def __str__(self): 3102 return 'PointSeriesFeature' 3103 def fromXML(self,csmlFrag): 3104 AbstractSeriesCoverageFeature.fromXML(self,csmlFrag) 3105 for frag in csmlFrag.getchildren(): 3106 if frag.tag == CSML('PointSeriesDomain'): 3107 self.domain = PointSeriesDomain() 3108 self.domain.fromXML(frag) 3109 def toXML(self,csmlFrag): 3110 AbstractSeriesCoverageFeature.toXML(self,csmlFrag) 3111 if hasattr(self,'domain'): 3112 frag = Element(CSML('PointSeriesDomain')) 3113 PointSeriesDomain.toXML(self.domain,frag) 3114 csmlFrag.append(frag) 3115 return csmlFrag 3116 2814 return csmlFrag 2815 2816 class PointCollectionFeature(AbstractStaticCoverageFeature): 2817 """PointCollectionFeature(AbstractStaticCoverageFeature) 2818 """ 2819 def __init__(self, *args,**kwargs): 2820 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2821 def fromXML(self,csmlFrag): 2822 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2823 def toXML(self,csmlFrag): 2824 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2825 return csmlFrag 2826 2827 class PointSeriesFeature(AbstractStaticCoverageFeature): 2828 """PointSeriesFeature(AbstractStaticCoverageFeature) 2829 """ 2830 def __init__(self, *args,**kwargs): 2831 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2832 def fromXML(self,csmlFrag): 2833 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2834 def toXML(self,csmlFrag): 2835 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2836 return csmlFrag 2837 2838 class MovingPointSeriesFeature(AbstractStaticCoverageFeature): 2839 """MovingPointSeriesFeature(AbstractStaticCoverageFeature) 2840 """ 2841 def __init__(self, *args,**kwargs): 2842 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2843 def fromXML(self,csmlFrag): 2844 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2845 def toXML(self,csmlFrag): 2846 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2847 return csmlFrag 2848 3117 2849 class ProfileFeature(AbstractStaticCoverageFeature): 3118 2850 """ProfileFeature(AbstractStaticCoverageFeature) 3119 +domain[0..1]:ProfileDomain 3120 """ 3121 def __init__(self,domain=None,*args,**kwargs): 2851 """ 2852 def __init__(self, *args,**kwargs): 3122 2853 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 3123 if domain:3124 self.domain=domain3125 def __str__(self):3126 return 'ProfileFeature'3127 2854 def fromXML(self,csmlFrag): 3128 2855 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 3129 for frag in csmlFrag.getchildren():3130 if frag.tag == CSML('ProfileDomain'):3131 self.domain = ProfileDomain()3132 ProfileDomain.fromXML(self.domain,frag)3133 2856 def toXML(self,csmlFrag): 3134 2857 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 3135 if hasattr(self,'domain'): 3136 frag = Element(CSML('ProfileDomain')) 3137 ProfileDomain.toXML(self.domain,frag) 3138 csmlFrag.append(frag) 3139 return csmlFrag 3140 2858 return csmlFrag 3141 2859 3142 2860 class ProfileSeriesFeature(AbstractStaticCoverageFeature): 3143 2861 """ProfileSeriesFeature(AbstractStaticCoverageFeature) 3144 +domain[0..1]:ProfileSeriesDomain 3145 """ 3146 def __init__(self,domain=None,*args,**kwargs): 2862 """ 2863 def __init__(self, *args,**kwargs): 3147 2864 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 3148 if domain:3149 self.domain=domain3150 def __str__(self):3151 return 'ProfileSeriesFeature'3152 2865 def fromXML(self,csmlFrag): 3153 2866 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 3154 for frag in csmlFrag.getchildren():3155 if frag.tag == CSML('ProfileSeriesDomain'):3156 self.domain = ProfileSeriesDomain()3157 ProfileSeriesDomain.fromXML(self.domain,frag)3158 2867 def toXML(self,csmlFrag): 3159 2868 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 3160 if hasattr(self,'domain'): 3161 frag = Element(CSML('ProfileSeriesDomain')) 3162 ProfileSeriesDomain.toXML(self.domain,frag) 3163 csmlFrag.append(frag) 3164 return csmlFrag 3165 2869 return csmlFrag 2870 2871 class Section(AbstractStaticCoverageFeature): 2872 """Section(AbstractStaticCoverageFeature) 2873 """ 2874 def __init__(self, *args,**kwargs): 2875 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2876 def fromXML(self,csmlFrag): 2877 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2878 def toXML(self,csmlFrag): 2879 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2880 return csmlFrag 2881 2882 class RaggedSectionFeature(AbstractStaticCoverageFeature): 2883 """RaggedSectionFeature(AbstractStaticCoverageFeature) 2884 """ 2885 def __init__(self, *args,**kwargs): 2886 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2887 def fromXML(self,csmlFrag): 2888 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2889 def toXML(self,csmlFrag): 2890 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2891 return csmlFrag 3166 2892 3167 2893 class GridFeature(AbstractStaticCoverageFeature): 3168 2894 """GridFeature(AbstractStaticCoverageFeature) 3169 +domain[0..1]:GridDomain 3170 """ 3171 3172 def __init__(self,domain=None,*args,**kwargs): 2895 """ 2896 def __init__(self, *args,**kwargs): 3173 2897 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 3174 if domain:3175 self.domain=domain3176 def __str__(self):3177 return 'GridFeature'3178 2898 def fromXML(self,csmlFrag): 3179 2899 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 3180 for frag in csmlFrag.getchildren():3181 if frag.tag == CSML('GridDomain'):3182 self.domain = GridDomain()3183 self.domain.fromXML(frag)3184 2900 def toXML(self,csmlFrag): 3185 2901 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 3186 if hasattr(self,'domain'): 3187 frag = Element(CSML('GridDomain')) 3188 GridDomain.toXML(self.domain,frag) 3189 csmlFrag.append(frag) 3190 return csmlFrag 3191 2902 return csmlFrag 3192 2903 3193 2904 class GridSeriesFeature(AbstractStaticCoverageFeature): 3194 2905 """GridSeriesFeature(AbstractStaticCoverageFeature) 3195 +domain[0..1]:GridSeriesDomain 3196 """ 3197 def __init__(self,domain=None,*args,**kwargs): 2906 """ 2907 def __init__(self, *args,**kwargs): 3198 2908 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 3199 if domain:3200 self.domain=domain3201 def __str__(self):3202 return 'GridSeriesFeature'3203 2909 def fromXML(self,csmlFrag): 3204 2910 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 3205 for frag in csmlFrag.getchildren():3206 if frag.tag == CSML('GridSeriesDomain'):3207 self.domain = GridSeriesDomain()3208 self.domain.fromXML(frag)3209 2911 def toXML(self,csmlFrag): 3210 2912 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 3211 if hasattr(self,'domain'): 3212 frag = Element(CSML('GridSeriesDomain')) 3213 GridSeriesDomain.toXML(self.domain,frag) 3214 csmlFrag.append(frag) 3215 return csmlFrag 3216 3217 3218 2913 return csmlFrag 2914 2915 class ScanningRadarFeature(AbstractStaticCoverageFeature): 2916 """ScanningRadarFeature(AbstractStaticCoverageFeature) 2917 """ 2918 def __init__(self, *args,**kwargs): 2919 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2920 def fromXML(self,csmlFrag): 2921 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2922 def toXML(self,csmlFrag): 2923 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2924 return csmlFrag 2925 2926 class PointFeature(AbstractStaticCoverageFeature): 2927 """PointFeature(AbstractStaticCoverageFeature) 2928 """ 2929 def __init__(self, *args,**kwargs): 2930 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2931 def fromXML(self,csmlFrag): 2932 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2933 def toXML(self,csmlFrag): 2934 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2935 return csmlFrag 2936 2937 2938 2939 2940 2941 3219 2942 class AbstractFeature(AbstractGML): 3220 2943 """AbstractFeature(AbstractStaticCoverageFeature) … … 3241 2964 return csmlFrag 3242 2965 3243 class AbstractGeometryFeature(AbstractFeature): 3244 """ AbstractGeometryFeature(AbstractFeature) 3245 """ 3246 def __init__(self,*args,**kwargs): 3247 AbstractFeature.__init__(self,*args,**kwargs) 3248 def fromXML(self,csmlFrag): 3249 AbstractFeature.fromXML(self,csmlFrag) 3250 def toXML(self, csmlFrag): 3251 AbstractFeature.toXML(self,csmlFrag) 3252 return csmlFrag 3253 3254 3255 class TrajectoryFeature(AbstractGeometryFeature): 3256 """ TrajectoryFeature(AbstractGeometryFeature) 3257 +track[0..1]:Trajectory 3258 """ 3259 def __init__(self,track=None, *args, **kwargs): 3260 AbstractGeometryFeature.__init__(self,*args,**kwargs) 3261 if track: 3262 self.track=track 2966 2967 class PointFeature(AbstractStaticCoverageFeature): 2968 """PointFeature(AbstractStaticCoverageFeature) 2969 +domain[0..1]:PointDomain 2970 """ 2971 def __init__(self, domain=None,*args,**kwargs): 2972 AbstractStaticCoverageFeature.__init__(self,*args,**kwargs) 2973 if domain: 2974 self.domain=domain 3263 2975 def __str__(self): 3264 return 'TrajectoryFeature' 3265 def fromXML(self,csmlFrag): 3266 AbstractGeometryFeature.fromXML(self,csmlFrag) 3267 for frag in csmlFrag.getchildren(): 3268 if frag.tag == CSML('track'): 3269 self.track=Trajectory() 3270 Trajectory.fromXML(self.track,frag) 3271 def toXML(self,csmlFrag): 3272 AbstractGeometryFeature.toXML(self,csmlFrag) 3273 if hasattr(self, 'track'): 3274 frag = Element(CSML('track')) 3275 subFrag=Element(CSML('Trajectory')) 3276 Trajectory.toXML(self.track,subFrag) 3277 frag.append(subFrag) 3278 csmlFrag.append(frag) 3279 return csmlFrag 3280 2976 return 'PointFeature' 2977 def fromXML(self,csmlFrag): 2978 AbstractStaticCoverageFeature.fromXML(self,csmlFrag) 2979 for frag in csmlFrag.getchildren(): 2980 if frag.tag == CSML('PointDomain'): 2981 self.domain = PointDomain() 2982 self.domain.fromXML(frag) 2983 def toXML(self,csmlFrag): 2984 AbstractStaticCoverageFeature.toXML(self,csmlFrag) 2985 if hasattr(self,'domain'): 2986 frag = Element(CSML('PointDomain')) 2987 PointDomain.toXML(self.domain,frag) 2988 # if hasattr(self,'href'): 2989 # frag.attrib[XLINK('href')] = self.href 2990 csmlFrag.append(frag) 2991 return csmlFrag 2992 2993 2994 2995 3281 2996 class AbstractFeatureCollection(AbstractFeature): 3282 2997 """GML AbstractFeatureCollection class … … 3298 3013 self.members.append(PointFeature()) 3299 3014 self.members[-1].fromXML(featureFrag) 3015 if featureFrag.tag == CSML('PointCollectionFeature'): 3016 self.members.append(PointCollectionFeature()) 3017 self.members[-1].fromXML(featureFrag) 3018 if featureFrag.tag == CSML('PointSeriesFeature'): #trajectory? 3019 self.members.append(PointSeriesFeature()) 3020 self.members[-1].fromXML(featureFrag) 3021 if featureFrag.tag == CSML('MovingPointSeriesFeature'): 3022 self.members.append(MovingPointSeriesFeature()) 3023 self.members[-1].fromXML(featureFrag) 3300 3024 if featureFrag.tag == CSML('ProfileFeature'): 3301 3025 self.members.append(ProfileFeature()) 3026 self.members[-1].fromXML(featureFrag) 3027 if featureFrag.tag == CSML('ProfileSeriesFeature'): 3028 self.members.append(ProfileSeriesFeature()) 3029 self.members[-1].fromXML(featureFrag) 3030 if featureFrag.tag == CSML('RaggedSectionFeature'): 3031 self.members.append(RaggedSectionFeature()) 3032 self.members[-1].fromXML(featureFrag) 3033 if featureFrag.tag == CSML('SectionFeature'): 3034 self.members.append(SectionFeature()) 3302 3035 self.members[-1].fromXML(featureFrag) 3303 3036 if featureFrag.tag == CSML('GridFeature'): 3304 3037 self.members.append(GridFeature()) 3305 3038 self.members[-1].fromXML(featureFrag) 3306 if featureFrag.tag == CSML('PointSeriesFeature'):3307 self.members.append(PointSeriesFeature())3308 self.members[-1].fromXML(featureFrag)3309 if featureFrag.tag == CSML('ProfileSeriesFeature'):3310 self.members.append(ProfileSeriesFeature())3311 self.members[-1].fromXML(featureFrag)3312 3039 if featureFrag.tag == CSML('GridSeriesFeature'): 3313 3040 self.members.append(GridSeriesFeature()) 3314 3041 self.members[-1].fromXML(featureFrag) 3315 if featureFrag.tag == CSML(' TrajectoryFeature'):3316 self.members.append( TrajectoryFeature())3042 if featureFrag.tag == CSML('ScanningRadarFeature'): 3043 self.members.append(ScanningRadarFeature()) 3317 3044 self.members[-1].fromXML(featureFrag) 3045 3318 3046 def toXML(self,csmlFrag): 3319 3047 AbstractFeature.toXML(self,csmlFrag) -
TI02-CSML/branches/CSML2/parser_extra.py
r1485 r1686 91 91 class ParserPostProcessor: 92 92 def __init__(self,dataset): 93 if isinstance(dataset,csml.parser.Dataset): 94 self.dataset=dataset 95 else: 96 print 'Error: Could not post-process CSML document' 97 sys.exit() 98 99 def resolveReferences(self): 100 #if there are any phenomenon defintions then 101 #for every <parameter> element, need to resolve any hrefs. 102 if hasattr(self.dataset, 'phenomenonDefinitions'): 103 for feature in self.dataset.featureCollection.members: 104 if hasattr(feature, 'parameter'): 105 if hasattr(feature.parameter,'href'): 106 #search phenomenon definitions for a match. 107 for definition in self.dataset.phenomenonDefinitions.definitionMembers: 108 if definition.id == feature.parameter.href[1:]: #remove '#' 109 #remove href and replace with object 110 feature.parameter.href =None 111 feature.parameter=definition 112 break 113 114 #if there are embedded reference system definitions then 115 #for every DomainReference and DomainComplement need to resolve any srsNames 116 if hasattr(self.dataset, 'referenceSystemDefinitions'): 117 for feature in self.dataset.featureCollection.members: 118 if hasattr(feature, 'domain'): #Trajectory features don't have domains.. 119 if hasattr(feature.domain.domainReference, 'srsName'): 120 #search reference definitions for a match. 121 for definition in self.dataset.referenceSystemDefinitions.definitionMembers: 122 if definition.id == feature.domain.domainReference.srsName[1:]: 123 #remove srsName attribute and replace with Object. 124 pass #Leave this for now. 125 if hasattr(feature.domain, 'domainComplement'): 126 if hasattr(feature.domain.domainComplement, 'srsName'): 127 #search reference definitions for a match. 128 for definition in self.dataset.referenceSystemDefinitions.definitionMembers: 129 if definition.id == feature.domain.domainComplement.srsName[1:]: 130 #remove srsName attribute and replace with Object. 131 pass #Leave this for now. 132 133 #NetCDFExtracts etc may be referenced e.g. as <axisvalues> element. Need to fix these too. 134 if hasattr(self.dataset, 'arrayDescriptors'): 135 for feature in self.dataset.featureCollection.members: 136 if hasattr (feature, 'domain'): 137 if hasattr(feature.domain, 'domainComplement'): 138 if isinstance(feature.domain.domainComplement, csml.parser.Grid): 139 for ordinate in feature.domain.domainComplement.ordinates: 140 #search arrayDescriptors for a match. 141 for arrayDescriptor in self.dataset.arrayDescriptors: 142 if arrayDescriptor.id == ordinate.axisValues[1:]: #remove '#' 143 ordinate.axisValues = arrayDescriptor 144 break 145 #also times may reference 146 if hasattr(feature.domain, 'domainReference'): 147 if hasattr(feature.domain.domainReference, 'times'): 148 if hasattr(feature.domain.domainReference.times, 'href'): 149 #search descriptors for a match. 150 for arrayDescriptor in self.dataset.arrayDescriptors: 151 if arrayDescriptor.id == feature.domain.domainReference.times.href[1:]: #remove '#' 152 #remove times xlink attribute and replace with Object. 153 feature.domain.domainReference.times.timePositions= arrayDescriptor 154 break 155 156 #sys.exit() 157 #uom may reference UnitDefinitions. Replace these too 158 if hasattr(self.dataset,'arrayDescriptors'): 159 for feature in self.dataset.featureCollection.members: 160 if hasattr (feature, 'rangeSet'): 161 if hasattr (feature.rangeSet, 'aggregatedArray'): 162 if hasattr(feature.rangeSet.aggregatedArray, 'uom'): 163 if hasattr(self.dataset, 'unitDefinitions'): 164 for definition in self.dataset.unitDefinitions.definitionMembers: 165 if definition.id== feature.rangeSet.aggregatedArray.uom[1:]: 166 feature.rangeSet.aggregatedArray.uom=definition 167 break 168 #TODO need to also take into account the fact that UnitDefinitions could be externally defined. 93 #Needs rewriting for CSML v2 see V1original. 169 94 170 95 return self.dataset
Note: See TracChangeset
for help on using the changeset viewer.