Problem in writing data and time from GeoTIF into NetCDF

المشرف العام

Administrator
طاقم الإدارة
I tried to use this solution for my Image stacks, but I doesn't go through data!!

I think the problem could be from the pat which use regex to extract year and month from file path.

All the data are in a folder called data_sample and here is the name of the files:

R20060111_075725___SIG0__ASAWS___M1VVD_TUW_SGRT15A00_AF075M_E060N054T6.tifR20060114_080443___SIG0__ASAWS___M1VVD_TUW_SGRT15A00_AF075M_E060N054T6.tifR20060117_081036___SIG0__ASAWS___M1VVD_TUW_SGRT15A00_AF075M_E060N054T6.tifR20060118_073857___SIG0__ASAWS___M1VVD_TUW_SGRT15A00_AF075M_E060N054T6.tifthe output is:

dimensions: lon = 8000 ; lat = 8000 ; time = UNLIMITED ; // (0 currently)variables: float time(time) ; time:units = "days since 2006-01-11 00:00:00" ; time:standard_name = "time" ; float lon(lon) ; lon:standard_name = "longitude" ; float lat(lat) ; lat:standard_name = "latitude" ; int crs ; crs:long_name = "Lon/Lat Coords in WGS84" ; crs:grid_mapping_name = "latitude_longitude" ; crs:longitude_of_prime_meridian = 0. ; crs:semi_major_axis = 6378137. ; crs:inverse_flattening = 298.257223563 ; short tmn(time, lat, lon) ; tmn:_FillValue = -9999s ; tmn:units = "degC" ; tmn:scale_factor = 0.01 ; tmn:add_offset = 0. ; tmn:long_name = "minimum monthly temperature" ; tmn:standard_name = "air_temperature" ; tmn:grid_mapping = "crs" ;// global attributes: :Conventions = "CF-1.6" ;}and here is the code which I edited (@rich-signell)

import numpy as npimport datetime as dtimport osimport gdalimport netCDF4import reds = gdal.Open('/data_sample/R20060111_075725___SIG0__ASAWS___M1VVD_TUW_SGRT15A00_AF075M_E060N054T6.tif')a = ds.ReadAsArray()nlat, nlon = np.shape(a)b = ds.GetGeoTransform() # bbox, intervallon = np.arange(nlon)*b[1]+b[0]lat = np.arange(nlat)*b[5]+b[3]basedate = dt.datetime(2006, 01, 11, 0, 0, 0)# Create NetCDF filenco = netCDF4.Dataset('time_series.nc', 'w', clobber=True)chunk_lon = 16chunk_lat = 16chunk_time = 12# Create dimensions, variables and attributes:nco.createDimension('lon', nlon)nco.createDimension('lat', nlat)nco.createDimension('time', None)timeo = nco.createVariable('time', 'f4', ('time',))timeo.units = 'days since 2006-01-11 00:00:00'timeo.standard_name = 'time'lono = nco.createVariable('lon', 'f4', ('lon',))lono.standard_name = 'longitude'lato = nco.createVariable('lat', 'f4', ('lat',))lato.standard_name = 'latitude'# Create container variable for CRS: lon/lat WGS84 datumcrso = nco.createVariable('crs', 'i4')crso.long_name = 'Lon/Lat Coords in WGS84'crso.grid_mapping_name = 'latitude_longitude'crso.longitude_of_prime_meridian = 0.0crso.semi_major_axis = 6378137.0crso.inverse_flattening = 298.257223563# Create short integer variable for temperature data, with chunkingtmno = nco.createVariable('tmn', 'i2', ('time', 'lat', 'lon'), zlib=True, chunksizes=[chunk_time, chunk_lat, chunk_lon], fill_value=-9999)tmno.units = 'degC'tmno.scale_factor = 0.01tmno.add_offset = 0.00tmno.long_name = 'minimum monthly temperature'tmno.standard_name = 'air_temperature'tmno.grid_mapping = 'crs'tmno.set_auto_maskandscale(False)nco.Conventions = 'CF-1.6'# Write lon,latlono[:] = lonlato[:] = latpat = re.compile('(\w{1})(\d{4})(\d{2})(\d{2})_\d{6}')itime = 0# Step through data, writing time and data to NetCDFfor root, dirs, files in os.walk('/data_sample/'): dirs.sort() files.sort() for f in files: if re.match(pat, f): # read the time values by parsing the filename year = int(f[4:8]) mon = int(f[8:10]) date = dt.datetime(year, mon, 1, 0, 0, 0) print(date) dtime = (date-basedate).total_seconds()/86400. timeo[itime] = dtime # min temp tmn_path = os.path.join(root, f) print(tmn_path) tmn = gdal.Open(tmn_path) a = tmn.ReadAsArray() # data tmno[itime, :, :] = a itime = itime+1nco.close()Does someone know where could be a problem ?Thank you



أكثر...
 
أعلى