Commit e1677041 authored by saeed's avatar saeed

first commit

parents
#define NX (4020)
#define NY (1680)
#define NT (1)
#define NUM_THREADS (1)
#define WIDTH_INIT (1200)
#define HEIGHT_INIT (900)
#define SYM_SIZE (10)
#define WIDTH_TR1 (600)
#define HEIGHT_TR1 (600)
#define INCREASE_RADIUS (1.5)
#define VMAX (1.0)
#define VMIN (0.0)
#define useminmax 0
#define usesrgb 1
#define oceancolor 0xffffffff
#define landcolor 0xb1b1b1b1
#define stncolor 0x00bf0000
static void reverse(char *s, char *t, int sign, int npadzero){
char *tmp = s;
int len = 0;
while (*tmp !='\0' ){
tmp++;
len++;
}
tmp--;
if (sign < 0){
*t = '-';
t++;
}
while(npadzero--){
*t = '0';
t++;
}
while (len >0 ){
*t = *tmp;
tmp--;
len--;
t++;
}
*t = '\0';
}
static void intostr(int a, char *st){
int sign = 1;
if (a < 0){
sign = -1;
a = (-a);
}
int index = 0;
int r = -1 ;
int q = 0;
char cl[64] = {'\0'};
do {
r = a % 10;
q = (a - r) / 10;
*(cl+index) = (char)(r+'0');
a = q;
index++;
}while(a >0);
*(cl+index) = '\0';
reverse(cl,st,sign,0);
}
static void floatostr(float a, char *str, int nafterdecimalpoint){
int sign = 1;
long long int ia = (long long int) (a);
if (a < 0) {
//printf("we are here\n");
sign = -1;
a = (-a);
ia = (long long int) (a);
}
int npadzero = 0;
long long int n = 1;
float b = a - (float)ia;
float bt = b;
int hitnonzero = 0;
//printf("b is %f\n",b);
for(int i=0; i < nafterdecimalpoint;++i){
n *= 10L;
bt *= 10L;
if (((long long int)bt == 0) && (hitnonzero == 0)) npadzero++;
if ((long long int)bt != 0 ) hitnonzero = 1;
bt = bt - (long long int)bt;
}
if (nafterdecimalpoint == 1) npadzero = 0;
b *= n;
long long int ifrac = (long long int) b;
int index = 0;
long long int r = -1 ;
long long int q = 0;
char cl[64] = {'\0'};
char ctmp[64] = {'\0'};
do {
r = ia % 10;
q = (ia - r) / 10;
*(cl+index) = (char)(r+'0');
ia = q;
index++;
}while(ia >0);
*(cl+index) = '\0';
reverse(cl,ctmp,sign,0);
char ck[64] = {'\0'};
char ctmp1[64] = {'\0'};
r = -1 ;
q = 0;
index = 0;
do {
r = ifrac % 10;
q = (ifrac - r) / 10;
*(ck+index) = (char)(r+'0');
ifrac = q;
index++;
}while(ifrac >0);
*(ck+index) = '\0';
if (sign > 0){
reverse(ck,ctmp1,sign,npadzero);
}else{
reverse(ck,ctmp1,-sign,npadzero);
}
char *tmp = ctmp;
while(*tmp != '\0'){
*str = *tmp;
str++;
tmp++;
}
*str = '.';
str++;
tmp = ctmp1;
while(*tmp != '\0'){
*str = *tmp;
str++;
tmp++;
}
*str = '\0';
return;
}
This diff is collapsed.
static void drawonscreen_expose_thread(struct Vis *vis){
char *info = cinfo;
Pixmap pixmap = XCreatePixmap(vis->dsp, vis->win, vis->w, vis->h, vis->depth);
XSetForeground(vis->dsp, vis->gc, 0xffffffff);
XFillRectangle(vis->dsp, pixmap, vis->gc, 0, 0, vis->w, vis->h);
int txw = XTextWidth(vis->fontinfo,info,lenstring(info));
int direction;
int ascent;
int descent;
XCharStruct overall;
XTextExtents(vis->fontinfo,info,lenstring(info),&direction, &ascent, &descent, &overall);
int txh = (int)(ascent + descent);
XSetForeground(vis->dsp, vis->gc, 0x0);
XDrawString(vis->dsp, pixmap, vis->gc,vis->w / 2 - txw,vis->h / 2,info,lenstring(info));
XCopyArea (vis->dsp, pixmap, vis->win, vis->gc,0, 0,vis->w, vis->h,0, 0);
XFreePixmap(vis->dsp,pixmap);
}
static void drawonscreen_configure_thread(struct Vis *vis){
char *info = cinfo;
Pixmap pixmap = XCreatePixmap(vis->dsp, vis->win, vis->w, vis->h, vis->depth);
XSetForeground(vis->dsp, vis->gc, 0xffffffff);
XFillRectangle(vis->dsp, pixmap, vis->gc, 0, 0, vis->w, vis->h);
int txw = XTextWidth(vis->fontinfo,info,lenstring(info));
int direction;
int ascent;
int descent;
XCharStruct overall;
XTextExtents(vis->fontinfo,info,lenstring(info),&direction, &ascent, &descent, &overall);
int txh = (int)(ascent + descent);
XSetForeground(vis->dsp, vis->gc, 0x0);
XDrawString(vis->dsp, pixmap, vis->gc,vis->w / 2 - txw,vis->h / 2,info,lenstring(info));
XCopyArea (vis->dsp, pixmap, vis->win, vis->gc,0, 0,vis->w, vis->h,0, 0);
XFreePixmap(vis->dsp,pixmap);
odbinfo_onotherwindow(vis, &odbinfo);
}
static void drawonscreen_motion_thread(struct Vis *vis, XEvent *e){
while(XCheckTypedEvent(vis->dsp,MotionNotify,e));
int x = e->xmotion.x;
int y = vis->h - e->xmotion.y -1;
}
static void *handle_event_thread(void *varg){
struct Vis *vis = (struct Vis *) varg;
int done = 1;
XEvent e;
int palet = 0;
for(;done;){
XNextEvent (vis->dsp,&e);
if (e.type == Expose){
drawonscreen_expose_thread(vis);
}
}
#if 0
for(;done;){
XNextEvent (vis->dsp,&e);
if (e.type == FocusIn){
//printf("focus in from thread 1\n");
}
if (e.type == Expose){
//printf("expose from thread 1\n");
}
if (e.type == ConfigureNotify) {
XConfigureEvent xce = e.xconfigure;
vis->w = (int)xce.width;
vis->h = (int)xce.height;
drawonscreen_configure_thread(vis);
}
if (e.type == ButtonPress ){
char cinfo[1024] = {'\0'};
char ciind[64] = {'\0'};
char cjind[64] = {'\0'};
intostr(e.xbutton.x,ciind);
intostr(vis->h -e.xbutton.y-1,cjind);
writetostring(cinfo, sizeof(cinfo)-1,ciind," ",cjind,NULL);
showinfoonsecondwindow(vis,cinfo);
}
if (e.type == MotionNotify){
while(XCheckTypedEvent(vis->dsp,MotionNotify,&e));
drawonscreen_motion_thread(vis,&e);
}
if (e.type == KeyPress){
char buffer[80] = {'\0'};
KeySym keysym;
XLookupString(&e.xkey,buffer,sizeof(buffer) -1,&keysym,NULL);
if (keysym == XK_KP_Add){
vis->h = (int)(1.20 * vis->h);
vis->w = (int)(1.20 * vis->w);
XResizeWindow(vis->dsp,vis->win,vis->w,vis->h);
}
if (keysym == XK_KP_Subtract){
vis->h = (int)(0.80 * vis->h);
vis->w = (int)(0.80 * vis->w);
XResizeWindow(vis->dsp,vis->win,vis->w,vis->h);
}
if ((buffer[0] == 'q') || ( buffer[0]== 'Q') ){
done = 0;
}
}
}
#endif
return NULL;
}
This diff is collapsed.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include <X11/Xatom.h>
#include<X11/keysym.h>
#include<X11/cursorfont.h>
#include<pthread.h>
#include<stdarg.h>
#include<time.h>
#include"intostr.c"
#include"config.h"
#include"reportype.h"
#define __USE_XOPEN_EXTENDED (1)
#include<unistd.h>
#define ArrayCount(a) (sizeof(a) / sizeof(a[0]))
#define StringArrayCount(a) (ArrayCount(a) -1)
typedef unsigned int uint32;
typedef unsigned char uint8;
#pragma pack(push,1)
struct field {
short SSH[NX*NY];
uint8 image32[NX*NY*4];
float lat[NY];
float lon[NX];
};
#pragma pack(pop)
static struct field FLD = {{0},{0},{0.0},{0.0}};
static char cinfo[1024] = {'\0'};
struct station {
char name[256];
float lon;
float lat;
int ix;
int iy;
int radius;
int ns;
};
static struct station *stns;
struct odbinfo{
char *filename;
char ntotal[256];
char cycle[256];
char start_date[256];
char end_date[256];
char reportype[256];
char *desc;
char *group;
};
static struct odbinfo odbinfo = {NULL,{'\0'},{'\0'},{'\0'},{'\0'},{'\0'},NULL,NULL};
struct Vis{
Display *dsp;
Window win;
Visual *visual;
int screen_num;
XGCValues gr_values;
XFontStruct *fontinfo;
GC gc;
int w;
int h;
int depth;
Cursor font_cursor;
Colormap cmap;
};
struct dsp_field {
float *SSH;
uint8 *image32;
int nx;
int ny;
};
struct Colort{
float red, green, blue, alpha;
int ncol;
};
struct xdata {
Display *dsp;
int screen_num;
Window win;
GC gc;
};
struct symbol{
float radius;
};
struct obs{
int *iindex;
int *jindex;
int npx;
int npy;
};
struct mousepose{
int x;
int y;
};
struct textprop{
int direction;
int ascent;
int descent;
XCharStruct overall;
};
struct screen_dim{
int width;
int height;
float symbol_radius;
};
struct Index {
int i;
int j;
};
#include"util.c"
#include"renderer.c"
#include"renderer_thread.c"
int main(int argc, char *argv[]){
if (argc == 1){
printf("\nUsage %s odb_file\n\n",argv[0]);
}
if (argc == 2){
int width = WIDTH_INIT;
int height = HEIGHT_INIT;
int ncolor = 0;
struct screen_dim scdim = {0,0,0};
scdim.width = width;
scdim.height = height;
scdim.symbol_radius = SYM_SIZE;
struct Colort *scolor = NULL;
get2dfield_binary("bathy_precise_etopo2.bin",&FLD);
struct Vis visu = init_x(width,height);
struct dsp_field dspf = adjusttowindowsize(&visu, &FLD);
stns = getstn(argv[1],&odbinfo);
width = WIDTH_TR1;
height = HEIGHT_TR1;
struct Vis visu_1 = init_x(width,height);
map_window(&visu_1);
map_window(&visu);
pthread_t thread_id[NUM_THREADS];
for (int i=0; i < NUM_THREADS;++i){
pthread_create(&thread_id[i], NULL, handle_event_thread, (void *)&visu_1);
}
handle_event(&visu,&visu_1,&dspf,scolor,ncolor,&scdim);
/*
for (int i=0; i < NUM_THREADS;++i){
pthread_join(thread_id[i], NULL);
}
*/
printf("After Thread\n");
}
return 0;
}
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 void getreportype(char *filename,char *reportype){
char *beg = NULL;
char *end = NULL;
char *str = filename;
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;
//intostr(nl, odbinfo->ntotal);
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);
//printf("%s\n",result[iter].name);
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 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 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;
}