Export time dimension NetCDF to raster format using ArcPy

4736
24
Jump to solution
06-11-2018 05:06 AM
ShouvikJha
Occasional Contributor III

I have single NetCDF file which is time dimension NetCDF file, the netCDF file contains the monthly weather data for 20 years. I want to convert the NetCDF to raster tiff format and output of the raster as same the month and year name. below is code but it's producing the error in line number 29. Time format of the data is YY-MM-DD 2013-01-01 12:00, 2013-02-01 12:00, so on. How to loop all the time dimension.  

updated download link for input files: 2013.nc - Google Drive 

I tried to extract the layer manually using ArcGIS tool, it's successfully extracted. I have copied the code from ArcGIS 

arcpy.MakeNetCDFRasterLayer_md(in_netCDF_file="E:/Weather/2002.nc",
variable="slhf",
x_dimension="longitude",
y_dimension="latitude",
out_raster_layer="sshf_Layer",
band_dimension="",
dimension_values="time '01-01-2013 12:00:00'",
value_selection_method="BY_VALUE")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

#Workspace
outLoc = r"E:\Weather/"
inNetCDF = r"E:\Weather\2002.nc"
#Veriable
variable = "slhf"
x_dimension = "lon"
y_dimension = "lat"
band_dimension = ""
dimension = "time"
valueSelectionMethod = "BY_VALUE"

nc_FP = arcpy.NetCDFFileProperties(inNetCDF)
nc_Dim = nc_FP.getDimensions()

for dimension in nc_Dim:
        if dimension == "time":
            top = nc_FP.getDimensionSize(dimension)
            for i in range(0, top):

                dimension_values = nc_FP.getDimensionValue(dimension, i)
                nowFile = str(dimension_values)
                nowFile = nowFile.translate(None, '/')
                # I needed after 2013
                if int(nowFile.split()[0].split('-')[-1])==2013:

                    dv1 = ["time", dimension_values]
                    dimension_values = [dv1]

                    arcpy.MakeNetCDFRasterLayer_md(inNetCDF, variable, x_dimension, y_dimension, nowFile, band_dimension, dimension_values, valueSelectionMethod)
                    print "success"
                    outname = outLoc + nowFile

                    arcpy.CopyRaster_management(nowFile, outname,"", "", "", "NONE", "NONE", "")


                else: print "DATA OUT OF RANGE"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Error message

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000237: One or more dimensions are invalid
ERROR 000237: One or more dimensions are invalid
Failed to execute (MakeNetCDFRasterLayer).‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
24 Replies
DanPatterson_Retired
MVP Emeritus

Xander, can you run the snippet that I requested information for.

Good snag on confirming the dimensions provided were wrong

XanderBakker
Esri Esteemed Contributor

Hi Dan, sure, will do that tomorrow

0 Kudos
XanderBakker
Esri Esteemed Contributor

It returns an ordered dictionary:

OrderedDict([(u'longitude', <netCDF4.Dimension object at 0x01809C90>), (u'latitude', <netCDF4.Dimension object at 0x0A086690>), (u'time', <netCDF4.Dimension object at 0x0A0866C0>)])
0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌, Thank you. I ran the code but its showing data out of range. Which NC file I provided you, its contain the monthly data for 2013, but I don't understand how it's showing the year of 1900. maybe a problem with my data however I am providing you the updated link for data download, Kindly look at the updated file of NetCDF

Updated Download link - 2013.nc - Google Drive 

After running the code, its showing 

dimension_values 01-12-2013 12:00:00
nowFile (1): 01-12-2013 12:00:00
nowFile (2): 01-12-2013 12:00:00
test 2013
DATA OUT OF RANGE‍‍‍‍‍

I have taken a snapshot of the data from ArcGIS, the time spam of NC looks 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Shouvik Jha , the data is in a different format. Initially you removed the forward slashed in code from the date and this date format has dashes. That is what is different. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

