-
Notifications
You must be signed in to change notification settings - Fork 9
/
ncdata2envi.pro
181 lines (176 loc) · 5.98 KB
/
ncdata2envi.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
;+
; ;**********************************************************
; Author: shaodonghang
; NAME:
; PARAMETERS:
; 1、NcPath
; 2、EnviPath
; Write by :
; 2016-4-23
;;MODIFICATION HISTORY:
;; Modified :
;***********************************************************
;-
PRO NCDATA2ENVI, NcDirectory = NcDirectory, ENVIDirectory = ENVIDirectory
;**************************************************************************
;
; 1) Initialize the Common variables
;
;**************************************************************************
;just for Test
NcDirectory = 'I:\snow_data\'
ENVIDirectory = 'I:\snow_data\output\'
;**************************************************************************
;
; 2) Get the files to be convert
;
;**************************************************************************
NCFilePaths = FILE_SEARCH(NcDirectory,'*.nc', COUNT = nCount, /TEST_READ, /FULLY_QUALIFY_PATH)
IF(nCount LE 0) THEN RETURN
PRINT,'Procedure begins at ' + STRING(SYSTIME(/UTC))
FOR i=0, nCount-1 DO BEGIN
;Open Nc File
NID = NCDF_OPEN(NCFilePaths[i], /nowrite )
;Inquire about this file; returns structure
File_Info = NCDF_INQUIRE(NID)
;define the dimensions of this file
NS = 0L
NL = 0L
NB = 0L
;**************************************************************************
; 1) read dim info
;**************************************************************************
FOR dimid=0, FILE_INFO.NDIMS -1 DO BEGIN
NCDF_DIMINQ, NID, Dimid, Name, Size
SWITCH Name OF
"lon": BEGIN
NS = Size
BREAK
END
"lat": BEGIN
NL = Size
BREAK
END
"time": BEGIN
NB = Size
BREAK
END
ELSE: BEGIN
PRINT, 'There is no Dim'
END
ENDSWITCH
ENDFOR
;**************************************************************************
; 2) read data info
;**************************************************************************
;define proj request
StartX = 0.0;
StartY = 0.0;
EndX = 0.0;
EndY = 0.0;
GridSize = 0.0;
ndepth = FLTARR(NB)
FOR Varid=0, FILE_INFO.NVARS-1 DO BEGIN
IsWRITE = 1;
; inquire about the variable; returns structure
VAR = NCDF_VARINQ( NID, Varid )
;read data
dataID = NCDF_VARID(NID, VAR.NAME)
NCDF_VARGET, NID, dataID, Data
SWITCH VAR.NAME OF
"lon": BEGIN
StartX = Data[0];
nData = N_ELEMENTS(Data)
EndX = Data(nData-1)
IsWRITE = 0
BREAK
END
"lat": BEGIN
StartY = Data[0];
nData = N_ELEMENTS(Data)
EndY = Data(nData-1)
IsWRITE = 0
BREAK
END
"time": BEGIN
ndepth = Data
IsWRITE = 0
BREAK
END
ENDSWITCH
;**************************************************************************
;1) IF No project then Creat project
;**************************************************************************
; Projection
; GridSize = 0 illustrate iMap is null
IF (GridSize EQ 0) AND IsWRITE THEN BEGIN
GridSize = (ROUND(EndX)-ROUND(StartX)) / FLOAT(NS)
; start point
iProj = ENVI_PROJ_CREATE(/GEOGRAPHIC)
; Create the map information
ps = [GridSize, GridSize]
mc = [0D, 0D, StartX, EndY]
Datum = 'WGS-84'
Units = ENVI_TRANSLATE_PROJECTION_UNITS ('Degrees')
iMap = ENVI_MAP_INFO_CREATE(/GEOGRAPHIC,MC=MC,PS=PS,PROJ=iProj,UNITS=Units)
ENDIF
;**************************************************************************
; 2) Creat Envi File
;**************************************************************************
IF IsWRITE THEN BEGIN
OutFilePath = ENVIDirectory + VAR.NAME+'.tif'
OPENW, HData, OutFilePath, /GET_LUN
WRITEU, HData,REVERSE(Data,2)
FREE_LUN, HData
DATA_TYPE = 4
DataType = VAR.DATATYPE
SWITCH VAR.DATATYPE OF
"BYTE": BEGIN
DATA_TYPE = 1
BREAK
END
"CHAR": BEGIN
DATA_TYPE = 1
BREAK
END
"INT": BEGIN
DATA_TYPE = 2
BREAK
END
"LONG": BEGIN
DATA_TYPE = 3
BREAK
END
"FLOAT": BEGIN
DATA_TYPE = 4
BREAK
END
"DOUBLE": BEGIN
DATA_TYPE = 5
BREAK
END
ENDSWITCH
; Edit the envi header file
ENVI_SETUP_HEAD,FNAME=OutFilePath,NS=NS,NL=NL,NB=NB,INTERLEAVE=0,$
DATA_TYPE=DATA_TYPE,OFFSET=0,MAP_INFO=iMap,BNAMES=[VAR.NAME],/WRITE,$
/OPEN,R_FID=Data_FID
;**************************************************************************
; 3) read all attributes update Herder file
;**************************************************************************
;Set the header value
ENVI_ASSIGN_HEADER_VALUE, FID=Data_FID, KEYWORD ='Author', VALUE = "SDH"
ENVI_ASSIGN_HEADER_VALUE, FID=Data_FID, KEYWORD ='Soil Depth', VALUE = ndepth, PRECISION=5
FOR VarAttID =0, VAR.NATTS -1 DO BEGIN
AttName = NCDF_ATTNAME( NID, Varid, VarAttID )
NCDF_ATTGET, NID, varid, AttName, AttValue
ENVI_ASSIGN_HEADER_VALUE, FID=Data_FID, KEYWORD = AttName, VALUE = STRING(AttValue)
ENDFOR
;Write the values to the file header
ENVI_WRITE_FILE_HEADER, Data_FID
;remove file
ENVI_FILE_MNG, ID=Data_FID, /REMOVE
ENDIF
ENDFOR
ENDFOR
PRINT, 'Procedure ends at ' + SYSTIME(/UTC)
END