Category Archives: Computer Science

About computers, algorithms and unusual troubleshoots.

Google script to extract email information

This code helps to get summary of email such as subject, received date etc. Code is for the unread emails; remove lines with if condition check to read all emails.
Make a new google sheet> tools>script editor and copy and paste it. You will require to authorize the use of code while executing it.

function retriveUnreadMessages() {
// imports subject, from (email), received date of all unread email in inbox of gmail account
// the data is then written in the google spreadsheet
//reference:https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#appendRow(Object)
//

//-----------google sheet part
var ss=SpreadsheetApp.getActiveSheet();
var date1=new Date().toISOString().slice(0, 10);
ss.appendRow(["***** new execution = ",date1,"*****"]);// append a new information line where the new execution is done because data is appended at the end

//-----------------------------------------gmail part-----
// Log the subject lines of your Inbox
var threads = GmailApp.getInboxThreads();
// number of message to retrive
var n= threads.length;
for (var i = 0; i < n; i++) {

// check if there is unread messages in a thread
if(threads[i].isUnread)
{

var subject1=threads[i].getFirstMessageSubject(); // subjet of thread
var email1=threads[i].getMessages(); // messages inside the thread
// check if the individual message is unread
for (var j = 0; j < email1.length; j++) {
if(email1[j].isUnread)
{
var message_sender=email1[j].getFrom();
var message_sendDateTime=email1[j].getDate();
}
ss.appendRow([i,j,subject1,message_sender,message_sendDateTime]); //write data to spreadsheet.. it is appended to new row

}
}
}
}

Advertisement

Fractals in matlab/octave

Codes in matlab/octave for generating some popular fractals:

Mandelbrot

mandelbrot

function mandelbrotmain()
clear variables; clc;

maxit=200;
x=[-2,1];
y=[-1 1];

xpix=601;
ypix=401;

x=linspace(x(1),x(2),xpix);
y=linspace(y(1),y(2),ypix);
[xG, yG]=meshgrid(x,y);

c=mb(maxit,xG,yG);

figure
imagesc(x,y,c);
colormap([1 1 1;0 0 0]);
axis on;
grind on;

endfunction

function count=mb(maxItr,xG,yG)
c=xG+1i*yG;
count=ones(size(c));
z=c;

for n=1:maxItr
z=z.*z+c;
inside=abs(z)<=2;
count=count+inside;
endfor
endfunction

 

Sierpinski triangle
sierpinski

clc;
clear variables;
close windows;

clf

N=500;
x=zeros(1,N);y=x;r=x;
for a=2:N
c=randi([0 2]);
r(a)=c;
switch c
case 0
x(a)=0.5*x(a-1);
y(a)=0.5*y(a-1);
case 1
x(a)=0.5*x(a-1)+.25;
y(a)=0.5*y(a-1)+sqrt(3)/4;
case 2
x(a)=0.5*x(a-1)+.5;
y(a)=0.5*y(a-1);
end
end
plot(x,y,’o’)
title(‘Sierpinski’s triangle’)
legend(sprintf(‘N=%d Iterations’,N))

 

Fractal tree
fractaltrree

function treemain
tic
clear all;
depth = 7;
figure 1;
hold on;

drawTree2(0, 0, 90, depth);
toc;
endfunction

function drawTree2(x1, y1, angle, depth)
deg_to_rad = pi / 180.0;
rot=30; #degrees :: rotation from second iteration
branchLength=100*depth;
if (depth != 0)
x2 = x1 + cos(angle * deg_to_rad) * branchLength;
y2 = y1 + sin(angle * deg_to_rad) * branchLength ;
line([x1, x2], [y1, y2],’LineWidth’,depth);
drawTree2(x2, y2, angle – rot, depth – 1);
drawTree2(x2, y2, angle + rot, depth – 1);

endif
endfunction

 

A note on fractal (from HH Hardey):
There are upper and lower limits to describe natural objects by fractals. A figure may look like a fern leaf, but continued generation of the “leaf on larger and larger scales produces only a larger and larger fern leaf. A fern “plant” will never be produced. Similarly, there is a smallest scale to which a fractal description applies. As the overall fern leaf pattern is repeated on smaller and smaller scales, eventually the scale of a single fern plant cell will be reached. A single cell does not look like the overall shape.

