예제 #1
0
파일: sim-ws.py 프로젝트: physycom/slides
async def grid_get(request: Request, citytag: str = ''):
    client_ip = request.client.host
    log_print(f'Request from {client_ip} for {citytag} grid')

    # init conf
    try:
        cfg_file = os.path.join(os.environ['WORKSPACE'], 'slides', 'vars',
                                'conf', 'conf.json')
        with open(cfg_file) as cin:
            cfg = json.load(cin)
        cw = conf(cfg)

    except Exception as e:
        log_print('conf generation failed : {}'.format(e))
        raise HTTPException(
            status_code=500,
            detail='grid geojson conf init failed : {}'.format(e))

    if citytag not in cw.cparams:
        raise HTTPException(status_code=401,
                            detail='malformed url citytag {} not in {}'.format(
                                citytag, cw.cparams.keys()))

    wdir = cw.wdir
    try:
        os.chdir(wdir)
    except:
        os.mkdir(wdir)
        os.chdir(wdir)

    with open(cw.cparams[citytag]) as tin:
        simconf = json.load(tin)
    confs = json.dumps(simconf)
    with open(wdir + '/wsconf_grid_{}.json'.format(citytag), 'w') as outc:
        json.dump(simconf, outc, indent=2)
    #print(confs, flush=True)

    s = simulation(confs)

    if s.is_valid():
        base = wdir + '/grid_{}'.format(citytag)
        geojf = base + '.geojson'
        if not os.path.exists(geojf):
            s.dump_grid_geojson(geojf)
        with open(geojf) as gin:
            geoj = json.load(gin)
        ret = {'message': 'geojson ok', 'type': 'grid', 'geojson': geoj}
    else:
        raise HTTPException(
            status_code=501,
            detail='grid geojson creation for \'{}\' failed'.format(citytag))

    return ret
예제 #2
0
파일: sim-ws.py 프로젝트: physycom/slides
async def sim_post(body: body_sim, request: Request, citytag: str = 'null'):
    client_ip = request.client.host
    log_print('Request from {} city {}'.format(client_ip, citytag))

    start_date = body.start_date
    stop_date = body.stop_date
    sampling_dt = body.sampling_dt
    log_print('Parameters {} - {} sampling {} city {}'.format(
        start_date, stop_date, sampling_dt, citytag))

    # init conf
    try:
        cfg_file = os.path.join(os.environ['WORKSPACE'], 'slides', 'vars',
                                'conf', 'conf.json')
        with open(cfg_file) as cin:
            cfg = json.load(cin)
        cw = conf(cfg, logger)
    except Exception as e:
        log_print('conf init failed : {}'.format(e))
        raise HTTPException(status_code=500,
                            detail='conf init failed : {}'.format(e))

    # sanity check
    if citytag not in cw.cparams:
        raise HTTPException(
            status_code=400,
            detail='city \'{}\' not available. Current cities : {}'.format(
                citytag, list(cw.cparams.keys())))

    # generate config json
    try:
        simconf = cw.generate(start_date, stop_date, citytag)
    except Exception as e:
        log_print('config generation failed : {}'.format(e))
        raise HTTPException(status_code=500,
                            detail='conf generation failed : {}'.format(e))

    # set up environment and move execution to working dir
    wdir = cw.wdir
    try:
        os.chdir(wdir)
    except:
        os.mkdir(wdir)
        os.chdir(wdir)

    # override sim parameters
    simconf['start_date'] = start_date
    simconf['stop_date'] = stop_date
    simconf['sampling_dt'] = sampling_dt
    simconf['state_basename'] = wdir + '/r_{}'.format(citytag)
    simconf['enable_stats'] = True
    #simconf['explore_node'] = [0]
    confs = json.dumps(simconf)
    with open(wdir + '/wsconf_sim_{}.json'.format(citytag), 'w') as outc:
        json.dump(simconf, outc, indent=2)
    #print(confs, flush=True)

    # run simulation
    s = simulation(confs)
    log_print('sim info : {}'.format(s.sim_info()))
    if s.is_valid():
        tsim = datetime.now()
        s.run()
        log_print('{} simulation done in {}'.format(citytag,
                                                    datetime.now() - tsim))

        pof = s.poly_outfile()
        log_print('Polyline counters output file : {}'.format(pof))
        dfp = pd.read_csv(pof, sep=';')
        pp = {
            t: {i: int(x)
                for i, x in enumerate(v[1:]) if x != 0}
            for t, v in zip(dfp.timestamp, dfp.values)
        }

        ret = {'message': 'simulation OK', 'city': citytag, 'data': pp}
    else:
        raise HTTPException(status_code=501, detail='simulation init failed')
    return ret
