Exemplo n.º 1
0
nc = netCDF4.Dataset(data_url)
nc.variables.keys()

#I use a mapping of FVCOM variable names to common names so that the class methods can also
#work with SELFE and ADCIRC which have different var names
#This seemed easier than finding them by CF long_names etc
var_map = { 'longitude':'lon', \
            'latitude':'lat', \
            'time':'time', \
            'u_velocity':'u', \
            'v_velocity':'v', \
            'nodes_surrounding_ele':'nv',\
            'eles_surrounding_ele':'nbe',\
          }  

necofs = utools.ugrid(data_url)

print 'Downloading data'
#necofs.get_data(var_map,tindex=[0,1,1]) #First time step only
necofs.get_data(var_map) #All time steps in file

necofs.adjust_time() #GNOME can't handle pre 1980 start dates (in units)

necofs.get_bndry(bndry_file) 
#This file was pre-generated for this grid (somewhat manually as open water/land boundaries
#are not specified in the model output

necofs.atts['nbe']['order'] = 'cw'
#GNOME needs to know whether the elements are ordered clockwise (FVCOM) or counter-clockwise (SELFE)

print 'Writing to GNOME file'
Exemplo n.º 2
0
data_url = 'http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM2_FORECAST.nc'
bndry_file = 'GOM2.bry'

#I use a mapping of FVCOM variable names to common names so that the class methods can also
#work with SELFE and ADCIRC which have different var names
#This seemed easier than finding them by CF long_names etc
var_map = { 'longitude':'lon', \
            'latitude':'lat', \
            'time':'time', \
            'u_velocity':'u', \
            'v_velocity':'v', \
            'nodes_surrounding_ele':'nv',\
            'eles_surrounding_ele':'nbe',\
          }  

necofs = utools.ugrid(data_url)

print 'Downloading data'
#necofs.get_data(var_map,tindex=[0,1,1]) #First time step only
necofs.get_data(var_map) #All time steps in file

necofs.adjust_time() #GNOME can't handle pre 1980 start dates (in units)

necofs.get_bndry(bndry_file) 
#This file was pre-generated for this grid (somewhat manually as open water/land boundaries
#are not specified in the model output

necofs.atts['nbe']['order'] = 'cw'
#GNOME needs to know whether the elements are ordered clockwise (FVCOM) or counter-clockwise (SELFE)

print 'Writing to GNOME file'
Exemplo n.º 3
0
def mul_GNOME_inputs(number,yr,month,day,hr,period):
    
    '''
    Transform multiple SELFE combined binary outputs into GNOME's inputs in NetCDF format;
    number is SELFE number;
    Oil spill starting time: yr,month,day,hr; month is based on 30 days
    Oil spill simulation time period: period (hours)
    
    '''
        
    data_file = os.path.join(base_dir,'169_hvel.nc')
    
    var_map = { 'longitude':'lon', \
                'latitude':'lat', \
                'time':'', \
                'u_velocity':'u', \
                'v_velocity':'v', \
                'nodes_surrounding_ele':'ele',\
                'eles_surrounding_ele':'',\
              }  


    txselfe = utools.ugrid(data_file)

    print 'Downloading data dimensions'

    x = txselfe.Dataset.variables['x'][:]
    y = txselfe.Dataset.variables['y'][:]
    lon = np.ones_like(x); lat = np.ones_like(x)
    for ii in range(len(x)):
        lat[ii], lon[ii] = nctools.utmToLatLng(14,x[ii],y[ii])
    txselfe.data['lon'] = lon
    txselfe.data['lat'] = lat
    txselfe.atts['lon'] = {'long_name': 'longitude'}
    txselfe.atts['lat'] = {'long_name': 'latitude'}
    
    
    # get grid topo variables (nbe, nv)
    print 'Downloading grid topo variables'
    try:
        txselfe.get_grid_topo(var_map)
    except KeyError: #model output on server doesn't have nbe
        txselfe.build_face_face_connectivity()
        
    # GNOME requires boundary info -- this file can be read form data_files directory
    # if saved or generated
    print 'Loading/generating boundary segments'
    bndry_file = os.path.join(base_dir, 'txselfe.bry')
    try:
        txselfe.read_bndry_file(bndry_file)
    except IOError:
        txselfe.write_bndry_file('txselfe',bndry_file)
        txselfe.read_bndry_file(bndry_file)
    txselfe.data['nbe'] = txselfe.data['nbe']
    txselfe.data['nv'] = txselfe.data['nv']
    # GNOME needs to know whether the elements are ordered clockwise (FVCOM) or counter-clockwise (SELFE)
    txselfe.atts['nbe']['order'] = 'ccw'
    
    
    # get the SELFE data
    print 'Loading u/v'

    # specify SELFE output path
    for n in range(number):

        shutil.copy(base_dir+'/txselfe.bry',base_dir+'/'+str(n+1)+'/GNOME')
    
        selfe = pyselfe_v1.Dataset(base_dir+'/'+str(n+1)+'/outputs/169_hvel.64')

        for j in range(period):
    
            model_time = dt.datetime(yr,month+j/720,day+(j/24)%30,hr+j%24,0,0) 
            t_units = 'hours since 2012-01-01 00:00:00'
            txselfe.data['time'] = [date2num(model_time,t_units),]
            txselfe.atts['time'] = {'units':t_units}
        
            num_nodes = len(txselfe.data['lon'])
            txselfe.data['u'] = np.ones([1,num_nodes],)
            txselfe.data['v'] = np.ones([1,num_nodes],)
            txselfe.atts['u'] = {'long_name':'eastward_velocity','units':'m/s'}
            txselfe.atts['v'] = {'long_name':'northward_velocity','units':'m/s'}
    
            [t, t_iter, eta, dp, mdata] = selfe.read_time_series('hvel.64', nfiles=1, sfile=169+j, datadir=base_dir+'/'+str(n+1)+'/outputs/')    
    
            for i in range(num_nodes):
                txselfe.data['u'][0][i]=mdata[0,i,5,0]
                txselfe.data['v'][0][i]=mdata[0,i,5,1]
 
            print 'Writing to GNOME file'
            txselfe.write_unstruc_grid(os.path.join(base_dir, str(n+1)+'/GNOME/'+str(169+j)+'_hvel.nc'))