wzhengui / pylibs

This repository includes functions/classes/scripts for daily routine work. A part of it is about pre/post-processing schism model inputs/outputs
Apache License 2.0
12 stars 6 forks source link

Error: name 'read_schism_hgrid' is not defined #3

Closed Krys1202 closed 2 years ago

Krys1202 commented 2 years ago

Hello! I'm a user of SCHISM and I would like to use this script:


!/usr/bin/env python3

from pylib import *

def write_manning(grd,depths,mvalues,regions=None,rvalues=None,fname='manning.gr3'): ''' write manning coefficient based on depth and specified regions

Input:
    grd:     grid name (*.gr3 or *.npz, where *.npz is python format)
    depths:  two hgrid depth(m) to distinguish river, land and the transition zone
    mvalues: lower and upper limits of manning values

    regions: list of region file names. e.g. regions=('GoME_1.reg','GoME_2.reg')
    rvalues: list of values for each region:  e.g. (0.1,0.5)
'''
#read hgrid
if grd.endswith('.npz'):
    gd=loadz(grd).hgrid
else:
    gd=read_schism_hgrid(grd)

#compute manning coefficients
mval=mvalues[0]+(gd.dp-depths[0])*(mvalues[1]-mvalues[0])/(depths[1]-depths[0])
fpm=mval<mvalues[0]; mval[fpm]=mvalues[0]
fpm=mval>mvalues[1]; mval[fpm]=mvalues[1]

#set values in regions
if regions is not None:
    for m,region in enumerate(regions):
        print('modifying manning in {}'.format(region))
        bp=read_schism_bpfile(region,fmt=1)
        sind=inside_polygon(c_[gd.x,gd.y], bp.x,bp.y)
        fp=sind==1; mval[fp]=rvalues[m]

#save manning.gr3
gd.dp=mval
gd.write_hgrid(fname)

if name=="main":

------------------------------------------------------------------------------

#input
#------------------------------------------------------------------------------
#general parameters
grd='hgrid.gr3'      #grid name (*.gr3 or *.npz, where *.npz is python format)
depths=[-1,-3]       #two hgrid depth(m) to distinguish river, land and the transition zone
mvalues=[0.02,0.05]  #lower and upper limits of manning values

#regions for certain values
regions=('GoME_1.reg','GoME_2.reg')
rvalues=(0.2,0.005)

write_manning(grd,depths,mvalues,regions,rvalues,fname='../manning.gr3')

But when I try to run it I have this message: python3 ./write_manning.py

Traceback (most recent call last): File "./write_manning.py", line 53, in write_manning(grd,depths,mvalues,regions,rvalues,fname='../manning.gr3') File "./write_manning.py", line 21, in write_manning gd=read_schism_hgrid(grd) NameError: name 'read_schism_hgrid' is not defined

Could this be related to a problem with pylib?

Thanks for your help!

wzhengui commented 2 years ago

You need to add the path of pylib into your PYTHONPATH. See https://github.com/wzhengui/pylibs

Best, Zhengui

On Thu, Nov 25, 2021 at 10:15 PM Krys1202 @.***> wrote:

Hello! I'm a user of SCHISM and I would like to use this script:

!/usr/bin/env python3

from pylib import *

def write_manning(grd,depths,mvalues,regions=None,rvalues=None,fname='manning.gr3'): ''' write manning coefficient based on depth and specified regions

Input: grd: grid name (.gr3 or .npz, where *.npz is python format) depths: two hgrid depth(m) to distinguish river, land and the transition zone mvalues: lower and upper limits of manning values

regions: list of region file names. e.g. regions=('GoME_1.reg','GoME_2.reg')
rvalues: list of values for each region:  e.g. (0.1,0.5)

'''

read hgrid

if grd.endswith('.npz'): gd=loadz(grd).hgrid else: gd=read_schism_hgrid(grd)

compute manning coefficients

mval=mvalues[0]+(gd.dp-depths[0])*(mvalues[1]-mvalues[0])/(depths[1]-depths[0]) fpm=mval<mvalues[0]; mval[fpm]=mvalues[0] fpm=mval>mvalues[1]; mval[fpm]=mvalues[1]

set values in regions

if regions is not None: for m,region in enumerate(regions): print('modifying manning in {}'.format(region)) bp=read_schism_bpfile(region,fmt=1) sind=insidepolygon(c[gd.x,gd.y], bp.x,bp.y) fp=sind==1; mval[fp]=rvalues[m]

save manning.gr3

gd.dp=mval gd.write_hgrid(fname)

if name=="main":

------------------------------------------------------------------------------

input

------------------------------------------------------------------------------

general parameters

grd='hgrid.gr3' #grid name (.gr3 or .npz, where *.npz is python format) depths=[-1,-3] #two hgrid depth(m) to distinguish river, land and the transition zone mvalues=[0.02,0.05] #lower and upper limits of manning values

regions for certain values

regions=('GoME_1.reg','GoME_2.reg') rvalues=(0.2,0.005)

write_manning(grd,depths,mvalues,regions,rvalues,fname='../manning.gr3')


But when I try to run it I have this message: python3 ./write_manning.py

Traceback (most recent call last): File "./write_manning.py", line 53, in write_manning(grd,depths,mvalues,regions,rvalues,fname='../manning.gr3') File "./write_manning.py", line 21, in write_manning gd=read_schism_hgrid(grd) NameError: name 'read_schism_hgrid' is not defined

Could this be related to a problem with pylib?

Thanks for your help!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/wzhengui/pylibs/issues/3, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFFROBJPDAFJVPDAZCAKBSDUN33WJANCNFSM5IZXBRNQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Krys1202 commented 2 years ago

Thanks !

Best