Skip to content

Add iotools function to write SAM-formatted CSV files #1371

Open
@williamhobbs

Description

@williamhobbs

Is your feature request related to a problem? Please describe.
Manual formatting is required before writing weather data into a CSV file that NREL SAM can read.

Describe the solution you'd like
An iotools function, e.g., pvlib.iotools.write_sam(), that writes data to a CSV file in SAM's format in a standardized way would be nice.

Describe alternatives you've considered
Manually re-formatting data, adding relevant headers, and writing a CSV. An example of code I've used is below:

## Write SAM Weather file - SURFRAD Goodwin Creek 2019
# note: start with dataframes for data and metadata created with pvlib.iotools.read_surfrad, e.g., `(surfrad_data, surfrad_metadata) = pvlib.iotools.read_surfrad(...)`

sam_data = surfrad_data

# Make sure file starts at midnight local time. Here I use roll (circular shift) as a quick fix
sam_data = sam_data.reindex(index=np.roll(surfrad_data.index,-6*60))    

# metadata
source='SURFRAD Goodwin Creek 2019'
latitude=surfrad_metadata.loc['latitude',0]
longitude=surfrad_metadata.loc['longitude',0]
elevation=surfrad_metadata.loc['elevation',0]
timezone_offset = -6 # standard time in hours ahead of GMT, e.g., -7 for Denver

filename = source + '.csv'

# make a header        
header = '\n'.join(
        [ 'Source,Latitude,Longitude,Time Zone,Elevation',
        source + ',' + str(latitude) + ',' + str(longitude) + ',' + str(timezone_offset) + ',' + str(elevation),
        'year,month,day,hour,minute,ghi,dni,dhi,temperature,wspd',
        ]) + '\n'

# write 1st part of header
with open(filename, 'w') as fp:
    fp.write(header)

# write rest of header with SAM-friendly names and write weather data
columns = ['year','month','day','hour','minute','ghi','dni','dhi','temp_air','wind_speed']
sam_data.to_csv(filename, columns = columns, mode='a', index=False, header=False)

Additional context
There is an open issue in https://github.com/NREL/SAM/, NREL/SAM#866, to update SAM to allow weather variable names used by pvlib. That would simplify things.

There may need to be additional consideration given to weather datasets that:

  • include less than one year, which currently will not work in SAM
  • include a full year, but don't start January 1 at midnight. Automatically "rolling" could fix this
  • include multiple years. The user would either have to pick the year to be written, or multiple weather files would need to be written, each containing one year (preferred).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions