def save_data_to_HELP_format(filename, years, data, city, lat=None): """Formats a time series to the HELP format.""" root, ext = osp.splitext(filename) ext = ext[1:] if ext == 'D4': # precipitation year_format = '{0:>10}' data_format = '{0:>5.1f}' elif ext == 'D7': # air temperature year_format = '{0:>5}' data_format = '{0:>6.1f}' elif ext == 'D13': # global solar radiation year_format = '{0:>5}' data_format = '{0:>6.2f}' if lat is None: raise ValueError("A value must be specified for lat.") else: raise ValueError("%s is not a valid file extension." % ext) # ---- Format Header itype = 3 # Precipitation data for {city} was entered by the user. iunits = 2 # 1 for IP and 2 for SI fcontent = [ ['{0:>2}'.format(itype)], ['{0:>2}'.format(iunits)], ['{0:<40}'.format(city[:40])], ] if ext == 'D13': # Append the latitude if the data are solar radiation. fcontent.append(['{0:>6.2f}'.format(lat)]) else: fcontent.append([]) # ---- Format Data for year in np.unique(years): # Selects the data and asserts that the data are complete for # that year : indexes = np.where(years == year)[0] days_in_year = 366 if calendar.isleap(year) else 365 assert len(indexes) == days_in_year # Adds zeros to complete de last row and reshape the data # in a 37 x 10 grid: year_data = data[indexes] year_data = np.hstack([year_data, np.zeros(10 - len(year_data) % 10)]) year_data = year_data.reshape(37, 10).tolist() # Save the data in a format compatible with HELP : for line_data in year_data: formated_line = year_format.format(year) for i in range(10): formated_line += data_format.format(line_data[i]) fcontent.append([formated_line]) save_content_to_csv(filename, fcontent)
def generate_input_from_MDELCC_grid(self, outdir, lat_dd, lon_dd, year_range): """ Generate input data files from the MDDELCC grid. Generate PyHelp csv data file inputs for daily precipitation and average air temperature using data from the MDDELCC spatially distributed daily precipitation and minimum and maximum air temperature grid for a set of lat/lon coordinates. """ if not osp.exists(outdir): os.makedirs(outdir) lat_idx, lon_idx = self.get_idx_from_latlon(lat_dd, lon_dd, unique=True) lat_dd = [self.lat[i] for i in lat_idx] lon_dd = [self.lon[i] for i in lon_idx] # Fetch the daily weather data from the netCDF files. years = range(year_range[0], year_range[1] + 1) tasavg, precip, years = self.get_data_from_idx(lat_idx, lon_idx, years) # Create an array of datestring and lat/lon Ndt, Ndset = np.shape(tasavg) start = datetime.datetime(years[0], 1, 1) datetimes = [start + datetime.timedelta(days=i) for i in range(Ndt)] datestrings = [dt.strftime("%d/%m/%Y") for dt in datetimes] # Fill -999 with 0 in daily precip. precip[:, :][precip[:, :] == -999] = 0 # Fill -999 with linear interpolation in daily air temp. time_ = np.arange(Ndt) for i in range(Ndset): indx = np.where(tasavg[:, i] != -999)[0] tasavg[:, i] = np.interp(time_, time_[indx], tasavg[:, i][indx]) # Convert and save the weather data to PyHelp csv input files. for var in ['precip', 'airtemp']: if var == 'precip': varname = 'Precipitation in mm' data = nan_as_text_tolist(precip) elif var == 'airtemp': varname = 'Average daily air temperature in \u00B0C' data = nan_as_text_tolist(tasavg) fname = osp.join(outdir, var + '_input_data.csv') print('Saving {} data to {}...'.format(var, fname), end=' ') fheader = [[varname], ['', ''], ['Created by ' + __namever__], ['Created on ' + strftime("%d/%m/%Y")], ['Created from MDDELCC grid'], ['', ''], ['Latitude (dd)'] + lat_dd, ['Longitude (dd)'] + lon_dd, ['', '']] fdata = [[datestrings[i]] + data[i] for i in range(Ndt)] fcontent = fheader + fdata save_content_to_csv(fname, fcontent) print('done')
def save_solrad_to_HELP(filename, years, precip, city, lat): """ Formats and saves a daily global solar radiation time series in MJ/m2/day to the HELP format. """ root, ext = osp.splitext(filename) filename = filename if ext == '.D13' else filename + '.D13' fheader = format_weather_header_for_HELP(3, 2, city, lat) fdata = format_timeseries_for_HELP(years, precip, '{0:>5}', '{0:>6.2f}') save_content_to_csv(filename, fheader + fdata)
def save_airtemp_to_HELP(filename, years, precip, city): """ Formats and saves a daily average air temperature time series in Celcius to the HELP format. """ root, ext = osp.splitext(filename) filename = filename if ext == '.D7' else filename + '.D7' fheader = format_weather_header_for_HELP(3, 2, city) fdata = format_timeseries_for_HELP(years, precip, '{0:>5}', '{0:>6.1f}') save_content_to_csv(filename, fheader + fdata)
def save_precip_to_HELP(filename, years, precip, city): """ Formats and saves a daily precipitation time series in mm to the HELP format. """ root, ext = osp.splitext(filename) filename = filename if ext == '.D4' else filename + '.D4' fheader = format_weather_header_for_HELP(3, 2, city) fdata = format_timeseries_for_HELP(years, precip, '{0:>10}', '{0:>5.1f}') save_content_to_csv(filename, fheader + fdata)
def generate_input_from_cweeds(outdir, cweed2_paths, cweed3_paths, year_range): """Generate an input PyHelp data file from CWEED files.""" if not isinstance(cweed2_paths, (list, tuple)): cweed2_paths = [cweed2_paths] if not isinstance(cweed3_paths, (list, tuple)): cweed3_paths = [cweed3_paths] print('Reading CWEEDS files...', end=' ') lat_dd = [] lon_dd = [] stations = [] data = [] for cweed2, cweed3 in zip(cweed2_paths, cweed3_paths): daily_wy2 = read_cweeds_file(cweed2, format_to_daily=True) daily_wy3 = read_cweeds_file(cweed3, format_to_daily=True) wy23_df = join_daily_cweeds_wy2_and_wy3(daily_wy2, daily_wy3) lat_dd.append(wy23_df['Latitude']) lon_dd.append(wy23_df['Longitude']) stations.append(wy23_df['Location']) indexes = np.where((wy23_df['Years'] >= year_range[0]) & (wy23_df['Years'] <= year_range[1]))[0] data.append(wy23_df['Irradiance'][indexes]) data = nan_as_text_tolist(np.array(data).astype(float).transpose()) print('done') fname = osp.join(outdir, 'solrad_input_data.csv') print('Saving {} data to {}...'.format('solrad', fname), end=' ') # Create an array of datestring and lat/lon Ndt = len(wy23_df['Years'][indexes]) start = datetime.datetime(year_range[0], 1, 1) datetimes = [start + datetime.timedelta(days=i) for i in range(Ndt)] datestrings = [dt.strftime("%d/%m/%Y") for dt in datetimes] # Save the data to file. fheader = [['Global solar irradiance in MJ/m²'], ['', ''], ['Created by ' + __namever__], ['Created on ' + strftime("%d/%m/%Y")], ['Created from CWEED files'], ['', ''], ['Stations'] + stations, ['Latitude (dd)'] + lat_dd, ['Longitude (dd)'] + lon_dd, ['', '']] fdata = [[datestrings[i]] + data[i] for i in range(Ndt)] fcontent = fheader + fdata save_content_to_csv(fname, fcontent) print('done')