• The method dispFileSystem.OpenTextFile takes three arguments.
The first one is the path and the name of the text file, here "C:\text.dat"
The second one is a number, here “2”.
“2” means, that the file is for writing.
“1”: file is for reading only.
“8”: data is appended to file.
The third argument, here “true” means, that a new file is created, if the specified file
name does not exist. If “false” is used, the file is not created.
• In case you want to open the created file with the program “Origin”, the file should be
of the type “.dat”. With “\t”, a new column is started, with “\n”, a new line begins.
In “Origin”, it is very important that each line has the same number of columns.
8.7
Get the current date and time
When evaluating spectra, it can be useful to store the results in file which file name includes
the current time or date of the computer. This guarantees that if the evaluation is executed
again, the results of the old evaluation will not be overwritten but a new file with the new
date and time is created. In the following example at first the entire current date and time is
displayed in the Output window. Afterwards, month, day, year (date) and hours, minutes,
seconds (time) are accessed separately and displayed as well.
39
// the System namespace provides access to date and time
import System;
var month, day, year;
var hours, minutes, seconds;
// store date and time in the variable CurrentDateTime
var CurrentDateTime = new Date();
// display CurrentDateTime
Console.WriteLine("Date and Time:
"+CurrentDateTime);
// retrieve the different parts of the date
// January corresponds to number "0", therefore add "1"
month = CurrentDateTime.getMonth()+1;
day = CurrentDateTime.getDay();
year = CurrentDateTime.getYear();
// display the date
Console.WriteLine("Date:
"+month+"."+day+"."+year);
// retrieve the different parts of the time
hours = CurrentDateTime.getHours();
// make sure "hours" constitutes of two numbers:
e.g.
9 is transformed to 09
if (hours < 10) hours = "0"+hours;
minutes = CurrentDateTime.getMinutes();
if (minutes < 10) minutes = "0"+minutes;
seconds = CurrentDateTime.getSeconds();
if (seconds < 10) minutes = "0"+seconds;
// display the time
Console.WriteLine("Time:
"+hours+":"+minutes+":"+seconds);
The output looks like this:
Date and Time: Sat Jun 21 15:33:50 UTC+1 2008
Date: 6.6.2008
Time: 15:33:50
Remarks
The program “Origin” only recognizes time data in the format hh:mm:ss. So make use of
this example if you want to store the time in a result file, which you later want to edit with
“Origin”.
8.8
Control number of digits
When fitting with DOASIS the fit coefficient and other fit results are calculated with very
high precision. However many decimal places of numbers are confusing if they are just
meant for control purposes e.g. in the Output window. In this example, the decimal number
123456789.123456789 is displayed in different modes and everybody can decide by himself,
which on suits best.
40
import System;
var x = 123456789.123456789;
Console.WriteLine("Original number:
"+x);
// includes a blank line
Console.WriteLine("");
Console.WriteLine("Exponential with 5 digits:
"+x.toExponential(5));
Console.WriteLine("Exponential with 5 digits:
"+x.ToString("e5"));
Console.WriteLine("Number with 5 digits:
"+x.ToString("f5"));
Console.WriteLine("Automatic mode (see Remarks):
"+x.ToString("g5"));
The output looks like this:
Original number: 123456789.12345679
Exponential with 5 digits: 1.23457e+8
Exponential with 5 digits: 1.23457e+008
Number with 5 digits: 123456789.12346
Automatic mode (see Remarks): 1.2346e+08
Remarks
The recommended option is:
x.ToString("g5")
It generally choses the best way of representing the number. The “5” does represent the
number of digits, but the “numbers” altogether.
For example: 4.3 has two “numbers”, 13.45 has four, 14.567 has seven, 1.23e+05 has 3 etc.
8.9
Open and Save a spectrum
Make sure you have the spectrum called oldspec.sp2 in the folder c:\scripttest\, before run-
ning the script.
41
import System;
// The class "Spectra" of the namespace "DoasCore" is necessary to handle spectra
import DoasCore.Spectra;
// open an empty spectrum sheet with the name "Spectrum" and store it
// in the variable "Spec"
var Spec:
ISpectrum = Specbar.GetSpectrum("Spectrum");
// load the spectrum "oldspec.sp2" in the empty sheet
Spec.Open("c:
\\scripttest\\oldspec.sp2");
// save it as a new name
Spec.SaveAs("C:
\\scripttest\\newspec.sp2");
Remarks
• If you handle spectra with JScript, they will always be of the type “ISpectrum”. It
will not be correct in the sense of informatics, but imagine it this way:
A whole number is of the type “integer”, a spectrum is of the type “ISpectrum”.
• The Programming Documentation in the Help menu of DOASIS shows all the mem-
bers, that are connected with the type “ISpectrum”. Many following examples will
make this clearer!
• In most cases we will deal with automatized opening and saving of large numbers of
spectra. Therefore we will make use of the class “AutoFileName()”.
8.10
Saving spectra with the “AutoFileName”-class
When measurements are automatized, it is useful to store the measured spectra in a certain
structure. During automatized evaluation, we can then use this structure again. In the fol-
lowing example, the current spectrum will be stored in such a structure. For the following
script, the spectrum of the active spectrum sheet will be stored in a folder with the name
S00000000 which will be created under the path c:\scripttest. The first file stored will have
the name S0000000.sp2 the next one will be S0000001.sp2 and so on.
42
Dostları ilə paylaş: |