{"cells":[{"metadata":{},"cell_type":"markdown","source":"I recognized that the y-axis / latitude is reversed from this [kernel](https://www.kaggle.com/jyesawtellrickson/averaging-satellite-data), thanks to @JyeSR. \n\nI explored Puerto Rico NO2 emissions directly from [Earth Engine](https://code.earthengine.google.com/?scriptPath=Examples%3ADatasets%2FCOPERNICUS_S5P_OFFL_L3_NO2) and it makes more sense that NO2 emissions are located near San Juan city.\n\n<img src=\"https://i.imgur.com/Aoyls5m.png\" width=\"75%\" />\n\n"},{"metadata":{},"cell_type":"markdown","source":"**After checking the metadata from this [kernel](https://www.kaggle.com/paultimothymooney/explore-image-metadata-s5p-gfs-gldas), Only S5P NO2 dataset has reversed y-axis where bottom bounding box (18.5649) is larger than top bounding box (17.9001). GLDAS and GFS dataset are fine, no need for transformation.** \n\nHere is some quick fix to rotate y-axis using R."},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"library(tidyverse)\nlibrary(raster)\nlibrary(tmap)\nlibrary(sf)\nlibrary(repr)\noptions(repr.plot.width=11, repr.plot.height = 4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"emission <- raster(\"../input/ds4g-environmental-insights-explorer/eie_data/s5p_no2/s5p_no2_20180701T161259_20180707T175356.tif\", band = 2)\nemission <- emission * 10^6\nnames(emission) <- \"no2_tropo\"\nemission %>% \n    as.data.frame(xy = TRUE) %>% \n    ggplot() +\n    geom_raster(aes(x, y, fill = no2_tropo), alpha = 0.8) +\n    coord_sf() +\n    theme_minimal() +\n    scale_fill_gradientn(bquote(atop(\"TVCD\" ~ NO^2, paste(\"(\", 10^-6, \" \", mol/m^2, \")\") )),\n                         colours = c(\"white\", \"#84B2D3\", \"#EEAE48\", \"#642103\"),\n                         values = c(0,0.2,0.4,0.6,0.8,0.9,1)) +\n    theme(legend.position = \"bottom\", axis.title = element_blank(), panel.grid = element_blank()) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Quick fix using `raster::flip` function for one image"},{"metadata":{"trusted":true},"cell_type":"code","source":"emission_fix <- flip(emission, direction = \"y\")\nnames(emission_fix) <- \"no2_tropo\"\nemission_fix %>% \n    as.data.frame(xy = TRUE) %>% \n    ggplot() +\n    geom_raster(aes(x, y, fill = no2_tropo), alpha = 0.8) +\n    coord_sf() +\n    theme_minimal() +\n    scale_fill_gradientn(bquote(atop(\"TVCD\" ~ NO^2, paste(\"(\", 10^-6, \" \", mol/m^2, \")\") )),\n                         colours = c(\"white\", \"#84B2D3\", \"#EEAE48\", \"#642103\"),\n                         values = c(0,0.2,0.4,0.6,0.8,0.9,1)) +\n    theme(legend.position = \"bottom\", axis.title = element_blank(), panel.grid = element_blank()) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Using average data"},{"metadata":{"trusted":true},"cell_type":"code","source":"file_list <- list.files(\"../input/ds4g-environmental-insights-explorer/eie_data/s5p_no2/\", pattern = \"*tif\", full.names = TRUE)\nemission_yearly <- stack(file_list[1], bands = 2)\n\n# for (i in 2:length(emission_yearly))\n# let's do for a month for faster calculation\nfor (i in 2:30) {\n  emission <- stack(file_list[i], bands = 2)\n  comb <- merge(extent(emission_yearly), extent(emission))\n  extent(emission_yearly) <- comb\n  extent(emission) <- comb\n  emission_yearly <- stack(emission_yearly, emission)\n}\nemission_yearly <- calc(emission_yearly * 10^6, mean, na.rm = TRUE)\nemission_yearly_fix <- flip(emission_yearly, direction = \"y\")\nnames(emission_yearly) <- names(emission_yearly_fix) <- \"no2_tropo\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"emission_yearly %>% \n    as.data.frame(xy = TRUE) %>% \n    ggplot() +\n    geom_raster(aes(x, y, fill = no2_tropo), alpha = 0.8) +\n    coord_sf() +\n    theme_minimal() +\n    scale_fill_gradientn(bquote(atop(\"TVCD\" ~ NO^2, paste(\"(\", 10^-6, \" \", mol/m^2, \")\") )),\n                         colours = c(\"white\", \"#84B2D3\", \"#EEAE48\", \"#642103\"),\n                         values = c(0,0.2,0.4,0.6,0.8,0.9,1)) +\n    theme(legend.position = \"bottom\", axis.title = element_blank(), panel.grid = element_blank()) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"emission_yearly_fix %>% \n    as.data.frame(xy = TRUE) %>% \n    ggplot() +\n    geom_raster(aes(x, y, fill = no2_tropo), alpha = 0.8) +\n    coord_sf() +\n    theme_minimal() +\n    scale_fill_gradientn(bquote(atop(\"TVCD\" ~ NO^2, paste(\"(\", 10^-6, \" \", mol/m^2, \")\") )),\n                         colours = c(\"white\", \"#84B2D3\", \"#EEAE48\", \"#642103\"),\n                         values = c(0,0.2,0.4,0.6,0.8,0.9,1)) +\n    theme(legend.position = \"bottom\", axis.title = element_blank(), panel.grid = element_blank()) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Save the raster file"},{"metadata":{"trusted":true},"cell_type":"code","source":"writeRaster(emission_yearly_fix, \"emission_tropo_no2_avg_fix.tif\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Hope this can help"}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":4}