This code generates a set of data outputs from FIA data obtained fom the US Forest Service DataMart, and generates biomass estimates using three procedures.
pgfutter
from lukasmartinelli
Github page Note: this SOP written using version 0.3.2; old versions available under releases. Most likely you are using a 64-bit machine, so download the files ending in amd64.exe
. pgfutter
is a commandline tool to import CSV and JSON files into PostgreSQL.AppData/Roaming/postgresql/
). For example, with a password of password
, these lines provide access to the database postgres
for the user postgres
(the administrator), and access to all databases and users (with this password), respectively:localhost:5432:postgres:postgres:password
*:*:*:*:password
Note that multiple lines can be included in this file.
pgfutter
.exe
file (for example pgfutter_windows_amd64.exe
) to a new directory somewhere on your computer.pgfutter.exe
PATH
:
Computer
(or This PC
) in your File Manager and choose Properties
. NOTE: Changes to the System PATH
will only take effect when a Commandline window is re-opened. You will not be able to access pgfutter
from any windows that are currently open.
The syntax for pgfutter
on Windows is as follows (once you've opened a command line window using cmd
):
> pgfutter --pw password csv yourcsv.csv
Note: If you've set a unique password, you'll replace the word password
with your own. If using custom host, port, or username, type:
pgfutter --help
for additional global options to customize.
To import all csv files in a directory, cd
to that directory in cmd
and enter:
> for %f in (*.csv) do pgfutter --pw password csv %f
The data is imported to the Postgres database under the Schema “import” by default.
Change the name of this Schema; if dealing with many states, change it to the state abbreviation. This is easiest accomplished by opening PG Admin III and right clicking on the desired schema and selecting “Properties.” If importing multiple states and want to keep each in separate schema, do this between each import.
Note: Indiana cannot be left as IN, as specifying this schema in a query causes problems as “in” is a command; so this schema should be names ind, and all files within the schema should follow the pattern ‘ind’ instead is ‘in’.
Ensure upload worked correctly by viewing some of the imported tables.
Use a backup file titled ‘FIA_states’ to restore a database containing all state data, generated using the command
> pg_dump -U postgres -v FIA_states > FIA_states.sql
This backup also includes the combined plot, tree and survey tables, but not the true coordinates. The backup is currently a zipped file, and needs to be unzipped prior to restoring.
From the command line, create a new database titled FIA_states
> createdb -E UTF8 -U postgres FIA_states
Note: these commands were executed using Windows cmd, with the PostgreSQL superuser ‘postgres,’ and a password file.
Within the same directory as the backup file, execute the following command to restore the backed up database to the new database
> psql -U postgres -d FIA_states -f FIA_states.sql
This database includes state data for the 16 Midwest and Northeast states, in addition to the combined states tables outlined below in the section “Combine similar tables between states.”
Note: To run a query in Pg Admin III, click on the “Execute SQL Queries” button and in the new window, enter text and press the “Execute query” button.
biomass_product_workflow/read_postgres.sh
to create the Postgres database (code set up for a Linux machine).biomass_product_workflow/create_sqlite_db.R
.Use the text in Query4.txt to create a table needed for the R script control_file . Note this query pulls the correct lat/lon coordinates, and relabels them at ‘lat’ and ‘lon.’
Use the R script control_file.Rmd to import FIA data from SQL (or alternatively, read in the csv file full_fia_long.csv located at data/output/), estimate forest parameters at the tree and species level, and generate output rasters. To run, this file requires the location data file plt_cn_values.csv to be located at data/plt_location/.
Note that the R scripts used to create those sourced within control_file.Rmd are located within the directory R_scripts/original_scripts/. If you wish to manipulate the code as a whole, you can use these 'rough' scripts, and then will need to execute the SQL queries located within the text files Query3.txt and Query4.txt within postgreSQL.