scripts/make_csv.py
def make_csv(
    idir: str,
    odir: str
    ) -> None:
    """Process a directory of NetCDF RouteLink files to their CSV equivalents.
    
    Parameters
    ----------
    idir: str
        Input directory containing Routelink files.
    odir: str
        Output directory to save CSV files.
        
    Returns
    -------
    None
    """
    # Get list of files
    file_list = Path(idir).glob("*.nc")

    # Output directory
    odir = Path(odir)
    odir.mkdir(exist_ok=True, parents=True)

    for ifile in file_list:
        # Open dataset
        ds = xr.open_dataset(ifile)

        # Convert to a dataframe
        df = ds.to_dataframe()
        no_gage = b'               '
        df = df[df.gages != no_gage]
        df = df.rename(columns={
            'link': 'nwm_feature_id',
            'gages': 'usgs_site_code',
            'lat': 'latitude',
            'lon': 'longitude'
            })
        df['usgs_site_code'] = df['usgs_site_code'].str.decode("utf-8").str.strip()

        # Extract global attributes
        output = ''
        for key, val in ds.attrs.items():
            output += f"{key}: {val}\n"
        output += message + '\n'

        # Re-comment newlines
        output = '# ' + output.replace('\n', '\n# ') + '\n'

        # Extract data
        output += df.to_csv(index=True)

        # Write data
        stem = ifile.stem
        ofile = odir / f'{stem}.csv'
        with ofile.open("w") as fo:
            fo.write(output)
Process a directory of NetCDF RouteLink files to their CSV equivalents.
    
    Parameters
    ----------
    idir: str
        Input directory containing Routelink files.
    odir: str
        Output directory to save CSV files.
        
    Returns
    -------
    None
    
Process a directory of NetCDF RouteLink files to their CSV equivalents.
Parameters

str
Input directory containing Routelink files.
odir: str
Output directory to save CSV files.

Returns

None
Process a directory of NetCDF RouteLink files to their CSV equivalents . Parameters str Input directory containing Routelink files . odir : str Output directory to save CSV files . Returns None
def make_csv(
    idir: str,
    odir: str
    ) -> None:
    file_list = Path(idir).glob("*.nc")
    odir = Path(odir)
    odir.mkdir(exist_ok=True, parents=True)
    for ifile in file_list:
        ds = xr.open_dataset(ifile)
        df = ds.to_dataframe()
        no_gage = b'               '
        df = df[df.gages != no_gage]
        df = df.rename(columns={
            'link': 'nwm_feature_id',
            'gages': 'usgs_site_code',
            'lat': 'latitude',
            'lon': 'longitude'
            })
        df['usgs_site_code'] = df['usgs_site_code'].str.decode("utf-8").str.strip()
        output = ''
        for key, val in ds.attrs.items():
            output += f"{key}: {val}\n"
        output += message + '\n'
        output = '# ' + output.replace('\n', '\n# ') + '\n'
        output += df.to_csv(index=True)
        stem = ifile.stem
        ofile = odir / f'{stem}.csv'
        with ofile.open("w") as fo:
            fo.write(output)
def make_csv ( idir : str , odir : str ) -> None : file_list = Path ( idir ) . glob ( "*.nc" ) odir = Path ( odir ) odir . mkdir ( exist_ok = True , parents = True ) for ifile in file_list : ds = xr . open_dataset ( ifile ) df = ds . to_dataframe ( ) no_gage = b'               ' df = df [ df . gages != no_gage ] df = df . rename ( columns = { 'link' : 'nwm_feature_id' , 'gages' : 'usgs_site_code' , 'lat' : 'latitude' , 'lon' : 'longitude' } ) df [ 'usgs_site_code' ] = df [ 'usgs_site_code' ] . str . decode ( "utf-8" ) . str . strip ( ) output = '' for key , val in ds . attrs . items ( ) : output += f"{key}: {val}\n" output += message + '\n' output = '# ' + output . replace ( '\n' , '\n# ' ) + '\n' output += df . to_csv ( index = True ) stem = ifile . stem ofile = odir / f'{stem}.csv' with ofile . open ( "w" ) as fo : fo . write ( output )
Process a directory of NetCDF RouteLink files to their CSV equivalents.
Process a directory of NetCDF RouteLink files to their CSV equivalents .
"""Process a directory of NetCDF RouteLink files to their CSV equivalents.
    
    Parameters
    ----------
    idir: str
        Input directory containing Routelink files.
    odir: str
        Output directory to save CSV files.
        
    Returns
    -------
    None
    """ # Get list of files # Output directory # Open dataset # Convert to a dataframe # Extract global attributes # Re-comment newlines # Extract data # Write data
idir
str
odir
str