Skip to content

Instantly share code, notes, and snippets.

@basilleaf
Created August 14, 2013 00:02
Show Gist options
  • Save basilleaf/6226869 to your computer and use it in GitHub Desktop.
Save basilleaf/6226869 to your computer and use it in GitHub Desktop.
fixing COUVIS ring_obs_ids in .TAB files
# fixing COUVIS ring_obs_ids in .TAB files
filename = 'ring/COUVIS_0012_ring_summary.tab'
new_filename = 'new/' + filename
new_file = open(new_filename, "w")
for l in open(filename).readlines():
fname = l.split(',')[1] # file spec name in field 1, quotes intact
ring_obs_id = l.split(',')[2][1:].strip('"').strip() # ring_obs_id in field 2 and strip out quotes and spaces
ring_obs_id_split = ring_obs_id.split('/') # ring_obs_id as components list
if len(fname.split('/')[-1].split('_')) > 4: # how many components are in file name split by '_'
# > 4 means this obs has seconds too, add to ring_obs_id time field
ring_obs_id_split[3] += "-" + fname.split('/')[-1].split('_')[-1].split('.')[0]
new_ring_obs_id = '/'.join(ring_obs_id_split).strip().ljust(32)
else:
new_ring_obs_id = ring_obs_id.ljust(32)
# print fname + ',"' + new_ring_obs_id + '"'
line = l.split(',')
line[2] = '"' + new_ring_obs_id + '"' # replace ring_obs_id with new one in our line
new_file.write(','.join(line))
new_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment