This paper describes a system based onPythonlanguage, reading a large number of them one by one.nc
A method of exporting all the time information contained in a multi-temporal raster file in the format.
.nc
beNetCDF
(The extension of a Network Common Data Form (NCDF) file, indicating a commonly used format for storing scientific data.NetCDF
is a self-describing, portable binary file format for storing large data sets in science and engineering; due to its own properties, the.nc
The data are widely used in meteorology, oceanography, earth sciences, climate research, atmospheric sciences, GIS, and other fields.
First, let's clarify the needs of this article. Now there is a folder with a large number of.nc
format raster file, as shown below.
Of these, each.nc
The files in the format all have themultiple time-phase (e.g. solar)(or rathermany dimensional), andnot justIt's just a temporal phase. We hope that reading the entirety of this folder of.nc
format file and get each of the time phases it represents.
After clarifying the requirements, we can start the specific operation. First of all, the code required for this article is as follows.
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 31 20:28:03 2023
@author: fkxxgis
"""
import os
import netCDF4
from netCDF4 import Dataset
def list_nc_dates(folder_path):
nc_dates = []
for file_name in (folder_path):
if file_name.endswith(".nc"):
file_path = (folder_path, file_name)
try:
dataset = Dataset(file_path)
time_var = ["time"]
time_values = time_var[:]
time_units = time_var.units
time_calendar = time_var.calendar
dates = []
for value in time_values:
date = netCDF4.num2date(value, units=time_units, calendar=time_calendar)
(("%Y-%m-%d %H:%M:%S"))
nc_dates.append((file_name, dates))
except Exception as e:
print(f"Error reading file {file_name}: {str(e)}")
return nc_dates
folder_path = "F:/Data_Reflectance_Rec/soil_1"
nc_dates = list_nc_dates(folder_path)
for nc_file, dates in nc_dates:
for date in dates:
print(date)
The overall idea of this code is also clear.
First, we import the required modules. Here, it is necessary to import thePython(used form a nominal expression)os
module for handling file and folder path operations; also importsnetCDF4
libraries and proceeds to extract from thenetCDF4
Importing from librariesDataset
class for opening and reading.nc
file. Here, if you need to configure thenetCDF4
Library, you can refer to the articleConfiguration of h5py, netCDF4 library: Anaconda environment。
Next, we define a file namedlist_nc_dates
function that accepts a folder path as an argument. In the function, an empty list is first creatednc_dates
The following is a list of the data that is used to store each of the.nc
file and its corresponding list of dates; subsequently, use the()
function iterates through all the files in a folder by checking if the filenames start with.nc
ending to filter out.nc
Documentation. Immediately afterward, for the filtered.nc
file, using the()
function constructs its full path.
Secondly, the use ofDataset
Open class.nc
file and assigns the open file object to thedataset
variable; subsequently, get the.nc
The timing of the document, in this paper's.nc
in the data, which is also calledtime
variable and reads the value of the time variable into thetime_values
in the variable. Next, get the units and time type of the time variable, respectively.
We then create an empty listdates
, which is used to store the date string. Iterate over each value of the time variable using thenetCDF4.num2date()
function converts the time value to a date object. Immediately after that, the date object is converted to a string of the specified format and added to thedates
List. In addition, here are the.nc
The list of filenames and corresponding dates is added as a tuple to thenc_dates
list, making it easy for us to check the dates at a later stage. At the end of the function, it returns the list of dates containing each.nc
A list of documents and their corresponding dates.
Outside of the function, we set the path to the folder, and then we can call thelist_nc_dates
function, passing it the folder path and assigning the returned result to thenc_dates
variable. Finally, just go through the loop and print each date.
Execute the above code, you can appear as shown below (the results are very long, just a part of the intercept). Since in this article, each.nc
Each dimension of the format file (i.e., eachmetallurgical) are all accurate to the day, so the hours, minutes, and seconds after the days in the chart below are all00
. Of course, if everyone's.nc
Format file dimensions are many, and the time phase is not well displayed completely if you print it out, so you can consider exporting the time information to a table file, etc.; for example, you can export everydate
put them allDataFrameand subsequently exported as.csv
Documentation.
At this point, the job is done.