break_downn_section_file.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. #
  5. #
  6. # L. Brodeau, 2017
  7. #
  8. import sys
  9. from os import path
  10. csn = sys.argv[0]
  11. def get_sections(cfile):
  12. list_sections = []
  13. list_coord = []
  14. list_refts = []
  15. f = open(cfile, 'r') ; cread_lines = f.readlines() ; f.close()
  16. jl=0 ; l_stop=False
  17. while not l_stop:
  18. ll = cread_lines[jl]; ls = ll.split(); cc = ls[0]
  19. if cc == 'EOF':
  20. l_stop=True
  21. elif cc[0] != '#':
  22. if cc != 'EOF' and cc[0:13] != 'ref_temp_sali':
  23. #print cc
  24. list_sections.append(cc)
  25. #
  26. # now time to read the coordinates:
  27. jl=jl+1
  28. lok=False
  29. while not lok:
  30. ll = cread_lines[jl]; ls = ll.split(); cc = ls[0]
  31. #print ' ls => ', ls
  32. #print ' cc => ', cc
  33. if cc == 'EOF' or cc[0:13] == 'ref_temp_sali':
  34. print 'break_downn_section_file.py get_sections ERROR1!'; sys.exit(0)
  35. elif cc == '#':
  36. print '#'
  37. else:
  38. #print ' to append ls => ', ls
  39. # Read
  40. list_coord.append(ls)
  41. lok=True
  42. # Check if ref_temp_sali to read:
  43. ll = cread_lines[jl+1]; ls = ll.split(); cc = ls[0]
  44. if cc[0:13] == 'ref_temp_sali':
  45. list_refts.append(ls)
  46. jl=jl+1
  47. else:
  48. list_refts.append(['no','no','no'])
  49. else:
  50. print ' .... '
  51. jl=jl+1
  52. return list_sections, list_coord, list_refts
  53. narg = len(sys.argv)
  54. if narg != 3:
  55. print 'Usage: {} <ASCII section file> <max_number_sect_per_file>'.format(csn)
  56. sys.exit(0)
  57. cf_in = sys.argv[1]
  58. cdum = sys.argv[2]
  59. max_number_sect_per_file = int(cdum)
  60. if not path.exists(cf_in): print ' File: '+cf_in+' is not there!'; sys.exit(0)
  61. cnm = path.basename(cf_in)
  62. cout = cnm[:11]
  63. if cnm[:13] == 'transport_ice': cout = 'transport_ice'
  64. list_sn, list_co, list_ts = get_sections(cf_in)
  65. #list_sections = bt.get_sections_from_file(cf_in)
  66. print ''
  67. #print '\n\n Section:\n', list_sn[:]
  68. #print '\n\n Coor:\n', list_co[:]
  69. #print '\n\n Ref:\n', list_ts[:]
  70. nbs = len(list_sn)
  71. #print ' *** Number of sections =', nbs
  72. #print ' *** Max number of sections in new files =', max_number_sect_per_file
  73. nbfiles = nbs/max_number_sect_per_file
  74. if nbfiles*max_number_sect_per_file < nbs: nbfiles = nbfiles+1
  75. #print ' *** nbfiles =', nbfiles ; # number of sections per file!
  76. #lok = False
  77. #while not lok:
  78. #nbfiles = nbs/(max_number_sect_per_file)
  79. #if nbs%(max_number_sect_per_file) > nbfiles: nbfiles = nbfiles+1
  80. #n_extra_last_file = 0
  81. #if nbfiles*max_number_sect_per_file < nbs: n_extra_last_file = nbs - nbfiles*max_number_sect_per_file
  82. #print ' *** n_extra_last_file =', n_extra_last_file
  83. jcum = 0
  84. for jf in range(nbfiles):
  85. clab = '%2.2i'%(jf+1)
  86. cf_out = cout+'_'+clab+'.dat'
  87. f = open(cf_out, 'wb')
  88. for jsf in range(max_number_sect_per_file):
  89. if jcum < nbs:
  90. f.write(list_sn[jcum]) ; f.write('\n')
  91. vv = list_co[jcum] ; cs=''
  92. for cv in vv:
  93. f.write(cs+cv); cs=' '
  94. f.write('\n')
  95. vv = list_ts[jcum] ; cs=''
  96. if vv[0] != 'no':
  97. for cv in vv: f.write(cs+cv); cs=' '
  98. f.write('\n')
  99. jcum = jcum + 1
  100. f.write('EOF\n')
  101. f.close()
  102. print ' *** '+cf_out+' written!\n'
  103. sys.exit(0)
  104. f = open('data_new.txt', 'wb')
  105. for js in range(nbs):
  106. f.write(list_sn[js]) ; f.write('\n')
  107. vv = list_co[js] ; cs=''
  108. for cv in vv:
  109. f.write(cs+cv); cs=' '
  110. f.write('\n')
  111. vv = list_ts[js] ; cs=''
  112. if vv[0] != 'no':
  113. for cv in vv:
  114. f.write(cs+cv); cs=' '
  115. f.write('\n')
  116. f.write('EOF\n')
  117. f.close()
  118. print ''+csn+' done...\n'
  119. # LocalWords: jl