0

I have a large collection of netcdf files which I need to crop with specific latitudes and longitudes and rewrite it as a new file.

What I'm having trouble to do is: when writing this new netcdf file I want to name it with its respective date and time, something like "yyyymmddhhmm".nc, but I don't know how to extract the file's date. Below are some of the file's info:

processing_level: National Aeronautics and Space Administration (NASA) L2
date_created: 2020-01-01T14:10:01.2Z
cdm_data_type: Image
time_coverage_start: 2020-01-01T14:00:21.7Z
time_coverage_end: 2020-01-01T14:09:52.5Z
timeline_id: ABI Mode 6
production_data_source: Realtime
id: e9ac2711-c550-4b8a-9c27-babcd1fc49f6
dimensions(sizes): lon(5777), lat(5777)
variables(dimensions): |S1 crs(), float64 lat(lat), float64 lon(lon), int16 CMI(lat, lon)
RodX
  • 25
  • 6
  • hi there, I'm kind of assuming that you don't actually want to use the date created however, as this is unlikely to coincide with the date that the data refers to (and a particular creation date may be used for many different files, say for example, data was reprocessed). Don't you instead want the date from the time variable of the data which would be unique? – ClimateUnboxed Feb 01 '21 at 12:10
  • For this particular case, they are the same time, the file in question is a GOES-16 image, it starts collecting the data at 14:00 UTC, ends at 14:09 and the file is created at 14:10 – RodX Feb 02 '21 at 13:42
  • I would still always use the time from the dimension that the data refers to, and not the file creation time. They are the same for the files you checked but if files are later reprocessed or the timeslot is close to mid night, it can lead to infinite confusion. (speaking from experience ;-) ) – ClimateUnboxed Feb 02 '21 at 14:05

2 Answers2

1

You should be able to solve this using xarray, assuming the file times are suitably formatted. Try the following:

import xarray as xr
ds = xr.open_dataset("infile.nc)
ds.time.values
Robert Wilson
  • 3,192
  • 11
  • 19
  • I got this `AttributeError: 'Dataset' object has no attribute 'time'` :/ – RodX Jan 20 '21 at 12:22
  • Strange. I'm assuming the file was produced by NASA, in which case time should show up. Not sure if further help can be given without further info on the data – Robert Wilson Jan 20 '21 at 12:52
  • actually, while going through the xarray documentation I understood your answer, and found a solution – RodX Jan 20 '21 at 13:14
0

So, thanks to Robert for helping me out, this is how I managed to solve it:

import xarray as xr
ds = xr.open_dataset(infile.nc)
date = ds.attrs['date_created']

Or, using netCDF4:

import netCDF4
ds = netCDF4.Dataset(infile.nc, 'r')
date = ds.__dict__['date_created']
RodX
  • 25
  • 6