I want to save string data into a single column in the NetCDF file using MATLAB, there are no options given for string. Can someone tell me how to save string data into the NetCDF file?
S_rebuilt=["101670";"101670";"101670";"101670"]
nccreate('file_name.nc','S_rebuilt',...
'Dimensions', {'x',size(S_rebuilt,1),'y',size(S_rebuilt,2)},...
'FillValue','disable');
ncwrite('file_name.nc','S_rebuilt',S_rebuilt);
With using format netcdf4, one can use datatype string in the MatLab. So, to save the variable S_rebuilt as string, I suggest code:
filename = 'file_name.nc'
S_rebuilt = ["101670";"101670";"101670";"101670"]
nccreate(filename,'S_rebuilt',...
'Dimensions', {'nvars',length(S_rebuilt)},...
'Datatype','string','format','netcdf4');
% ----------------------------------------------
ncwrite(filename,'S_rebuilt',S_rebuilt);
Related
How to remove the time from the csv file in python
I have a csv file in this format: "SSP_Ac_INVOICE_DISTRIBUTIONS_17022023072701.csv"
I am trying to remove the time which is after2023.my expectation was SSP_Ac_INVOICE_DISTRIBUTIONS_17022023.csv
I tried to use strptime but getting below error:
s = "SSP_AP_INVOICE_DISTRIBUTIONS_17022023072701.csv"
temp = dt.datetime.strptime(SSP_AP_INVOICE_DISTRIBUTIONS_17022023072701, '%d%m%Y')
final = temp.strftime('%d-%m-%Y')
print(final)
In the strptime function, you are passing the string 'SSP_AP_INVOICE_DISTRIBUTIONS_17022023072701.csv' instead of the variable s. Also, you are using the wrong format string in strptime. Since the date string in your filename is in the format %d%m%Y%H%M%S, you need to include %H%M%S in the format string to parse the time as well.
The code should look something like this:
import datetime as dt
filename = "SSP_AP_INVOICE_DISTRIBUTIONS_17022023072701.csv"
# Parse the date from the filename
date_str = filename.split('_')[3]
date = dt.datetime.strptime(date_str, '%d%m%Y%H%M%S')
# Format the date as required
new_filename = f"{filename.split('_')[0]}_{filename.split('_')[1]}_{filename.split('_')[2]}_{date.strftime('%d%m%Y')}.csv"
print(new_filename)
This code first splits the filename by the underscore character to extract the date string, and then uses strptime to parse the date and time. Finally, it formats the new filename using the date and the other parts of the original filename that were not changed.
What is the appropriate way to take in files that have a filename with a timestamp in it and read properly?
One way I'm thinking of so far is to take these filenames into one single text file to read all at once.
For example, filenames such as
1573449076_1570501819_file1.txt
1573449076_1570501819_file2.txt
1573449076_1570501819_file3.txt
Go into a file named filenames.txt
Then something like
with open('/Documents/filenames.txt', 'r') as f:
for item in f:
if item.is_file():
file_stat = os.stat(item)
item = item.replace('\n', '')
print("Fetching {}".format(convert_times(file_stat)))
My question is how would I go about this where I can properly read the names in the text file given that they have timestamps in the actual names? Once figuring that out I can convert them.
If you just want to get the timestamps from the file names, assuming that they all use the same naming convention, you can do so like this:
import glob
import os
from datetime import datetime
# Grab all .txt files in the specified directory
files = glob.glob("<path_to_dir>/*.txt")
for file in files:
file = os.path.basename(file)
# Check that it contains an underscore
if not '_' in file:
continue
# Split the file name using the underscore as the delimiter
stamps = file.split('_')
# Convert the epoch to a legible string
start = datetime.fromtimestamp(int(stamps[0])).strftime("%c")
end = datetime.fromtimestamp(int(stamps[1])).strftime("%c")
# Consume the data
print(f"{start} - {end}")
...
You'll want to add some error checking and handling; for instance, if the first or second index in the stamps array isn't a parsable int, this will fail.
I am facing issues with a U SQL script. I am trying to get files which were created on current day from a directory. the file name will have the date in yyyyMMdd format. But when i try to extract data instead of taking only one days files i am getting all the files inside the directory. I am using the below script.
DECLARE #file_set_path string ="/XXXX/Sample_{date:yyyy}{date:MM}{date:dd}{*}.csv";
#searchlog =
EXTRACT PART_NUMBER string, date DateTime FROM #file_set_path USING Extractors.Tsv(skipFirstNRows:1);
Can someone please help me on this.
You can use the Date property of the DateTime object to compare dates without including the time component, something like this:
DECLARE #file_set_path string ="/Sample_{date:yyyy}{date:MM}{date:dd}{*}.csv";
DECLARE #now DateTime = DateTime.Now;
#searchlog =
EXTRACT PART_NUMBER string,
date DateTime
FROM #file_set_path
USING Extractors.Csv(skipFirstNRows : 1);
#output =
SELECT *,
#now AS now,
date.Date AS x,
#now.Date AS y
FROM #searchlog
WHERE date.Date == #now.Date;
OUTPUT #output
TO "/output/output.csv"
USING Outputters.Csv();
NB I noticed you are using the Tsv extractor with Csv files. It may not matter when there is only one column or possibly this is a typo?
I want to use a for loop to export the names of a folder of .mat files into one .csv file by row.
I am trying to use xlswrite, but don't understand how to access the filename of the .mat files.
I am working with to write them to the csv.
xlswrite(fullfile(dest_dir,'result.csv'), FILENAME HERE, ['A' num2str(i)]);
xlswrite creates a excel workbook
With dir command you can get a structure whose one of field is name of file.
Using simple file IO function your requirement can be met.
I think either of this will do what you need:
fid=fopen('result.csv','wt');
files=dir('*.mat');
x={files(:).name};
csvFun = #(str)sprintf('%s,',str);
xchar = cellfun(csvFun, x,'UniformOutput', false);
xchar = strcat(xchar{:});
xchar = strcat(xchar(1:end-1),'\n');
fprintf(fid,xchar);
fclose(fid);
Or
If you just need .mat names in a column form:
fid=fopen('result.csv','wt');
files=dir('*.mat');
x={files(:).name};
[rows,cols]=size(x);
for i=1:rows
fprintf(fid,'%s,',x{i,1:end-1});
fprintf(fid,'%s\n',x{i,end});
end
fclose(fid);
To get your filename, load the files using dir command, read them individually in your for loop, and then write them to file. (I think I already posted something like this?) Here is the prototype:
files = dir('*.mat'); % obtain all files with .mat extenstion
fid = fopen('result.csv','a');
for k = 1:length(data)
filename = files(k).name; % get the filename
fprintf(fid, '%s,\n', filename);
end
fid = fclose(fid);
Unfortunately, you cannot just save the file names as strings in a cell and write it as you might a matrix using say csvwrite, so you can just do this.
I have binary data stored in a file. I am doing this:
byte[] fileBytes = File.ReadAllBytes(#"c:\carlist.dat");
string ascii = Encoding.ASCII.GetString(fileBytes);
This is giving me following result with lot of invalid characters. What am i doing wrong?
?D{F ?x#??4????? NBR-OF-CARSNUMBER-OF-CARS!"#??? NBR-OF-CARS$%??1y0#123?G??#$ NBR-OF-CARS%45??1y# NUMBER-OF-CARSd?
hmm... seems like a save was made from a byte buffer where after NBR-OF-CARS was written some numeric data. If you have an access to the code that saves the file could you check if there are numbers over there and if there are - check does the code converts numbers to string before witing the value into the binary stream.