int open_net(char *ip_address, int iPort) { /* Save the address information */ STRcopy(Ip_address,ip_address, MAX_PATH_LEN); Port = iPort; /* set up signals */ PORTsignal(SIGPOLL, handle_sigpoll); PORTsignal(SIGIO, handle_sigpoll); /* Open the connection */ do { PMU_auto_register("Trying to connect to client"); gDevPollArray[0].fd = SKU_open_client_timed(ip_address, iPort, 1000); if (gDevPollArray[0].fd < 0) { fprintf(stderr, "Error %d opening client on address %s, port %d\n", SKUerrno, ip_address, iPort); sleep(1); } } while (gDevPollArray[0].fd < 0); return( FALSE); }
int IwrfExport::_writeToOutputFmq(bool force) { // if the message is large enough, write to the FMQ int nParts = _outputMsg.getNParts(); // DLOG << "==>> WriteToOutputFmq, nparts: " << nParts; if (!force && nParts < _config.iwrf_fmq_npackets_per_message()) { return 0; } PMU_auto_register("writeToFmq"); void *buf = _outputMsg.assemble(); int len = _outputMsg.lengthAssembled(); QWriteLocker wLocker(&_fmqAccessLock); if (_outputFmq.writeMsg(0, 0, buf, len)) { ELOG << "ERROR - IwrfExport"; ELOG << " Cannot write FMQ: " << _fmqPath; _outputMsg.clearParts(); return -1; } _outputMsg.clearParts(); return 0; }
void process_test_stream(void) { int forever = TRUE; char *callsign; double lat, lon, alt; date_time_t wall_time; time_t last_store = 0; init_test_data(); while (forever) { PMU_auto_register("Got data"); get_test_data(&callsign, &lat, &lon, &alt); /* * substitute wall clock time for gps time */ wall_time.unix_time = time(NULL); uconvert_from_utime(&wall_time); if((wall_time.unix_time - last_store) >= Glob->params.data_interval) { store_line(&wall_time, lat, lon, alt, callsign, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0); if (Glob->params.use_spdb) { if (Glob->params.single_database) { store_single_spdb(&wall_time, lat, lon, alt, callsign); } else { store_mult_spdb(&wall_time, lat, lon, alt, callsign); } } /* if (Glob->params.use_spdb) */ last_store = wall_time.unix_time; } /* if((wall_time.unix_time - last_store) ... */ sleep(1); } /* while (forever) */ free_test_data(); }
void IwrfExport::_readNextPulse() { PMU_auto_register("reading pulses"); // read next pulse data for each channel _readNextH(); _readNextV(); // synchronize the pulses and burst to have same sequence number, // reading extra pulses as required _syncPulses(); if (_pulseSeqNum < 0) { // first time _prevPulseSeqNum = _pulseH->getPulseSeqNum() - 1; _prevTimeSecs = _pulseH->getTimeSecs(); _prevNanoSecs = _pulseH->getNanoSecs(); } else { _prevPulseSeqNum = _pulseSeqNum; _prevTimeSecs = _timeSecs; _prevNanoSecs = _nanoSecs; } _pulseSeqNum = _pulseH->getPulseSeqNum(); _timeSecs = _pulseH->getTimeSecs(); _nanoSecs = _pulseH->getNanoSecs(); if (! (_pulseSeqNum % 10000)) { DLOG << "got 10000 pulses, seqNum, secs, nanoSecs: " << _pulseSeqNum << ", " << _timeSecs << ", " << _nanoSecs; } if (_pulseSeqNum != _prevPulseSeqNum + 1) { int nMissing = _pulseSeqNum - _prevPulseSeqNum - 1; WLOG << "Missing pulses - nmiss, prevNum, thisNum: " << nMissing << ", " << _prevPulseSeqNum << ", " << _pulseSeqNum; } }
time_t forecast_generate(storm_file_handle_t *s_handle, track_file_handle_t *t_handle, vol_file_handle_t *v_handle, vol_file_handle_t *map_v_handle, date_time_t *scan_times, si32 scan_num, double lead_time_requested) { ui08 *forecast; char rdata_file_path[MAX_PATH_LEN]; char map_file_dir[MAX_PATH_LEN]; char map_file_path[MAX_PATH_LEN]; int n_scans; double lead_time_used, lead_time_hr; double precip_lookup[256]; double coeff, expon; time_t time_this_scan, time_latest_scan; time_t scan_time_diff; time_t forecast_time; date_time_t *stime, ftime; cart_params_t *cart; mdv_grid_t grid; field_params_t *dbz_fparams; PMU_auto_register("In forecast_generate"); lock_and_read_headers(s_handle, t_handle); /* * set up times */ stime = scan_times + scan_num; time_this_scan = stime->unix_time; scan_time_diff = time_latest_scan - time_this_scan; n_scans = s_handle->header->n_scans; time_latest_scan = scan_times[n_scans - 1].unix_time; /* * compute forecast time */ forecast_time = time_this_scan + lead_time_requested; if (Glob->params.round_forecast_times) { forecast_time = (time_t) ((int) floor((double) forecast_time / Glob->params.rounding_interval + 0.5) * Glob->params.rounding_interval); } lead_time_used = forecast_time - time_this_scan; lead_time_hr = lead_time_used / 3600.0; initialize(lead_time_used); /* * get path name for map file */ ftime.unix_time = stime->unix_time; uconvert_from_utime(&ftime); sprintf(map_file_dir, "%s%s%.4ld%.2ld%.2ld", Glob->params.map_dir, PATH_DELIM, (long) ftime.year, (long) ftime.month, (long) ftime.day); sprintf(map_file_path, "%s%s%.2ld%.2ld%.2ld.%s", map_file_dir, PATH_DELIM, (long) ftime.hour, (long) ftime.min, (long) ftime.sec, Glob->params.output_file_ext); /* * Check if we should write this file. If not, return -1 */ if (!check_write(map_file_path)) { unlock_files(s_handle, t_handle); return (-1); } sprintf(rdata_file_path, "%s%s%.4ld%.2ld%.2ld%s%.2ld%.2ld%.2ld.%s", Glob->params.rdata_dir, PATH_DELIM, (long) stime->year, (long) stime->month, (long) stime->day, PATH_DELIM, (long) stime->hour, (long) stime->min, (long) stime->sec, Glob->params.output_file_ext); if (Glob->params.debug >= DEBUG_NORM) { fprintf(stderr, "\nScan %ld, time %s\n", (long) scan_num, utimestr(stime)); fprintf(stderr, "Rdata path: %s\n", rdata_file_path); fprintf(stderr, "Map path: %s\n", map_file_path); fprintf(stderr, "lead_time_requested: %g\n", lead_time_requested); fprintf(stderr, "lead_time_used: %g\n", lead_time_used); } /* if (Glob->params.debug >= DEBUG_NORM) */ /* * read in radar volume */ v_handle->vol_file_path = rdata_file_path; if (RfReadVolume(v_handle, "forecast_generate")) { fprintf(stderr, "ERROR - %s:forecast_generate\n", Glob->prog_name); fprintf(stderr, "Could not read in Mdv radar volume\n"); perror(rdata_file_path); unlock_files(s_handle, t_handle); return (-1); } /* * get ZR params */ if (Glob->params.get_zr_from_file) { if (RfGetZrClosest(Glob->params.zr_dir, stime->unix_time, 7200, &coeff, &expon)) { coeff = Glob->params.ZR.coeff; expon = Glob->params.ZR.expon; } } else { coeff = Glob->params.ZR.coeff; expon = Glob->params.ZR.expon; } dbz_fparams = v_handle->field_params[Glob->params.dbz_field]; compute_precip_lookup(precip_lookup, dbz_fparams, coeff, expon); /* * check geometry */ cart = &v_handle->vol_params->cart; if (check_cart_geom(cart, &s_handle->scan->grid)) { fprintf(stderr, "ERROR - %s:forecast_generate\n", Glob->prog_name); fprintf(stderr, "Radar and storm file cart geometry does not match\n"); unlock_files(s_handle, t_handle); return (-1); } cart_params_to_mdv_grid(cart, &grid, s_handle->scan->grid.proj_type); /* * read in storm file scan */ if (RfReadStormScan(s_handle, scan_num, "forecast_generate")) { unlock_files(s_handle, t_handle); return (-1); } init_map_index(map_v_handle, v_handle, &ftime); /* * compute forecast */ if (Glob->params.thresholded_forecast) { forecast = thresholded_forecast(s_handle, t_handle, v_handle, map_v_handle, &grid, scan_num, precip_lookup); } else { /* if (Glob->params.thresholded_forecast) */ forecast = unthresholded_forecast(s_handle, t_handle, v_handle, map_v_handle, &grid, scan_num, precip_lookup); } /* if (Glob->params.thresholded_forecast) */ if (forecast != NULL) { map_v_handle->field_plane[0][0] = forecast; /* * write the map file */ sprintf(map_v_handle->vol_params->note, "%s\n%s%g\n%s%g\n%s%g\n", "Precipitation forecast", "Refl threshold: ", s_handle->header->params.low_dbz_threshold, "Z-R coeff : ", coeff, "Z-R expon : ", expon); sprintf(map_v_handle->field_params[0]->name, "%g hr forecast ahead of time", lead_time_hr); RfWriteDobson(map_v_handle, FALSE, Glob->params.debug, Glob->params.map_dir, Glob->params.output_file_ext, Glob->prog_name, "forecast_generate"); } unlock_files(s_handle, t_handle); return (ftime.unix_time); }
void read_radar(void) { int iret; int radar_fd = -1; int forever = TRUE; bprp_response_t response; while (forever) { PMU_auto_register("In read_data() - top"); if (radar_fd < 0) { /* * open connection to radar - we are the client in this */ if ((radar_fd = SKU_open_client(Glob->params.input_host, Glob->params.input_port)) < 0) { PMU_auto_register("In read_data() - waiting for connection"); /* * failure - sleep and retry later */ sleep(1); } else { if (Glob->params.debug) { fprintf(stderr, "%s: connected to radar\n", Glob->prog_name); } } } else { /* * wait on packet, timing out after 10 secs each time */ while ((iret = SKU_read_select(radar_fd, 10000)) == -1) { /* * timeout */ PMU_auto_register("In read_data() - waiting for a beam"); } if (iret == 1) { /* * read a beam and process */ if (SKU_read(radar_fd, &response, sizeof(bprp_response_t), 20) == sizeof(bprp_response_t)) { /* * success */ handle_response(&response); } else { /* * read error - disconnect and try again later */ if (Glob->params.debug) { fprintf(stderr, "%s: read error - closing connection to radar\n", Glob->prog_name); } SKU_close(radar_fd); radar_fd = -1; } /* SKU_read */ } else { /* * read error - disconnect and try again later */ if (Glob->params.debug) { fprintf(stderr, "%s: read error - closing connection to radar\n", Glob->prog_name); } SKU_close(radar_fd); radar_fd = -1; } /* if (iret == 1) */ } /* if (radar_fd < 0) */ } /* while (forever) */ return; }
void updateRegistration() { // Make sure we remain registered with PMU, so it knows we're alive PMU_auto_register("running"); }
int main(int argc, char *argv[]) { // Let logx get and strip out its arguments logx::ParseLogArgs(argc, argv); // Instantiate our QCoreApplication App = new QCoreApplication(argc, argv); // XML-RPC server port number static const int DEFAULT_XMLRPC_PORT = HCREXECUTIVE_PORT; int xmlrpcPortNum; // procmap instance name std::string instanceName; // Get HcrExecutive's options po::options_description opts("Options"); opts.add_options() ("help,h", "Describe options") ("instance,i", po::value<std::string>(&instanceName)->default_value("ops"), "instance name for procmap connection") ("xmlrpcPortNum,x", po::value<int>(&xmlrpcPortNum)->default_value(DEFAULT_XMLRPC_PORT), "XML-RPC server port number") ; bool argError = false; po::variables_map vm; try { po::store(po::command_line_parser(argc, argv).options(opts).run(), vm); po::notify(vm); } catch (...) { argError = true; } // Give usage information and exit if 1) help was requested, or 2) there // is an argument error if (vm.count("help") || argError) { std::cout << "Usage: " << argv[0] << " [OPTION]..." << std::endl; std::cout << opts << std::endl; exit(1); } // Initialize registration with procmap if instance is specified if (instanceName.size() > 0) { PMU_auto_init("HcrExecutive", instanceName.c_str(), PROCMAP_REGISTER_INTERVAL); ILOG << "Initializing procmap registration as instance '" << instanceName << "'"; } ILOG << "HcrExecutive (" << getpid() << ") started"; PMU_auto_register("initializing"); // Initialize our RPC server xmlrpc_c::registry myRegistry; myRegistry.addMethod("getStatus", new GetStatusMethod); myRegistry.addMethod("setApsValveControl", new SetApsValveControlMethod); myRegistry.addMethod("setRequestedHmcMode", new SetRequestedHmcModeMethod); myRegistry.addMethod("setHvRequested", new SetHvRequestedMethod); QXmlRpcServerAbyss rpcServer(&myRegistry, xmlrpcPortNum); // Start a thread to get HcrPmc730Daemon status on a regular basis. HcrPmc730StatusThread hcrPmc730StatusThread("localhost", HCRPMC730DAEMON_PORT); QObject::connect(App, SIGNAL(aboutToQuit()), &hcrPmc730StatusThread, SLOT(quit())); hcrPmc730StatusThread.start(); // Start a thread to get MotionControlDaemon status on a regular basis MotionControlStatusThread mcStatusThread("localhost", MOTIONCONTROLDAEMON_PORT); QObject::connect(App, SIGNAL(aboutToQuit()), &mcStatusThread, SLOT(quit())); mcStatusThread.start(); // MaxPowerFmqClient instance MaxPowerFmqClient maxPowerClient("fmqp://localhost/tmp/fmq/max_power/wband/shmem_15000"); QObject::connect(App, SIGNAL(aboutToQuit()), &maxPowerClient, SLOT(quit())); // Instantiate the object which will monitor pressure and control the // Active Pressurization System (APS) TheApsControl = new ApsControl(hcrPmc730StatusThread); // Instantiate the object which will implement safety monitoring for the // transmitter TheTransmitControl = new TransmitControl(hcrPmc730StatusThread, mcStatusThread, maxPowerClient); // catch a control-C or kill to shut down cleanly signal(SIGINT, sigHandler); signal(SIGTERM, sigHandler); // Set up a timer to periodically register with PMU so it knows we're still // alive. QTimer registrationTimer; registrationTimer.setInterval(REGISTRATION_INTERVAL_SECS * 1000); // interval in ms QFunctionWrapper registrationFuncWrapper(updateRegistration); QObject::connect(®istrationTimer, SIGNAL(timeout()), ®istrationFuncWrapper, SLOT(callFunction())); registrationTimer.start(); // Set up a timer to log status information occasionally QTimer statusTimer; statusTimer.setInterval(LOG_STATUS_INTERVAL_SECS * 1000); QFunctionWrapper statusFuncWrapper(logStatus); QObject::connect(&statusTimer, SIGNAL(timeout()), &statusFuncWrapper, SLOT(callFunction())); statusTimer.start(); // Now just run the application until somebody or something interrupts it try { App->exec(); } catch (std::exception & e) { ELOG << "Application stopped on exception: " << e.what(); } catch (...) { ELOG << "Application stopped on unknown exception"; } // Unregister with procmap PMU_auto_unregister(); // Clean up before exit delete(TheTransmitControl); delete(TheApsControl); // Wait up to 1/2 second (max) for all of our threads to finish int maxWaitMsecs = 500; QDateTime giveUpTime(QDateTime::currentDateTime().addMSecs(maxWaitMsecs)); if (! mcStatusThread.wait(QDateTime::currentDateTime().msecsTo(giveUpTime))) { WLOG << "mcStatusThread is still running at exit"; } if (! hcrPmc730StatusThread.wait(QDateTime::currentDateTime().msecsTo(giveUpTime))) { WLOG << "hcrPmc730StatusThread is still running at exit"; } ILOG << "HcrExecutive (" << getpid() << ") exiting"; return 0; }
void tkcomp(dimension_data_t *dim_data, float ***az, float ***el, int nel, int **el_time, int k1, int k2, int **num_az, double vel_scale, double vel_bias, double dbz_scale, double dbz_bias, float *u, float *v, float *x, float *y, float *z, float *dop, int max_vec, int *num_vec, float *rng, int num_gates, int *iaz1, int *iaz2, int date, ui08 ****vel, ui08 ****dbz, ui08 ***flg) { static int first_call = TRUE; static long **base; static float **store_cor; static ui08 *numnoi; long d,idbz_max; long sum1,sum2,sumsq1,sumsq2,sum12,sumv,sumsqv; float uu,vv,azsum,avgangl,delaz,cosel1,cosel2,deltaa,ritera,tkmove; float radsq,ref; float s1,s2,s12,ssq1,ssq2; float dt,sinel1,sinel2,var,avgd,corcoef,cormax,vrt,vdif; float ridxa,ridxb,a1,riterb,rninv,az2,angl1,x1,x2,z1,r1,y1,y2,rg; float dtsecx,dtsecy,a2,sinang,cosang,angl2; float vrad,xpa,xpb,xnd,ynd,sumc,avgu,avgv,avgcor,deltab; float avg_sumv,avg_sqv,numpts,fraction,vtan,numst,fractst; float nvpts,half_npts; int i,j,k,iel,idista,isiza,ioffa,lasta,nvec; int idxa,idxb,ia1,ia2,ia3,ia4,lastb,ib3,ib4,iib2,iia2,ia,ib; int iamx,ibmx,nc,idistb,isizb,ioffb,iib,iia; int iref,numdif; int ndop,nbin[50],ibin; int vbyte; float percent[50],sump,velmin,velmax,sumdif; FILE *fp,*sp; char output_file_path[MAX_PATH_LEN]; char stats_file_path[MAX_PATH_LEN]; if (Glob->params.debug) { fprintf(stderr, "*** tkcomp ***\n"); } PMU_auto_register("starting tkcomp"); /* * allocate memory */ if (first_call) { base = (long **) umalloc2(200, 200, sizeof(long)); store_cor = (float **) umalloc2(200, 200, sizeof(float)); numnoi = (ui08 *) umalloc(Glob->params.max_naz * sizeof(ui08)); first_call = FALSE; } iaz1[0]=1; sprintf(stats_file_path, "%s%s%s", Glob->params.local_dir, PATH_DELIM, "stats.out"); if ((sp = fopen(stats_file_path,"w")) == NULL) { fprintf(stderr, "ERROR - %s:tkcomp\n", Glob->prog_name); fprintf(stderr, "Unable to create file %s\n", stats_file_path); perror(stats_file_path); tidy_and_exit(-1); } sprintf(output_file_path, "%s%s%s", Glob->params.local_dir, PATH_DELIM, "trec.out"); if ((fp = fopen(output_file_path,"w")) == NULL) { fprintf(stderr, "ERROR - %s:tkcomp\n", Glob->prog_name); fprintf(stderr, "Unable to create file %s\n", output_file_path); perror(output_file_path); tidy_and_exit(-1); } nvec=0; idbz_max = (Glob->params.dbz_max - dbz_bias) / dbz_scale; if (Glob->params.debug) { fprintf(stderr, "idbz= %ld \n",idbz_max); } for(iel=0; iel<=nel; iel++) { PMU_auto_register("tkcomp - computing"); velmin=999.; velmax=-999.; if (Glob->params.debug) { fprintf(fp,"iel,k1,k2= %d %d %d\n",iel,k1,k2); } ndop=0; for(i=0; i<50; i++) { nbin[i]=0; } radius(el_time,iel,k1,k2,&tkmove,&dt); if (Glob->params.debug) { fprintf(stderr, "trecing elevation = %6.1f\n", el[0][iel][k1]); fprintf(stderr, "iel,el_time1,el_time2,dt= %d %d %d %6.1f\n", iel, el_time[iel][k1], el_time[iel][k2], dt); fprintf(fp,"el_time1,el_time2,date,el,tkmove,dt= " "%7d %7d %7d %6.1f %6.1f %6.1f\n", el_time[iel][k1], el_time[iel][k2],date,el[50][iel][k1],tkmove,dt); fprintf(fp,"ngates= %d\n",num_gates); if(iel==0) fprintf(stderr, "num_az,k1= %d %d\n", num_az[iel][k1], k1); } azsum = 0.; for(i=0; i<num_az[iel][k1]-1; i++) { numnoi[i]=0; delaz=fabs(az[i][iel][k1]-az[i+1][iel][k1]); if(delaz > 180.)delaz=360.-delaz; azsum += delaz; if (Glob->params.debug) { fprintf(fp,"i,iel,az,el= %5d %5d %6.1f %6.1f\n", i,iel,az[i][iel][k1], el[i][iel][k1]); } } i=num_az[iel][k1]; numnoi[i]=0; avgangl=azsum/num_az[iel][k1]; if (Glob->params.debug) { fprintf(stderr, "naz,avgangl= %d %6.1f\n", num_az[iel][k1], avgangl); } uu=0.; vv=0.; nc=0; sumdif=0.; sumc=0.; numdif=0; cosel1 = cos(el[0][iel][k1]*PI/180.); cosel2 = cos(el[0][iel][k2]*PI/180.); sinel1 = sin(el[0][iel][k1]*PI/180.); sinel2 = sin(el[0][iel][k2]*PI/180.); deltaa = Glob->params.gate_spacing * cosel1; idista = tkmove / deltaa; isiza = Glob->params.box_size / deltaa + 1.5; ioffa = (isiza-1) * 0.5; ritera = Glob->params.box_spacing / deltaa; if (ritera < 1.) ritera = 1.; lasta=num_gates-idista-isiza+1; ridxa=idista+1.0+5.0/deltaa; idxa=ridxa; while(idxa<lasta) { PMU_auto_register("tkcomp - computing"); r1 = rng[idxa+ioffa]; a1=rng[idxa+ioffa]*cosel2; ia1 = idxa - idista; ia2 = idxa + idista; deltab = 2*a1*sin(avgangl*(0.5*PI/180.)); idistb = tkmove/deltab; isizb = Glob->params.box_size / deltab + 1.5; /* IF(MOD(ISIZB,2).EQ.0)ISIZB=ISIZB+1 */ ioffb = (isizb-1)*0.5; riterb = Glob->params.box_spacing / deltab; if(riterb < 1.0)riterb=1.; lastb = num_az[iel][k2]-idistb-isizb+1; rninv = 1./(isiza*isizb); half_npts=0.5*(float)(isiza*isizb); ridxb = idistb+1.; idxb = ridxb; while(idxb < lastb) { PMU_auto_register("tkcomp - computing"); angl1 = az[idxb+ioffb][iel][k2]*0.0174532; x1 = a1*sin(angl1); y1 = a1*cos(angl1); /* specify r1 and sinel1 */ z1 = r1 * sinel1 + r1 * r1 / 17014. + Glob->params.radar_altitude; if(x1 >= dim_data->min_x && x1 <= dim_data->max_x && y1 >= dim_data->min_y && y1 <= dim_data->max_y) { az2 = az[idxb][iel][k2]; azindx(az2,az,idistb,iel,k1,num_az[iel][k1],&ib3,&ib4,isizb); if(ib3 != -1) { cormax=-999.; numpts = 0.; numst = 0.; for(j=0; j<isizb; j++) { for(k=0; k<isiza; k++) { base[j][k] = dbz[k2][iel][idxb+j][idxa+k]; if(base[j][k] > idbz_max) numpts += 1.0; if(flg[iel][idxb+j][idxa+k] == 2) numst += 1.0; } } ref = dbz_scale * (float) dbz[k2][iel][idxb+ioffb][idxa+ioffa] + dbz_bias; iref = dbz[k2][iel][idxb+ioffb][idxa+ioffa]; fraction = numpts / ((float)(isiza*isizb)); fractst = numst / ((float)(isiza*isizb)); if (fraction <= Glob->params.fract && fractst <= Glob->params.fractst) { rg=sqrt(x1*x1+y1*y1); dtsecx=16.6667*x1/(rg*dt); dtsecy=16.6667*y1/(rg*dt); sum1 = 0; sumsq1 = 0; sumv=0; sumsqv=0; nvpts=0.; for(j=0; j < isizb; j++) { for(i=0; i < isiza; i++) { d=base[j][i]; vbyte = vel[k2][iel][idxb+j][idxa+i]; if(vbyte != 0){ sumv += vbyte; sumsqv += vbyte * vbyte; nvpts +=1.; } sum1 += d; sumsq1 += d*d; } } x[nvec] = x1; y[nvec] = y1; z[nvec] = z1; if(nvpts > half_npts){ avg_sumv = (float)sumv/nvpts; avg_sqv = (float)sumsqv/nvpts; avgd = (float)vel_scale*(sumv/nvpts)+vel_bias; var = (avg_sqv - avg_sumv*avg_sumv)*vel_scale*vel_scale; } else { avgd = -999.; var = -999.; } if (var <= Glob->params.var_thr) { for(ib=ib3; ib<=ib4; ib++) { iib2 = ib + isizb-1; angl2 = az[ib+ioffb][iel][k1]*0.01745329; sinang = sin(angl2); cosang = cos(angl2); for (ia = ia1; ia <= ia2; ia += Glob->params.rng_skip) { a2 = rng[ia+ioffa]*cosel2; iia2 = ia +isiza -1; x2 = a2*sinang; y2 = a2*cosang; radsq=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); if(radsq <= tkmove*tkmove) { vrad = (x2-x1) * dtsecx + (y2-y1) * dtsecy; if ((vrad >= (avgd - Glob->params.vel_rad) && vrad <= (avgd + Glob->params.vel_rad)) || avgd<-900.) { sum2=0; sumsq2 = 0; sum12 = 0; for(iib=ib; iib<=iib2; iib++) { for(iia=ia; iia<=iia2; iia++) { d=dbz[k1][iel][iib][iia]; sum2 += d; sumsq2 += d*d; sum12 += base[iib-ib][iia-ia]*d; } } s12 = (float)sum12; s1 = (float)sum1; s2 = (float)sum2; ssq1 = (float)sumsq1; ssq2 = (float)sumsq2; corcoef = (s12-rninv*s1*s2)/ sqrt((ssq1-rninv*s1*s1)* (ssq2-rninv*s2*s2)); #ifdef NOTNOW store_cor[ia-ia1][ib-ib3] = corcoef; #endif if(corcoef > cormax) { cormax = corcoef; iamx = ia; ibmx = ib; } } /* if(vrad>= */ } /* if((x2-x1)*x2-x1) */ } /* for(ia=ia1 */ } /* for(ib=ib1 */ if (cormax > 0.) { if (Glob->params.rng_skip > 1) { ib3 = ibmx - 1; ib4 = ibmx + 1; ia3 = iamx - Glob->params.rng_skip + 1; ia4 = iamx + Glob->params.rng_skip - 1; for(ib=ib3; ib<=ib4; ib++) { iib2 = ib+isizb-1; angl2 = az[ib+ioffb][iel][k1]; for(ia=ia3; ia<=ia4; ia++) { a2 = rng[ia+ioffa]*cosel2; iia2 = ia+isiza-1; x2 = a2*sin(angl2); y2 = a2*cos(angl2); sum2 = 0; sumsq2 = 0; sum12 = 0; for(iib=ib; iib<=iib2; iib++) { for(iia=ia; iia<=iia2; iia++) { d=dbz[k1][iel][iib][iia]; sum2 += d; sumsq2 += d*d; sum12 += base[iib-ib][iia-ia]*d; } } s12 = (float)sum12; s1 = (float)sum1; s2 = (float)sum2; ssq1 = (float)sumsq1; ssq2 = (float)sumsq2; corcoef = (s12-rninv*s1*s2)/ sqrt((ssq1-rninv*s1*s1)* (ssq2-rninv*s2*s2)); #ifdef NOTNOW store_cor[ia-ia1][ib-ib3] = corcoef; #endif if(corcoef > cormax) { cormax = corcoef; iamx = ia; ibmx = ib; } } /* for(ia=ia3 */ } /* for(ib=ib3 */ } /* if (Glob->params.rng_skip > 1) */ #ifdef NOTNOW vcterp(store_cor,rng,iamx,az,ibmx,ia1,ib3, ioffa,ioffb,iel,k1,k2,&xpa,&xpb); #endif xpa = rng[iamx+ioffa]; xpb = az[ibmx+ioffb][iel][k1]; xnd = xpa*cosel1*sin(xpb*0.0174532); ynd = xpa*cosel1*cos(xpb*0.0174532); u[nvec] = (xnd-x1)/dt*(1000./60.); v[nvec] = (ynd-y1)/dt*(1000./60.); #ifdef NOTNOW if (Glob->params.anal_fun) { amp= 7.*z[nvec]; u[nvec] = amp*cos(2.*PI*x[nvec]/50.); v[nvec] = 0.; } #endif rg = sqrt(x1*x1+y1*y1); vrt = u[nvec]*x1/rg+v[nvec]*y1/rg; vtan= -u[nvec]*y1/rg+v[nvec]*x1/rg; vdif = fabs(vrt-avgd); if(avgd == -999.) vdif=0.; #ifdef NOTNOW u[nvec]=avgd*x1/rg-vtan*y1/rg; v[nvec]=avgd*y1/rg+vtan*x1/rg; vrt = u[nvec]*x1/rg+v[nvec]*y1/rg; vdif = fabs(vrt-avgd); #endif dop[nvec]=avgd; if (cormax < Glob->params.cor_min_thr || cormax > Glob->params.cor_max_thr || vdif > Glob->params.vel_dif_thr) { cormax = -999.; if (Glob->params.debug) { fprintf(fp,"bad vector-x,y,cor,var,vdif,avgd= " "%6.1f %6.1f %5.2f %6.1f %6.1f %6.1f\n", x1,y1,cormax,var,vdif,avgd); } } else { if (Glob->params.debug) { fprintf(fp,"n,x,y,u,v,cor,var,vdif,avgd,ref= " "%6d %6.1f %6.1f %6.1f %6.1f %5.2f " "%6.1f %6.1f %6.1f %6.1f\n", nvec,x1,y1,u[nvec],v[nvec],cormax, var,vdif,avgd,ref); } uu += u[nvec]; vv += v[nvec]; if(avgd > -50. ){ numdif++; sumdif +=avgd; if(avgd > velmax) velmax=avgd; if(avgd < velmin) velmin=avgd; } ibin= (int)vdif; if(avgd != -999.){ nbin[ibin]++; ndop++; } sumc += cormax; nc++; if (nvec < max_vec - 1) { nvec++; } } } /* if(cormax > 0.) */ } /* if (var <= Glob->params.var_thr) */ } /* if(numpts/ */ } /* if (ib3 != -1) */ } /* if (x1 >= dim_data->min_x */ ridxb += riterb; idxb = ridxb; } /* while(idxb<lastb) */ ridxa += ritera; idxa = ridxa; } /* while(idxa<lasta) */ if(nc > 0) { avgu = uu/nc; avgv = vv/nc; avgcor = sumc/nc; for(i=0; i<50; i++){ sump=0.; for(j=i; j<50; j++){ sump = sump + (float)nbin[j]; } percent[i]=100.*sump/(float)ndop; } if (Glob->params.debug) { fprintf(stderr, "percent 0,1,2,3,4,= %6.0f %6.0f %6.0f %6.0f %6.0f\n", percent[0],percent[1],percent[2],percent[3],percent[4]); fprintf(sp,"%7d %7d %7d %6.0f %6.0f %6.0f %6.0f " "%6.0f %6.0f %6.1f %6.1f %6.1f %5d %5.2f %5d %6.1f\n", el_time[iel][k1],el_time[iel][k2],date,percent[0], percent[1],percent[2],percent[3],percent[4], percent[5],velmin,velmax,el[0][iel][k1], nc,avgcor,numdif,sumdif/numdif); fprintf(stderr, "avg u= %6.1f avg v= %6.1f nvec= %5d avg cor= %6.2f\n", avgu,avgv,nc,avgcor); } } iaz2[iel] = iaz1[iel]+nc-1; iaz1[iel+1] = iaz2[iel]+1; } /* for(iel=0; iel<nel; iel++) */ *num_vec=nvec; if (Glob->params.debug) { fprintf(stderr, "total num of vectors= %5d\n",nvec); } fclose(fp); fclose(sp); } /*end routine */
void process_realtime_stream(FILE *tty_file) { int forever = TRUE; char line[BUFSIZ]; char spaced_line[BUFSIZ]; char callsign[8]; int ilat, ilon; int itas, igps_alt, ipres_alt; int itdry, ilwjw, idewpt; int ice_count; int left_burn, right_burn, flare_state, snowmax_state; int igps_var, igps_err, iarnav_warn; date_time_t gps_time, wall_time; time_t last_store = 0; while (forever) { /* * wait for data, registering once per second while waiting */ while (ta_read_select(tty_file, 1000) != 1) { PMU_auto_register("Waiting for data"); } PMU_auto_register("Got data"); if (fgets(line, BUFSIZ, tty_file) == NULL) { fprintf(stderr, "ERROR - %s:process_stream\n", Glob->prog_name); perror(Glob->params.input_device); return; } add_spaces(line, spaced_line); if (Glob->params.debug) { fprintf(stderr, "Input line:\n%s", line); fprintf(stderr, "Spaced line:\n%s\n", spaced_line); } if (sscanf(spaced_line, "%s %d %d %d %d %d %d %d %d %d" "%d %d %d %d %2d %2d %2d %d %d %d", callsign, &ilat, &ilon, &itas, &igps_alt, &ipres_alt, &itdry, &ilwjw, &idewpt, &ice_count, &left_burn, &right_burn, &flare_state, &snowmax_state, &gps_time.hour, &gps_time.min, &gps_time.sec, &igps_var, &igps_err, &iarnav_warn) == 20) { if (Glob->params.debug) { fprintf(stderr, "Decoding : %s", line); fprintf(stderr, "Callsign : %s\n", callsign); fprintf(stderr, "lat : %g\n", ilat / 10000.0); fprintf(stderr, "lon : %g\n", ilon / 10000.0); fprintf(stderr, "tas : %d\n", itas); fprintf(stderr, "gps_alt : %g\n", igps_alt / 100.0); fprintf(stderr, "pres_alt : %g\n", ipres_alt / 100.0); fprintf(stderr, "tdry : %g\n", itdry / 10.0); fprintf(stderr, "lwJW : %g\n", ilwjw / 100.0); fprintf(stderr, "dewpt : %g\n", idewpt / 10.0); fprintf(stderr, "left burn : %d\n", left_burn); fprintf(stderr, "right burn : %d\n", right_burn); fprintf(stderr, "flare state: %d\n", flare_state); fprintf(stderr, "snow state : %d\n", snowmax_state); fprintf(stderr, "time : %.2d:%.2d:%.2d\n", gps_time.hour, gps_time.min, gps_time.sec); fprintf(stderr, "gps var : %d\n", igps_var); fprintf(stderr, "gps err : %d\n", igps_err); fprintf(stderr, "arnav code : %d\n", iarnav_warn); } /* * substitute wall clock time for gps time */ wall_time.unix_time = time(NULL); uconvert_from_utime(&wall_time); if((wall_time.unix_time - last_store) >= Glob->params.data_interval) { store_line(&wall_time, (double) ilat / 10000.0, (double) ilon / 10000.0, (double) ipres_alt / 100.0, callsign, itas, (double) itdry / 10.0, (double) ilwjw / 100.0, (double) idewpt / 10.0, left_burn, right_burn, flare_state, snowmax_state); if (Glob->params.use_spdb) { if (Glob->params.single_database) { store_single_spdb(&wall_time, (double) ilat / 10000.0, (double) ilon / 10000.0, (double) ipres_alt / 100.0, callsign); } else { store_mult_spdb(&wall_time, (double) ilat / 10000.0, (double) ilon / 10000.0, (double) ipres_alt / 100.0, callsign); } } /* if (Glob->params.use_spdb) */ last_store = wall_time.unix_time; } /* if((wall_time.unix_time - last_store) ... */ } else { if (Glob->params.debug) { fprintf(stderr, "WARNING - %s:process_stream\n", Glob->prog_name); fprintf(stderr, "Cannot decode: %s\n", (char *) spaced_line); } } } /* while (forever) */ }
void operate(void) { int forever = TRUE; int protofd, sockfd; vol_file_handle_t v_handle; cdata_comm_t com; /* * initialize volume file handle */ RfInitVolFileHandle(&v_handle, Glob->prog_name, (char *) NULL, (FILE *) NULL); /* * initialize socket connections */ if((protofd = SKU_open_server(Glob->params.port)) < 0) { fprintf(stderr,"ERROR - %s:operate\n", Glob->prog_name); fprintf(stderr,"Couldn't open socket for listening\n"); perror(""); tidy_and_exit(-1); } while (forever) { /* * get next client - will timeout after set period */ while ((sockfd = SKU_get_client_timed(protofd, LISTEN_MSECS)) < 0) { /* * register process */ PMU_auto_register("Timed out in operate while loop"); /* * register server with the server mapper */ SMU_auto_register(); } PMU_force_register("Reading request from socket"); /* * Read in comms */ if(SKU_read((int) sockfd, (char *) &com, (si32) sizeof(com), -1) != sizeof(com)) { fprintf(stderr,"Problems reading Message\n"); } else { BE_to_array_32((ui32 *) &com, (ui32) sizeof(cdata_comm_t)); process_request(&com, sockfd, &v_handle); } close(sockfd); } /* while (forever) */ }
static void process_request(cdata_comm_t *com, int sockfd, vol_file_handle_t *v_handle) { ui08 *data_ptr = NULL; /* pointer to data */ cdata_info_t info; cdata_reply_t reply; Glob->time_last_request = time((time_t *) NULL); Glob->n_data_requests++; if(Glob->params.debug) { fprintf(stderr, "Received request\n"); } PMU_auto_register("Processing request"); /* * if plane_heights are requested, request info as well */ if (com->primary_com & GET_PLANE_HEIGHTS) { com->primary_com |= GET_INFO; } /* * Set default values in reply */ initialize_reply(&reply, com); /* * In the case of a request for new data when no realtime file * is available, change the request to GET_MOST_RECENT */ if (com->primary_com & GET_NEW && !Glob->params.use_realtime_file) { com->primary_com &= ~GET_NEW; com->primary_com |= GET_MOST_RECENT; } /* * read in the data volume */ PMU_auto_register("Reading in data volume"); if (read_volume(v_handle, com)) { /* * read failed */ reply.status |= NO_INFO | NO_DATA; } else if (com->data_field >= v_handle->vol_params->nfields) { /* * field number is out of range */ if (Glob->params.debug) { fprintf(stderr, "Field num %ld exceeds max of %ld\n", (long) com->data_field, (long) v_handle->vol_params->nfields); } reply.status |= NO_INFO | NO_DATA; } else { if (com->primary_com & GET_NEW) { reply.status |= NEW_DATA; } copy_vol_params_to_info(v_handle, &info); copy_grid_params_to_info(v_handle, com, &info); copy_field_params_to_info(v_handle, &info, com->data_field); /* * get the data if requested */ if(com->primary_com & GET_DATA) { data_ptr = (ui08 *) NULL; if(Glob->params.debug) { fprintf(stderr, "getting data\n"); } data_ptr = get_grid_data(com, &reply, &info, v_handle); } } /* if (read_volume(v_handle, com)) */ PMU_auto_register("Sending reply"); send_reply(com, &reply, &info, data_ptr, sockfd, v_handle); if(data_ptr != NULL) { ufree((char *) data_ptr); if (Glob->params.free_volume) RfFreeVolPlanes(v_handle, "process_request"); } }
int NetIORead(ui08 *pBuf, int count) { int chCount, lenmsg, nread, nbyts; PMU_auto_register("Getting network data"); chCount = count; if (count > byteCount) { if (byteCount) { memcpy( pBuf, pExtract, byteCount); pBuf += byteCount; chCount -= byteCount; } pExtract = buffer; for (byteCount = 0; lenmsg = kNetRdSz, byteCount < kNetRdSz; /* NULL increment */) { nread = kNetRdSz; if (nread > (lenmsg - byteCount)) nread = lenmsg - byteCount; do { /* if (Params.debug_level >= DEBUG_EXTRA) * fprintf(stderr, * "Trying to read %d bytes from socket\n", * nread); */ nbyts = SKU_read_timed_hb(gDevPollArray[0].fd, (void *)&buffer[byteCount], nread, READ_RETRIES, WAIT_MSECS, 1, PMU_auto_register); /* if (Params.debug_level >= DEBUG_EXTRA) * fprintf(stderr, * " %d bytes read\n", nbyts); */ } while (nbyts == -1); if (nread > 0 && nbyts <= 0) { /* * Try to reconnect to the socket in case it died */ fprintf(stderr, "*** Error %d returned from SKU_read_timed_hb(), reconnecting...\n", nbyts); SKU_close(gDevPollArray[0].fd); open_net(Ip_address, Port); perror("receive"); return(nbyts); } byteCount += nbyts; } if (chCount > byteCount) { fprintf(stderr, "\nNetIORead Size Exceeded!\n"); return( -1); } } memcpy(pBuf, pExtract, chCount); pExtract += chCount; byteCount -= chCount; return(count); }
int ReadMsg(char *program_name, ui08 *pBuf) { ui32 len; int msgLen; ui08 *pTmp, c1, c2, c3, c4; /***** Initializing I/O Buffers *****/ pTmp = pBuf; c4 = 1; msgLen = 0; FOREVER { /* look for sync pattern for next message */ FOREVER { PMU_auto_register("Looking for sync pattern"); /* shift the bytes read in so far */ c1 = c2; c2 = c3; c3 = c4; if (NetIORead(&c4, 1) != 1) { fprintf(stderr, "\nSync Read returned an Error"); return( -1); } if (!c1 && c2==0xff && !c3 && c4==0xff) break; } c4 = 1; FOREVER { PMU_auto_register("Reading next message from socket"); if (NetIORead((ui08 *)&len, sizeof(len)) != sizeof(len)) { fprintf(stderr, "\nReadMsg Length Read returned an Error"); return( -1); } len = ntohl(len); if (len == 0) return(msgLen); else if (len == -1) { fprintf(stderr, "\n%s: error in data\n", program_name); pTmp = pBuf; msgLen = 0; break; } else if (len & 0xff000000) { fprintf(stderr, "\n%s: out of sync with wsidata\n", program_name); pTmp = pBuf; msgLen = 0; break; } if (NetIORead(pTmp, len) != len) { fprintf(stderr, "\nReadMsg data Read returned an Error"); return( -1); } msgLen += len; pTmp += len; } } }