Sono nuovo di JSL ma con l'aiuto di questa community sono riuscito a scrivere un codice per raccogliere dati sulla qualità dell'aria da un URL per un determinato inquinante e periodo di tempo. Tutto funziona bene nel codice tranne quando non ci sono dati nell'URL e quindi il ciclo For si interrompe. C'è un modo per mettere una condizione in modo che il ciclo For ignori la tabella senza dati? Incollo il codice qui sotto. Se specifichi 2021 come 'end_year', il codice funziona correttamente, ma se specifichi 2022 come 'end_year' il codice si interrompe poiché non ci sono ancora dati nella seconda metà dell'anno (riga 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 ) );Potresti saltare gli URL che hanno 0 righe di dati con qualcosa del genere:
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);
);
se vuoi interrompere (interrompere) il ciclo for sulla prima tabella senza dati, puoi usare Break() all'interno del ciclo for:
If(N Rows(dt_temp) == 0,
Close(dt_temp, no save);
break();
);
Grazie molto @jthi per la pronta risposta.