def main(): fname_wrapper = 'PythonWrapper.f95' fname_signature = 'pyshtools.pyf' print('now cracking Fortran file SHTOOLS.f95 using f2py function...') crackfortran.verbose = False crackfortran.dolowercase = False cracked_shtools = crackfortran.crackfortran(fname_wrapper) for subroutine in cracked_shtools: subroutine['f2pyenhancements'] = \ {'fortranname': subroutine['name'].lower()} for subroutine in cracked_shtools: subroutine['name'] = subroutine['name'][2:] interface = { 'block': 'interface', 'name': 'unknown_interface', 'from': '', 'body': cracked_shtools, 'externals': [], 'interfaced': [], 'vars': {} } module = { 'block': 'python module', 'name': '_SHTOOLS', 'from': '', 'body': interface, 'externals': [], 'interfaced': [], 'vars': {} } out = crackfortran.crack2fortran(module) with open(fname_signature, 'w') as outfile: outfile.write(out)
def main(): fname_wrapper = 'PythonWrapper.f95' fname_signature = 'pyshtools.pyf' print('now cracking Fortran file SHTOOLS.f95 using f2py function...') crackfortran.verbose = False crackfortran.dolowercase = False cracked_shtools = crackfortran.crackfortran(fname_wrapper) for subroutine in cracked_shtools: subroutine['f2pyenhancements'] = {'fortranname': subroutine['name'].lower()} for subroutine in cracked_shtools: subroutine['name'] = subroutine['name'][2:] interface = {'block': 'interface', 'name': 'unknown_interface', 'from': '', 'body': cracked_shtools, 'externals': [], 'interfaced': [], 'vars': {}} module = {'block': 'python module', 'name': '_SHTOOLS', 'from': '', 'body': interface, 'externals': [], 'interfaced': [], 'vars': {}} out = crackfortran.crack2fortran(module) with open(fname_signature, 'w') as outfile: outfile.write(out)
# Special setups extra_link_args = [] if sys.platform == 'darwin': extra_link_args += [ '-bundle', '-bundle_loader ' + sys.prefix + '/bin/python' ] kwext = dict(libraries=libs, library_dirs=libdirs, extra_link_args=extra_link_args) if __name__ == '__main__': # Generate pyf files crackfortran.f77modulename = '_core' pyfcode = crackfortran.crack2fortran( crackfortran.crackfortran(['lib/spanlib/spanlib_pywrap.f90'])) f = open('lib/spanlib/spanlib.pyf', 'w') f.write(pyfcode) f.close() crackfortran.f77modulename = 'anaxv' pyfcode = crackfortran.crack2fortran( crackfortran.crackfortran(['lib/spanlib/anaxv.f90'])) f = open('lib/spanlib/anaxv.pyf', 'w') f.write(pyfcode) f.close() # Setup the python module s = setup( name="spanlib", version=version, description=description,
def main(): fname_fortran = 'SHTOOLS.f95' fname_wrapper = 'PythonWrapper.f95' outfile = open(fname_wrapper, 'w') print 'now cracking Fortran file SHTOOLS.f95 using f2py function...' crackfortran.verbose = False crackfortran.dolowercase = False cracked_shtools = crackfortran.crackfortran(fname_fortran) print 'decending through shtools module tree...' module = cracked_shtools[0] interface_old = module['body'][0] interface_new = deepcopy(interface_old) for subroutine in interface_new['body']: modify_subroutine(subroutine) print 'create interface string...' wrapper = crackfortran.crack2fortran(interface_new) wrapperlines = wrapper.split('\n') print 'add implicit none statements' # search for the indices of 'use shtools,' to insert 'implicit none' after iusestatement = [ iline for iline, line in enumerate(wrapperlines) if 'use shtools,' in line ] assert len(iusestatement) == len( interface_new['body']), 'number of subroutines don\'t match' for iline in iusestatement[::-1]: wrapperlines.insert(iline + 1, 2 * crackfortran.tabchar + 'implicit none') print 'add shtools subroutine calls...' # search for the indices of 'end subroutine' iendsubroutine = [ iline for iline, line in enumerate(wrapperlines) if 'end subroutine' in line or 'end function' in line ] assert len(iendsubroutine) == len( interface_new['body']), 'number of subroutines don\'t match' # insert call statements before 'end subroutine' line starting from the end such that we # don't change the preceding indices for sroutine_new, sroutine_old, iline in zip(interface_new['body'], interface_old['body'], iendsubroutine)[::-1]: if sroutine_new['block'] == 'function': newline = 2 * crackfortran.tabchar +\ '%s=%s(' % (sroutine_new['name'], sroutine_old['name']) +\ ','.join(sroutine_old['args']) + ')' elif sroutine_new['block'] == 'subroutine': newline = 2 * crackfortran.tabchar +\ 'call %s(' % sroutine_old['name'] +\ ','.join(sroutine_old['args']) + ')' wrapperlines.insert(iline + 1, '') wrapperlines.insert(iline, newline) print 'writing wrapper to file %s' % fname_wrapper for iline, line in enumerate(wrapperlines): try: firstword = line.split()[0] secondword = line.split()[1] words = [ 'real*8', 'integer', 'integer(kind=4)', 'character*(*)', 'complex*16' ] for word in words: if firstword == word and not secondword[ 0] == ':' or secondword[0] == ',': line = line.replace(word, word + ',') wrapperlines[iline] = line except IndexError: pass for line in wrapperlines[4:-5]: line = line.replace('! in SHTOOLS.f95:SHTOOLS:unknown_interface', '') if len(line) <= 100: outfile.write(line + '\n') else: elems = line.split(',') newline = elems[0] for elem in elems[1:]: if len(newline) > 100: outfile.write(newline + '&\n') newline = ' ' * len(elems[0]) newline += ',' + elem outfile.write(newline + '\n') outfile.close() print '\n==== ALL DONE ====\n'
def main(): fname_fortran = 'SHTOOLS.f95' fname_wrapper = 'PythonWrapper.f95' outfile = open(fname_wrapper, 'w') print 'now cracking Fortran file SHTOOLS.f95 using f2py function...' crackfortran.verbose = False crackfortran.dolowercase = False cracked_shtools = crackfortran.crackfortran(fname_fortran) print 'decending through shtools module tree...' module = cracked_shtools[0] interface_old = module['body'][0] interface_new = deepcopy(interface_old) for subroutine in interface_new['body']: modify_subroutine(subroutine) print 'create interface string...' wrapper = crackfortran.crack2fortran(interface_new) wrapperlines = wrapper.split('\n') print 'add implicit none statements' # search for the indices of 'use shtools,' to insert 'implicit none' after iusestatement = [iline for iline, line in enumerate(wrapperlines) if 'use shtools,' in line] assert len(iusestatement) == len(interface_new['body']), 'number of subroutines don\'t match' for iline in iusestatement[::-1]: wrapperlines.insert(iline + 1, 2 * crackfortran.tabchar + 'implicit none') print 'add shtools subroutine calls...' # search for the indices of 'end subroutine' iendsubroutine = [iline for iline, line in enumerate(wrapperlines) if 'end subroutine' in line or 'end function' in line] assert len(iendsubroutine) == len(interface_new['body']), 'number of subroutines don\'t match' # insert call statements before 'end subroutine' line starting from the end such that we # don't change the preceding indices for sroutine_new, sroutine_old, iline in zip(interface_new['body'], interface_old['body'], iendsubroutine)[::-1]: if sroutine_new['block'] == 'function': newline = 2 * crackfortran.tabchar +\ '%s=%s(' % (sroutine_new['name'], sroutine_old['name']) +\ ','.join(sroutine_old['args']) + ')' elif sroutine_new['block'] == 'subroutine': newline = 2 * crackfortran.tabchar +\ 'call %s(' % sroutine_old['name'] +\ ','.join(sroutine_old['args']) + ')' wrapperlines.insert(iline + 1, '') wrapperlines.insert(iline, newline) print 'writing wrapper to file %s' % fname_wrapper for iline, line in enumerate(wrapperlines): try: firstword = line.split()[0] secondword = line.split()[1] words = ['real*8', 'integer', 'integer(kind=4)', 'character*(*)', 'complex*16'] for word in words: if firstword == word and not secondword[0] == ':' or secondword[0] == ',': line = line.replace(word, word + ',') wrapperlines[iline] = line except IndexError: pass for line in wrapperlines[4:-5]: line = line.replace('! in SHTOOLS.f95:SHTOOLS:unknown_interface', '') if len(line) <= 100: outfile.write(line + '\n') else: elems = line.split(',') newline = elems[0] for elem in elems[1:]: if len(newline) > 100: outfile.write(newline + '&\n') newline = ' ' * len(elems[0]) newline += ',' + elem outfile.write(newline + '\n') outfile.close() print '\n==== ALL DONE ====\n'
def main(): fname_fortran = 'SHTOOLS.f95' fname_wrapper = 'cWrapper.f95' explicite_dims_file = "explicite_dimensions.yml" with open(explicite_dims_file, 'r') as exp_dim_fid: explicite_dims = yaml.safe_load(exp_dim_fid.read()) print('now cracking Fortran file SHTOOLS.f95 using f2py function...') crackfortran.verbose = False crackfortran.dolowercase = False cracked_shtools = crackfortran.crackfortran(fname_fortran) print('decending through shtools module tree...') module = cracked_shtools[0] interface_old = module['body'][0] interface_new = deepcopy(interface_old) for subroutine in interface_new['body']: if subroutine['name'] in explicite_dims: modify_subroutine(subroutine, explicite_dims[subroutine['name']]) else: modify_subroutine(subroutine) print('create interface string...') wrapper = crackfortran.crack2fortran(interface_new) wrapperlines = wrapper.split('\n') # search for the indices of 'use shtools,' to insert 'implicit none' after iusestatement = [ iline for iline, line in enumerate(wrapperlines) if 'use shtools,' in line ] assert len(iusestatement) == len(interface_new['body']), \ 'number of subroutines don\'t match' # search for the indices of 'end subroutine' iendsubroutine = [ iline for iline, line in enumerate(wrapperlines) if 'end subroutine' in line or 'end function' in line ] assert len(iendsubroutine) == len(interface_new['body']), \ 'number of subroutines don\'t match' print('sort variables...') for i in range(len(iusestatement)): declaration = wrapperlines[iusestatement[i] + 1:iendsubroutine[i]] current_order = [(line.split('::')[1]).strip() for line in declaration] order = interface_new['body'][i]['sortvars'] idx = [] for j in range(len(order)): idx.append(current_order.index(order[j])) declaration[:] = [declaration[k] for k in idx] wrapperlines[iusestatement[i] + 1:iendsubroutine[i]] = declaration[:] print('add implicit none statements') for iline in iusestatement[::-1]: wrapperlines.insert(iline + 1, 2 * crackfortran.tabchar + 'implicit none') print('add shtools subroutine calls...') # search for the indices of 'end subroutine' iendsubroutine = [ iline for iline, line in enumerate(wrapperlines) if 'end subroutine' in line or 'end function' in line ] assert len(iendsubroutine) == len(interface_new['body']), \ 'number of subroutines don\'t match' # insert call statements before 'end subroutine' line starting from the # end such that we don't change the preceding indices for sroutine_new, sroutine_old, iline in list( zip(interface_new['body'], interface_old['body'], iendsubroutine))[::-1]: args = create_arg_list(sroutine_old) if sroutine_new['block'] == 'function': newline = 2 * crackfortran.tabchar +\ '%s=%s(' % (sroutine_new['name'], sroutine_old['name']) +\ args+ ')' elif sroutine_new['block'] == 'subroutine': newline = 2 * crackfortran.tabchar +\ 'call %s(' % sroutine_old['name'] +\ args+ ')' wrapperlines.insert(iline + 1, '') wrapperlines.insert(iline, newline) print('add bind statment...') p = re.compile('\s*(subroutine|function)') # search for the indices of 'subroutine' isubroutine = [ iline for iline, line in enumerate(wrapperlines) if p.match(line) is not None ] assert len(isubroutine) == len(interface_new['body']), \ 'number of subroutines don\'t match' for sroutine_new, sroutine_old, iline in list( zip(interface_new['body'], interface_old['body'], isubroutine))[::-1]: wrapperlines[iline] = wrapperlines[iline] + ' bind(c, name=\"' \ + sroutine_old['name'] + '\")' newline = 2 * crackfortran.tabchar + 'use, intrinsic :: iso_c_binding' wrapperlines.insert(iline + 1, newline) print('writing wrapper to file %s' % fname_wrapper) for iline, line in enumerate(wrapperlines): try: firstword = line.split()[0] secondword = line.split()[1] words = [ 'real(kind=c_double)', 'complex(kind=c_double_complex)', 'integer(kind=c_int)', 'character(kind=c_char)', 'real(kind=8)', 'real*8', 'integer', 'integer(kind=4)', 'character*(*)', 'complex*16' ] for word in words: if (firstword == word and not secondword[0] == ':' or secondword[0] == ','): line = line.replace(word, word + ',') wrapperlines[iline] = line except IndexError: pass with open(fname_wrapper, 'w') as outfile: for line in wrapperlines[4:-5]: line = line.replace('! in SHTOOLS.f95:SHTOOLS:unknown_interface', '') if len(line) <= 80: outfile.write(line + '\n') else: elems = line.split(',') newline = elems[0] for elem in elems[1:]: if len(newline) > 80: outfile.write(newline + '&\n') newline = ' ' * len(elems[0]) newline += ',' + elem outfile.write(newline + '\n') print('\n==== ALL DONE ====\n')
# - final setup if len(site_libs): libs = site_libs if len(site_libdirs): libdirs.extend(site_libdirs) # Special setups extra_link_args=[] if sys.platform=='darwin': extra_link_args += ['-bundle','-bundle_loader '+sys.prefix+'/bin/python'] kwext = dict(libraries=libs, library_dirs=libdirs, extra_link_args=extra_link_args) if __name__=='__main__': # Generate pyf files crackfortran.f77modulename = '_core' pyfcode = crackfortran.crack2fortran(crackfortran.crackfortran(['lib/spanlib/spanlib_pywrap.f90'])) f = open('lib/spanlib/spanlib.pyf', 'w') f.write(pyfcode) f.close() crackfortran.f77modulename = 'anaxv' pyfcode = crackfortran.crack2fortran(crackfortran.crackfortran(['lib/spanlib/anaxv.f90'])) f = open('lib/spanlib/anaxv.pyf', 'w') f.write(pyfcode) f.close() # Setup the python module s = setup(name="spanlib", version=version, description=description, author=author, author_email=author_email,