n4 mp_sc_ring_pop(vp ring_ptr, vp elem) { u4 ridx; u4 max_ridx; ring_p ring; ring = ring_ptr; ridx = ring->read_pos; max_ridx = ring->max_read_idx; // the ring is empty if (IS_EQ(ring_index(ridx, ring->unit_num), ring_index(max_ridx, ring->unit_num))) { // RET_INT(-2, "push ridx = %u, max_ridx = %u", ridx, max_ridx); RET_INT(-2, nil_str); } CPY(elem, ring->data + ring_index(ridx, ring->unit_num) * ring->unit_size, ring->unit_size); lisk_sync_add(ring->read_pos, 1); lisk_sync_sub(ring->size, 1); RET_INT(0, nil_str); }
n4 sp_mc_ring_pop(vp ring_ptr, vp elem) { u4 ridx; u4 widx; ring_p ring; ridx = 0; widx = 0; ring = ring_ptr; do { ridx = ring->read_pos; widx = ring->write_pos; // the ring is empty if (IS_EQ(ring_index(ridx, ring->unit_num), ring_index(widx, ring->unit_num))) { // RET_INT(-2, "push widx = %u, ridx = %u", widx, ridx); RET_INT(-2, nil_str); } CPY(elem, ring->data + ring_index(ridx, ring->unit_num) * ring->unit_size, ring->unit_size); if (IS_EQ(lisk_sync_cas(ring->read_pos, ridx, ridx + 1), ridx)) { lisk_sync_sub(ring->size, 1); RET_INT(0, nil_str); } } while(1); RET_INT(-2, nil_str); }
n4 mp_mc_ring_pop(vp ring_ptr, vp elem) { u4 ridx; u4 max_ridx; ring_p ring; ridx = 0; max_ridx = 0; ring = ring_ptr; do { ridx = ring->read_pos; max_ridx = ring->max_read_idx; // the ring is empty or a procuder has got space // from the ring and dumping data into it now if (IS_EQ(ring_index(ridx, ring->unit_num), ring_index(max_ridx, ring->unit_num))) { // RET_INT(-2, "push ridx = %u, max_ridx = %u", ridx, max_ridx); RET_INT(-2, nil_str); } CPY(elem, ring->data + ring_index(ridx, ring->unit_num) * ring->unit_size, ring->unit_size); if (IS_EQ(lisk_sync_cas(ring->read_pos, ridx, ridx + 1), ridx)) { lisk_sync_sub(ring->size, 1); RET_INT(0, nil_str); } } while(1); RET_INT(-2, nil_str); }
n4 sp_ring_push(vp ring_ptr, vp elem) { u4 ridx; u4 widx; ring_p ring; ring = ring_ptr; ridx = ring->read_pos; widx = ring->write_pos; //the ring is full if (IS_EQ(ring_index(widx + 1, ring->unit_num), ring_index(ridx, ring->unit_num))) { // RET_INT(-2, "push widx = %u, ridx = %u", widx, ridx); RET_INT(-2, nil_str); } CPY(ring->data + ring_index(widx, ring->unit_num) * ring->unit_size, elem, ring->unit_size); lisk_sync_add(ring->write_pos, 1); lisk_sync_add(ring->size, 1); RET_INT(0, nil_str); }
n4 mp_ring_push(vp ring_ptr, vp elem) { u4 ridx; u4 widx; ring_p ring; ridx = 0; widx = 0; ring = ring_ptr; do { ridx = ring->read_pos; widx = ring->write_pos; //the ring is full if (IS_EQ(ring_index(widx + 1, ring->unit_num), ring_index(ridx, ring->unit_num))) { // RET_INT(-2, "push widx = %u, ridx = %u", widx, ridx); RET_INT(-2, nil_str); } } while(IS_NEQ(lisk_sync_cas(ring->write_pos, widx, widx + 1), widx)); CPY(ring->data + ring_index(widx, ring->unit_num) * ring->unit_size, elem, ring->unit_size); while (IS_NEQ(lisk_sync_cas(ring->max_read_idx, widx, widx + 1), widx)) { sched_yield(); } lisk_sync_add(ring->size, 1); RET_INT(0, nil_str); }
n4 sp_sc_ring_pop(vp ring_ptr, vp elem) { u4 ridx; u4 widx; ring_p ring; ring = ring_ptr; ridx = ring->read_pos; widx = ring->write_pos; // the ring is empty if (IS_EQ(ring_index(ridx, ring->unit_num), ring_index(widx, ring->unit_num))) { // RET_INT(-2, "push widx = %u, ridx = %u", widx, ridx); RET_INT(-2, nil_str); } CPY(elem, ring->data + ring_index(ridx, ring->unit_num) * ring->unit_size, ring->unit_size); // lisk_sync_add(ring->read_pos, 1); // lisk_sync_sub(ring->size, 1); ring->read_pos++; ring->size++; RET_INT(0, nil_str); }
nemo_main() { stream denstr, velstr, outstr, tabstr; real center[2], cospa, sinpa, cosi, sini, cost, costmin, x, y, xt, yt, r, den; real vr, wt, frang, dx, dy, xmin, ymin, rmin, rmax, ave, tmp, rms; real sincosi, cos2i, tga, dmin, dmax, dval, dr, area, fsum1, fsum2; int i, j, k, nx, ny, ir, nring, nundf, nout, nang, nsum; string outmode; int mode = -1; velstr = stropen(getparam("in"),"r"); read_image(velstr,&velptr); nx = Nx(velptr); ny = Ny(velptr); if (hasvalue("out")) { outmode = getparam("mode"); mode = string_index(outmodes, outmode); if (mode < 0) error("Illegal mode=%s [%d], valid:",outmode,mode,outmodes); warning("New out= mode mode=%s [%d]",outmode,mode); Qout = TRUE; outstr = stropen(getparam("out"),"w"); copy_image(velptr,&outptr); } if (hasvalue("den")) { Qden = TRUE; denstr = stropen(getparam("den"),"r"); read_image(denstr,&denptr); } else if (mode==2) error("Need den="); if (hasvalue("tab")) { Qtab = TRUE; tabstr = stropen(getparam("tab"),"w"); } nrad = nemoinpd(getparam("radii"),rad,MAXRING); if (nrad < 2) error("got no rings (%d), use radii=",nrad); nring = nrad-1; if (hasvalue("center")) { if (nemoinpd(getparam("center"),center,2) != 2) error("not enuf values for center=, need 2"); xpos = center[0]; ypos = center[1]; } else { xpos = (Nx(velptr)-1.0)/2.0; ypos = (Ny(velptr)-1.0)/2.0; } pa = getdparam("pa"); inc = getdparam("inc"); vsys = getdparam("vsys"); undf = getdparam("blank"); frang = getdparam("frang"); cospa = cos(pa*PI/180.0); sinpa = sin(pa*PI/180.0); sini = sin(inc*PI/180.0); cosi = cos(inc*PI/180.0); costmin = sin(frang*PI/180.0); sincosi = sini*cosi; cos2i = cosi*cosi; for (i=0; i<nring; i++) pixe[i] = vsum[i] = vsqu[i] = wsum[i] = 0; nundf = nout = nang = 0; ymin = Ymin(velptr); xmin = Xmin(velptr); dx = Dx(velptr); dx = ABS(dx); dx = -dx; dy = Dy(velptr); dy = ABS(dy); rmin = -nx*dx*10.0; rmax = 0.0; dprintf(0,"Map %d x %d pixels, size %g x %g\n", Nx(velptr), Ny(velptr), -dx*Nx(velptr), dy*Ny(velptr)); dprintf(0,"Pixel size: %g x %g\n",-dx, dy); dmin = dmax = vsys; /* loop over the map, accumulating data for fitting process */ for (j=0; j<ny; j++) { y = (j-ypos)*dy; for (i=0; i<nx; i++) { if (MapValue(velptr,i,j) == undf) { nundf++; if (Qout) MapValue(outptr,i,j) = undf; continue; } x = (i-xpos)*dx; yt = x*sinpa + y*cospa; /* major axis now along Y */ xt = x*cospa - y*sinpa; /* minor axis along X */ xt /= cosi; /* deproject to the circle */ r = sqrt(xt*xt+yt*yt); /* radius in the disk */ rmin = MIN(r,rmin); rmax = MAX(r,rmax); ir = ring_index(nrad,rad,r); dprintf(2,"r=%g ir=%d (x,y)=%g,%g (xt,yt)=%g,%g\n", r,ir,x,y,xt,yt); if (ir < 0) { nout++; continue; } cost = yt/r; dval = MapValue(velptr,i,j); if (mode==1) dval /= r; else if (mode==2) dval *= MapValue(denptr,i,j) / r; if (outptr) { if (ABS(cost) > costmin) { MapValue(outptr,i,j) = dval; if (dval > dmax) dmax = dval; if (dval < dmin) dmin = dval; } else { MapValue(outptr,i,j) = undf; } } /* now some ring accumulation, remnant of the velfit fitting */ if (ABS(cost) > costmin) { pixe[ir] += 1; wsum[ir] += 1.0; vsum[ir] += dval; vsqu[ir] += dval*dval; } else nang++; } /* i */ } /* j */ /* write output map(s), if needed */ if (Qout) { dprintf(0,"Data min/max = %g %g\n",dmin,dmax); MapMin(outptr) = dmin; MapMax(outptr) = dmax; write_image(outstr,outptr); } /* report on the rings */ if (Qtab) { fprintf(tabstr,"# r I rms I_sum1 I_sum2 i_ring Npoints\n"); fsum1 = 0.0; /* this will count rings with average values */ fsum2 = 0.0; /* this will count up flux whenever it fell in a ring */ nsum = 0; for (i=0; i<nring; i++) { if (wsum[i] == 0.0) continue; nsum++; r = 0.5*(rad[i] + rad[i+1]); dr = rad[i+1] - rad[i]; area = PI*(sqr(rad[i+1]) - sqr(rad[i])); ave = vsum[i]/wsum[i]; rms = vsqu[i]/wsum[i]-ave*ave; if (rms < 0) rms=0.0; rms = sqrt(rms); fsum1 += ave*area; fsum2 += vsum[i]; fprintf(tabstr,"%g %g %g %g %g %d %d\n", r,ave,rms,fsum1,fsum2,i+1,pixe[i]); } } dprintf(0,"Nundf=%d/%d Nout=%d Nang=%d (sum=%d)\n", nundf,nx*ny,nout,nang,nout+nundf+nang); dprintf(0,"Rmin/max = %g %g\n",rmin,rmax); }
local void histogram(void) { int i,j,k, l, kmin, kmax, lcount = 0; real count[MAXHIST], under, over; real xdat,ydat,xplt,yplt,dx,r,sum,sigma2, q, qmax; real mean, sigma, mad, skew, kurt, h3, h4, lmin, lmax, median; real rmean, rsigma, rrange[2]; Moment m; dprintf (0,"read %d values\n",npt); dprintf (0,"min and max value in column(s) %s: %g %g\n",getparam("xcol"),xmin,xmax); if (!Qauto) { xmin = xrange[0]; xmax = xrange[1]; dprintf (0,"min and max value reset to : %g %g\n",xmin,xmax); lmin = xmax; lmax = xmin; for (i=0; i<npt; i++) { if (x[i]>xmin && x[i]<=xmax) { lmin = MIN(lmin, x[i]); lmax = MAX(lmax, x[i]); } } dprintf (0,"min and max value in range : %g %g\n",lmin,lmax); } for (k=0; k<nsteps; k++) count[k] = 0; /* init histogram */ under = over = 0; ini_moment(&m, 4, Qrobust||Qmad ? npt : 0); for (i=0; i<npt; i++) { if (Qbin) { k=ring_index(nsteps,bins,x[i]); } else { if (xmax != xmin) k = (int) floor((x[i]-xmin)/(xmax-xmin)*nsteps); else k = 0; dprintf(2,"%d k=%d %g\n",i,k,x[i]); } if (k==nsteps && x[i]==xmax) k--; /* include upper edge */ if (k<0) { under++; continue; } if (k>=nsteps) { over++; continue; } count[k] = count[k] + 1; dprintf (4,"%d : %f %d\n",i,x[i],k); accum_moment(&m,x[i],1.0); } if (under > 0) error("bug: under = %d",under); if (over > 0) error("bug: over = %d",over); under = Nunder; over = Nover; mean = mean_moment(&m); sigma = sigma_moment(&m); skew = skewness_moment(&m); kurt = kurtosis_moment(&m); h3 = h3_moment(&m); h4 = h4_moment(&m); if (Qmad) mad = mad_moment(&m); if (nsigma > 0) { /* remove outliers iteratively, starting from the largest */ iq = (int *) allocate(npt*sizeof(int)); for (i=0; i<npt; i++) { #if 1 iq[i] = x[i] < xmin || x[i] > xmax; #else iq[i] = 0; #endif } lcount = 0; do { /* loop to remove outliers one by one */ qmax = -1.0; for (i=0, l=-1; i<npt; i++) { /* find largest deviation from current mean */ if (iq[i]) continue; /* but skip previously flagged points */ q = (x[i]-mean)/sigma; q = ABS(q); if (q > qmax) { qmax = q; l = i; } } if (qmax > nsigma) { lcount++; iq[l] = 1; decr_moment(&m,x[l],1.0); mean = mean_moment(&m); sigma = sigma_moment(&m); skew = skewness_moment(&m); kurt = kurtosis_moment(&m); h3 = h3_moment(&m); h4 = h4_moment(&m); if (Qmad) mad = mad_moment(&m); dprintf(1,"%d/%d: removing point %d, m/s=%g %g qmax=%g\n", lcount,npt,l,mean,sigma,qmax); if (sigma <= 0) { /* RELATED TO presetting MINMAX */ warning("BUG"); accum_moment(&m,x[l],1.0); mean = mean_moment(&m); sigma = sigma_moment(&m); skew = skewness_moment(&m); kurt = kurtosis_moment(&m); h3 = h3_moment(&m); h4 = h4_moment(&m); dprintf(1,"%d/%d: LAST removing point %d, m/s=%g %g qmax=%g\n", lcount,npt,l,mean,sigma,qmax); break; } } else dprintf(1,"%d/%d: keeping point %d, m/s=%g %g qmax=%g\n", lcount,npt,l,mean,sigma,qmax); /* if (lcount > npt/2) break; */ } while (qmax > nsigma); dprintf(0,"Removed %d/%d points for nsigma=%g\n",lcount,npt,nsigma); /* @algorithm left shift array values from mask array */ /* now shift all points into the array, decreasing npt */ /* otherwise the median is not correctly computed */ for (i=0, k=0; i<npt; i++) { dprintf(1,"iq->%d\n",iq[i]); if (iq[i]) k++; if (k==0) continue; /* ?? */ if (i-k < 0) continue; dprintf(1,"SHIFT: %d <= %d\n",i-k,i); x[i-k] = x[i]; } npt -= lcount; /* correct for outliers */ free(iq); } /* nsigma > 0 */ if (npt != n_moment(&m)) error("Counting error, probably in removing outliers..."); dprintf (0,"Number of points : %d\n",npt); if (npt>1) dprintf (0,"Mean and dispersion : %g %g %g\n",mean,sigma,sigma/sqrt(npt-1.0)); else dprintf (0,"Mean and dispersion : %g %g 0.0\n",mean,sigma); if (Qmad) dprintf (0,"MAD : %g\n",mad); dprintf (0,"Skewness and kurtosis: %g %g\n",skew,kurt); dprintf (0,"h3 and h4 : %g %g\n", h3, h4); if (Qmedian) { if (npt % 2) median = x[(npt-1)/2]; else median = 0.5 * (x[npt/2] + x[npt/2-1]); dprintf (0,"Median : %g\n",median); } else if (Qtorben) { median = median_torben(npt,x,xmin,xmax); dprintf (0,"Median_torben : %g\n",median); } dprintf (0,"Sum : %g\n",show_moment(&m,1)); if (Qrobust) { compute_robust_moment(&m); rmean = mean_robust_moment(&m); rsigma = sigma_robust_moment(&m); robust_range(&m, rrange); dprintf (0,"Robust N : %d\n",n_robust_moment(&m)); dprintf (0,"Robust Mean Disp : %g %g\n",rmean,rsigma); dprintf (0,"Robust Range : %g %g\n",rrange[0],rrange[1]); if (outstr) { for (i=0; i<npt; i++) { if (x[i]<rrange[0] || x[i]>rrange[1]) continue; fprintf(outstr,"%g %d\n",x[i],i+1); } } } if (lcount > 0) { warning("Recompute histogram because of outlier removals"); /* recompute histogram if we've lost some outliers */ for (k=0; k<nsteps; k++) count[k] = 0; /* init histogram */ under = over = 0; for (i=0; i<npt; i++) { if (xmax != xmin) k = (int) floor((x[i]-xmin)/(xmax-xmin)*nsteps); else k = 0; if (k==nsteps && x[i]==xmax) k--; /* include upper edge */ if (k<0) { under++; continue; } if (k>=nsteps) { over++; continue; } count[k] = count[k] + 1; dprintf (4,"%d : %f %d\n",i,x[i],k); } if (under > 0 || over > 0) error("under=%d over=%d in recomputed histo",under,over); } dprintf (3,"Histogram values : \n"); dx=(xmax-xmin)/nsteps; kmax=0; sum=0.0; for (k=0; k<nsteps; k++) { sum = sum + dx*count[k]; if (ylog) { if (count[k]>0.0) count[k] = log10(count[k]); else count[k] = -1.0; } if (count[k]>kmax) kmax=count[k]; dprintf (3,"%f ",count[k]); if (Qcumul) { if (k==0) count[k] += under; else count[k] += count[k-1]; } } dprintf (3,"\n"); sigma2 = 2.0 * sigma * sigma; /* gaussian */ sum /= sigma * sqrt(2*PI); /* scaling factor for equal area gauss */ if (ylog && over>0) over = log10(over); if (ylog && under>0) under = log10(under); kmax *= 1.1; /* add 10% */ if (Qcumul) kmax = npt; if (maxcount>0) /* force scaling by user ? */ kmax=maxcount; if (Qtab) { maxcount = 0; for (k=0; k<nsteps; k++) maxcount = MAX(maxcount,count[k]); if (maxcount>0) r = 29.0/maxcount; else r = 1.0; printf(" Bin Value Number\n"); printf(" Underflow %d\n",Nunder); for (k=0; k<nsteps; k++) { j = (int) (r*count[k]) + 1; if (ylog) printf("%3d %13.6g %13.6g ", k+1, xmin+(k+0.5)*dx, count[k]); else printf("%3d %13.6g %8d ", k+1, xmin+(k+0.5)*dx, (int)count[k]); while (j-- > 0) printf("*"); printf("\n"); } printf(" Overflow %d\n",Nover); stop(0); } #ifdef YAPP /* PLOTTING */ plinit("***",0.0,20.0,0.0,20.0); xplot[0] = xmin; xplot[1] = xmax; yplot[0] = 0.0; yplot[1] = (real) kmax; xaxis (2.0, 2.0, 16.0, xplot, -7, xtrans, xlab); xaxis (2.0, 18.0,16.0, xplot, -7, xtrans, NULL); yaxis (2.0, 2.0, 16.0, yplot, -7, ytrans, ylab); yaxis (18.0, 2.0, 16.0, yplot, -7, ytrans, NULL); pljust(-1); /* set to left just */ pltext(input,2.0,18.2,0.32,0.0); /* filename */ pljust(1); pltext(headline,18.0,18.2,0.24,0.0); /* headline */ pljust(-1); /* return to left just */ xdat=xmin; dx=(xmax-xmin)/nsteps; plmove(xtrans(xmin),ytrans(0.0)); for (k=0; k<nsteps; k++) { /* nsteps= */ xplt = xtrans(xdat); yplt = ytrans((real)count[k]); plline (xplt,yplt); xdat += dx; xplt = xtrans(xdat); plline (xplt,yplt); } plline(xplt,ytrans(0.0)); for (i=0; i<nxcoord; i++) { plmove(xtrans(xcoord[i]),ytrans(yplot[0])); plline(xtrans(xcoord[i]),ytrans(yplot[1])); } if (Qgauss) { /* plot model and residuals */ if (ylog) plmove(xtrans(xmin),ytrans(-1.0)); else plmove(xtrans(xmin),ytrans(0.0)); for (k=0; k<100; k++) { xdat = xmin + (k+0.5)*(xmax-xmin)/100.0; ydat = sum * exp( -sqr(xdat-mean)/sigma2); if (ylog) ydat = log10(ydat); plline(xtrans(xdat), ytrans(ydat)); } } if (Qresid) { plltype(0,2); /* dotted residuals */ xdat = xmin+0.5*dx; dprintf(1,"# residuals from gauss\n"); for (k=0; k<nsteps; k++, xdat +=dx) { ydat = sum * exp( -sqr(xdat-mean)/sigma2); dprintf(1,"%g %g %g\n",xdat,count[k],ydat); if (ylog) ydat = log10(ydat); ydat = count[k] - ydat; if (k==0) plmove(xtrans(xdat),ytrans(ydat)); else plline(xtrans(xdat),ytrans(ydat)); } plltype(0,1); /* back to normal line type */ } plstop(); #endif }