Commit 46dee600 authored by Jürgen Matzka's avatar Jürgen Matzka
Browse files
parents f8cceaee 85deba7f
......@@ -21,8 +21,9 @@
% attachment: same file
clear all
startdate = jd2000(2018, 04, 01); % HERE START DATE FOR non-online
enddate = jd2000(2018, 05, 31); % HERE END DATE FOR non-online
startdate = jd2000(2018, 05, 23); % HERE START DATE FOR non-online
enddate = jd2000(2018, 05, 23); % HERE END DATE FOR non-online
%station = 'VNA1';
%station = 'WNG1';
......
File added
......@@ -38,9 +38,9 @@ timeshift_array = [-300:1:300]';
number_DI = 1000; % read in all DI-measurements for this year
number_DI = 5; % read in the last x measurements in this year
number_DI = 0; % use date range from below
year = 2017;
start_mmdd = '1201';
end___mmdd = '1231';
year = 2014;
start_mmdd = '0101';
end___mmdd = '0101';
......
xlwrite Read xls-files with Linux
function hhmmss=dday2hhmmss(dday)
% Convert decimal day to HHMMSS as number
if isnumeric(dday)
% Hours
hh=floor(dday*24);
% Minutes
mm=floor(mod(dday*24,1)*60);
% seconds
ss=round(mod(dday*24*60,1)*60);
if (ss==60)
ss=0;
if (mm==59)
mm=0;
hh=hh+1;
else
mm=mm+1;
end
end
hhmmss=sprintf('%02u%02u%02u',hh,mm,ss);
else
a1 = strtrim(dday);
hhmmss=[a1(1:2) a1(4:5) a1(7:8)];
end
end
\ No newline at end of file
function state=my_xlswrite(opsys,varargin)
% Call correct xls(x) writing routne - depending on O/S
if (opsys(1:2) == 'PC')
state=xlswrite(varargin{:});
elseif (opsys(1:2) == 'GL')
state=xlwrite(varargin{:});
end
end
\ No newline at end of file
function write_DI_xls(path,DI,file_template,del)
% /////////////////////////////////////////////////////////////////////////
% Load libraries
% /////////////////////////////////////////////////////////////////////////
opsys = computer;
if strcmp(opsys(1:2), 'PC') == 1
bs = '\'; % backslash
elseif strcmp(opsys(1:2), 'GL') == 1
bs = '/'; % slash
javaaddpath('lib/xlwrite/poi_library/poi-3.8-20120326.jar');
javaaddpath('lib/xlwrite/poi_library/poi-ooxml-3.8-20120326.jar');
javaaddpath('lib/xlwrite/poi_library/poi-ooxml-schemas-3.8-20120326.jar');
javaaddpath('lib/xlwrite/poi_library/xmlbeans-2.3.0.jar');
javaaddpath('lib/xlwrite/poi_library/dom4j-1.6.1.jar');
javaaddpath('lib/xlwrite/poi_library/stax-api-1.0.1.jar');
else
display('operating system not recognised');
end
% /////////////////////////////////////////////////////////////////////
% Create new xlsx-Files
% /////////////////////////////////////////////////////////////////////
% Create folders
if (~exist(path,'dir')); mkdir(path); end
% Create filename
file_new=[path bs 'DI_' DI.INFO.station(1:3) '_' ...
num2str(DI.DATE.yyyymmdd) num2str(DI.D.EU.time(1:4)) '.xls'];
if (exist(file_new,'file')==2)
if del
warning(['File ' file_new ' will be overwritten.']);
else
warning(['File ' file_new ' already exists.']);
return;
end
end
% Copy file from template
copyfile(file_template,file_new);
% /////////////////////////////////////////////////////////////////////
% Write Info
% /////////////////////////////////////////////////////////////////////
% Observatory station name
tmp{1}=DI.INFO.station;
% Year of abs. measurement
tmp{2}=DI.DATE.yyyymmdd(1:4);
% MMDD
tmp{3}=DI.DATE.yyyymmdd([5:8]);
my_xlswrite(opsys,file_new,tmp,1,'A8');
clearvars tmp
% Observer name
tmp{1}=DI.INFO.observer;
% Azimuth mark
tmp{2}=DI.AZ.number;
% Theodolite type
tmp{3}='020';
% Units
tmp{4}='dm';
my_xlswrite(opsys,file_new,tmp,1,'E8');
clearvars tmp
% Pillar number
tmp{1}=1;
my_xlswrite(opsys,file_new,tmp,1,'F13');
clearvars tmp
% /////////////////////////////////////////////////////////////////////
% Write azimuth mark readings
% /////////////////////////////////////////////////////////////////////
% Sensor dn / Sensor up
fields={'SD' 'SU'};
for i=1:length(fields)
tmpval=DI.AZ.(fields{i}).D;
tmpval=floor(tmpval)+mod(tmpval,1)*0.6;
tmp{2*i-1}=tmpval(1);
if length(tmpval)>1 && ~isnan(tmpval(2))
tmp{2*i}=tmpval(2);
else
tmp{2*i}=tmpval(1);
DI.INFO.comment{end+1} = ['Only one azimuth reading (' fields{i} ...
') before D.'];
end
end
tmpD=tmp;
my_xlswrite(opsys,file_new,tmp,1,'A13');
clearvars tmp
dataflag=1;
for i=1:length(fields)
tmpval=DI.AZ.(fields{i}).I;
tmpval=floor(tmpval)+mod(tmpval,1)*0.6;
if isnan(tmpval)
tmpval=tmpD{2*i-1};
DI.INFO.comment{end+1} = ['No azimuth reading (' fields{i} ...
') before I.'];
dataflag=0;
end
tmp{2*i-1}=tmpval(1);
if length(tmpval)>1 && ~isnan(tmpval(2))
tmp{2*i}=tmpval(2);
else
tmp{2*i}=tmpval(1);
if dataflag
DI.INFO.comment{end+1} = ['Only one azimuth reading (' fields{i} ...
') before I.'];
end
end
end
clearvars tmpval tmpvalD dataflag
my_xlswrite(opsys,file_new,tmp,1,'A31');
clearvars tmp
% /////////////////////////////////////////////////////////////////////
% Write declination readings
% /////////////////////////////////////////////////////////////////////
% Time
fields={'EU' 'WU' 'ED' 'WD'};
for i=1:length(fields)
tmpval=DI.D.(fields{i}).time;
tmp{2*i-1} = tmpval;
tmp{2*i} = tmpval;
end
my_xlswrite(opsys,file_new,tmp',1,'B19');
clearvars tmp
% Readings
for i=1:length(fields)
tmpval=DI.D.(fields{i}).val;
tmpval=floor(tmpval)+mod(tmpval,1)*0.6;
tmp{2*i-1} = tmpval;
tmp{2*i} = tmpval;
end
my_xlswrite(opsys,file_new,tmp',1,'C19');
clearvars tmp
% Residuals
tmp=num2cell(zeros(8,1));
my_xlswrite(opsys,file_new,tmp,1,'E19');
clearvars tmp
% /////////////////////////////////////////////////////////////////////
% Write inclination readings
% /////////////////////////////////////////////////////////////////////
fields={'NU' 'SD' 'ND' 'SU'};
% Time
for i=1:length(fields)
tmpval=DI.I.(fields{i}).time;
tmp{2*i-1} = tmpval;
tmp{2*i} = tmpval;
end
tmp{end+1}=tmp{end};
my_xlswrite(opsys,file_new,tmp',1,'B37');
clearvars tmp
% Readings
for i=1:length(fields)
tmpval=DI.I.(fields{i}).val;
tmpval=floor(tmpval)+mod(tmpval,1)*0.6;
tmp{2*i-1} = tmpval;
tmp{2*i} = tmpval;
end
tmp{end+1}=NaN;
my_xlswrite(opsys,file_new,tmp',1,'D37');
clearvars tmp
% Residuals
tmp=num2cell(zeros(9,1));
my_xlswrite(opsys,file_new,tmp,1,'E37');
clearvars tmp
% Meridian
tmp{1}=mod(floor(DI.M)+mod(DI.M,1)*0.6,180);
my_xlswrite(opsys,file_new,tmp,1,'C37');
clearvars tmp
% /////////////////////////////////////////////////////////////////////
% Write comment
% /////////////////////////////////////////////////////////////////////
for i=0:length(DI.INFO.comment)-1
my_xlswrite(opsys,file_new,DI.INFO.comment(i+1),1,...
[char(65+4*(mod(i,2))) num2str(floor((i)/2)+59)]);
end
end
function num=pos_cell2num(cell)
% Convert reading in DD/MM/SS stored in entries of a cell to DD.MMSS stored
% as a number
num=cell{1}+cell{2}/1e2+cell{3}/1e4;
end
function hhmmss_n=hhmmss_addsec(hhmmss,sec)
% Add minutes to time in hhmmss format
if (round(sec) ~= sec)
disp('ERROR: ADD SEC INSTEAD OF MINUTES!');
end
if (sec>=60 || sec<=60)
hhmmss=hhmmss_addmin(hhmmss,floor(sec/60));
sec=sec-floor(sec/60);
elseif (sec>=1440 || sec<=1440)
disp('ERROR: ADD HOUR INSEAD OF SECONDS!');
end
tmp_ss=str2double(hhmmss(5:6))+sec;
if (tmp_ss<0)
tmp_ss=tmp_ss+60;
tmp_mm=str2double(hhmmss(3:4))+1;
elseif (tmp_ss>60)
tmp_ss=tmp_ss-60;
tmp_mm=str2double(hhmmss(3:4))-1;
else
tmp_mm=str2double(hhmmss(3:4));
end
hhmmss_n=([ hhmmss(1:2) num2str(tmp_mm*1e2+tmp_ss,'%04d') ]);
end
function hhmmss_n=hhmmss_addmin(hhmmss,min)
% Add minutes to time in hhmmss format
if (min>=60 || min<=60)
disp('ERROR: ADD HOUR INSEAD OF MINUTES!');
end
if (round(min) ~= min)
disp('ERROR: ADD SEC INSTEAD OF MINUTES!');
end
tmp_mm=str2double(hhmmss(3:4))+min;
if (tmp_mm<0)
tmp_mm=tmp_mm+60;
tmp_hh=str2double(hhmmss(1:2))+1;
elseif (tmp_mm>60)
tmp_mm=tmp_mm-60;
tmp_hh=str2double(hhmmss(1:2))-1;
else
tmp_hh=str2double(hhmmss(1:2));
end
if (tmp_hh > 24 || tmp_hh < 0)
disp(['Error: Measurements were done between two days!']);
end
hhmmss_n=([ num2str(tmp_hh*1e2+tmp_mm,'%04d') hhmmss(5:6) ]);
end
\ No newline at end of file
%% Small demonstration on how to use XLWRITE
%% Initialisation of POI Libs
% Add Java POI Libs to matlab javapath
javaaddpath('poi_library/poi-3.8-20120326.jar');
javaaddpath('poi_library/poi-ooxml-3.8-20120326.jar');
javaaddpath('poi_library/poi-ooxml-schemas-3.8-20120326.jar');
javaaddpath('poi_library/xmlbeans-2.3.0.jar');
javaaddpath('poi_library/dom4j-1.6.1.jar');
javaaddpath('poi_library/stax-api-1.0.1.jar');
%% Data Generation for XLSX
% Define an xls name
fileName = 'test_xlwrite.xlsx';
sheetName = 'this_is_sheetname';
startRange = 'B3';
% Generate some data
xlsData = {'A Number' 'Boolean Data' 'Empty Cells' 'Strings';...
1 true [] 'String Text';...
5 false [] 'Another very descriptive text';...
-6.26 false 'This should have been an empty cell but I made an error' 'This is text';...
1e8 true [] 'Last cell with text';...
1e3 false NaN NaN;...
1e2 true [] 'test'}
%% Generate XLSX file
xlwrite(fileName, xlsData, sheetName, startRange);
\ No newline at end of file
This diff is collapsed.
function [status, message]=xlwrite(filename,A,sheet, range)
% XLWRITE Write to Microsoft Excel spreadsheet file using Java
% XLWRITE(FILE,ARRAY) writes ARRAY to the first worksheet in the Excel
% file named FILE, starting at cell A1. It aims to have exactly the same
% behaviour as XLSWRITE. See also XLSWRITE.
%
% XLWRITE(FILE,ARRAY,SHEET) writes to the specified worksheet.
%
% XLWRITE(FILE,ARRAY,RANGE) writes to the rectangular region
% specified by RANGE in the first worksheet of the file. Specify RANGE
% using the syntax 'C1:C2', where C1 and C2 are opposing corners of the
% region.
%
% XLWRITE(FILE,ARRAY,SHEET,RANGE) writes to the specified SHEET and
% RANGE.
%
% STATUS = XLWRITE(FILE,ARRAY,SHEET,RANGE) returns the completion
% status of the write operation: TRUE (logical 1) for success, FALSE
% (logical 0) for failure. Inputs SHEET and RANGE are optional.
%
% Input Arguments:
%
% FILE String that specifies the file to write. If the file does not
% exist, XLWRITE creates a file, determining the format based on
% the specified extension. To create a file compatible with Excel
% 97-2003 software, specify an extension of '.xls'. If you do not
% specify an extension, XLWRITE applies '.xls'.
% ARRAY Two-dimensional logical, numeric or character array or, if each
% cell contains a single element, a cell array.
% SHEET Worksheet to write. One of the following:
% * String that contains the worksheet name.
% * Positive, integer-valued scalar indicating the worksheet
% index.
% If SHEET does not exist, XLWRITE adds a new sheet at the end
% of the worksheet collection.
% RANGE String that specifies a rectangular portion of the worksheet to
% read. Not case sensitive. Use Excel A1 reference style.
% * If you specify a SHEET, RANGE can either fit the size of
% ARRAY or specify only the first cell (such as 'D2').
% * If you do not specify a SHEET, RANGE must include both
% corners and a colon character (:), even for a single cell
% (such as 'D2:D2').
% * If RANGE is larger than the size of ARRAY, Excel fills the
% remainder of the region with #N/A. If RANGE is smaller than
% the size of ARRAY, XLWRITE writes only the subset that fits
% into RANGE to the file.
%
% Note
% * This function requires the POI library to be in your javapath.
% To add the Apache POI Library execute commands:
% (This assumes the POI lib files are in folder 'poi_library')
% javaaddpath('poi_library/poi-3.8-20120326.jar');
% javaaddpath('poi_library/poi-ooxml-3.8-20120326.jar');
% javaaddpath('poi_library/poi-ooxml-schemas-3.8-20120326.jar');
% javaaddpath('poi_library/xmlbeans-2.3.0.jar');
% javaaddpath('poi_library/dom4j-1.6.1.jar');
% * Excel converts Inf values to 65535. XLWRITE converts NaN values to
% empty cells.
%
% EXAMPLES
% % Write a 7-element vector to testdata.xls:
% xlwrite('testdata.xls', [12.7, 5.02, -98, 63.9, 0, -.2, 56])
%
% % Write mixed text and numeric data to testdata2.xls
% % starting at cell E1 of Sheet1:
% d = {'Time','Temperature'; 12,98; 13,99; 14,97};
% xlwrite('testdata2.xls', d, 1, 'E1')
%
%
% REVISIONS
% 20121004 - First version using JExcelApi
% 20121101 - Modified to use POI library instead of JExcelApi (allows to
% generate XLSX)
% 20121127 - Fixed bug: use existing rows if present, instead of
% overwrite rows by default. Thanks to Dan & Jason.
% 20121204 - Fixed bug: if a numeric sheet is given & didn't exist,
% an error was returned instead of creating the sheet. Thanks to Marianna
% 20130106 - Fixed bug: use existing cell if present, instead of
% overwriting. This way original XLS formatting is kept & not
% overwritten.
% 20130125 - Fixed bug & documentation. Incorrect working of NaN. Thanks Klaus
% 20130227 - Fixed bug when no sheet number given & added Stax to java
% load. Thanks to Thierry
%
% Copyright 2012-2013, Alec de Zegher
%==============================================================================
% Check if POI lib is loaded
if exist('org.apache.poi.ss.usermodel.WorkbookFactory', 'class') ~= 8 ...
|| exist('org.apache.poi.hssf.usermodel.HSSFWorkbook', 'class') ~= 8 ...
|| exist('org.apache.poi.xssf.usermodel.XSSFWorkbook', 'class') ~= 8
error('xlWrite:poiLibsNotLoaded',...
'The POI library is not loaded in Matlab.\nCheck that POI jar files are in Matlab Java path!');
end
% Import required POI Java Classes
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
status=0;
% If no sheet & xlrange is defined, attribute an empty value to it
if nargin < 3; sheet = []; end
if nargin < 4; range = []; end
% Check if sheetvariable contains range data
if nargin < 4 && ~isempty(strfind(sheet,':'))
range = sheet;
sheet = [];
end
% check if input data is given
if isempty(A)
error('xlwrite:EmptyInput', 'Input array is empty!');
end
% Check that input data is not bigger than 2D
if ndims(A) > 2
error('xlwrite:InputDimension', ...
'Dimension of input array should not be higher than two.');
end
% Set java path to same path as Matlab path
java.lang.System.setProperty('user.dir', pwd);
% Open a file
xlsFile = java.io.File(filename);
% If file does not exist create a new workbook
if xlsFile.isFile()
% create XSSF or HSSF workbook from existing workbook
fileIn = java.io.FileInputStream(xlsFile);
xlsWorkbook = WorkbookFactory.create(fileIn);
else
% Create a new workbook based on the extension.
[~,~,fileExt] = fileparts(filename);
% Check based on extension which type to create. If no (valid)
% extension is given, create XLSX file
switch lower(fileExt)
case '.xls'
xlsWorkbook = HSSFWorkbook();
case '.xlsx'
xlsWorkbook = XSSFWorkbook();
otherwise
xlsWorkbook = XSSFWorkbook();
% Also update filename with added extension
filename = [filename '.xlsx'];
end
end
% If sheetname given, enter data in this sheet
if ~isempty(sheet)
if isnumeric(sheet)
% Java uses 0-indexing, so take sheetnumer-1
% Check if the sheet can exist
if xlsWorkbook.getNumberOfSheets() >= sheet && sheet >= 1
xlsSheet = xlsWorkbook.getSheetAt(sheet-1);
else
% There are less number of sheets, that the requested sheet, so
% return an empty sheet
xlsSheet = [];
end
else
xlsSheet = xlsWorkbook.getSheet(sheet);
end
% Create a new sheet if it is empty
if isempty(xlsSheet)
warning('xlwrite:AddSheet', 'Added specified worksheet.');
% Add the sheet
if isnumeric(sheet)
xlsSheet = xlsWorkbook.createSheet(['Sheet ' num2str(sheet)]);
else
% Create a safe sheet name
sheet = WorkbookUtil.createSafeSheetName(sheet);
xlsSheet = xlsWorkbook.createSheet(sheet);
end
end
else
% check number of sheets
nSheets = xlsWorkbook.getNumberOfSheets();
% If no sheets, create one
if nSheets < 1
xlsSheet = xlsWorkbook.createSheet('Sheet 1');
else
% Select the first sheet
xlsSheet = xlsWorkbook.getSheetAt(0);
end
end
% if range is not specified take start row & col at A1
% locations are 0 indexed
if isempty(range)
iRowStart = 0;
iColStart = 0;
iRowEnd = size(A, 1)-1;
iColEnd = size(A, 2)-1;
else
% Split range in start & end cell
iSeperator = strfind(range, ':');
if isempty(iSeperator)
<