rich-iannone / splitr

Use the HYSPLIT model from inside R and do more with it
Other
141 stars 60 forks source link

dispersion model generates empty output in linux (but works in R interpreter) #78

Closed rmadhok closed 1 year ago

rmadhok commented 1 year ago

I am running many dispersion models in a loop. It works perfectly fine, and gives me the data on particle locations, when I run it from Rstudio. It also works when I load R on linux and test the code in the R interpreter. However, it fails when I submit the R script as a batch job (through slurm). It's weird because the code runs fine, but it generates empty dataframes.

The error says "in system(sys_cmd): error in running command"

Use `spec()` to retrieve the full column specification for this data.
Specify the column types or set `show_col_types = FALSE` to quiet this message.
[1] "ID-51-plant-152-unit-1-date-2015-04-20"
trying URL 'ftp://arlftp.arlhq.noaa.gov/archives/reanalysis/RP201504.gbl'
Content type 'unknown' length 119139360 bytes (113.6 MB)
==================================================
[1] "ID-51-plant-152-unit-1-date-2015-05-20"
trying URL 'ftp://arlftp.arlhq.noaa.gov/archives/reanalysis/RP201505.gbl'
Content type 'unknown' length 123110672 bytes (117.4 MB)
==================================================
[1] "ID-52-plant-153-unit-2-date-2015-04-01"
trying URL 'ftp://arlftp.arlhq.noaa.gov/archives/reanalysis/RP201504.gbl'
Content type 'unknown' length 119139360 bytes (113.6 MB)
==================================================
[1] "ID-52-plant-153-unit-2-date-2015-05-01"
trying URL 'ftp://arlftp.arlhq.noaa.gov/archives/reanalysis/RP201505.gbl'
Content type 'unknown' length 123110672 bytes (117.4 MB)
==================================================
Warning messages:
1: In system(sys_cmd) : error in running command
2: In system(sys_cmd) : error in running command
3: In system(sys_cmd) : error in running command
4: In system(sys_cmd) : error in running command
5: In system(sys_cmd) : error in running command
6: In system(sys_cmd) : error in running command
7: In system(sys_cmd) : error in running command
8: In system(sys_cmd) : error in running command

And my code:

for(i in 1:10){

   path_plant <- file.path(getwd(), paste('plant', plants$sno[i], '_unit', plants$unit[i], sep=''), fsep='/')
   dir.create(path_plant, showWarnings=F)
   setwd(path_plant)

   datelist <- seq(plants$date_comm[i], make_datetime(2017,06,01), by='1 month')

   for(j in 1:length(datelist)){

      path_date <- file.path(path_plant, substring(datelist[j], 1, 10), fsep='/')
      dir.create(path_date, showWarnings=F)
      setwd(path_date)

      print(paste('ID-', j,'/', length(datelist), '-plant-', plants$sno[i], '-unit-', plants$unit[i], '-date-', substr(datelist[j],1,10), sep=''))

      while(TRUE){
         dispersion_model <-
         try(
          create_dispersion_model() %>%
            add_source(
              lat = plants$latitude[i], 
              lon = plants$longitude[i], 
              height = 50,
              rate = 1, 
              pdiam = 1, 
              density = 1, 
              shape_factor = 1,
              release_start = datelist[j], 
              release_end = datelist[j] + hours(1)
              ) %>%
            add_dispersion_params(
              start_time = datelist[j], 
              end_time = datelist[j] + hours(24),
              direction = 'forward', 
              model_height = 900,
              met_type = "reanalysis"
              ) %>%
            run_model()
          )
      if(!is(dispersion_model, 'try-error')) {
        break
      }
    }

   df <- dispersion_model$disp_df

   write_csv(df, paste('model-output-', substr(datelist[j], 1,10), '.csv', sep=''))

   setwd(path_plant)
   }

  setwd('..')
   }

Again, this code works and give me the dispersion_model object with all the data when I run it directly in Rstudio. But the dataframe comes out empty when I submit the script as a job in linux.

Any idea why this might be happening, and how to solve it? Thanks