%% plasiscf.m takes the output of a slice run generated
%% by plasisc.m and stores the electron densities in a
%% text file with the name GPIDyyyydddtt.txt where yyyy is
%% the year, ddd is the day number and tt is the UT time
%% (rounded to nearest hour). 
%% The file contains the altitudes of the denities, the 
%% geographic longitudes and latitudes. It then records 
%% the densities at each altitude for a given longitude 
%% and latitude point. 
%% Note that the resulting files can be VERY large. 
%%

disp('Phil - currently using real(eden) to remove occassional complex results')

dno=daynum(day,month,year);
re=6371;
[lonmat,latmat]=mag2geo3(lonlsh*ones(size(latmatm)),latmatm);

loni=round(rad2deg(lonlsh));
if loni<0
 loni=loni+360;
end 
lonms=num2str(loni,'%03.0f');
filse=['GPID_',int2str(year),'_',num2str(dno,'%03.0f'),'_UT',num2str(round(timmat),'%02.0f'),'_lonm',lonms,'_e.txt'];

fid=fopen(filse,'w');
tits=['GPID Electron Density (m^-3)  Lonm=',lonms,'  UT=',num2str(round(timmat)),'  Day number=',int2str(dno),'  Year=',int2str(year)];
fprintf(fid,[tits,'\n\n']);
fprintf(fid,'No. of long.-lat. points and no. of alt. points\n');
fprintf(fid,'%3.0f %3.0f\n\n',size(eden));  
fprintf(fid,'Altitudes (km)\n');
fprintf(fid,'%5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f\n',dismat(1,:)-re);
fprintf(fid,'\n');
fprintf(fid,'Longitudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(lonmat(:,1)));
fprintf(fid,'\n\n');
fprintf(fid,'Lattudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(latmat(:,1)));
fprintf(fid,'\n\n');
fprintf(fid,'Electron densities (m^-3) at each altitude for given longitude-latitude location\n');
for i=1:size(eden,1)
 fprintf(fid,'Longitude = %4.1f Latitude = %4.1f\n',[rad2deg(lonmat(i)) rad2deg(latmat(i))]);
 fprintf(fid,'%3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e\n',real(eden(i,:)));
 fprintf(fid,'\n\n');
end
fclose(fid);

