我是 JSL 的新手,但在這個社區的幫助下,我能夠編寫一段代碼來從 URL 收集特定污染物和時間段的空氣質量數據。 除了 URL 中沒有數據然後 For 循環中斷之外,代碼中的一切正常。 有沒有辦法設置條件,以便 For 循環忽略沒有數據的表? 我正在粘貼下面的代碼。 如果您將 2021 年指定為 'end_year',則代碼可以正常工作,但如果您將 2022 年指定為 'end_year',則代碼會中斷,因為下半年還沒有數據(第 35 行)。
Names Default To Here(1);
//************************************INPUT****************************//
parameter= "SO2"; // 1. Pick from "NO2", "OZONE", "PM25HR", "NOX", "NO", "CO", "SO2"//
start_year= 2019; // 2. Specify start year
end_year= 2021; // 3. specify end year
//--------------------------------------------------------------------------------------------------------------
//This URL downloads data from Jan 1st until June 30
dl_url1= "https://www.arb.ca.gov/aqmis2/display.php?sitelist=All&filefmt=csv&fname=" || parameter || "_PICKDATA_^temp_year^-6-30&datafmt=dvde&download=y&first_date=^temp_year^-1-1¶m=" || parameter || "&units=001&year=^temp_year^&county_name=--COUNTY--&basin=SC-South+Coast&latitude=--PART+OF+STATE--&report=PICKDATA&order=basin%2Ccounty_name%2Cs.name&submit=All+Sites&ptype=aqd&std15=&o3switch=new&hours=all&statistic=&qselect=Screened&start_mon=1&start_day=1&mon=6&day=30&rows=18";
tableList = {};
For(i = start_year, i <= end_year, i++,
temp_year= Char(i);
year_url = Eval Insert(dl_url1);
dt_temp = Open(year_url, columns(
New Column( "site", Character, "Nominal" ),
New Column( "date",
Numeric,
"Continuous",
Format( "yyyy-mm-dd", 10 ),
Input Format( "yyyy-mm-dd" )
)));
dt_temp << delete rows( Index( N Rows(dt_temp) - 11, N Rows( dt_temp) ) ); //this code deletes the last 11 rows of data
Insert Into( tableList, dt_temp << get name );
);
//This URL downloads data from July 1st 1st until Dec 31
dl_url2= "https://www.arb.ca.gov/aqmis2/display.php?sitelist=All&filefmt=csv&fname=" || parameter || "_PICKDATA_^temp_year^-12-31&datafmt=dvde&download=y&first_date=^temp_year^-7-1¶m=" || parameter || "&units=001&year=^temp_year^&county_name=--COUNTY--&basin=SC-South+Coast&latitude=--PART+OF+STATE--&report=PICKDATA&order=basin%2Ccounty_name%2Cs.name&submit=All+Sites&ptype=aqd&std15=&o3switch=new&hours=all&statistic=&qselect=Screened&start_mon=7&start_day=1&mon=12&day=31&rows=18";
For(i = start_year, i <= end_year, i++,
temp_year= Char(i);
year_url = Eval Insert(dl_url2);
dt_temp = Open(year_url, columns(
New Column( "site", Character, "Nominal" ),
New Column( "date",
Numeric,
"Continuous",
Format( "yyyy-mm-dd", 10 ),
Input Format( "yyyy-mm-dd" )
)));
dt_temp << delete rows( Index( N Rows(dt_temp) - 11, N Rows( dt_temp) ) );
Insert Into( tableList, dt_temp << get name );
);
//The following code concat all tables
all_dt= new Table("All " || parameter || " Data");
For( i = 1, i <= N Items( tableList ), i++,
Data Table(all_dt) << Concatenate(
Data Table( tableList[i] ),
append to first table(1)
);
Close( Data Table( tableList[i] ), No Save );
);
// Create Time (PST) Column
Data Table(all_dt) << New Column( "Column 4",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Selected
) << Move Selected Columns( {:Column 4}, after( :start_hour ) );
Data Table(all_dt):Column 4 << Input Format( "m/d/y h:m" ) <<
Set Name( "Time (PST)" ) << Format( "m/d/y h:m", 19 ) <<
Set Formula( :date + :start_hour * 3600 );
// Create Month Column
Data Table(all_dt) << New Column( "Month",
Character,
"Nominal",
Formula( Substr( Format( :"Time (PST)"n, "Monddyyyy" ), 1, 3 ) )
) << Move Selected Columns( {:"Month"n}, after( :"Time (PST)"n ) );
// Create Year Column
Data Table(all_dt) << New Column( "Year",
Character,
"Nominal",
Formula( Substr( Format( :"Time (PST)"n, "Monddyyyy" ), 6, 4 ) )
) << Move Selected Columns( {:"Year"n}, after( :"Month"n ) );您可以跳過具有 0 行數據的網址,如下所示:
If(N Rows(dt_temp) > 0,
dt_temp << delete rows(Index(N Rows(dt_temp) - 11, N Rows(dt_temp)));
Insert Into(tableList, dt_temp << get name);
,
Close(dt_temp, no save);
);
如果你想在第一個沒有數據的表上停止(中斷)for 循環,你可以在 for 循環中使用 Break():
If(N Rows(dt_temp) == 0,
Close(dt_temp, no save);
break();
);