/****************************************************************************** * * * * ******************************************************************************/ int open_csv_input(const char *fname, const char *timefmt) { FILE *f = NULL; int cols; if ( _n_inf >= MAX_IN_FILES ) { fprintf(stderr, "Too many csv_files open\n"); return -1; } if ( (f = fopen(fname, "r")) == NULL ) { fprintf(stderr, "Cannot find file \"%s\"\n", fname); return -1; } csv_if[_n_inf].f = f; csv_if[_n_inf].header = break_line( read_line(f), &cols ); csv_if[_n_inf].n_cols = cols; csv_if[_n_inf].curLine = malloc(sizeof(AED_REAL)*cols); if (timefmt != NULL) csv_if[_n_inf].tf = decode_time_format(timefmt); else csv_if[_n_inf].tf = NULL; load_csv_line(_n_inf); return _n_inf++; }
/****************************************************************************** * * * * ******************************************************************************/ 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 } } }
/****************************************************************************** * * ******************************************************************************/ 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]; }