| 1 | FITS_data = {} |
| 2 | |
| 3 | function sanitize_line(line) |
| 4 | local line = line:gsub("^%s*", "") |
| 5 | return line |
| 6 | end |
| 7 | |
| 8 | function strip_comment(line) |
| 9 | local comment_index = line:find("%s+/%s+") |
| 10 | if comment_index then |
| 11 | return line:sub(1, comment_index - 1) |
| 12 | else |
| 13 | return line:gsub("%s*$", "") |
| 14 | end |
| 15 | end |
| 16 | |
| 17 | function load_fits(filename) |
| 18 | local header = {} |
| 19 | local general_match = "^([%w%-]+)%s*=%s*(.+)%s*" |
| 20 | local end_match = "END%s*$" |
| 21 | local filepath = kpse.find_file(filename) |
| 22 | if not filepath then |
| 23 | tex.print('\\luaescapestring{\\FITSerr{FITS file not found: ' .. filename .. '}}') |
| 24 | else |
| 25 | frame = io.open(filepath, 'rb') |
| 26 | if not frame then |
| 27 | tex.print('\\luaescapestring{\\FITSerr{Error opening FITS file: ' .. filename .. '}}') |
| 28 | end |
| 29 | repeat |
| 30 | local line = frame:read(80) |
| 31 | if not line then break end |
| 32 | local new_line = strip_comment(sanitize_line(line)) |
| 33 | local header_key, header_value = new_line:match(general_match) |
| 34 | if header_key and header_value then |
| 35 | header[header_key] = header_value |
| 36 | end |
| 37 | until line:match(end_match) or not line |
| 38 | local count = 0 |
| 39 | for k, v in pairs(header) do |
| 40 | if v:sub(1, 1) == "'" and v:sub(-1, -1) == "'" then |
| 41 | header[k] = v:sub(2, -2):gsub("%s*$", "") |
| 42 | end |
| 43 | end |
| 44 | frame:close() |
| 45 | end |
| 46 | return header |
| 47 | end |
| 48 | |
| 49 | function show_fits(header) |
| 50 | for k, v in pairs(header) do |
| 51 | texio.write_nl(k .. ': ' .. v) |
| 52 | end |
| 53 | texio.write('\n') |
| 54 | end |
| 55 | |
| 56 | function get_header_key(header, key) |
| 57 | local header_key = header[tostring(key)] |
| 58 | if header_key ~= nil then |
| 59 | tex.print(header_key) |
| 60 | end |
| 61 | end |