Debian Codegraph @

src/imageclasses/fun2method_imagelisttype.py

raw symbols SCIP
1 # first 20172403 by [email protected]
2
3 def fun2method(inputfile, outputfile_basename, json_path):
4 """converts MIALib C function declarations into JIPLib C++ methods (outputfile_basename.cc file) and C++ method declarations (outputfile_basename.h file). Currently only convert desctuctive functions, i.e., ERROR_TYPE functions, with IMAGE * as first argument (IMAGE ** not yet taken into account).
5
6 :param inputfile: string for input file containing extern declarations
7 :param outputfile_basename: string for output file basename (i.e. without extension)
8 :returns: True on success, False otherwise
9 :rtype:
10
11 """
12
13 import re
14 import json
15 import sys
16 import os
17 with open(json_path, 'r') as f:
18 old2newDic = json.load(f)
19
20 #old2newDic = json.load(open("old2NewNames.json"))
21
22 ifp=open(inputfile, 'r')
23
24 f = open(outputfile_basename+'.cc', 'w')
25 fh = open(outputfile_basename+'.h', 'w')
26
27
28 lines=ifp.readlines()
29
30 for line in lines:
31 print(line)
32
33 name=re.match(r'extern IMAGE \*\*(.*)\((.*)\);',line)
34 args=re.split(',', name.group(2))
35 re.sub('\**', '', re.sub('.* ', '', args[0]))
36
37 arglist = []
38
39 for i in args:
40 atype=re.match(' *(.* \**)', i).group(1)
41 aname=re.sub('\**', '', re.sub('.* ', '', i))
42 arglist.append([atype, aname])
43
44
45 a= { "name" : name.group(1),
46 "arguments" : arglist }
47
48 print(a)
49
50 imDeclare=[]
51 imRasterArray=[]
52
53 GTDeclare=[]
54 GTVars=[]
55
56 GDTypes = ['GDT_Byte', 'GDT_Int16', 'GDT_UInt16', 'GDT_Int32', 'GDT_UInt32', 'GDT_Float32', 'GDT_Float64']
57 MIATypes = ['uc_', 's_', 'us_', 'i32_', 'u32_', 'f_', 'd_']
58 CTypes = ['unsigned char', 'short int', 'unsigned short int', 'int', 'unsigned int', 'float', 'double']
59
60 methodDeclaration='JimList Jim::'+old2newDic.get(a.get("name"))+'('
61 print(methodDeclaration)
62
63 cSeparator=', '
64 separator = '' # default value in case there are no arguments besides the input image
65
66 if a.get("arguments")[0][0] != 'IMAGE *':
67 print('WARNING: ',line,' does not have IMAGE * as first argument')
68 continue
69 else:
70 imDeclare.append(a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+'=this->getMIA(iband);')
71 cCall=a.get("arguments")[0][1]
72
73 imCount=1
74 for idx, arg in enumerate(a.get("arguments")[1:len(a.get("arguments"))]):
75 print(methodDeclaration)
76 print(idx)
77 print(idx)
78 print(separator)
79 print(arg)
80 print(arg[0])
81
82 if (arg[0]=='IMAGE *'):
83 methodDeclaration+=separator+'Jim& imRaster_'+arg[1]
84 imRasterArray.append('imRaster_'+arg[1])
85 imDeclare.append(arg[0]+' '+arg[1]+'=imRaster_'+arg[1]+'.getMIA(iband);')
86 elif (arg[0]=='G_TYPE '): # G_TYPE as double in python
87 GTDeclare.append(arg[0]+' '+arg[1]+';')
88 GTVars.append(arg[1])
89 methodDeclaration+=separator+'double '+'d_'+arg[1]
90 else:
91 methodDeclaration+=separator+arg[0]+' '+arg[1]
92
93 cCall+=cSeparator+arg[1]
94 separator=', '
95
96 fh.write(re.sub(r'Jim::','',methodDeclaration+separator+'int iband=0);\n'))
97
98 methodDeclaration+=separator+'int iband)'
99
100 f.write(methodDeclaration+'{')
101 f.write('\n\tJimList listout;')
102 f.write('\n\ttry{')
103 f.write('\n\t\tint noutput=2;//todo: depends on miallib function')
104 f.write('\n\t\tif(nrOfBand()<=iband){')
105 f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";')
106 f.write('\n\t\t\tthrow(errorString);')
107 f.write('\n\t\t}')
108 for i in imRasterArray:
109 f.write('\n\t\tif('+i+'.nrOfBand()<=iband){')
110 f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";')
111 f.write('\n\t\t\tthrow(errorString);')
112 f.write('\n\t\t}')
113 #f.write('\n\t\t'+a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+' = 0;') # assigned later depending on destructive or not
114 f.write('\n\t\t'+'IMAGE ** imout = 0;')
115 llen=len(imDeclare)
116 for i in range(0,llen):
117 f.write('\n\t\t'+imDeclare[i])
118 for i in GTDeclare:
119 f.write('\n\t\t'+i)
120
121 if len(GTDeclare)>0:
122 f.write('\n\t\tswitch(getDataType()){')
123 for idx, GDType in enumerate(GDTypes):
124 f.write('\n\t\tcase('+GDType+'):')
125 for var_idx, GTVar in enumerate(GTVars):
126 f.write('\n\t\t\t'+GTVar+'.'+MIATypes[idx]+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');')
127 if (GDType=='GDT_Byte'):
128 f.write('\n\t\t\t'+GTVar+'.'+'generic_'+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');')
129 f.write('\n\t\t\tbreak;')
130 f.write('''
131 \t\tdefault:
132 \t\t\tstd::string errorString="Error: data type not supported";
133 \t\t\tthrow(errorString);
134 \t\t\tbreak;
135 \t\t}''')
136
137
138 f.write('\n\t\timout =::'+a.get("name")+'('+cCall+');')
139 f.write('\n\t\tthis->setMIA(iband);')
140
141 f.write('\n\t\tif (imout){')
142 f.write('\n\t\t\tfor(int iim=0;iim<noutput;++iim){')
143 f.write('\n\t\t\t\tstd::shared_ptr<Jim> imgWriter=std::make_shared<Jim>(imout[iim]);')
144 f.write('\n\t\t\t\timgWriter->copyGeoTransform(*this);')
145 f.write('\n\t\t\t\timgWriter->setProjection(getProjectionRef());')
146 f.write('\n\t\t\t\tlistout.pushImage(imgWriter);')
147 f.write('\n\t\t\t}')
148 f.write('\n\t\t\treturn(listout);')
149 f.write('\n\t\t}')
150
151
152 f.write('\n\t\telse{')
153 f.write('\n\t\t\tstd::string errorString="Error: '+a.get("name")+'() function in MIA failed, returning NULL pointer";')
154 f.write('\n\t\t\tthrow(errorString);')
155 f.write('\n\t\t}')
156
157 f.write('\n\t}')
158
159
160 f.write('''
161 catch(std::string errorString){
162 \tstd::cerr << errorString << std::endl;
163 throw;
164 }
165 catch(...){
166 throw;
167 }
168 }\n''')
169
170 ifp.close()
171
172 f.close()
173 fh.close()
174
175 return(True)
176
177
178
179 ####################################################################
180
181 import sys, getopt
182
183 def main(argv):
184 inputfile="miallib_imagelisttype"
185 outputfile="fun2method_imagelisttype"
186 json_path = "old2NewNames.json"
187 try:
188 opts, args = getopt.getopt(argv,"hi:o:j:",["ifile=","ofile=", "json="])
189 except getopt.GetoptError:
190 print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>')
191 sys.exit(2)
192 for opt, arg in opts:
193 if opt == '-h':
194 print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>')
195 sys.exit()
196 elif opt in ("-i", "--ifile"):
197 inputfile = arg
198 elif opt in ("-o", "--ofile"):
199 outputfile = arg
200 elif opt in ("-j", "--json"):
201 json_path = arg
202 print('Input file is "', inputfile)
203 print('Output file is "', outputfile)
204 print('JSON file is "', json_path)
205
206 fun2method(inputfile, outputfile, json_path)
207
208 if __name__ == "__main__":
209 main(sys.argv[1:])
210
211
212 # cat /usr/local/include/miallib/miallib_*.h | grep '^extern IMAGE \*\*[^\*]' > miallib_imagelist_type
213 # python fun2method_imagelisttype.py -i miallib_imagelist_type -o fun2method_imagelisttype
214 # to automatically insert content of fun2method in jim.h within placeholder //start insert from fun2method -> //end insert from fun2method
215 # sed -i -ne '/\/\/start insert from fun2method_imagelisttype/ {p; r fun2method_imagelisttype.h' -e ':a; n; /\/\/end insert from fun2method_imagelisttype/ {p; b}; ba}; p' jim.h