function num_lines = specobjid2deg(specobjid_filepath, deg_filepath, len_format)
%Author: shizhixin
%Email: szhixin@gmail.com
%Date: Dec 11,2011
%This function can change specobjid which is the form of hh mm ss.s+dd mm ss.s to degree
%ex: SDSSJ110851.04+065901.4 --> A1 167.212667 6.983722
%or 110851.04+065901.4 --> A1 167.212667 6.983722
%We do not care about the characters behind SDSSJ110851.04+065901.4 or 110851.04+065901.4
%so these input strings can also be handled like:
%SDSS110851.04+065901.4 0.1816 0.5 (1.5) 0.9 (2.7) Binary AGNa -->
%A1 167.212667 6.983722
%or 110851.04+065901.4 0.1816 0.5 (1.5) 0.9 (2.7) Binary AGNa -->
%A1 167.212667 6.983722
% call this function:
% num_lines = specobjid2deg('specobjid.txt', 'deg.txt', 23)
%specobjid format like: SDSSJ110851.04+065901.4 or 110851.04+065901.4
%len_format = 23(SDSSJ110851.04+065901.4) or len_format=18(110851.04+065901.4)
% hh mm ss.s
% dd:mm:ss.s
% specobjid_filepath = 'specobjid.txt';
% deg_filepath = 'deg.txt';
% len_format = 23;
if len_format == 23 %specobjid format like: SDSSJ000249.07+004504.8
pos_start = 6;
elseif len_format == 18 %specobjid format like: 000249.07+004504.8
pos_start = 1;
else
disp('read_len input error, it must be 23 or 18');
return
end
fid = fopen(specobjid_filepath,'r');
if fid == -1
disp('fopen cannot open the file');
fclose(fid);
return;
end
% name ra dec
% A1 167.212667 6.983722
fid_deg = fopen(deg_filepath,'wt');
fprintf(fid_deg,'%s %s %s\n','name','ra','dec');
num_lines=0;
read_line = fgetl(fid)
while (read_line)~=-1
read_line = read_line(1:len_format)
num_lines = num_lines+1;
pos = pos_start;
hh = str2num(read_line(pos:pos+1));
pos = pos+2;
mm = str2num(read_line(pos:pos+1));
pos = pos+2;
ss = str2num(read_line(pos:pos+4));
pos = pos+5;
if read_line(pos)== '-'
d_sign = -1;
else
d_sign = 1;
end
pos = pos+1;
dd = str2num(read_line(pos:pos+1));
pos = pos+2;
d_mm = str2num(read_line(pos:pos+1));
pos = pos+2;
d_ss = str2num(read_line(pos:pos+3));
ra = (hh + mm/60 + ss/3600)*15;
dec = (dd + d_mm/60 + d_ss/3600)*d_sign;
fprintf(fid_deg,'A%d %f %f\n',num_lines,ra,dec);
read_line = fgets(fid)
end
fclose(fid);
fclose(fid_deg);
end %function specobjid2deg