Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
williamhobbs opened this issue Jan 7, 2022 · 0 comments · May be fixed by #1556
Open

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

williamhobbs opened this issue Jan 7, 2022 · 0 comments · May be fixed by #1556

Comments

@williamhobbs
Copy link
Contributor

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).
shirubana added a commit to shirubana/pvlib-python that referenced this issue Sep 22, 2022
adds a sam.py, and pytest
@shirubana shirubana linked a pull request Sep 22, 2022 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants