-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxarray_netcdf_video.py
115 lines (94 loc) · 3.52 KB
/
xarray_netcdf_video.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 6 08:42:49 2023
@author: lukem
"""
import xarray as xr # For creating a NetCDF dataset
import pandas as pd # For reading in data (CSV, xlsx etc) to a dataframe
from datetime import datetime as dt # Handling dates and times
import numpy as np # Good for working with multidimensional arrays and mathematical functions
df = pd.read_csv(r'C:\Users\Nico\Desktop\UBB\2022-2\Tesis\csv\ih.csv')
latitude = sorted(list(set(df['lat'])))
longitude = sorted(list(set(df['lon'])))
#time = sorted(list(set(df['Day'])))
sea_surface_skin_temperature = np.array(df['ih']).reshape(97,407)
xrds = xr.Dataset(
coords = dict(
longitude = longitude, # These coordinate names are compliant with CF conventions (all lower case)
latitude = latitude, #
#time = time #
),
data_vars = dict(
sea_surface_skin_temperature = (["lat", "lon"], sea_surface_skin_temperature)
)
)
xrds['time'].attrs['standard_name'] = 'time'
xrds['time'].attrs['long_name'] = 'time'
xrds['time'].attrs['units'] = 'days since 2020-07-10T12:00:00Z'
xrds['time'].attrs['coverage_content_type'] = 'coordinate'
xrds['latitude'].attrs = {
'standard_name': 'latitude',
'long_name':'decimal latitude in degrees north',
'units': 'degrees_north',
'coverage_content_type': 'coordinate'
}
xrds['longitude'].attrs = {
'standard_name': 'longitude',
'long_name':'decimal longitude in degrees east',
'units': 'degrees_east',
'coverage_content_type': 'coordinate'
}
# xrds['depth'].attrs = {
# 'standard_name': 'depth',
# 'long_name':'depth below sea level',
# 'units': 'meters',
# 'coverage_content_type': 'coordinate',
# 'positive': 'down'
# }
# xrds['pressure'].attrs = {
# 'standard_name': 'sea_water_pressure',
# 'long_name':'sea_water_pressure',
# 'units': 'Pa',
# 'coverage_content_type': 'coordinate'
# }
# xrds['altitude'].attrs = {
# 'standard_name': 'altitude',
# 'long_name':'altitude above mean sea level',
# 'units': 'meters',
# 'coverage_content_type': 'coordinate',
# 'positive': 'up'
# }
xrds['sea_surface_skin_temperature'] += 273.15 # Converting from degrees celsius to kelvin
xrds['sea_surface_skin_temperature'].attrs = {
'standard_name':'sea_surface_skin_temperature',
'long_name':'Temperature of the sea water directly below the surface',
'units': 'K',
'coverage_content_type': 'physicalMeasurement'
}
xrds.attrs['title'] = 'my title'
global_attributes = pd.read_excel('multidimensional_sea_water_temperature_global_attributes.xlsx', index_col=0)
global_attributes_transposed = global_attributes.transpose()
global_attributes_dic = global_attributes_transposed.to_dict('records')[0]
xrds.attrs=global_attributes_dic
xrds.attrs['date_created'] = dt.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
xrds.attrs['history'] = f'File create at {dt.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")} using xarray in Python'
myencoding = {
'time': {
'dtype': 'int32',
'_FillValue': None # Coordinate variables should not have fill values.
},
'latitude': {
'dtype': 'float32',
'_FillValue': None # Coordinate variables should not have fill values.
},
'longitude': {
'dtype': 'float32',
'_FillValue': None # Coordinate variables should not have fill values.
},
'sea_surface_skin_temperature': {
'_FillValue': -999,
'zlib': False
}
}
xrds.to_netcdf('ih.nc',encoding=myencoding)