예제 #3
0
async def sim_post(body: body_sim, request: Request, citytag: str = 'null', current_user: User = Depends(get_current_active_user)):
  client_ip = request.client.host
  logger.info(f'Request from {client_ip} city {citytag}'.format())

  start_date = body.start_date
  stop_date = body.stop_date
  sampling_dt = body.sampling_dt
  out_type = body.out_type
  logger.info(f'Parameters {start_date} - {stop_date} sampling {sampling_dt} city {citytag} out_type {out_type}')

  sim_id = int(datetime.now().strftime('%y%m%d%H%M%S%f'))
  logger.info(f'Simulation id {sim_id} ')

  # init conf
  try:
    cfg_file = os.path.join(os.environ['WORKSPACE'], 'slides', 'vars', 'conf', 'conf.json')
    with open(cfg_file, encoding="utf8") as cin: cfg = json.load(cin)
    cw = conf(cfg)
  except Exception as e:
    logger.error(f'conf init failed : {e}')
    raise HTTPException(status_code=500, detail='conf init failed : {}'.format(e))

  # sanity check
  if citytag not in cw.cparams:
    raise HTTPException(status_code=400, detail='city \'{}\' not available. Current cities : {}'.format(citytag, list(cw.cparams.keys())))

  # generate config json
  try:
    simconf = cw.generate(start_date, stop_date, citytag)
  except Exception as e:
    logger.error(f'config generation failed : {e}')
    raise HTTPException(status_code=500, detail='conf generation failed : {}'.format(e))

  # set up environment and move execution to working dir
  wdir = cw.wdir
  if not os.path.exists(wdir): os.mkdir(wdir)
  statefiledir = f'{wdir}/statefile'
  if not os.path.exists(statefiledir): os.makedirs(statefiledir)

  try:
    os.chdir(wdir)
  except:
    os.mkdir(wdir)
    os.chdir(wdir)

  # override sim parameters
  simconf['start_date'] = start_date
  simconf['stop_date'] = stop_date
  simconf['sampling_dt'] = sampling_dt
  basename = statefiledir + '/r_{}_{}'.format(citytag, sim_id)
  simconf['state_basename'] = basename
  simconf['enable_stats'] = True

  simconf['enable_netstate'] = True
  simconf['enable_influxgrid'] = True
  simconf['state_grid_cell_m'] = DEFAULT_GRID_SIZE

  if out_type == 'poly':
    simconf['enable_influxgrid'] = False
  elif out_type == 'grid':
    simconf['enable_netstate'] = False

  confs = json.dumps(simconf)
  with open(basename + '_conf.json', 'w') as outc: json.dump(simconf, outc, indent=2)
  #print(confs, flush=True)

  # run simulation
  s = simulation(confs)
  logger.debug(f'sim info :')
  for line in s.sim_info().split('\n'):
    if line != '': logger.debug(line)

  if s.is_valid():
    tsim = datetime.now()
    s.run()
    logger.info(f'{citytag} simulation done in {datetime.now() - tsim}')

    if out_type == 'poly' or out_type == 'both':
      pof = s.poly_outfile()
      logger.info(f'Polyline counters output file : {pof}')
      dfp = pd.read_csv(pof, sep = ';')
      poly_cnt = { t : { i : int(x) for i, x in enumerate(v[1:]) if x != 0 } for t, v in zip(dfp.timestamp, dfp.values)}
    else:
      poly_cnt = None

    if out_type == 'grid' or out_type == 'both':
      pof = s.grid_outfile()
      logger.info(f'Grid counters output file : {pof}')

      dfp = pd.read_csv(pof, sep=" |,|=", usecols=[2,4,5], engine='python')
      dfp.columns = ['id', 'cnt', 'timestamp']
      dfp.timestamp = (dfp.timestamp * 1e-9).astype('int')
      #dfp['datetime'] = pd.to_datetime(dfp.timestamp, unit='s', utc=True).dt.tz_convert('Europe/Rome')
      dfp = dfp[ dfp.cnt != 0 ]
      grid_cnt = {
          int(ts) : { str(gid) : int(val) for gid, val in dft[['id', 'cnt']].astype(int).values }
        for ts, dft in dfp.groupby('timestamp')
      }
    else:
      grid_cnt = None

    ret = response_sim(
      message='simulation OK',
      city=citytag,
      sim_id=sim_id,
      poly_cnt=poly_cnt,
      grid_cnt=grid_cnt,
    )

    if cw.remove_local_output:
      print(basename)
      garbage = glob.glob(f'{basename}*')
      for trash in garbage:
        os.remove(trash)
      #print(garbage)

  else:
    raise HTTPException(status_code=501, detail='simulation init failed')
  return ret