Exemple #1
0
def d3d(tmpdir, dic):
    #initialize a model
    rpath = str(tmpdir) + '/'
    dic.update({'rpath': rpath})  # use tmpdir for running the model
    b = pyPoseidon.model(**dic)

    try:
        b.execute()
        a = pyPoseidon.read_model(rpath + 'd3d_model.json')  # read model
        a.execute()
        return True
    except:
        return False
Exemple #2
0
def to_html(path='./',tag='schism'):

    logger.info('create html file for reporting')
    
    m = pyPoseidon.read_model(path + tag +'_model.json')
    m.get_data(online=True)

    d = m.data.Dataset

    vmin = d.elev.min().compute()
    vmax = d.elev.max().compute()

    x = d.SCHISM_hgrid_node_x.values
    y = d.SCHISM_hgrid_node_y.values
    tri = d.SCHISM_hgrid_face_nodes.values

    nodes = pd.DataFrame({'lon':x,'lat':y})
    tria = pd.DataFrame(tri ,columns=['a','b','c'])

    tria['ap'] = tria.apply(lambda x : nodes.loc[x.a,['lon','lat']].values,axis=1)
    tria['bp'] = tria.apply(lambda x : nodes.loc[x.b,['lon','lat']].values,axis=1)
    tria['cp'] = tria.apply(lambda x : nodes.loc[x.c,['lon','lat']].values,axis=1)

    tria['geometry'] =  tria.apply(
         lambda x : shapely.geometry.Polygon([x.ap,x.bp,x.cp]),axis=1)

   
    colormap = branca.colormap.LinearColormap(['green', 'yellow', 'red'], vmin=vmin.values, vmax=vmax.values)
    colormap.caption = 'Elevation'
      
    # geopandas
    gf_ = gp.GeoDataFrame(tria, crs={'init' :'epsg:4326'})    

    gf_ = gf_.drop(['a','b','c','ap','bp','cp'], axis=1)
    

    
    colormap = branca.colormap.LinearColormap(['green', 'yellow', 'red'], vmin=vmin.values, vmax=vmax.values)
    colormap.caption = 'Elevation'


    # MAXH
    mh = d.elev.max('time').values
        
    tria['value'] = tria.apply(lambda x : np.mean([mh[x.a],mh[x.b],mh[x.c]]),axis=1)

    gf_ = gp.GeoDataFrame(tria, crs={'init' :'epsg:4326'})    

    gf_ = gf_.drop(['a','b','c','ap','bp','cp'], axis=1)


    maxh = folium.GeoJson(
    gf_,
    style_function=lambda x: {
        "fillColor": colormap(x['properties']['value']),
        'weight':0,
        "fillOpacity": 1
    },
    )

    maxh.layer_name = 'Maximum Elevation'

    # GRID

    
    g = folium.GeoJson(
    gf_,
    style_function=lambda x: {
        "fillColor": "transparent",
        "color": "black",
        'weight':.2


        },
    )

    g.layer_name = 'Grid'

    #VALIDATION
    
    tgs = m.data.obs.locations.loc[m.data.obs.locations.Group=='TD UNESCO']
    
    points = tgs[['lat','lon']].values
    
    mlat,mlon = points.mean(axis=0)
    
    tg = m.data.obs.data
    tg.index.levels[1].name = 'date'
    tg.index.levels[0].name = 'location'
    tg = tg.drop(['Level m','Tide m'],axis=1)
    tg.columns=['height']

    toponames = tgs.Name.to_list()
    toponames = [x.strip(' ') for x in toponames]
    
    st = m.data.time_series.to_dataframe()
    st.index.levels[1].name = 'date'
    st.index.levels[0].name = 'location'
    st.columns=['height']
    st.index = st.index.set_levels(m.data.obs.locations.Name.str.strip(' '), level=0)
    st = st.loc[toponames]    

    mc = plugins.MarkerCluster()
    for name,latlon in zip(toponames,points):
        popup = folium.Popup()
        tg_ = tg.loc[name.strip(' ')]
        st_ = st.loc[name.strip(' ')]
        if tg_.shape[0] <= 1:
            tg_ = st_.copy()
            tg_.height = 0
        s = source(tg_,st_)
        gr = graph(s, name)
        vis = gr.to_json()
        folium.VegaLite(vis, height=350, width=750).add_to(popup)
        mc.add_child(folium.Marker(latlon, popup=popup))
    
    mc.layer_name = 'Validation'
    
    #FINAL MAP
    
    Map = folium.Map(location=(mlat, mlon), tiles='OpenStreetMap', date=m.date.__str__(), zoom_start=5)
    
    plugins.Fullscreen(
    position='topleft',
    title='Expand me',
    title_cancel='Exit me',
    force_separate_button=True
    ).add_to(Map)

    Map.add_child(maxh)
    Map.add_child(g)
    Map.add_child(mc)
    Map.add_child(folium.map.LayerControl())
    Map.add_child(colormap)


    Map.save(path + 'report.html')
    
    logger.info('... saved')
    
    return
    
Exemple #3
0
def create_features(path='./', tag='schism'):

    logger.info('create geojson file for elevation')
    m = pyPoseidon.read_model(path + tag + '_model.json')
    m.get_data()

    d = m.data.Dataset

    vmin = d.elev.min().compute()
    vmax = d.elev.max().compute()

    x = d.SCHISM_hgrid_node_x.values
    y = d.SCHISM_hgrid_node_y.values
    tri = d.SCHISM_hgrid_face_nodes.values

    nodes = pd.DataFrame({'lon': x, 'lat': y})
    tria = pd.DataFrame(tri, columns=['a', 'b', 'c'])

    tria['ap'] = tria.apply(lambda x: nodes.loc[x.a, ['lon', 'lat']].values,
                            axis=1)
    tria['bp'] = tria.apply(lambda x: nodes.loc[x.b, ['lon', 'lat']].values,
                            axis=1)
    tria['cp'] = tria.apply(lambda x: nodes.loc[x.c, ['lon', 'lat']].values,
                            axis=1)

    tria['geometry'] = tria.apply(
        lambda x: shapely.geometry.Polygon([x.ap, x.bp, x.cp]), axis=1)

    colormap = branca.colormap.LinearColormap(['green', 'yellow', 'red'],
                                              vmin=vmin.values,
                                              vmax=vmax.values)
    colormap.caption = 'Elevation'

    # geopandas
    gf_ = gp.GeoDataFrame(tria, crs={'init': 'epsg:4326'})

    gf_ = gf_.drop(['a', 'b', 'c', 'ap', 'bp', 'cp'], axis=1)

    ## All frames
    fs = []
    for l in range(d.time.shape[0]):
        fr = d.elev.isel(time=l)
        a = fr.sel(nSCHISM_hgrid_node=tria.a.to_list()).values
        b = fr.sel(nSCHISM_hgrid_node=tria.b.to_list()).values
        c = fr.sel(nSCHISM_hgrid_node=tria.c.to_list()).values
        tria['value'] = np.mean([a, b, c], axis=0)
        tria['color'] = [colormap(x) for x in tria.value.to_list()]
        fs.append(
            create_geojson_features(
                tria, d.time[l].values.astype(str).split('.')[0]))

    tf = [j for i in fs for j in i]

    if not os.path.exists(path + 'server'):
        os.makedirs(path + 'server')

    json.dump(tf, open(path + 'server/anim.json', 'w'))

    gf_.to_file(path + 'server/grid.geojson', driver='GeoJSON')

    logger.info('... saved')

    return