Ideas for making PNG Loop

Hello Community,
I am here for some suggestions.
I would like to create a loop for creating png images for an entire month. Currently, I am using this code:

library(png)

#for assign coordinates to the png plot
longitude <- lon[,451]
latitude <- lat[468,]

#I have 30 values called monthday (monthday1, 2,3,4..., 30)
#I  am replacing the data called "chl" with the monthday values  (these are calculated with other #procedure) with this line:

BFG <- replace(chl, c(ocean), monthday1) 

#PNG filename and definition
png("figures/April/modis2020/2020-04-01_Modis_2020.jpg", 
    width=2400, height=1800, res=300 )

##Image data. Body

color <- colorRampPalette(c("purple", "#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000", "black"))

imagep(longitude, latitude, log10(BFG), col=color, zlim=log10(c(0.01, 20)), missingColor=1, xlab="Longitude", ylab="Latitude",zlab='mg m^-3')
box()
title(main = "2020-04-01 Modis 2020",font.main=2,col.main="black", cex.main=1.2)

#Final image closing process
dev.off()

After this part, I got one png (I will attach here), and the same is for all the days.

My request is, how could I reformulate the code for:

- Create the variable BFG with each different day consequently
- Create the consequents days (day 1, 2,3, etc.)

Thanks so much for your time and knowledge.

Christian.


Ideally you should input the monthday values in a vector or list:

monthdays <- c("monthday1", "monthday2", ...)

Then it becomes easy to loop on the values in the vector or use vectorized functions. For example to create BFG:

stringr::str_replace("aa bb cc", "bb", c("dd","ee"))
#> [1] "aa dd cc" "aa ee cc"

Similarly, if you have the days in a vector, it is easy to generate all the filenames:

days <- c("2020-04-01", "2020-04-02")
paste0("figures/April/modis2020/",days, "_Modis_2020.png")
#> [1] "figures/April/modis2020/2020-04-01_Modis_2020.png"
#> [2] "figures/April/modis2020/2020-04-02_Modis_2020.png"

So what I would recommend is to generate a data frame, with one row per day (i.e. one row per png to generate), put the actual code generating the png (including png(), imagep() and dev.off()) in a function, then use purrr::walk() to loop over the rows of the dataframe and generate all the pngs.

If you're not comfortable with these functions, the other approach is to generate all the vectors needed to make the figures, then to use a for loop:

all_monthdays <- c("xx", "yy", ...)
all_BFGs <- stringr::str_replace(chl, ocean, all_monthdays)
days <- str_extract(all_monthdays, "2020-[0-9]{2}-[0-9]{2}")
all_paths <- paste0("figures/April/modis2020/",days, "_Modis_2020.png")
all_titles <- ...

color <- colorRampPalette(...)

for(i in seq_along(all_BFGs)){
  png(all_paths[i], width=2400)
  imagep(longitude, latitude, log10(all_BFGs[i]), ...)
  title(main = all_titles[i])
  dev.off()
}
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.