There is something funky going on here... With the new file I'm still getting the year 1900 as you can see below:

nc_FP: <geoprocessing NetCDF Workspace object object at 0x06640230>
nc_Dim: [u'latitude', u'longitude', u'time']
dimension: latitude
dimension: longitude
dimension: time
top: 12
i 0
dimension_values 4/02/1900 9:28:12 a. m.
nowFile (1): 4/02/1900 9:28:12 a. m.
nowFile (2): 4021900 9:28:12 a. m.
test 4021900
success
out_name: sshf_lay_0.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_0.tif
CopyRaster OK...
i 1
dimension_values 4/02/1900 10:05:24 a. m.
nowFile (1): 4/02/1900 10:05:24 a. m.
nowFile (2): 4021900 10:05:24 a. m.
test 4021900
success
out_name: sshf_lay_1.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_1.tif
CopyRaster OK...
i 2
dimension_values 4/02/1900 10:39:00 a. m.
nowFile (1): 4/02/1900 10:39:00 a. m.
nowFile (2): 4021900 10:39:00 a. m.
test 4021900
success
out_name: sshf_lay_2.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_2.tif
CopyRaster OK...
i 3
dimension_values 4/02/1900 11:16:12 a. m.
nowFile (1): 4/02/1900 11:16:12 a. m.
nowFile (2): 4021900 11:16:12 a. m.
test 4021900
success
out_name: sshf_lay_3.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_3.tif
CopyRaster OK...
i 4
dimension_values 4/02/1900 11:52:12 a. m.
nowFile (1): 4/02/1900 11:52:12 a. m.
nowFile (2): 4021900 11:52:12 a. m.
test 4021900
success
out_name: sshf_lay_4.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_4.tif
CopyRaster OK...
i 5
dimension_values 4/02/1900 12:29:24 p. m.
nowFile (1): 4/02/1900 12:29:24 p. m.
nowFile (2): 4021900 12:29:24 p. m.
test 4021900
success
out_name: sshf_lay_5.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_5.tif
CopyRaster OK...
i 6
dimension_values 4/02/1900 1:05:24 p. m.
nowFile (1): 4/02/1900 1:05:24 p. m.
nowFile (2): 4021900 1:05:24 p. m.
test 4021900
success
out_name: sshf_lay_6.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_6.tif
CopyRaster OK...
i 7
dimension_values 4/02/1900 1:42:36 p. m.
nowFile (1): 4/02/1900 1:42:36 p. m.
nowFile (2): 4021900 1:42:36 p. m.
test 4021900
success
out_name: sshf_lay_7.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_7.tif
CopyRaster OK...
i 8
dimension_values 4/02/1900 2:19:48 p. m.
nowFile (1): 4/02/1900 2:19:48 p. m.
nowFile (2): 4021900 2:19:48 p. m.
test 4021900
success
out_name: sshf_lay_8.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_8.tif
CopyRaster OK...
i 9
dimension_values 4/02/1900 2:55:48 p. m.
nowFile (1): 4/02/1900 2:55:48 p. m.
nowFile (2): 4021900 2:55:48 p. m.
test 4021900
success
out_name: sshf_lay_9.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_9.tif
CopyRaster OK...
i 10
dimension_values 4/02/1900 3:33:00 p. m.
nowFile (1): 4/02/1900 3:33:00 p. m.
nowFile (2): 4021900 3:33:00 p. m.
test 4021900
success
out_name: sshf_lay_10.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_10.tif
CopyRaster OK...
i 11
dimension_values 4/02/1900 4:09:00 p. m.
nowFile (1): 4/02/1900 4:09:00 p. m.
nowFile (2): 4021900 4:09:00 p. m.
test 4021900
success
out_name: sshf_lay_11.tif
out_ras: C:\GeoNet\NetCDF\sshf_lay_11.tif
CopyRaster OK...

Could this be something related to regional settings?

ShouvikJha
Occasional Contributor III