Using Math Kernal Library (MKL) in visual studio with Fortran

First install MKL from intel website

To use intel MKL library inside visual studio do the followings:
1. Right click the project>Properties
2. In Property page>Fortran>Libraries set (a) Runtime library : Multithreaded and (b) Use Intel MKL: Sequential
mklsetting

Once this setting is done, you can compile the file in DEBUG mode. Repeat the same process (1-2) for RELEASE mode too once more.(Note exe of DEBUG mode is far slower than exe of RELEASE mode. The settings to set are listed below.

To do the same thing MANUALLY (which gives more insight to the code) do the following in each DEBUG and RELEASE mode.
1. Property page>Fortran>General>Additional Include Directories: C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019.4.245\windows\mkl\include
2. Property page>Fortran>Preprocessor>Preprocess source file: Yes
3. Property page>Linker>General>Additional Library Directories:C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019.4.245\windows\mkl\lib\ia32_win
4. Property page>Linker>Input>Additional Dependencies: mkl_intel_c.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib

The manual setting is described in the video as well:

For method to install Fortran in visual studio see this post.

Installing Fortran Compiler for Visual Studio 2017

Fortran has come a long way than we had studied in high-school in 19xx. Its now fully supported within Microsoft visual studio. To install Fortran with VS IDE support, ensure the followings:

Modify/Install the installation file of VS 2017 Community edition (its free!) and install support for Desktop development with C++ .  This step will ensure that the Fortran compiler is available from within the VS2017. (Ref: https://software.intel.com/en-us/articles/installing-microsoft-visual-studio-2017-for-use-with-intel-compilers).

  1. During installation/modification under the Workloads view (shown below), select the checkbox to install the Desktop development with C++ component.
  2. To build applications to run on Windows XP*, check the Windows XP support for C++ component in Summary window
  3. Continue with the installation

Download Fortran compiler- Intel® Parallel Studio XE (its free too!) from https://software.intel.com/en-us/qualify-for-free-software/student. You need to register with academic email address. Install it after downloading.

For offline installation you can download licence file from your profile page after logging in. (Ref: https://software.intel.com/en-us/articles/resend-license-file)

 

  • Click on any active serial number to go to the corresponding Manage License page.
  • There are two icons on the License File header ‘Download license file’ and ‘Resend license file to my email’.

 

interFortranVisualStudio

 

 

Catchment area calculation in QGIS

The method is similar to any other GIS method. The steps are as follows; the main step is highlighted in bold fonts. It is up to you to beautify the map.

  1. Get the DEM files (for eg. from https://earthexplorer.usgs.gov/)
  2. Clip it if necessary. This can reduce the computation time.
  3. Reproject the lat/long DEM file to metric base e.g. UTM (this is essential for any area based analysis).
    Raster>Projection> Wrap
  4. Make sure the projection coordinate used in DEM and your data frame matches.
  5. Fill the projected DEM for any inconsistent data.
    Processing toolbox>SAGA>Terrain analysis(hydrology)>Fill sinks (wang & liu)
  6. Calculate Strahaler order for the filled raster. This is necessary to know the river lines. The intake location should lie exactly on the river line shown by Strahaler .
    Processing toolbox>SAGA>Terrain analysis(channels)>Strahaler order

    [Note steps 5 and 6 can be done in single command by using Processing toolbox>Terraine Analysis Channels>Channel Network and Drainage basin]
  7. Delineate the catchment using upslope command. Choose the X,Y coordinate lying on the line calculated by Strahaler  and filled DEM as input elevation. Coordinate should be in meters (i.e. Projected coordinate).
    Processing toolbox>SAGA>Terraine analysis>Terrain analysis(hydrology)>Upslope area
  8. Using Raster to vector convert the delineated raster to vector.
    Raster>Conversion>Polygonize(Raster to vector)
  9. Go to attribute table, add new field with formula “$area”. The area is calculated in square meters.