Location>code7788 >text

Methods for extracting data from NC raster representations of the time dimension

Popularity:333 ℃/2024-10-22 17:12:13

  This paper describes a system based onPythonlanguage, reading a large number of them one by one.ncA method of exporting all the time information contained in a multi-temporal raster file in the format.

  .ncbeNetCDF(The extension of a Network Common Data Form (NCDF) file, indicating a commonly used format for storing scientific data.NetCDFis a self-describing, portable binary file format for storing large data sets in science and engineering; due to its own properties, the.ncThe 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.ncformat raster file, as shown below.

image

  Of these, each.ncThe 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.ncformat 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)osmodule for handling file and folder path operations; also importsnetCDF4libraries and proceeds to extract from thenetCDF4Importing from librariesDatasetclass for opening and reading.ncfile. Here, if you need to configure thenetCDF4Library, you can refer to the articleConfiguration of h5py, netCDF4 library: Anaconda environment

  Next, we define a file namedlist_nc_datesfunction that accepts a folder path as an argument. In the function, an empty list is first creatednc_datesThe following is a list of the data that is used to store each of the.ncfile 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.ncending to filter out.ncDocumentation. Immediately afterward, for the filtered.ncfile, using the()function constructs its full path.

  Secondly, the use ofDatasetOpen class.ncfile and assigns the open file object to thedatasetvariable; subsequently, get the.ncThe timing of the document, in this paper's.ncin the data, which is also calledtimevariable and reads the value of the time variable into thetime_valuesin 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 thedatesList. In addition, here are the.ncThe list of filenames and corresponding dates is added as a tuple to thenc_dateslist, 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.ncA 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_datesfunction, passing it the folder path and assigning the returned result to thenc_datesvariable. 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.ncEach 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.ncFormat 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 everydateput them allDataFrameand subsequently exported as.csvDocumentation.

  At this point, the job is done.