void Input::redintegrate_ad_terminum() { if(EffectiveDateToday.value() && !global_settings::instance().regression_testing()) { EffectiveDate = calendar_date(); } }
/****************************************************************************** * * * * ******************************************************************************/ void find_day(int csv, int time_idx, int jday) { int y,m,d; AED_REAL tr; if ( !check_it(csv, time_idx) ) { fprintf(stderr, "Fatal error in find_day: file %d index %d\n", csv, time_idx); #if DEBUG CRASH("find_day"); #else exit(1); #endif } while( (tr = get_csv_val_r(csv, time_idx)) < jday) { if ( !load_csv_line(csv) ) { calendar_date(jday, &y, &m, &d); fprintf(stderr,"Day %d (%d-%02d-%02d) not found\n", jday, y, m, d); #if DEBUG CRASH("find_day"); #else exit(1); #endif } } }
std::string Authenticity::Assay (calendar_date const& candidate ,fs::path const& data_path ) { // The cached date is valid unless it's the peremptorily-invalid // default value of JDN zero. if ( calendar_date(jdn_t(0)) != Instance().CachedDate_ && candidate == Instance().CachedDate_ ) { return "cached"; } ResetCache(); std::ostringstream oss; // Read the passkey and valid-date-range files each time // because they might change while the program is running. // They'll be validated against validated md5sums a fraction // of a second later, to guard against fraudulent manipulation. // Read saved passkey from file. std::string passkey; { fs::path passkey_path(data_path / "passkey"); fs::ifstream is(passkey_path); if(!is) { oss << "Unable to read passkey file '" << passkey_path << "'. Try reinstalling." ; return oss.str(); } is >> passkey; if(!is.eof()) { oss << "Error reading passkey file '" << passkey_path << "'. Try reinstalling." ; return oss.str(); } if(passkey.size() != chars_per_formatted_hex_byte * md5len) { oss << "Length of passkey '" << passkey << "' is " << passkey.size() << " but should be " << chars_per_formatted_hex_byte * md5len << ". Try reinstalling." ; return oss.str(); } } // Read valid date range [begin, end) from file. calendar_date begin(last_yyyy_date ()); calendar_date end (gregorian_epoch()); { fs::path expiry_path(data_path / "expiry"); fs::ifstream is(expiry_path); if(!is) { oss << "Unable to read expiry file '" << expiry_path << "'. Try reinstalling." ; return oss.str(); } is >> begin >> end; if(!is || !is.eof()) { oss << "Error reading expiry file '" << expiry_path << "'. Try reinstalling." ; return oss.str(); } } // Make sure candidate date is within valid range. if(candidate < begin) { oss << "Current date " << candidate.str() << " is invalid: this system cannot be used before " << begin.str() << ". Contact the home office." ; return oss.str(); } if(end <= candidate) { oss << "Current date " << candidate.str() << " is invalid: this system cannot be used after " << (-1 + end).str() << ". Contact the home office." ; return oss.str(); } // Validate all data files. fs::path original_path(fs::current_path()); if(0 != chdir(data_path.string().c_str())) { oss << "Unable to change directory to '" << data_path << "'. Try reinstalling." ; return oss.str(); } try { system_command("md5sum --check --status " + std::string(md5sum_file())); } catch(...) { report_exception(); oss << "At least one required file is missing, altered, or invalid." << " Try reinstalling." ; return oss.str(); } if(0 != chdir(original_path.string().c_str())) { oss << "Unable to restore directory to '" << original_path << "'. Try reinstalling." ; return oss.str(); } // The passkey must match the md5 sum of the md5 sum of the file // of md5 sums of secured files. char c_passkey[md5len]; unsigned char u_passkey[md5len]; std::FILE* md5sums_file = std::fopen ((data_path / md5sum_file()).string().c_str() ,"rb" ); md5_stream(md5sums_file, u_passkey); std::fclose(md5sums_file); std::memcpy(c_passkey, u_passkey, md5len); md5_buffer(c_passkey, md5len, u_passkey); std::memcpy(c_passkey, u_passkey, md5len); md5_buffer(c_passkey, md5len, u_passkey); std::string expected = md5_hex_string (std::vector<unsigned char>(u_passkey, u_passkey + md5len) ); if(passkey != expected) { oss << "Passkey is incorrect for this version." << " Contact the home office." ; return oss.str(); } // Cache the validated date. Instance().CachedDate_ = candidate; return "validated"; }
/****************************************************************************** * * ******************************************************************************/ void read_daily_met(int julian, MetDataType *met) { int csv, i, idx, err = 0; AED_REAL now, tomorrow, t_val, sol; now = julian; tomorrow = now + 1.0; loaded_day = now; dbgprt("read_daily_met (SUBDAY_MET) in\n"); csv = metf; find_day(csv, time_idx, julian); for (i = 0; i < n_steps; i++) memset(&submet[i], 0, sizeof(MetDataType)); i = 0; while ( (t_val = get_csv_val_r(csv, time_idx)) < tomorrow) { if ( i >= n_steps ) { int dd,mm,yy; calendar_date(now,&yy,&mm,&dd); fprintf(stderr, "Warning! Too many steps in met for %4d-%02d-%02d\n", yy,mm,dd); break; } idx = floor((t_val-floor(t_val))*24+1.e-8); // add 1.e-8 to compensate for rounding error // fprintf(stderr, "Read met for %16.8f ; %15.12f (%2d)\n", t_val, (t_val-floor(t_val))*24., idx); if ( idx != i ) { if ( !err ) { int dd,mm,yy; calendar_date(now,&yy,&mm,&dd); fprintf(stderr, "Possible sequence issue in met for day %4d-%02d-%02d\n", yy,mm,dd); } idx = i; err = 1; } if (idx >= n_steps) { int dd,mm,yy; calendar_date(now,&yy,&mm,&dd); fprintf(stderr, "Step error for %4d-%02d-%02d!\n", yy,mm,dd); break; } // Rain is the exception - goes as is submet[idx].Rain = get_csv_val_r(csv, rain_idx) * rain_factor; submet[idx].RelHum = get_csv_val_r(csv, hum_idx) * rh_factor; if ( submet[idx].RelHum > 100. ) submet[idx].RelHum = 100.; if ( lwav_idx != -1 ) submet[idx].LongWave = get_csv_val_r(csv, lwav_idx) * lw_factor; else submet[idx].LongWave = 0.; if ( sw_idx != -1 ) submet[idx].ShortWave = get_csv_val_r(csv, sw_idx) * sw_factor; else submet[idx].ShortWave = 0.; switch ( rad_mode ) { case 0 : // use the value already read. case 1 : case 2 : break; case 3 : case 4 : case 5 : sol = calc_bird(Longitude, Latitude, julian, idx*3600, timezone_m); if ( rad_mode == 4 ) sol = clouded_bird(sol, submet[idx].LongWave); if ( rad_mode == 3 ) submet[idx].LongWave = cloud_from_bird(sol, submet[idx].ShortWave); submet[idx].ShortWave = sol; break; } submet[idx].AirTemp = get_csv_val_r(csv, atmp_idx) * at_factor; submet[idx].WindSpeed = get_csv_val_r(csv, wind_idx) * wind_factor; // Read in rel humidity into svd (%), and convert to satvap submet[idx].SatVapDef = (submet[idx].RelHum/100.) * saturated_vapour(submet[idx].AirTemp); if ( have_snow ) submet[idx].Snow = get_csv_val_r(csv,snow_idx); else submet[idx].Snow = 0. ; if ( have_rain_conc ) { submet[idx].RainConcPO4 = get_csv_val_r(csv, rpo4_idx); submet[idx].RainConcTp = get_csv_val_r(csv, rtp_idx); submet[idx].RainConcNO3 = get_csv_val_r(csv, rno3_idx); submet[idx].RainConcNH4 = get_csv_val_r(csv, rnh4_idx); submet[idx].RainConcTn = get_csv_val_r(csv, rtn_idx); submet[idx].RainConcSi = get_csv_val_r(csv, rsi_idx); } else { submet[idx].RainConcPO4 = 0.; submet[idx].RainConcTp = 0.; submet[idx].RainConcNO3 = 0.; submet[idx].RainConcNH4 = 0.; submet[idx].RainConcTn = 0.; submet[idx].RainConcSi = 0.; } i++; if (!load_csv_line(csv) ) break; } *met = submet[0]; }