Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: How to download met data for use in back trajectories #367

Open
Anuragsahucuraj opened this issue Sep 8, 2023 · 4 comments
Open
Labels
hysplit 🌐 HYSPLIT trajectories & associated openair functions question Questions about function use or interpretation

Comments

@Anuragsahucuraj
Copy link

Anuragsahucuraj commented Sep 8, 2023

library(openair)

setwd('C:/Users/Anurag Sahu/Downloads/NP') 

getMet <- function(year = 2018:2023, month = 1:12, path_met = 'TrajData/') 
{
  for (i in seq_along(year)) 
    {
    for (j in seq_along(month)) 
      {
      download.file(url=paste0('ftp://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP',
                               year[i],sprintf('%02d',month[j]),'.gbl'),
                    destfile=paste0(path_met,'RP',year[i],sprintf('%02d',month[j]),'.gbl'),
                    mode='wb')
    }
  }
}

getMet(year=2018,month=2:12,path_met='TrajData/')

I am trying to download the back trajectory data but its not make 23mb the .gbl file which is not completed

Error:trying URL 'http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP201802.gbl'
Error in download.file(url = paste0("http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP",  : 
  cannot open URL 'http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP201802.gbl'
In addition: Warning message:
In download.file(url = paste0("http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP",  :
  URL 'http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP201802.gbl': Timeout of 60 seconds was reached 
@Anuragsahucuraj Anuragsahucuraj added the question Questions about function use or interpretation label Sep 8, 2023
@jack-davison jack-davison changed the title [Question]: [Question]: How to download met data for use in back trajectories Sep 18, 2023
@jack-davison
Copy link
Collaborator

I can't seem to recreate your issue on my end, everything seems to install successfully.

Perhaps there was a temporary issue with NOAA, is it still failing now?

getMet <- function(year = 2018:2023, month = 1:12, path_met = 'TrajData/') 
{
  for (i in seq_along(year)) 
  {
    for (j in seq_along(month)) 
    {
      download.file(url=paste0('ftp://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP',
                               year[i],sprintf('%02d',month[j]),'.gbl'),
                    destfile=paste0(path_met,'RP',year[i],sprintf('%02d',month[j]),'.gbl'),
                    mode='wb')
    }
  }
}

getMet(year = 2018,
       month = 2:12,
       path_met = 'temp/')

fs::dir_ls("temp")
#> temp/RP201802.gbl temp/RP201803.gbl temp/RP201804.gbl temp/RP201805.gbl 
#> temp/RP201806.gbl temp/RP201807.gbl temp/RP201808.gbl temp/RP201809.gbl 
#> temp/RP201810.gbl temp/RP201811.gbl temp/RP201812.gbl

Created on 2023-09-18 with reprex v2.0.2

@jack-davison jack-davison added the hysplit 🌐 HYSPLIT trajectories & associated openair functions label Sep 18, 2023
@Anuragsahucuraj
Copy link
Author

Anuragsahucuraj commented Sep 18, 2023

Yes,
I try your code also i am getting same error "Timeout of 60 seconds was reached "
In my TrajData folder some file are start downloading
but its only 28 mb
#> TrajData/RP201801.gbl
#> TrajData/RP201802.gbl
after 60 sec its showing same error
Error:trying URL 'http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP201802.gbl'
Error in download.file(url = paste0("http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP", :
cannot open URL 'http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP201802.gbl'
In addition: Warning message:
In download.file(url = paste0("http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP", :
URL 'http://arlftp.arlhq.noaa.gov/pub/archives/reanalysis/RP201802.gbl': Timeout of 60 seconds was reached

Error

@jack-davison
Copy link
Collaborator

Ah - if your WiFi is poor, or if the NOAA servers are being slow, it is likely that R's in-built "timeout" time is being reached.

You can increase it as in this SO thread: https://stackoverflow.com/questions/35282928/how-do-i-set-a-timeout-for-utilsdownload-file-in-r

getOption('timeout')
# [1] 60

options(timeout=100)

@Anuragsahucuraj
Copy link
Author

Anuragsahucuraj commented Sep 18, 2023

Ok i downloaded this file from another sources but not when i am trying to make the .gbl file in .Rdate using this code

Code
add.met <- function(month,Year,met,bat.file)
{

if (month==0)
{
month<- 12
Year<- as.numeric(Year) -1
}
if(month <11) month <- paste('0',month,sep='')

Add first line

write.table(paste('echo',met,' >>CONTROL'),
bat.file,col.names=FALSE,
row.names=FALSE,quote=FALSE,append=TRUE)
x <- paste('echo RP',Year,month,'.gbl >>CONTROL',sep='')
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)
}

procTraj <- function(lat=25.8154,lon=79.9187, year= 2021,name='IGP_CARE',
met='./TrajData/',out='./TrajProc/',hours=120,height=500,
hy.path='/hysplit/')
{

lapply(c('openair','plyr','reshape2'),require,character.only=TRUE)

setwd(paste0(hy.path,'/working/'))

Remove existing 'tdump' files//

path.files <- paste0(hy.path,'/working/')
bat.file <- paste0(hy.path,'working/test.bat') ## Name of BAT file to add to/run
files <- list.files(path=path.files,pattern='tdump')
lapply(files,function(x) file.remove(x))

start <- paste(year,'-01-01',sep='')
end <- paste(year,'-3-31 18:00',sep='')
dates <- seq(as.POSIXct(start,'GMT'),as.POSIXct(end,'GMT'),by='3 hour')

for (i in 1:length(dates))
{
year <- format(dates[i],'%y')
Year <- format(dates[i],'%Y') # Long format
month <- format(dates[i],'%m')
day <- format(dates[i],'%d')
hour <- format(dates[i],'%H')

x <- paste('echo',year,month,day,hour,'          >CONTROL')
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE)

x <- 'echo 1          >>CONTROL'
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)

x <- paste('echo',lat,lon,height, '          >>CONTROL')
write.table(x,bat.file,col.names=FALSE,row.names = FALSE,quote=FALSE,append=TRUE)

x <- paste('echo ','-',hours, '   >>CONTROL',sep='')
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)

x <- 'echo 0          >>CONTROL
echo 10000.0    >>CONTROL
echo 3          >>CONTROL'

write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)


months <- as.numeric(unique(format(dates[i],'%m')))
months <- c(months,months + 1:2)
months <- months -1
months <- months[months <= 12]
if (length(months) ==2) months <- c(min(months) - 1,months)

for (i in 1:3)
  add.met(months[i],Year,met,bat.file)

x <- 'echo ./          >>CONTROL'
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)

x <- paste('echo tdump', year, month,day,hour, '          >>CONTROL',sep='')
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)

x <- '\\hysplit4\\exec\\hyts_std'
write.table(x,bat.file,col.names=FALSE,row.names=FALSE,quote=FALSE,append=TRUE)


system(paste0(hy.path,'working/test.bat'))

}
traj_NS <- read.files(hours,hy.path)

file.name <- paste(out,name,Year,'.RData',sep='')
save(traj_NS,file=file.name)
}

read.files <- function(hours = 120, hy.path)
{

files <- Sys.glob('tdump*')
output <- file('Rcombined.txt','w')

for (i in files) {
input <- readLines(i)
input <- input[-c(1:7)] # Delete header
writeLines(input,output)
}
close(output)

traj_NS <- read.table(paste0(hy.path,'working/Rcombined.txt'),header=FALSE)
traj_NS <- subset(traj_NS,select = -c(V2, V7, V8))

traj_NS <- rename(traj_NS, c(V1='receptor',V3='year',V4='month',V5='day',
V6='hour',V9='hour.inc',V10='lat',V11='lon',
V12='height',V13='pressure'))

year <- traj_NS$year[1]
if (year < 50) traj_NS$year <- traj_NS$year + 2000 else traj_NS$year <- traj_NS$year + 1900

traj_NS$date2 <- with(traj_NS, ISOdatetime(year,month,day,hour,min=0,sec=0,tz="Asia/Calcutta"))

traj_NS$date <- traj_NS$date - 3600 * traj_NS$hour.inc
traj_NS
}

for(i in 2020){
procTraj(lat=25.8154,lon=79.9187, year=i,
name='IGP_CARE',hours=120,
met='/Users/Anurag Sahu/Documents/TrajData/',
out='/Users/Anurag Sahu/Documents/TrajData/',
hy.path='/hysplit/')
}

load("~/TrajProc/IGP_CARE2021.Rdata")
view(traj_NS)

I am getting this error
Error in setwd(paste0(hy.path, "/working/")) :
cannot change working directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hysplit 🌐 HYSPLIT trajectories & associated openair functions question Questions about function use or interpretation
Projects
None yet
Development

No branches or pull requests

2 participants