process satellite data from path
[JPSSData.git] / ncvarinfo.py
blobc6717cb1e62efb6e20e725c36d664b81f59cda85
1 import netCDF4 as nc
2 import numpy as np
3 import pandas as pd
5 def ncvarinfo(ncid,varid):
6 # ncvarinfo(ncid,varid)
7 # get info on variable number varid in file f
8 # returns a structure with fields containing the
9 # variable propertices and attributes
11 # Lauren Hearn, September 2018
12 # transcribed into python from an earlier Matlab code by Jan Mandel and Jon Beezley
14 v = ncvarinfo()
15 [v.varname,v.vartype,v.dimids,v.natts]=nc.inqVar(ncid,varid)
16 v.ndims = length(v.dimids)
17 # translate variable type
18 [v.vartype_nc,v.vartype_m] = ncdatatype(v.vartype);
19 # get dimensions
20 for idim in v.ndims():
21 dimid = v.dimids(idim)
22 [dimname,dimlength] = nc.inqDim(ncid,dimid)
23 dimname = v.dimname(idim)
24 if isempty(regexp(dimname,'_subgrid$','ONCE')):
25 dimlength = v.dimlength(idim)
26 else: # fix fire grid variables
27 #v.dimlength(idim) = 0 # default
28 stagname = [regexprep(dimname,'_subgrid$',''),'_stag']
29 try:
30 stagid = nc.inqDimID(ncid,stagname)
31 [tmp,staglen] = netcdf.inqDim(ncid,stagid)
32 except Exception as e:
33 print 'Warning: dimension ',stagname,' not found' % e
34 staglen = 0
36 atmname = dimname(1,-8)
37 try:
38 atmid = nc.inqDimID(ncid,atmname)
39 [tmp,atmlen] = nc.inqDim(ncid,atmid)
40 except Exception as e:
41 print 'Warning: dimension ',atmname,' not found' % e
42 atmlen = 0
44 if atmlen and staglen > 0:
45 if atmlen + 1 != staglen:
46 print 'Warning: inconsistent',atmname,' and ',stagname
48 if atmlen == 0 and staglen == 0:
49 print 'Warning: dimensions ',stagname,' or ',atmname,' not found, cannot fix fire variable size'
50 dimlength = v.dimlength(idim)
51 else:
52 ratio = dimlength / max(staglen,atmlen+1)
53 dimlength = dimlength-ratio
55 # get attributes
56 v.att_name = pd.DataFrame(0,v.natts)
57 v.att_datatype = np.zeros(0,v.natts)
58 v.att_datatype_m = pd.DataFrame(0,v.natts)
59 v.att_len = np.zeros(0,v.natts)
60 v.att_value = pd.DataFrame(0,v.natts)
61 for iatt in v.natts():
62 attname = nc.inqAttName(ncid,varid,iatt-1)
63 [datatype,attlen] = nc.inqAtt(ncid,varid,attname)
64 [att_type_nc,att_type_m] = type(type); # TEXT or DOUBLE
65 att_value = nc.getAtt(ncid,varid,attname)
66 attname = v.att_name(iatt)
67 datatype = v.att_datatype(iatt)
68 att_type_m = v.att_datatype_m(iatt)
69 attlen = v.att_len(iatt)
70 att_value = v.att_value(iatt)