Commit 5c2187c0 authored by saeed's avatar saeed
Browse files

m

parent 682c4f31
#include<stdio.h>
#include<stdlib.h>
#define LEN(a) (sizeof(a) / sizeof(a[0]))
struct range{
float lat1, lat2, lon1, lon2;
};
static char cbase[1024] = {'\0'};
static void getbase(char *path){
char *sl = path;
char *t = path;
if (t){
while (*t != '\0'){
if (*t == '/'){
sl = t;
}
t++;
}
}
int i = 0;
if (*sl == '/'){
for (char *c = sl +1;*c; ++c){
*(cbase+i) = *c;
i++;
}
}
else{
for (char *c = sl;*c; ++c){
*(cbase+i) = *c;
i++;
}
}
*(cbase+i) = '\0';
}
static int *uniq(int *ar, int nx){
int j = 0;
int nc = 0;
int *result = NULL;
for (int i=0; i < nx;++i){
for (j=0; j < i;++j){
if (ar[j] == ar[i])break;
}
if (j == i) nc++;
}
j = 0;
if (nc > 0){
result = (int *)malloc(sizeof(int) *nc);
int nt = 0;
for (int i=0; i < nx;++i){
for (j=0; j < i;++j){
if (ar[j] == ar[i])break;
}
if (j == i) result[nt++] = ar[i];
}
}
}
static int isendofline(char c){
int result = ((c == '\n') || (c == '\r')) ? 1 : 0;
return result;
}
struct Line{
int date;
int time;
float lat;
float lon;
float obsvalue;
int varno;
int codetype;
float count;
};
struct file_content{
char **date;
float *obscount;
};
static void skipline(FILE *file, int nl){
if (file){
int nn = 0;
char c = ' ';
while(( c = fgetc(file)) != EOF){
if (isendofline(c) ) nn++;
if (nn == nl) break;
}
}
}
static int *uniq_line(struct Line *ar, int nx, int *nout){
int j = 0;
int nc = 0;
int *result = NULL;
for (int i=0; i < nx;++i){
for (j=i-1; j < i;++j){
if (i == 0 ){
break;
}
else if (ar[j].date == ar[i].date){
break;
}
}
if (j == i || i == 0){
nc++;
printf("nc is %d \n",nc);
}
}
*nout = nc;
j = 0;
if (nc > 0){
result = (int *)malloc(sizeof(int) *nc);
int nt = 0;
for (int i=0; i < nx;++i){
for (j=(i-1); j < i ;++j){
if (i == 0 ){
break;
}
else if (ar[j].date == ar[i].date){
break;
}
}
if (j == i || i == 0){
printf("%d %d\n",nt,ar[i].date);
result[nt] = ar[i].date;
nt++;
}
}
printf("nt %d \n",nt);
}
return result;
}
static long int numberoflines(FILE *file){
long int result = 0L;
if (file){
char c = ' ';
while((c = fgetc(file)) != EOF){
if (isendofline(c) ) result++;
}
}
return result;
}
static int getLen(char *str){
int result = 0;
if (str){
while(*str != '\0'){
result++;
str++;
}
}
return result;
}
static void concat(char *str1, char *str2, char *str3, int N){
char *result =str3;
int ns = getLen(str1) + getLen(str2) + 1;
if (ns < N){
while(*str1 != '\0'){
*result = *str1;
result++;
str1++;
}
while(*str2 != '\0'){
*result = *str2;
result++;
str2++;
}
*result = '\0';
}
}
static int find(char *reg, char *str, int *first, int *last){
int lnr = getLen(reg);
int lns = getLen(str);
if ( lnr == 0 || lns == 0) return -1;
if (lnr > lns) return -1;
int i = 0;
int ncount = 0;
int k = 0;
int result = -1;
int ip = 0;
for (int j=0; j < lns; ++j){
i = 0;
if (str[j] == reg[i]){
*first = j;
k = j+1;
ip = i + 1;
ncount = 1;
while(str[k] == reg[ip]){
++k;
++ip;
++ncount;
}
if (ncount == lnr){
result = j;
*last = (k-1);
break;
}
else{
}
}
}
return result;
}
static int isinrange(float lon, float lat, struct range range){
int result = 0;
if (lon >= range.lon1 && lon <= range.lon2 && lat >= range.lat1 && lat<= range.lat2 ) result = 1;
return result;
}
static void process(char *filename, char * ccycle){
struct range range = {20.292, 75.3466,-58.105, 74.1046};
if (filename){
int cycle = atoi(ccycle);
FILE *file = fopen(filename,"r");
getbase(filename);
if (file){
skipline(file,1);
long int nl = numberoflines(file);
rewind(file);
skipline(file,1);
struct Line Line = {0};
struct Line *cont = (struct Line *)malloc(sizeof(struct Line) * nl);
for (long int i =0L; i < nl;++i){
fscanf(file,"%d%d%f%f%f%d%d%f",&Line.date,&Line.time,&Line.lat,&Line.lon,&Line.obsvalue,&Line.varno,&Line.codetype,&Line.count);
//printf("%d %d %f %f %f %d %d %f\n",Line.date,Line.time,Line.lat,Line.lon,Line.obsvalue,Line.varno,Line.codetype,Line.count);
cont[i].date = Line.date;
cont[i].time = Line.time;
cont[i].lat = Line.lat;
cont[i].lon = Line.lon;
cont[i].obsvalue = Line.obsvalue;
cont[i].varno = Line.varno;
cont[i].codetype = Line.codetype;
cont[i].count = Line.count;
}
fclose(file);
int nu = 0;
//here we sort cont
int *undate = uniq_line(cont, nl,&nu);
printf("%d nu is \n",nu);
for (int i=0; i < nu ;++i)printf("%d\n",undate[i]);
int first = 0, last = 0;
find(".txt", cbase, &first, &last);
char cout1[512] = {'\0'};
int ns = 0;
for (int i=0; i < first;++i){
cout1[i] = cbase[i];
ns++;
}
cout1[ns] = '\0';
char cout2[512] = {'\0'};
char cout3[512] = {'\0'};
char cout4[512] = {'\0'};
char cout5[512] = {'\0'};
concat(cout1, "_cycle", cout2, LEN(cout2));
concat(cout2, ccycle, cout3, LEN(cout3));
concat(cout3, "_thin.txt", cout4, LEN(cout4));
concat(cout3, ".txt", cout5, LEN(cout5));
printf("creating file %s \n",cout4);
printf("creating file %s \n",cout5);
FILE *fout_vis = fopen(cout5,"w");
float sumg = 0;
for (int i=0; i < nu ;++i){
float sum = 0.0;
for (long int j =0L; j < nl;++j){
//printf("%d \n",isinrange(cont[j].lon,cont[j].lat,range));
if (cont[j].date == undate[i] && (cont[j].time / 10000) == cycle && isinrange(cont[j].lon,cont[j].lat,range)) sum += cont[j].count;
if (cont[j].date == undate[i] && (cont[j].time / 10000) == cycle && isinrange(cont[j].lon,cont[j].lat,range)) fprintf(fout_vis,"%d\t%d\t%f\t%f\t%d\t%d\t%d\n",cont[j].date,cont[j].time,cont[j].lat,cont[j].lon,cont[j].varno,cont[j].codetype,(int)cont[j].count);
}
sumg += sum;
}
fclose(fout_vis);
free(cont);
free(undate);
printf("here\n");
}
}
}
int main(int argc, char *argv[]){
if (argc == 3){
process(argv[1],argv[2]);
}
else{
printf("Usage %s odb_file.txt cycle\n",argv[0]);
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment