Importing raster data

Andrea Giacomelli andreag at crs4.it
Wed Jan 28 07:00:00 EST 1998



> I'm trying to import a raster file in ascii/text format with lat/long
> coordinates into GRASS. The file has the following format:
> 
> north: 47.937500000000
> south: 24.062500000000
> east: -66.479166666667
> west: -90.520833333333
> rows: 573
> cols: 577
> 785 790 786 783 779 771 768 766 ...
> 

Hi,
I don't know if you have solved your problem in the meantime.
Some time ago, I wrote this simple/informal (*VERY INFORMAL*) 
C program to automate some typical
reformatting tasks in files coming from r.out.ascii.
Maybe it can be of help.

NOTE: I don't think awk will do it, with 577 columns.

I give you my regards, and I will be glad to have feedback from you!
(source is after the signature...)


                =========================================
                Andrea Giacomelli
                Centre for Advanced Studies, Research and
                Development in Sardinia
                Environment Division

                http://www.crs4.it/~andreag
                =========================================

---8<----CUT HERE---8<------------------------

/* WYD input GRASS ascii; output various formats (3-d plot, correlation etc) */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define CHKP printf("output format for z values: %s\n\n",pformat);
#define CHKC printf("%f %f %f %f %f %f\n",n,s,e,w,nr,nc);

int   rcd_plot();
int  tred_plot();
int  dued_plot();
int  read_head();
char *ask_form();

/*----------globals-----------*/

float n,s,e,w,nr,nc;
FILE *fpin,*fpin2,*fpout;
char fnin[100],fnin2[100],fnout[100];
char pformat[50];
float divby,divby_x,divby_y;

/*----------vai cosi'---------*/

main()
{       char option,ancora;

        ancora='Y';

                printf("Reads a GRASS ascii file and re-formats it\n");
                printf("============================================\n");

                printf("Choose output format for data >\n");
                printf("[A] array with rows cols data\n");
                printf("[B] vector for 3-d plot\n");
                printf("[C] vector for 2-d correlation plot\n");
                printf("Allora ? ");
                option=getchar();

                switch(toupper(option))
                {       case 'A':       rcd_plot();
                                        break;
                
                        case 'B':       tred_plot();
                                        break;

                        case 'C':       dued_plot();
                                        break;

                        default :       printf("\07\07Jimi Hendrix lives...\n")
;
                                        break;
                }


        printf("\n\nTanti saluti da Andreuzzo...\n\n\n");

}

/*============= Read header data ==============*/

int read_head(fp)
FILE *fp;
{       char dummy[10];

        fscanf(fp,"%s %f",dummy,&n);
        fscanf(fp,"%s %f",dummy,&s);
        fscanf(fp,"%s %f",dummy,&e);
        fscanf(fp,"%s %f",dummy,&w);
        fscanf(fp,"%s %f",dummy,&nr);
        fscanf(fp,"%s %f",dummy,&nc);

        CHKC

}

/*===============================================================*/

char *ask_form(question)
char question[];
{       char whichf[30];

        printf("printf() format for %s (e.g %% 5.2f) ? ",question);
        fscanf(stdin,"%s",whichf);

        printf("chosen format is: <%s>\n\n",whichf);

        return(whichf);
}

/*===============================================================*/

