Friday, December 18, 2009

Using the Geospatial Data Abstraction Library (GDAL) in Ubuntu

For a project, I need to process SRTM elevation data. GDAL is a software library that makes it easy to read and write raster geospatial data formats. I decided to give it a try since SRTM data uses the ESRI grid format supported by the library.

1. Install the the GDAL libraries and utilities
$ apt-get install libgdal1-1.5.0 gdal-bin
2. Sample code. The SRTM data file path is in the variable pszFilename. The code is based on the tutorial.
#include <stdio.h>
#include <gdal.h>>
int main()
{
    GDALDatasetH  hDataset;
    char *pszFilename = "/media/DATA/srtm_61_10.asc";
    GDALDriverH   hDriver;
    double        adfGeoTransform[6];
    GDALRasterBandH hBand;
    int             nBlockXSize, nBlockYSize;
    int             bGotMin, bGotMax;
    double          adfMinMax[2];
    float *pafScanline;
    int   nXSize;
    int i;

    GDALAllRegister();
    hDataset = GDALOpen( pszFilename, GA_ReadOnly );
    if( hDataset == NULL )
    {
        printf("Cannot open file.");
    }
    hDriver = GDALGetDatasetDriver( hDataset );
    printf( "Driver: %s/%s\n",
            GDALGetDriverShortName( hDriver ),
            GDALGetDriverLongName( hDriver ) );
    printf( "Size is %dx%dx%d\n",
            GDALGetRasterXSize( hDataset ), 
            GDALGetRasterYSize( hDataset ),
            GDALGetRasterCount( hDataset ) );
    if( GDALGetProjectionRef( hDataset ) != NULL )
        printf( "Projection is `%s'\n", GDALGetProjectionRef( hDataset ) );
    if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None )
    {
        printf( "Origin = (%.6f,%.6f)\n",
                adfGeoTransform[0], adfGeoTransform[3] );
        printf( "Pixel Size = (%.6f,%.6f)\n",
                adfGeoTransform[1], adfGeoTransform[5] );
    }
    hBand = GDALGetRasterBand( hDataset, 1 );
    GDALGetBlockSize( hBand, &nBlockXSize, &nBlockYSize );
    printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
            nBlockXSize, nBlockYSize,
            GDALGetDataTypeName(GDALGetRasterDataType(hBand)),
            GDALGetColorInterpretationName(
            GDALGetRasterColorInterpretation(hBand)) 
    );
    adfMinMax[0] = GDALGetRasterMinimum( hBand, &bGotMin );
    adfMinMax[1] = GDALGetRasterMaximum( hBand, &bGotMax );
    if( ! (bGotMin && bGotMax) )
      GDALComputeRasterMinMax( hBand, TRUE, adfMinMax );
    printf( "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
    if( GDALGetOverviewCount(hBand) > 0 )
      printf( "Band has %d overviews.\n", GDALGetOverviewCount(hBand));
    if( GDALGetRasterColorTable( hBand ) != NULL )
      printf( "Band has a color table with %d entries.\n", 
                 GDALGetColorEntryCount(
                 GDALGetRasterColorTable( hBand ) ) 
      );
    nXSize = GDALGetRasterBandXSize( hBand );
    pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize);
    GDALRasterIO( hBand, GF_Read, 0, 0, nXSize, 1, 
                    pafScanline, nXSize, 1, GDT_Float32, 
                     0, 0 );
    for (i=0;i<20;i++){
      printf("%f\n", pafScanline[i]);
    }
}

3. Build the example
$ gcc -lgdal1.5.0 -I/usr/include/gdal -o testgdal.exe testgdal.c
4. Run
$./testgdal.exe
Driver: AAIGrid/Arc/Info ASCII Grid
Size is 6001x6001x1
Projection is `GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9108"]],AXIS["Lat",NORTH],AXIS["Long",EAST],AUTHORITY["EPSG","4326"]]'
Origin = (119.999583,15.000417)
Pixel Size = (0.000833,-0.000833)
Block=6001x1 Type=Int16, ColorInterp=Undefined
Min=-27.000d, Max=2375.000

Tuesday, December 15, 2009

On Research Methodologies

Last December 14, 2009 I attended the  Second CAS Student-Faculty Research Conference held at the CAS auditorium. The objective of the conference is to present the research output of the different departments and institutes of the College of Arts and Sciences. There were four plenary presentations in the conference from IMSP, DSS, IC and ICS, and IBS.  The parallel sessions held in the afternoon were divided into four clusters. 
  • Social Sciences, Humanities, MST and Education Cluster
  • Physics and Chemistry Cluster
  • Mathematics, Statistics, and Computer Science Cluster
  • Biology and Environment Cluster
I decided to attend at least one presentation from each cluster in order to know what methodologies are used by the researchers in their field and work. Based on my samples, majority of the researchers conducted some form of survey and performed statistical analysis (frequency analysis, regression, test of significance).  Conclusions were drawn from the result of the statistical analysis. Another methodology used is simulation and modeling (Kinetic-Monte-Carlo, Time Series Analysis, Neural Networks, Regression Models). The main justification for using modeling and simulation is that conducting the actual experiment is expensive and time consuming. Model validation, however, was lacking in some of the researches. Product development is another methodology employed. Researchers developed a system (hardware or software), a chemical product, or an experimental setup. The product addresses some problem in the field. A common keyword in the title of the research papers presented  is characterization. Based on my understanding, characterization is providing a quantitative(or qualitative) description of some entity, structure, substance, behaviour, problem, algorithm, etc. Thus, we can probably include characterization as one of the research methodologies. Although, characterization, I think is a broad term. Also, the validity of results of the researches are strengthened if statistical analysis was conducted.

Tuesday, November 24, 2009

Making a DOS-bootable USB disk from Ubuntu

It is again the start of a new semester. We need to prepare the laboratory computers for students use. We do this by creating a source disk and doing a disk-copy to the other computers. This approach works because computers in a laboratory have similar specifications.

Last weekend, I run into the problem of making a USB disk DOS-bootable for use in copying hard disks. I needed to create one because the floppy drives in the laboratory computers are not working. The Norton Ghost program needs to run in DOS. The following are the steps I followed to make the disk.

  1. Downloaded a FreeDOS disk image from http://www.finnix.org/Balder.
  2. Installed Qemu.
    $sudo apt-get install qemu
  3. Prepared the USB disk by creating a single FAT16 partition. In my Ubuntu box, my USB disk is /dev/sdb.
    $sudo gparted /dev/sdb
  4. Ran Qemu using the FreeDOS image for the first floppy(A:)  and the USB disk as the second floppy (B:).
  5. $qemu -fda balder10.img -fdb /dev/sdb
  6. From within FreeDOS, I formatted drive B: and made it bootable.
    A:\>format /s b:
  7. Tested if the USB disk is now bootable.
    $qemu -fda /dev/sdb
  8. Copied the Norton Ghost program to the USB disk.
After changing some settings in the BIOS of the PCs in the laboratory, the DOS bootable USB disk from Ubuntu did its job.

Tuesday, October 6, 2009

UPLB now has full text online access to important sources of computing literature

As a novice researcher, a brilliant idea suddenly comes into your mind. You start your web browser and typed a few keywords on your favourite search engine to know if somebody has already thought of your brilliant idea. Then comes the results of the search. Somebody has already thought of your brililant idea and even published a journal article! You hurriedly click on the link, hoping to know more of the author's work. Unfortunately, what you get is only an abstract because you need to pay to access the entire article.

Yes, we have Wikipedia and Citeseer as sources for our literature review. But sometimes we just can't find the journal articles that are very important for our research from these two. In an effort to increase research productivity and output, UP obtained subscriptions of several journals delivered electronically. In the field of computing, the following are important sources of literature that we can use for our research.  They are accessible within the campus network. The question to ask now is: "Are we using these valuable resources?"
There are also several bibliographies for computer science available online.

Thursday, October 1, 2009

How to succeed in your SP proposal defense

(Updated: January 2024)

Make sure you and your adviser HAVE AGREED on the Title, Statement of the Problem, Objectives, and Materials and Methods sections of your SP proposal before your defense.

What to say during the defense in 5-7 minutes?
  • The title of your SP (memorized)
  • 5 sentences background (memorized)
  • 2 sentences Statement of the Problem (memorized)
  • 1 sentence General Objective (memorized)
  • 3-4 statements of Specific Objectives
  • Scope and Limitations
  • Materials and Methods (Use Case Diagram, ERD, Tech Stack, Evaluation Process, 12-week Gantt Chart)
  • Expected output and sample screenshots
  • Related work (those done in ICS if available)
(The best way is to write a script that you can memorize!)

Common comments from panelists

  • Too simple/Too complex. Some panelists will suggest to add/remove features to make your SP simpler/more complex. Redirect this comment/question to your adviser.
  • Novelty. You should be able to answer what is new in your work compared to previous work. If you can't, redirect this comment/question to your adviser.
  • Quantify. If one of your objectives is to "speed up" something, you should be able to answer how you will measure this quantity. Also specify metrics and measurements to gather.
  • Users and Stakeholders. Have you consulted them during the development of your proposal?
  • Comparison and Evaluation. If your work is an improvement over previous approaches, include in your objectives a comparison of results of your work over them in terms of speed, performance, accuracy, etc. To be safe, include a usability evaluation using the System Usability Scale (SUS). 
  • Specificity. Be VERY specific and careful in your statements. They should be easy to validate.
  • Feasibility. Make sure that what you are proposing is feasible and doable in 12 weeks, implementation-wise.
  • Initial experiments and results. If your work involves image processing and ImageLab, make sure you've made some initial trials (aside from binarization!). You should include in your defense the results, whether good or bad. This is also applicable for other types of SP, not just image processing.
  • Interfaces. Make sure you discuss the connection of your module with the modules of your SP groupmates, if you have any.
  • Validation. State how you are going to validate your claims.
  • ERD/DFD. If your SP needs a database, make sure you include an Entity-Relationship Diagram for the database design and Data Flow Diagram for data transformation. Include Use Case diagrams and wireframes.
  • References. Make sure you have a good number of references (at least 10). Don't forget to cite them properly in your paper. Also include related work by past ICS students.
  • Tech Stack. Be able to justify the choice of your tech stack.
Handling comments from panelists
  • In general, always treat comments from the panelists as CONSTRUCTIVE. Their statements will help improve your SP and give you directions in your work.
  • Panelists will be suggesting A LOT of things. Take note of ALL OF THEM and negotiate with your adviser later if you will consider SOME OF THEM in your SP.
  • If you do not know the answer to the question from a panelist, just tell them that "I'll look into that.". Make sure you do later!
Practice
  • It is important that you practice your defense. This can be done with your adviser or with some of your friends.
  • Before the defense, make sure that your mind is clear and specific with what you are going to do in your SP. You can achieve this by repeating in your mind the General Objective of your proposal. Visualize your expected output so that you can tell a story about it to the panelists during the defense.

Sunday, April 12, 2009

Writing a PS/2 mouse driver for your hobby OS

It took me a while to search for a good tutorial on how to write a PS/2 mouse driver for a hobby OS. Luckily, I was able to make an existing driver work. So for those who are having trouble writing one, I came up with this guide. However, I do not take full credit in the codes that I will be posting.

I assume that your hobby OS is 32-bit targetted on x86 machines. The following outlines the steps.
  1. Program the 8259 controller to enable IRQ2, the cascade line. The PS/2 mouse IRQ is IRQ12 which is in the slave PIC. Normally the IRQ numbers are remapped in 32-bit OSes.
    #define IRQ_TIMER 1
    #define IRQ_CASCADE 4
    #define IRQ_KEYBOARD 2
    #define IRQ_FDC 64
    #define IRQ_MOUSE 16
    void program8259(unsigned char b){
    unsigned char b1=0xFF;
    //remap the IRQs
    outportb(0x20,0x11);
    outportb(0xA0,0x11);
    outportb(0x21,0x20); //IRQ0-IRQ7 -> interrupts 0x20-0x27
    outportb(0xA1,0x28); //IRQ8-IRQ15 -> interrupts 0x28-0x2F
    outportb(0x21,4);
    outportb(0xA1,2);
    outportb(0x21,1);
    outportb(0xA1,1);
    b1^=b;
    outportb(0x21,b1);
    outportb(0xA1, inportb(0xA1) & ~0x10);  //enable IRQ12
    };
    
    //Here's the call to enable needed IRQ Lines called usually in the main() function
    program8259(IRQ_TIMER | IRQ_KEYBOARD | IRQ_FDC | IRQ_CASCADE);
    

  2. And here's the driver code courtesy of SANiK.
    //Mouse.inc by SANiK
    //License: Use as you wish, except to cause damage
    unsigned char mouse_cycle=0;
    signed char mouse_byte[3];
    signed char mouse_x=0;
    signed char mouse_y=0;
    
    /* The interrupt handler */
    void mouse_irq(){
    DWORD flags;
    switch(mouse_cycle){
    case 0:
    mouse_byte[0]=inportb(0x60);
    mouse_cycle++;
    break;
    case 1:
    mouse_byte[1]=inportb(0x60);
    mouse_cycle++;
    break;
    case 2:
    mouse_byte[2]=inportb(0x60);
    mouse_x=mouse_byte[1];
    mouse_y=mouse_byte[2];
    mouse_cycle=0;
    break;
    }
    }
    
    inline void mouse_wait(unsigned char  a_type){
    unsigned int _time_out=100000; //unsigned int
    if(a_type==0){
    while(_time_out--){ //Data
    if((inportb(0x64) & 1)==1){
    return;
    }
    }
    return;
    }else{
    while(_time_out--){ //Signal
    if((inportb(0x64) & 2)==0){
    return;
    }
    }
    return;
    }
    }
    
    inline void mouse_write(unsigned char a_write){
    //Wait to be able to send a command
    mouse_wait(1);
    //Tell the mouse we are sending a command
    outportb(0x64, 0xD4);
    //Wait for the final part
    mouse_wait(1);
    //Finally write
    outportb(0x60, a_write);
    }
    
    unsigned char mouse_read(){
    //Get's response from mouse
    mouse_wait(0);
    return inportb(0x60);
    }
    
    void installmouse(){ /* This enables the mouse, called in main() */
    unsigned char _status;
    
    //Enable the auxiliary mouse device
    mouse_wait(1);
    outportb(0x64, 0xA8);
    
    //Enable the interrupts
    mouse_wait(1);
    outportb(0x64, 0x20);
    mouse_wait(0);
    _status=(inportb(0x60) | 2);
    mouse_wait(1);
    outportb(0x64, 0x60);
    mouse_wait(1);
    outportb(0x60, _status);
    
    //Tell the mouse to use default settings
    mouse_write(0xF6);
    mouse_read();  //Acknowledge
    
    //Enable the mouse
    mouse_write(0xF4);
    mouse_read();  //Acknowledge
    //Depends on your OS!
    setinterruptvector(0x2C,idtbase,0x8E,mousewrapper,SYS_CODE_SEL);
    printf("Mouse successfully initialized.\n");
    }
    
    void get_mouse_pos(signed char *x,signed char *y){
    *x=mouse_x;
    *y=mouse_y;
    }
    

  3. The mousewrapper code is important for the IRET so it is written in assembly. It just actually calls mouse_irq defined in the driver above..
    global mousewrapper
    mousewrapper:
    push ebp
    push gs
    push fs
    push es
    push ss
    push ds
    pusha
    call mouse_irq
    ;re-enable interrupts, this is IMPORTANT!
    mov al,0x20
    out 0xA0,al
    out 0x20,al
    popa
    pop ds
    pop ss
    pop es
    pop fs
    pop gs
    pop ebp
    iret
    
That's it!Enjoy your mouse!