Xander Bakker‌ same data i am running on my PC with the same code provided by u but i am getting data out of range. 

Please could you clarify why it is happening and when i opening the same data in ArcGIS , the time spam showing 2013 which i shown in attached image, but your code displays 1900 . 

I am getting confused 

0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌, please could you open the data in ArcGIS to cross-check the input file with my attached image from ArcGIS 

0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌ the same data which I provided you and same code, I ran on my system and it is showing 

nc_FP: <geoprocessing NetCDF Workspace object object at 0x0295B530>
nc_Dim: [u'longitude', u'latitude', u'time']
dimension: longitude
dimension: latitude
dimension: time
top: 12
i 0
dimension_values 1/1/2013 12:00:00 PM
nowFile (1): 1/1/2013 12:00:00 PM
nowFile (2): 112013 12:00:00 PM
test 112013
DATA OUT OF RANGE
i 1
dimension_values 2/1/2013 12:00:00 PM
nowFile (1): 2/1/2013 12:00:00 PM
nowFile (2): 212013 12:00:00 PM
test 212013
DATA OUT OF RANGE
i 2
dimension_values 3/1/2013 12:00:00 PM
nowFile (1): 3/1/2013 12:00:00 PM
nowFile (2): 312013 12:00:00 PM
test 312013
DATA OUT OF RANGE
i 3
dimension_values 4/1/2013 12:00:00 PM
nowFile (1): 4/1/2013 12:00:00 PM
nowFile (2): 412013 12:00:00 PM
test 412013
DATA OUT OF RANGE
i 4
dimension_values 5/1/2013 12:00:00 PM
nowFile (1): 5/1/2013 12:00:00 PM
nowFile (2): 512013 12:00:00 PM
test 512013
DATA OUT OF RANGE
i 5
dimension_values 6/1/2013 12:00:00 PM
nowFile (1): 6/1/2013 12:00:00 PM
nowFile (2): 612013 12:00:00 PM
test 612013
DATA OUT OF RANGE
i 6
dimension_values 7/1/2013 12:00:00 PM
nowFile (1): 7/1/2013 12:00:00 PM
nowFile (2): 712013 12:00:00 PM
test 712013
DATA OUT OF RANGE
i 7
dimension_values 8/1/2013 12:00:00 PM
nowFile (1): 8/1/2013 12:00:00 PM
nowFile (2): 812013 12:00:00 PM
test 812013
DATA OUT OF RANGE
i 8
dimension_values 9/1/2013 12:00:00 PM
nowFile (1): 9/1/2013 12:00:00 PM
nowFile (2): 912013 12:00:00 PM
test 912013
DATA OUT OF RANGE
i 9
dimension_values 10/1/2013 12:00:00 PM
nowFile (1): 10/1/2013 12:00:00 PM
nowFile (2): 1012013 12:00:00 PM
test 1012013
DATA OUT OF RANGE
i 10
dimension_values 11/1/2013 12:00:00 PM
nowFile (1): 11/1/2013 12:00:00 PM
nowFile (2): 1112013 12:00:00 PM
test 1112013
DATA OUT OF RANGE
i 11
dimension_values 12/1/2013 12:00:00 PM
nowFile (1): 12/1/2013 12:00:00 PM
nowFile (2): 1212013 12:00:00 PM
test 1212013
DATA OUT OF RANGE
0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌ which you have shown in print statement, the line number 11 shows the same digit for every output, but it should not be the same as month change the value should also change. 

Please could you check once, the input file 

nc_FP: <geoprocessing NetCDF Workspace object object at 0x06640230>
nc_Dim: [u'latitude', u'longitude', u'time']
dimension: latitude
dimension: longitude
dimension: time
top: 12
i 0
dimension_values 4/02/1900 9:28:12 a. m.
nowFile (1): 4/02/1900 9:28:12 a. m.
nowFile (2): 4021900 9:28:12 a. m.
test 4021900
0 Kudos