static float getradius(int w, int h, struct screen_dim *scdim){ float result = (float)(((float)(w *h)/ (float)(scdim->width * scdim->height)) * scdim->symbol_radius); return result; } static void copy_string(char *str1, char *str2, int N){ for (int i=0; i < N;++i){ str2[i] = str1[i]; } str2[N] = '\0'; } static int Isdigit(char C){ int result = (C >= '0' && C <= '9') ? 1 : 0; return result; } static int getLen(char *str){ int result = 0; if (str){ while(*str != '\0'){ result++; str++; } } return result; } static int find(char *reg, char *str, int *first, int *last){ int result = -1; int lnr = getLen(reg); int lns = getLen(str); if ( lnr == 0 || lns == 0) return result; if (lnr > lns) return result; int i = 0; int ncount = 0; int k = 0; 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 void getreportype(char *filename,char *reportype){ char *era = "odb_era5_"; char *precise = "odb_precise_"; char *beg = NULL; char *end = NULL; char *str = filename; int first = -1, last = -1; find(era, filename, &first, &last); if (first == -1 || last == -1){ first = -1; last = -1; find(precise, filename, &first, &last); if ( first == -1 || last == -1){ return ; } else{ str = str + last + 1; } } else{ str = str + last + 1; } while (*str != '\0'){ beg = NULL; if (Isdigit(*str)){ beg = str; char *tt = beg + 1; end = NULL; while (*tt != '\0'){ if (!Isdigit(*tt)){ break; } else{ end = tt; } tt++; } } if (beg != NULL && end != NULL)break; if (beg != NULL && end == NULL)break; str++; } int nc = 0; if (beg != NULL && end != NULL){ for (char *c = beg; c<= end;c++){ reportype[nc] = *c; nc++; } reportype[nc] = '\0'; } if (beg != NULL && end == NULL){ reportype[0] = *beg; reportype[1] = '\0'; } } static int Strcmp(char *str1, char *str2){ int result = 1; int len1 = 0, len2 = 0; char *t1 = str1, *t2 = str2; while(*t1 != '\0'){ len1++; t1++; } while(*t2 != '\0'){ len2++; t2++; } t1 = str1; t2 = str2; while (*t1 != '\0' && *t2 != '\0'){ if (*t1 != *t2){ result = 0; break; } t1++; t2++; } result = (result == 1 && (len1 == len2)) ? 1 : 0; return result; } static void getdescandgroup(struct odbinfo *odbinfo){ for (int i=0; i < ArrayCount(reports);++i){ if (Strcmp(reports[i].code,odbinfo->reportype)){ odbinfo->desc = reports[i].desc; odbinfo->group = reports[i].group; break; } } } static struct station *getstn(char *filename, struct odbinfo *odbinfo){ odbinfo->filename = filename; getreportype(filename,odbinfo->reportype); getdescandgroup(odbinfo); struct station *result = NULL; FILE *fin = fopen(filename,"r"); if (fin){ int nl = 0; char c = ' '; while ((c=getc(fin)) != EOF){ if (c == '\n' || c == '\r') nl++; } rewind(fin); result = (struct station *)malloc(sizeof(struct station) * nl); result->ns = nl; int date, cycle, dum1, dum2, dum3; float lat, lon; int iter = 0; int sum = 0; while (fscanf(fin,"%d %d %f %f %d %d %d",&date,&cycle,&lat,&lon,&dum1,&dum2,&dum3) && !feof(fin)){ if (iter == 0){ intostr(cycle, odbinfo->cycle); intostr(date, odbinfo->start_date); } sum += dum3; char cname[256] = {'\0'}; intostr(iter, cname); copy_string(cname,result[iter].name, 255); result[iter].lon = lon; result[iter].lat = lat; result[iter].ix = 0; result[iter].iy = 0; result[iter].radius = 0; if (iter == (nl -1)){ intostr(date, odbinfo->end_date); } iter++; } fclose(fin); intostr(sum, odbinfo->ntotal); } return result; } static struct stn_set getstn_set(int argc, char *argv[], struct odbinfo_set *odbinfo_set){ struct stn_set result = {NULL,0,0}; int nf = 0; if (argc > 1){ result.nt = argc -1; result.stns = (struct station **)malloc(sizeof(struct station *) * result.nt); odbinfo_set->odb_set = (struct odbinfo **)malloc(sizeof(struct odbinfo *) * result.nt); if (result.stns && odbinfo_set->odb_set){ for (int i=1; i< result.nt;++i){ *(odbinfo_set->odb_set + nf) = (struct odbinfo *)malloc(sizeof(struct odbinfo )); *(result.stns + nf ) = getstn(argv[i], *(odbinfo_set->odb_set + nf)); nf++; } } } return result; } static struct obs fill_obs(float *lonm, float *latm, int nx, int ny){ struct obs result = {NULL,NULL,stns->ns,stns->ns}; result.iindex = (int *)malloc(sizeof(int) *stns->ns); result.jindex = (int *)malloc(sizeof(int) *stns->ns); int im = -1; for (int i=0; i < stns->ns;++i){ im = -1; for(int j=0; j < ny;++j){ if (stns[i].lat >= latm[j]) im = j; } result.jindex[i] = im; } for (int i=0; i < stns->ns;++i){ im = -1; for(int j=0; j < nx;++j){ if (stns[i].lon >= lonm[j]) im = j; } result.iindex[i] = im; } return result; } static struct obs_set fill_obs_set(float *lonm, float *latm, int nx, int ny){ struct obs_set result = {NULL,0,stn_set.nt}; result.obs = (struct obs **)malloc(sizeof(struct obs *) *stn_set.nt ); if (result.obs){ for (int k=0; k < stn_set.nt;++k){ result.obs[k] = (struct obs *)malloc(sizeof(struct obs)); result.obs[k]->iindex = (int *)malloc(sizeof(int) * stn_set.stns[k]->ns); result.obs[k]->jindex = (int *)malloc(sizeof(int) *stn_set.stns[k]->ns); int im = -1; for (int i=0; i < stn_set.stns[k]->ns;++i){ im = -1; for(int j=0; j < ny;++j){ if (stn_set.stns[k]->lat >= latm[j]) im = j; } result.obs[k]->jindex[i] = im; } for (int i=0; i ns;++i){ im = -1; for(int j=0; j < nx;++j){ if (stn_set.stns[k]->lon >= lonm[j]) im = j; } result.obs[k]->iindex[i] = im; } } } return result; } static struct Index map(int i, int j, int w, int h, int nx, int ny){ struct Index result = {0,0}; float wm1 = (float)(w -1); float hm1 = (float)(h-1 ); float nx1 = (float)(nx-1); float ny1 = (float)(ny-1); result.i = (int)((wm1/nx1) * i); result.j = (int)((hm1/ny1) * j); return result; } static void showindex(struct Index *index){ printf("%d %d\n",index->i,index->j); } static uint32 lenstring(char *str){ if (str == NULL) return 0U; uint32 len = 0U; while (*str != '\0'){ len++; str++; } return len; } static void writetostring(char *buffer, int N, char *str,...){ if (str == NULL) return; va_list vv; va_start(vv,str); char *tmp = buffer; int counter = 0; while(*str!='\0'){ if (counter < N){ *tmp = *str; tmp++; } counter++; str++; } while((str=va_arg(vv,char *)) != NULL){ while(*str!='\0'){ if (counter < N ){ *tmp = *str; tmp++; } counter++; str++; } } va_end(vv); return; } static float minval(float *a, int n ){ float mv = a[0]; for (int i=1; i < n;++i){ if (a[i] <= mv) mv = a[i]; } return mv; } static float maxval(float *a, int n){ float mv = a[0]; for (int i=1; i < n;++i){ if (a[i] >= mv) mv = a[i]; } return mv; } static float minvall(float *a, long int n ){ float mv = a[0]; for (long int i=1; i < n;++i){ if (a[i] <= mv) mv = a[i]; } return mv; } static float maxvall(float *a, long int n){ float mv = a[0]; for (long int i=1; i < n;++i){ if (a[i] >= mv) mv = a[i]; } return mv; }