int dued_plot()
{       int a,b;
        float dat_x,dat_y;

        printf("X-file in ? ");
        fscanf(stdin,"%s",fnin);
        printf("Y-file in ? ");
        fscanf(stdin,"%s",fnin2);
        printf("File out ? ");
        fscanf(stdin,"%s",fnout); 

        printf("Divide x values by : ");
        fscanf(stdin,"%f",&divby_x);
        printf("Divide y values by : ");
        fscanf(stdin,"%f",&divby_y);

        strcpy(pformat,ask_form("X data"));
        strcat(pformat," ");
        strcat(pformat,ask_form("Y data"));
        strcat(pformat,"\n");

        CHKP

        if((fpin=fopen(fnin,"r"))==NULL) exit(-1);
        if((fpin2=fopen(fnin2,"r"))==NULL) exit(-1);
        if((fpout=fopen(fnout,"w"))==NULL) exit(-2);



/*------------- Read header data --------------*/

        read_head(fpin);
        read_head(fpin2);

/*--------------Write data---------------------*/

        for(a=1;a<=nr;a++)
                for(b=1;b<=nc;b++)
                {       fscanf(fpin,"%f",&dat_x);
                        fscanf(fpin2,"%f",&dat_y);
                fprintf(fpout,pformat,dat_x/divby_x,dat_y/divby_y);
/*                      fprintf(stdout,pformat,dat_x/divby_x,dat_y/divby_y); */
                }

        fclose(fpin);
        fclose(fpin2);
        fclose(fpout);

}
/*=================================================================*/
int tred_plot()
{       float a,b;
        float dat_out;
        float step_x,step_y;

        printf("file in ? ");
        scanf("%s",fnin);
        printf("File out ? ");
        scanf("%s",fnout);

        printf("\nDivide Z-values by : ");
        scanf("%f",&divby);


        if((fpin=fopen(fnin,"r"))==NULL) exit(-1);
        if((fpout=fopen(fnout,"w"))==NULL) exit(-2);
        read_head(fpin);


        step_x=(e-w)/nc;
        step_y=(s-n)/nr;

        printf("X interval is %f ; step is %f\n\n",e-w,step_x);
        strcpy(pformat,ask_form("X axis"));
        strcat(pformat," ");
        printf("Y interval is %f ; step is %f\n\n",n-s,step_y);
        strcat(pformat,ask_form("Y axis"));
        strcat(pformat," ");
        strcat(pformat,ask_form("Z data"));
        strcat(pformat,"\n"); 
        CHKP

/*------------compute--------------*/

        for(a=n;a>=s;a+=step_y)
        {
                for(b=w;b<=e;b+=step_x)
                {       fscanf(fpin,"%f",&dat_out);
                        fprintf(fpout,pformat,b-w,a-s,dat_out/divby);
                        fprintf(stdout,pformat,b-w,a-s,dat_out/divby);
                }
 fprintf(fpout,"\n");
        }
        fclose(fpin);
        fclose(fpout);

}



/*=============================================================*/

int rcd_plot()
{       int a,b,d_in;
        float d_out;
        int om_in,om_out;
        char opt;

        printf("File in ? ");
        scanf("%s",fnin);
        printf("File out ? ");
        scanf("%s",fnout);

        if((fpin=fopen(fnin,"r"))==NULL) exit(-1);
        if((fpout=fopen(fnout,"w"))==NULL) exit(-2);

        printf("Divide by ? ");
        scanf("%f",&divby);
        printf("Out of mask value of IN file ? ");
        scanf("%d",&om_in);
        printf("Out of mask value of OUT file ? ");
        scanf("%d",&om_out);

        strcpy(pformat,ask_form("output"));
        strcat(pformat," ");
        CHKP 

        printf("The format string is <%s>\n\n",pformat);

        read_head(fpin);

        
        printf("Do you want rows & cols written at the beginning of the file ? 
");
        scanf("%s",&opt);

        printf("HERE %c",opt);

        switch(toupper(opt))
        {     case 'Y': fprintf(stdout,"%4.0f %4.0f\n",nr,nc);
                        break;

              case 'N': printf("%4.0f %4.0f\n",nr,nc);
                        break;

              deafult:  printf("Tanti saluti a cicciuzzo!\n\n\n");
                        break;
        }

        for(a=1;a<=nr;a++)
        {       for(b=1;b<=nc;b++)
                {       fscanf(fpin,"%d",&d_in);
                        if(d_in==om_in)
                          fprintf(fpout,"%d ",om_out);
                        else
                        {  d_out=((float) d_in)/divby;
                           fprintf(fpout,pformat,d_out);
                        }
                }
 fprintf(fpout,"\n");
        }

        fclose(fpin);
        fclose(fpout);
}





More information about the grass-dev mailing list